2023-09-18 09:16:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-30 14:20:25 +00:00
|
|
|
"context"
|
2024-01-08 15:59:45 +00:00
|
|
|
"embed"
|
2024-12-30 14:20:25 +00:00
|
|
|
"flag"
|
2023-09-30 16:42:33 +00:00
|
|
|
"fmt"
|
2023-09-18 09:16:23 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2024-12-30 14:20:25 +00:00
|
|
|
"os/signal"
|
|
|
|
"time"
|
2024-01-23 13:58:37 +00:00
|
|
|
|
2024-12-30 14:20:25 +00:00
|
|
|
"git.mstar.dev/mstar/goutils/middleware"
|
|
|
|
"git.mstar.dev/mstar/goutils/other"
|
|
|
|
"github.com/rs/zerolog/hlog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2023-09-18 09:16:23 +00:00
|
|
|
)
|
|
|
|
|
2024-01-08 15:59:45 +00:00
|
|
|
const HTML_PREFIX = "<!-- "
|
|
|
|
const HTML_SUFFIX = " -->"
|
|
|
|
|
|
|
|
//go:embed templates
|
|
|
|
var embed_templates embed.FS
|
|
|
|
|
|
|
|
//go:embed static
|
|
|
|
var embed_static embed.FS
|
|
|
|
|
|
|
|
//go:embed well-known
|
|
|
|
var embed_well_known embed.FS
|
|
|
|
|
|
|
|
// go: embed robots.txt
|
|
|
|
var embed_robots_txt string
|
|
|
|
|
|
|
|
// go: embed humans.txt
|
|
|
|
var embed_humans_txt string
|
|
|
|
|
2023-09-18 09:16:23 +00:00
|
|
|
func main() {
|
2024-12-30 14:20:25 +00:00
|
|
|
other.SetupFlags()
|
|
|
|
portFlag := flag.String("port", "8080", "Set the port. Overwrites env vars")
|
|
|
|
flag.Parse()
|
|
|
|
other.ConfigureLoggingFromCliArgs()
|
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/{$}", handleRoot)
|
|
|
|
// Catchall that just redirects to Hetzner's 10GB speedtest file
|
|
|
|
// TODO: Maybe swap that for a forced zip bomb?
|
|
|
|
mux.Handle(
|
|
|
|
"/",
|
|
|
|
http.RedirectHandler("https://hil-speed.hetzner.com/10GB.bin", http.StatusMovedPermanently),
|
|
|
|
)
|
|
|
|
|
|
|
|
mux.HandleFunc("/cat/awawawa", awawaStream)
|
2024-01-08 15:59:45 +00:00
|
|
|
|
2023-09-30 16:42:33 +00:00
|
|
|
// static files in /static
|
2024-12-30 14:20:25 +00:00
|
|
|
mux.Handle("/static/", http.FileServer(http.FS(embed_static)))
|
2023-09-30 16:42:33 +00:00
|
|
|
|
|
|
|
// .well-known from /well-known
|
2024-12-30 14:20:25 +00:00
|
|
|
mux.Handle(
|
|
|
|
"/.well-known/",
|
|
|
|
http.StripPrefix("/.well-known/", http.FileServer(http.FS(embed_well_known))),
|
|
|
|
)
|
2023-09-30 16:42:33 +00:00
|
|
|
|
|
|
|
// Static files not in /static or /.well-known
|
2024-12-30 14:20:25 +00:00
|
|
|
mux.HandleFunc("/robots.txt", buildHTTPFileReader(embed_robots_txt))
|
|
|
|
mux.HandleFunc("/humans.txt", buildHTTPFileReader(embed_humans_txt))
|
2024-01-23 13:58:37 +00:00
|
|
|
|
2024-12-30 14:20:25 +00:00
|
|
|
server := http.Server{
|
|
|
|
Addr: ":" + *portFlag,
|
|
|
|
Handler: middleware.ChainMiddlewares(mux, middleware.LoggingMiddleware),
|
|
|
|
}
|
|
|
|
exitChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(exitChan, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
log.Info().Str("addr", server.Addr).Msg("Starting server")
|
|
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatal().Err(err).Msg("Server exited unexpectedly")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
<-exitChan
|
|
|
|
log.Info().Msg("Shutting down server due to Interrupt")
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
|
|
|
|
defer cancel()
|
|
|
|
err := server.Shutdown(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Failed to cleanly shut down")
|
2023-09-18 09:16:23 +00:00
|
|
|
}
|
2024-12-30 14:20:25 +00:00
|
|
|
log.Info().Msg("Clean shutdown complete")
|
2023-09-18 09:16:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
2024-01-08 15:59:45 +00:00
|
|
|
tmpl, err := template.ParseFS(embed_templates, "templates/index.html")
|
2023-09-18 09:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Couldn't parse template file", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-01-08 15:59:45 +00:00
|
|
|
err = tmpl.Execute(w, nil)
|
2023-09-18 09:16:23 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to execute template", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2023-09-30 16:42:33 +00:00
|
|
|
|
2024-01-08 15:59:45 +00:00
|
|
|
func buildHTTPFileReader(embed_content string) func(w http.ResponseWriter, r *http.Request) {
|
2023-09-30 16:42:33 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-01-08 15:59:45 +00:00
|
|
|
fmt.Fprintf(w, "%s", embed_content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func awawaStream(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
w.WriteHeader(206)
|
|
|
|
w.Header().Set("Status", "206")
|
2024-12-30 14:20:25 +00:00
|
|
|
genStub(
|
|
|
|
1024,
|
|
|
|
w,
|
|
|
|
) // Hardcoded. Firefox & Chrome both have this value and a len of 0 wouldn't work
|
|
|
|
hlog.FromRequest(r).Debug().Msg("Starting stream")
|
|
|
|
i := true
|
|
|
|
// Only run every 0.2 seconds, aka 5 times a second
|
|
|
|
timer := time.Tick(time.Millisecond * 200)
|
2024-01-08 15:59:45 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-r.Context().Done():
|
2024-12-30 14:20:25 +00:00
|
|
|
hlog.FromRequest(r).Debug().Msg("Stream done")
|
2023-09-30 16:42:33 +00:00
|
|
|
return
|
2024-12-30 14:20:25 +00:00
|
|
|
case <-timer:
|
|
|
|
if i {
|
2024-01-08 15:59:45 +00:00
|
|
|
fmt.Fprint(w, "a")
|
|
|
|
} else {
|
|
|
|
fmt.Fprint(w, "w")
|
|
|
|
}
|
2024-12-30 14:20:25 +00:00
|
|
|
i = !i
|
2023-09-30 16:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-08 15:59:45 +00:00
|
|
|
|
|
|
|
func genStub(length int, w http.ResponseWriter) {
|
|
|
|
PreSufLen := len(HTML_PREFIX) + len(HTML_SUFFIX)
|
|
|
|
fmt.Fprint(w, HTML_PREFIX)
|
|
|
|
for i := 0; i < length-PreSufLen; i++ {
|
|
|
|
fmt.Fprint(w, '\u0020')
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, HTML_SUFFIX)
|
|
|
|
}
|