mirror of https://github.com/jlelse/GoBlog
Promote Tor
This commit is contained in:
parent
4653e4480c
commit
bfee141015
10
cache.go
10
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 {
|
||||
|
|
2
http.go
2
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)
|
||||
})
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -1,10 +1,19 @@
|
|||
{{ define "footer" }}
|
||||
<footer>
|
||||
{{ with menu .Blog "footer" }}
|
||||
<p>
|
||||
{{ range $i, $item := .Items }}
|
||||
{{ if ne $i 0 }} • {{ end }}<a href="{{ $item.Link }}">{{ $item.Title }}</a>
|
||||
{{ end }}
|
||||
</p>
|
||||
{{ end }}
|
||||
<p translate="no">© {{ 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 }}
|
|
@ -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"
|
||||
|
|
|
@ -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
7
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,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue