package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/russross/blackfriday/v2"
)
var README []byte
func main() {
file, err := ioutil.ReadFile("README.md")
if err != nil {
fmt.Println("Could not locate README.md to generate help message")
README = []byte("")
} else {
README = []byte(blackfriday.Run(file))
}
http.HandleFunc("/", urlHandler)
http.ListenAndServe(":8476", nil)
}
func urlHandler(w http.ResponseWriter, r *http.Request) {
// Display our home page/help if the query is nil
if r.URL.RawQuery == "" {
w.Write(README)
return
}
naughtyURL, err := url.Parse(r.URL.RawQuery)
if err != nil {
fmt.Fprintf(w, "Sorry, I don't understand this url: %s\n", r.URL.RawQuery)
return
}
var niceHost string
switch naughtyURL.Host {
case "twitter.com", "www.twitter.com", "mobile.twitter.com":
niceHost = "nitter.cattube.org"
case "youtube.com", "www.youtube.com", "m.youtube.com":
// Redirect Youtube to Cloudtube until Invidious works again
niceHost = "tube.cadence.moe"
case "reddit.com", "www.reddit.com":
niceHost = "teddit.net"
case "instagram.com", "www.instagram.com":
niceHost = "bibliogram.art"
default:
fmt.Fprintf(w, "<html>")
fmt.Fprintf(w, "<b>Sorry, I haven't learned how to redirect %s yet.</b>\n\n", naughtyURL.Host)
w.Write(README)
fmt.Fprintf(w, "</html>")
return
}
niceURL := fmt.Sprintf("https://%s%s?%s", niceHost, naughtyURL.Path, naughtyURL.RawQuery)
http.Redirect(w, r, niceURL, http.StatusSeeOther)
}