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
|
package internal
import (
"flag"
"log/slog"
"net/http"
"net/url"
)
var (
onionDomain = flag.String("onion-domain", "", "The Tor hidden service domain that this website uses")
)
func OnionLocation(next http.Handler) http.Handler {
if *onionDomain == "" {
slog.Debug("no onion domain defined, ignoring OnionLocation middleware")
return next
}
slog.Debug("OnionLocation middleware enabled", "onion-domain", *onionDomain)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, err := url.Parse(r.URL.String())
if err != nil {
slog.Error("[unexpected] can't parse url", "err", err, "url", r.URL.String())
next.ServeHTTP(w, r)
return
}
u.Scheme = "http"
u.Host = *onionDomain
w.Header().Add("Onion-Location", u.String())
next.ServeHTTP(w, r)
})
}
|