~abyxcos/naughty.st

naughty.st/naughty.st.go -rw-r--r-- 1.5 KiB
7c8e4fbfabyxcos Add an instagram -> bibliogram mapping 7 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"

	"gopkg.in/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)
}