Promote Tor

This commit is contained in:
Jan-Lukas Else 2021-05-09 14:42:53 +02:00
parent 4653e4480c
commit bfee141015
7 changed files with 37 additions and 6 deletions

View File

@ -104,13 +104,15 @@ func cacheMiddleware(next http.Handler) http.Handler {
} }
func cacheKey(r *http.Request) string { func cacheKey(r *http.Request) string {
def := cacheURLString(r.URL) key := cacheURLString(r.URL)
// Special cases // Special cases
if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest { if asRequest, ok := r.Context().Value(asRequestKey).(bool); ok && asRequest {
return "as-" + def key = "as-" + key
} }
// Default if torUsed, ok := r.Context().Value(torUsedKey).(bool); ok && torUsed {
return def key = "tor-" + key
}
return key
} }
func cacheURLString(u *url.URL) string { func cacheURLString(u *url.URL) string {

View File

@ -615,7 +615,7 @@ func securityHeaders(next http.Handler) http.Handler {
w.Header().Set("X-Xss-Protection", "1; mode=block") w.Header().Set("X-Xss-Protection", "1; mode=block")
w.Header().Set("Content-Security-Policy", "default-src 'self'"+cspDomains) w.Header().Set("Content-Security-Policy", "default-src 'self'"+cspDomains)
if appConfig.Server.Tor && torAddress != "" { 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) next.ServeHTTP(w, r)
}) })

View File

@ -198,11 +198,13 @@ func initRendering() error {
type renderData struct { type renderData struct {
BlogString string BlogString string
Canonical string Canonical string
TorAddress string
Blog *configBlog Blog *configBlog
Data interface{} Data interface{}
LoggedIn bool LoggedIn bool
CommentsEnabled bool CommentsEnabled bool
WebmentionReceivingEnabled bool WebmentionReceivingEnabled bool
TorUsed bool
} }
func render(w http.ResponseWriter, r *http.Request, template string, data *renderData) { 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 { if data.Data == nil {
data.Data = map[string]interface{}{} 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 data.CommentsEnabled = data.Blog.Comments != nil && data.Blog.Comments.Enabled
// Check if able to receive webmentions // Check if able to receive webmentions
data.WebmentionReceivingEnabled = appConfig.Webmention == nil || !appConfig.Webmention.DisableReceiving 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 // Minify and write response
mw := minifier.Writer(contentTypeHTML, w) mw := minifier.Writer(contentTypeHTML, w)
defer func() { defer func() {

View File

@ -1,10 +1,19 @@
{{ define "footer" }} {{ define "footer" }}
<footer> <footer>
{{ with menu .Blog "footer" }} {{ with menu .Blog "footer" }}
<p>
{{ range $i, $item := .Items }} {{ range $i, $item := .Items }}
{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a> {{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
{{ end }} {{ end }}
</p>
{{ end }} {{ end }}
<p translate="no">&copy; {{ dateformat now "2006" }} {{ with user.Name }}{{ . }}{{ else }}{{ .Blog.Title }}{{ end }}</p> <p translate="no">&copy; {{ dateformat now "2006" }} {{ with user.Name }}{{ . }}{{ else }}{{ .Blog.Title }}{{ end }}</p>
{{ if .TorUsed }}
<p>🔐 {{ string .Blog.Lang "connectedviator" }}</p>
{{ else }}
{{ if .TorAddress }}
<p>🔓 <a href="{{ .TorAddress }}">{{ string .Blog.Lang "connectviator" }}</a> <a href="https://www.torproject.org/" target="_blank" rel="nofollow noopener noreferrer">{{ string .Blog.Lang "whatistor" }}</a></p>
{{ end }}
{{ end }}
</footer> </footer>
{{ end }} {{ end }}

View File

@ -3,6 +3,8 @@ chars: "Buchstaben"
comment: "Kommentar" comment: "Kommentar"
comments: "Kommentare" comments: "Kommentare"
confirmdelete: "Löschen bestätigen" confirmdelete: "Löschen bestätigen"
connectedviator: "Verbunden über Tor."
connectviator: "Über Tor verbinden."
create: "Erstellen" create: "Erstellen"
delete: "Löschen" delete: "Löschen"
docomment: "Kommentieren" docomment: "Kommentieren"
@ -33,6 +35,7 @@ update: "Aktualisieren"
updatedon: "Aktualisiert am" updatedon: "Aktualisiert am"
upload: "Hochladen" upload: "Hochladen"
view: "Anschauen" view: "Anschauen"
whatistor: "Was ist Tor?"
withoutdate: "Ohne Datum" withoutdate: "Ohne Datum"
words: "Wörter" words: "Wörter"
wordsperpost: "Wörter pro Post" wordsperpost: "Wörter pro Post"

View File

@ -8,6 +8,8 @@ chars: "Characters"
comment: "Comment" comment: "Comment"
comments: "Comments" comments: "Comments"
confirmdelete: "Confirm deletion" confirmdelete: "Confirm deletion"
connectedviator: "Connected via Tor."
connectviator: "Connect via Tor."
create: "Create" create: "Create"
delete: "Delete" delete: "Delete"
docomment: "Comment" docomment: "Comment"
@ -52,6 +54,7 @@ verified: "Verified"
view: "View" view: "View"
webmentions: "Webmentions" webmentions: "Webmentions"
websiteopt: "Website (optional)" websiteopt: "Website (optional)"
whatistor: "What is Tor?"
withoutdate: "Without date" withoutdate: "Without date"
words: "Words" words: "Words"
wordsperpost: "Words per post" wordsperpost: "Words per post"

7
tor.go
View File

@ -13,12 +13,15 @@ import (
"time" "time"
"github.com/cretz/bine/tor" "github.com/cretz/bine/tor"
"github.com/go-chi/chi/v5/middleware"
) )
var ( var (
torAddress string torAddress string
) )
var torUsedKey requestContextKey = "tor"
func startOnionService(h http.Handler) error { func startOnionService(h http.Handler) error {
torDataPath, err := filepath.Abs("data/tor") torDataPath, err := filepath.Abs("data/tor")
if err != nil { if err != nil {
@ -75,9 +78,11 @@ func startOnionService(h http.Handler) error {
defer onion.Close() defer onion.Close()
torAddress = onion.String() torAddress = onion.String()
log.Println("Onion service published on http://" + torAddress) log.Println("Onion service published on http://" + torAddress)
// Clear cache
purgeCache()
// Serve handler // Serve handler
s := &http.Server{ s := &http.Server{
Handler: h, Handler: middleware.WithValue(torUsedKey, true)(h),
ReadTimeout: 5 * time.Minute, ReadTimeout: 5 * time.Minute,
WriteTimeout: 5 * time.Minute, WriteTimeout: 5 * time.Minute,
} }