-
Notifications
You must be signed in to change notification settings - Fork 0
/
www.go
125 lines (104 loc) · 3.35 KB
/
www.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package main
import (
"fmt"
"html/template"
"log"
"math/rand"
"net/http"
"strings"
"time"
"unicode"
"github.com/julienschmidt/httprouter"
)
func logNow(request *http.Request, timer time.Time) {
logString := fmt.Sprintf("%s, %s, %s, %s, %s, took %s", request.UserAgent(), request.RemoteAddr, request.Method, request.Proto, request.URL, time.Since(timer))
if strings.Contains(strings.ToLower(logString), "bot") || strings.Contains(strings.ToLower(logString), "spider") {
return
}
log.Println(logString)
}
func setHeaders(writer http.ResponseWriter) http.ResponseWriter {
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
writer.Header().Set("Cache-Control", "public, max-age=3600")
writer.Header().Set("X-Powered-By", "Diet Coke")
writer.Header().Set("ETag", ucVersion)
return writer
}
func serveFilesFromTemplate(writer http.ResponseWriter, request *http.Request, templates []string, data interface{}, timer time.Time) {
tmpl, err := template.New("").ParseFiles(templates...)
if err != nil {
log.Print(err.Error())
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
return
}
err = tmpl.ExecuteTemplate(writer, "base", data)
if err != nil {
log.Print(err.Error())
http.Error(writer, "Internal Server Error", http.StatusInternalServerError)
return
}
logNow(request, timer)
}
func redirectToTLS(writer http.ResponseWriter, request *http.Request) {
log.Printf("Redirecting, %v, %v\n", request.RemoteAddr, request.URL)
http.Redirect(writer, request, "https://unicode.click:443"+request.RequestURI, http.StatusMovedPermanently)
}
func serveIndex(writer http.ResponseWriter, request *http.Request, timer time.Time) {
templateFiles := []string{
"./template/base.template.html",
"./template/index.template.html",
}
serveFilesFromTemplate(writer, request, templateFiles, struct{ UnicodeVersion string }{UnicodeVersion: unicode.Version}, timer)
}
func getRandomRune(maximum int) rune {
// a much better way to do this would
// be to randomly pick a table, then
// randomly pick a rune within it...
// I'm way too lazy to write that tho
if maximum == 0 {
// highest assigned
maximum = 1114112
}
randy := rune(rand.Intn(maximum))
// filter out hanja
// otherwise its literally always hanja
if unicode.In(randy, unicode.Han) {
if randy%6 == 0 {
return randy
}
return getRandomRune(maximum / 2)
}
if unicode.IsPrint(randy) {
return randy
}
return getRandomRune(maximum / 2)
}
func serveRandom(writer http.ResponseWriter, request *http.Request, timer time.Time) {
random := getRandomRune(0)
http.Redirect(writer, request, "https://unicode.click:443/cp/"+string(random), http.StatusSeeOther)
logNow(request, timer)
}
func serveUnicodeClick(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
timer := time.Now()
route := params.ByName("filepath")
// INFO: routes contain leading slash
switch {
case route == "/":
serveIndex(writer, request, timer)
return
case route == "/random":
serveRandom(writer, request, timer)
return
case len(route) >= 8 && route[0:6] == "/range":
route = route[7:]
serveRange(writer, request, route, timer)
return
case len(route) >= 5 && route[0:3] == "/cp":
route = route[4:]
serveCodepoint(writer, request, route, timer)
return
}
route = "public" + route
http.ServeFile(writer, request, route)
logNow(request, timer)
}