| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "embed" |
| 6 | "fmt" |
| 7 | "html/template" |
| 8 | "math/rand" |
| 9 | "net/http" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | //go:embed templates/*.html |
| 14 | var templatesFS embed.FS |
| 15 | var tmpl = template.Must(template.ParseFS(templatesFS, "templates/*.html")) |
| 16 | |
| 17 | //go:embed robots.txt |
| 18 | var robotsTxt string |
| 19 | |
| 20 | type PageData struct { |
| 21 | Title string |
| 22 | Content string |
| 23 | Links []string |
| 24 | } |
| 25 | |
| 26 | func RobotsHandler(writer http.ResponseWriter, req *http.Request) { |
| 27 | writer.Header().Set("Content-Type", "text/plain; charset=utf-8") |
| 28 | fmt.Fprint(writer, robotsTxt) |
| 29 | } |
| 30 | |
| 31 | var headEndTag = []byte("</head>") |
| 32 | |
| 33 | func splitPage(page []byte) (head, body []byte) { |
| 34 | if i := bytes.Index(page, headEndTag); i >= 0 { |
| 35 | return page[:i+len(headEndTag)], page[i+len(headEndTag):] |
| 36 | } |
| 37 | return nil, page |
| 38 | } |
| 39 | |
| 40 | func TarpitWrite(writer http.ResponseWriter, page []byte) { |
| 41 | head, body := splitPage(page) |
| 42 | if len(head) > 0 { |
| 43 | if _, err := writer.Write(head); err != nil { |
| 44 | return |
| 45 | } |
| 46 | if flusher, ok := writer.(http.Flusher); ok { |
| 47 | flusher.Flush() |
| 48 | } |
| 49 | } |
| 50 | drip(writer, body) |
| 51 | } |
| 52 | |
| 53 | func drip(writer http.ResponseWriter, data []byte) { |
| 54 | flusher, canFlush := writer.(http.Flusher) |
| 55 | for len(data) > 0 { |
| 56 | delayMs := dripDelayMinMs |
| 57 | if spread := dripDelayMaxMs - dripDelayMinMs; spread > 0 { |
| 58 | delayMs += rand.Intn(spread) |
| 59 | } |
| 60 | time.Sleep(time.Duration(delayMs) * time.Millisecond) |
| 61 | |
| 62 | chunk := 1 + rand.Intn(dripChunkMax) |
| 63 | if chunk > len(data) { |
| 64 | chunk = len(data) |
| 65 | } |
| 66 | if _, err := writer.Write(data[:chunk]); err != nil { |
| 67 | return |
| 68 | } |
| 69 | if canFlush { |
| 70 | flusher.Flush() |
| 71 | } |
| 72 | data = data[chunk:] |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func IndexHandler(writer http.ResponseWriter, req *http.Request) { |
| 77 | babbler := sharedBabbler() |
| 78 | var title string |
| 79 | switch { |
| 80 | case req.PathValue("slug2") != "": |
| 81 | title = req.PathValue("slug") + " " + req.PathValue("slug2") |
| 82 | case req.PathValue("slug") != "": |
| 83 | title = req.PathValue("slug") |
| 84 | default: |
| 85 | title = babbler.Generate(2) |
| 86 | } |
| 87 | content := babbler.Generate(500) |
| 88 | links := babbler.Links(5) |
| 89 | data := PageData{ |
| 90 | Title: title, |
| 91 | Content: content, |
| 92 | Links: links, |
| 93 | } |
| 94 | var buf bytes.Buffer |
| 95 | if err := tmpl.ExecuteTemplate(&buf, "index.html", data); err != nil { |
| 96 | http.Error(writer, "Internal server error", http.StatusInternalServerError) |
| 97 | return |
| 98 | } |
| 99 | TarpitWrite(writer, buf.Bytes()) |
| 100 | } |
| 101 | |
| 102 | type statusRecorder struct { |
| 103 | http.ResponseWriter |
| 104 | status int |
| 105 | } |
| 106 | |
| 107 | func (r *statusRecorder) WriteHeader(code int) { |
| 108 | r.status = code |
| 109 | r.ResponseWriter.WriteHeader(code) |
| 110 | } |
| 111 | |
| 112 | func (r *statusRecorder) Flush() { |
| 113 | if flusher, ok := r.ResponseWriter.(http.Flusher); ok { |
| 114 | flusher.Flush() |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func RequestMiddleware(handler http.Handler) http.Handler { |
| 119 | return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) { |
| 120 | start := time.Now() |
| 121 | recorder := &statusRecorder{ResponseWriter: writer, status: http.StatusOK} |
| 122 | |
| 123 | logger.Debug("request started", |
| 124 | "method", req.Method, |
| 125 | "path", req.URL.Path, |
| 126 | "remote", req.RemoteAddr, |
| 127 | "user_agent", req.UserAgent(), |
| 128 | ) |
| 129 | |
| 130 | writer.Header().Set("X-Robots-Tag", "noindex, nofollow") |
| 131 | handler.ServeHTTP(recorder, req) |
| 132 | |
| 133 | logger.Info("request", |
| 134 | "method", req.Method, |
| 135 | "path", req.URL.Path, |
| 136 | "status", recorder.status, |
| 137 | "duration", time.Since(start).Round(time.Millisecond), |
| 138 | "remote", req.RemoteAddr, |
| 139 | "user_agent", req.UserAgent(), |
| 140 | ) |
| 141 | }) |
| 142 | } |