chore: added tarpit write
01f0020e
3 file(s) · +79 −8
| 10 | 10 | // - [x] Serve robots.txt with noindex |
|
| 11 | 11 | // - [x] Serve root path html with babbler and links to new paths |
|
| 12 | 12 | // - [x] Serve dynamic path that takes in path param to serve babbler and more links |
|
| 13 | - | // - [ ] Slow down responses |
|
| 13 | + | // - [x] Slow down responses |
|
| 14 | 14 | ||
| 15 | 15 | func main() { |
|
| 16 | - | if portEnv == "" { |
|
| 17 | - | portEnv = "8090" |
|
| 18 | - | } |
|
| 19 | - | ||
| 20 | 16 | http.HandleFunc("/robots.txt", RobotsHandler) |
|
| 21 | 17 | http.HandleFunc("/", IndexHandler) |
|
| 22 | 18 | http.HandleFunc("/{slug}", IndexHandler) |
| 1 | 1 | package main |
|
| 2 | 2 | ||
| 3 | 3 | import ( |
|
| 4 | + | "bytes" |
|
| 4 | 5 | "embed" |
|
| 5 | 6 | "fmt" |
|
| 6 | 7 | "html/template" |
|
| 7 | 8 | "log" |
|
| 9 | + | "math/rand" |
|
| 8 | 10 | "net/http" |
|
| 9 | - | "os" |
|
| 11 | + | "time" |
|
| 10 | 12 | ) |
|
| 11 | 13 | ||
| 12 | - | var portEnv = os.Getenv("PORT") |
|
| 14 | + | var portEnv = envString("PORT", "8090") |
|
| 15 | + | ||
| 16 | + | var ( |
|
| 17 | + | dripDelayMinMs = envInt("DRIP_DELAY_MIN_MS", 50) |
|
| 18 | + | dripDelayMaxMs = envInt("DRIP_DELAY_MAX_MS", 150) |
|
| 19 | + | dripChunkMax = envInt("DRIP_CHUNK_MAX_BYTES", 60) |
|
| 20 | + | ) |
|
| 13 | 21 | ||
| 14 | 22 | //go:embed templates/*.html |
|
| 15 | 23 | var templatesFS embed.FS |
|
| 29 | 37 | fmt.Fprint(writer, robotsTxt) |
|
| 30 | 38 | } |
|
| 31 | 39 | ||
| 40 | + | var headEndTag = []byte("</head>") |
|
| 41 | + | ||
| 42 | + | func splitPage(page []byte) (head, body []byte) { |
|
| 43 | + | if i := bytes.Index(page, headEndTag); i >= 0 { |
|
| 44 | + | return page[:i+len(headEndTag)], page[i+len(headEndTag):] |
|
| 45 | + | } |
|
| 46 | + | return nil, page |
|
| 47 | + | } |
|
| 48 | + | ||
| 49 | + | func TarpitWrite(writer http.ResponseWriter, page []byte) { |
|
| 50 | + | head, body := splitPage(page) |
|
| 51 | + | if len(head) > 0 { |
|
| 52 | + | if _, err := writer.Write(head); err != nil { |
|
| 53 | + | return |
|
| 54 | + | } |
|
| 55 | + | if flusher, ok := writer.(http.Flusher); ok { |
|
| 56 | + | flusher.Flush() |
|
| 57 | + | } |
|
| 58 | + | } |
|
| 59 | + | drip(writer, body) |
|
| 60 | + | } |
|
| 61 | + | ||
| 62 | + | func drip(writer http.ResponseWriter, data []byte) { |
|
| 63 | + | flusher, canFlush := writer.(http.Flusher) |
|
| 64 | + | for len(data) > 0 { |
|
| 65 | + | delayMs := dripDelayMinMs |
|
| 66 | + | if spread := dripDelayMaxMs - dripDelayMinMs; spread > 0 { |
|
| 67 | + | delayMs += rand.Intn(spread) |
|
| 68 | + | } |
|
| 69 | + | time.Sleep(time.Duration(delayMs) * time.Millisecond) |
|
| 70 | + | ||
| 71 | + | chunk := 1 + rand.Intn(dripChunkMax) |
|
| 72 | + | if chunk > len(data) { |
|
| 73 | + | chunk = len(data) |
|
| 74 | + | } |
|
| 75 | + | if _, err := writer.Write(data[:chunk]); err != nil { |
|
| 76 | + | return |
|
| 77 | + | } |
|
| 78 | + | if canFlush { |
|
| 79 | + | flusher.Flush() |
|
| 80 | + | } |
|
| 81 | + | data = data[chunk:] |
|
| 82 | + | } |
|
| 83 | + | } |
|
| 84 | + | ||
| 32 | 85 | func IndexHandler(writer http.ResponseWriter, req *http.Request) { |
|
| 33 | 86 | babbler := NewBabbler() |
|
| 34 | 87 | var title string |
|
| 44 | 97 | Content: content, |
|
| 45 | 98 | Links: links, |
|
| 46 | 99 | } |
|
| 47 | - | if err := tmpl.ExecuteTemplate(writer, "index.html", data); err != nil { |
|
| 100 | + | var buf bytes.Buffer |
|
| 101 | + | if err := tmpl.ExecuteTemplate(&buf, "index.html", data); err != nil { |
|
| 48 | 102 | http.Error(writer, "Internal server error", http.StatusInternalServerError) |
|
| 103 | + | return |
|
| 49 | 104 | } |
|
| 105 | + | TarpitWrite(writer, buf.Bytes()) |
|
| 50 | 106 | } |
|
| 51 | 107 | ||
| 52 | 108 | func RequestMiddleware(handler http.Handler) http.Handler { |
|
| 1 | 1 | package main |
|
| 2 | 2 | ||
| 3 | 3 | import ( |
|
| 4 | + | "os" |
|
| 4 | 5 | "regexp" |
|
| 6 | + | "strconv" |
|
| 5 | 7 | "strings" |
|
| 6 | 8 | "unicode" |
|
| 7 | 9 | ||
| 8 | 10 | norm "golang.org/x/text/unicode/norm" |
|
| 9 | 11 | ) |
|
| 12 | + | ||
| 13 | + | func envInt(key string, fallback int) int { |
|
| 14 | + | if value := os.Getenv(key); value != "" { |
|
| 15 | + | if parsed, err := strconv.Atoi(value); err == nil && parsed > 0 { |
|
| 16 | + | return parsed |
|
| 17 | + | } |
|
| 18 | + | } |
|
| 19 | + | return fallback |
|
| 20 | + | } |
|
| 21 | + | ||
| 22 | + | func envString(key string, fallback string) string { |
|
| 23 | + | var value string |
|
| 24 | + | if value := os.Getenv(key); value != "" { |
|
| 25 | + | return fallback |
|
| 26 | + | } |
|
| 27 | + | return value |
|
| 28 | + | } |
|
| 10 | 29 | ||
| 11 | 30 | var nonAlnum = regexp.MustCompile(`[^a-z0-9]+`) |
|
| 12 | 31 |