diff --git a/cache.go b/cache.go index a33fc19..63e5f25 100644 --- a/cache.go +++ b/cache.go @@ -104,13 +104,15 @@ func cacheMiddleware(next http.Handler) http.Handler { } func cacheKey(r *http.Request) string { - def := cacheURLString(r.URL) + key := cacheURLString(r.URL) // Special cases if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest { - return "as-" + def + key = "as-" + key } - // Default - return def + if torUsed, ok := r.Context().Value(torUsedKey).(bool); ok && torUsed { + key = "tor-" + key + } + return key } func cacheURLString(u *url.URL) string { diff --git a/http.go b/http.go index 5071ff5..ca0e694 100644 --- a/http.go +++ b/http.go @@ -615,7 +615,7 @@ func securityHeaders(next http.Handler) http.Handler { w.Header().Set("X-Xss-Protection", "1; mode=block") w.Header().Set("Content-Security-Policy", "default-src 'self'"+cspDomains) if appConfig.Server.Tor && torAddress != "" { - w.Header().Set("Onion-Location", fmt.Sprintf("http://%v%v", torAddress, r.URL.Path)) + w.Header().Set("Onion-Location", fmt.Sprintf("http://%v%v", torAddress, r.RequestURI)) } next.ServeHTTP(w, r) }) diff --git a/render.go b/render.go index b809dd1..c0ec927 100644 --- a/render.go +++ b/render.go @@ -198,11 +198,13 @@ func initRendering() error { type renderData struct { BlogString string Canonical string + TorAddress string Blog *configBlog Data interface{} LoggedIn bool CommentsEnabled bool WebmentionReceivingEnabled bool + TorUsed bool } func render(w http.ResponseWriter, r *http.Request, template string, data *renderData) { @@ -223,6 +225,9 @@ func render(w http.ResponseWriter, r *http.Request, template string, data *rende } } } + if appConfig.Server.Tor && torAddress != "" { + data.TorAddress = fmt.Sprintf("http://%v%v", torAddress, r.RequestURI) + } if data.Data == nil { data.Data = map[string]interface{}{} } @@ -234,6 +239,10 @@ func render(w http.ResponseWriter, r *http.Request, template string, data *rende data.CommentsEnabled = data.Blog.Comments != nil && data.Blog.Comments.Enabled // Check if able to receive webmentions data.WebmentionReceivingEnabled = appConfig.Webmention == nil || !appConfig.Webmention.DisableReceiving + // Check if Tor request + if torUsed, ok := r.Context().Value(torUsedKey).(bool); ok && torUsed { + data.TorUsed = true + } // Minify and write response mw := minifier.Writer(contentTypeHTML, w) defer func() { diff --git a/templates/footer.gohtml b/templates/footer.gohtml index 1f22bed..0697025 100644 --- a/templates/footer.gohtml +++ b/templates/footer.gohtml @@ -1,10 +1,19 @@ {{ define "footer" }} {{ end }} \ No newline at end of file diff --git a/templates/strings/de.yaml b/templates/strings/de.yaml index bdb1be7..a94e877 100644 --- a/templates/strings/de.yaml +++ b/templates/strings/de.yaml @@ -3,6 +3,8 @@ chars: "Buchstaben" comment: "Kommentar" comments: "Kommentare" confirmdelete: "Löschen bestätigen" +connectedviator: "Verbunden über Tor." +connectviator: "Über Tor verbinden." create: "Erstellen" delete: "Löschen" docomment: "Kommentieren" @@ -33,6 +35,7 @@ update: "Aktualisieren" updatedon: "Aktualisiert am" upload: "Hochladen" view: "Anschauen" +whatistor: "Was ist Tor?" withoutdate: "Ohne Datum" words: "Wörter" wordsperpost: "Wörter pro Post" diff --git a/templates/strings/default.yaml b/templates/strings/default.yaml index ae8b73a..ba49371 100644 --- a/templates/strings/default.yaml +++ b/templates/strings/default.yaml @@ -8,6 +8,8 @@ chars: "Characters" comment: "Comment" comments: "Comments" confirmdelete: "Confirm deletion" +connectedviator: "Connected via Tor." +connectviator: "Connect via Tor." create: "Create" delete: "Delete" docomment: "Comment" @@ -52,6 +54,7 @@ verified: "Verified" view: "View" webmentions: "Webmentions" websiteopt: "Website (optional)" +whatistor: "What is Tor?" withoutdate: "Without date" words: "Words" wordsperpost: "Words per post" diff --git a/tor.go b/tor.go index 069a2df..ab98e45 100644 --- a/tor.go +++ b/tor.go @@ -13,12 +13,15 @@ import ( "time" "github.com/cretz/bine/tor" + "github.com/go-chi/chi/v5/middleware" ) var ( torAddress string ) +var torUsedKey requestContextKey = "tor" + func startOnionService(h http.Handler) error { torDataPath, err := filepath.Abs("data/tor") if err != nil { @@ -75,9 +78,11 @@ func startOnionService(h http.Handler) error { defer onion.Close() torAddress = onion.String() log.Println("Onion service published on http://" + torAddress) + // Clear cache + purgeCache() // Serve handler s := &http.Server{ - Handler: h, + Handler: middleware.WithValue(torUsedKey, true)(h), ReadTimeout: 5 * time.Minute, WriteTimeout: 5 * time.Minute, }