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 {
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 {

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("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)
})

View File

@ -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() {

View File

@ -1,10 +1,19 @@
{{ define "footer" }}
<footer>
{{ with menu .Blog "footer" }}
<p>
{{ range $i, $item := .Items }}
{{ if ne $i 0 }} &bull; {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
{{ end }}
</p>
{{ end }}
<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>
{{ end }}

View File

@ -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"

View File

@ -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"

7
tor.go
View File

@ -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,
}