mirror of
https://github.com/meehow/privtracker.git
synced 2026-09-02 22:56:07 +08:00
check FQDN
This commit is contained in:
13
announce.go
13
announce.go
@@ -17,7 +17,8 @@ type AnnounceResponse struct {
|
|||||||
Complete int `bencode:"complete"`
|
Complete int `bencode:"complete"`
|
||||||
Incomplete int `bencode:"incomplete"`
|
Incomplete int `bencode:"incomplete"`
|
||||||
Peers []byte `bencode:"peers"`
|
Peers []byte `bencode:"peers"`
|
||||||
PeersIPv6 []byte `bencode:"peers_ipv6"`
|
PeersIPv6 []byte `bencode:"peers6,omitempty"`
|
||||||
|
ExternalIP []byte `bencode:"external ip,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func announce(w http.ResponseWriter, r *http.Request) {
|
func announce(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -65,6 +66,7 @@ func announce(w http.ResponseWriter, r *http.Request) {
|
|||||||
Incomplete: numLeechers,
|
Incomplete: numLeechers,
|
||||||
Peers: peersIPv4,
|
Peers: peersIPv4,
|
||||||
PeersIPv6: peersIPv6,
|
PeersIPv6: peersIPv6,
|
||||||
|
ExternalIP: ip.To4(),
|
||||||
}
|
}
|
||||||
w.Header().Add("X-PrivTracker", fmt.Sprintf("s:%d l:%d", numSeeders, numLeechers))
|
w.Header().Add("X-PrivTracker", fmt.Sprintf("s:%d l:%d", numSeeders, numLeechers))
|
||||||
if err := bencode.Marshal(w, resp); err != nil {
|
if err := bencode.Marshal(w, resp); err != nil {
|
||||||
@@ -81,12 +83,15 @@ func getRemoteIP(r *http.Request) net.IP {
|
|||||||
ip := net.ParseIP(addr)
|
ip := net.ParseIP(addr)
|
||||||
if ip.IsPrivate() {
|
if ip.IsPrivate() {
|
||||||
ips := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
|
ips := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
|
||||||
if len(ips) > 0 {
|
for _, maybeIP := range ips {
|
||||||
ipForwarded := net.ParseIP(strings.TrimSpace(ips[0]))
|
ipForwarded := net.ParseIP(strings.TrimSpace(maybeIP))
|
||||||
if ipForwarded != nil {
|
if !ipForwarded.IsPrivate() {
|
||||||
ip = ipForwarded
|
ip = ipForwarded
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if len(ips) > 0 {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -9,9 +10,12 @@ func BenchmarkAnnounce(b *testing.B) {
|
|||||||
server := httptest.NewServer(router())
|
server := httptest.NewServer(router())
|
||||||
client := server.Client()
|
client := server.Client()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
_, err := client.Get(server.URL + "/test/announce?port=1234")
|
resp, err := client.Get(server.URL + "/test/announce?port=1234")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
b.Fatalf("unexpected status code: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
go.mod
11
go.mod
@@ -1,13 +1,12 @@
|
|||||||
module github.com/meehow/privtracker
|
module github.com/meehow/privtracker
|
||||||
|
|
||||||
go 1.22
|
go 1.24.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/jackpal/bencode-go v1.0.2
|
github.com/jackpal/bencode-go v1.0.2
|
||||||
golang.org/x/crypto v0.32.0
|
golang.org/x/crypto v0.37.0
|
||||||
|
golang.org/x/net v0.39.0
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require golang.org/x/text v0.24.0 // indirect
|
||||||
golang.org/x/net v0.21.0 // indirect
|
|
||||||
golang.org/x/text v0.21.0 // indirect
|
|
||||||
)
|
|
||||||
|
|||||||
12
go.sum
12
go.sum
@@ -1,8 +1,8 @@
|
|||||||
github.com/jackpal/bencode-go v1.0.2 h1:LcCNfZ344u0LpBPOZNjpCLps/wUOuN4r87Fy9+5yU8g=
|
github.com/jackpal/bencode-go v1.0.2 h1:LcCNfZ344u0LpBPOZNjpCLps/wUOuN4r87Fy9+5yU8g=
|
||||||
github.com/jackpal/bencode-go v1.0.2/go.mod h1:6jI9mUjO3GQbZti3JizEfxTzRfWOM8oBBcwbwlTfceI=
|
github.com/jackpal/bencode-go v1.0.2/go.mod h1:6jI9mUjO3GQbZti3JizEfxTzRfWOM8oBBcwbwlTfceI=
|
||||||
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
||||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
||||||
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
|
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
||||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
||||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -10,6 +10,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"golang.org/x/crypto/acme/autocert"
|
"golang.org/x/crypto/acme/autocert"
|
||||||
|
"golang.org/x/net/publicsuffix"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -20,7 +21,7 @@ func main() {
|
|||||||
handler := router(recoveryMiddleware, headersMiddleware, logRequestMiddleware)
|
handler := router(recoveryMiddleware, headersMiddleware, logRequestMiddleware)
|
||||||
if port == "443" {
|
if port == "443" {
|
||||||
go redirect80()
|
go redirect80()
|
||||||
fmt.Println("PrivTracker listening on https://0.0.0.0/")
|
fmt.Println("PrivTracker listening on https://0.0.0.0/ (please use your FQDN to access this server)")
|
||||||
log.Fatal(http.Serve(autocertListener(), handler))
|
log.Fatal(http.Serve(autocertListener(), handler))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("PrivTracker listening on http://0.0.0.0:%s/\n", port)
|
fmt.Printf("PrivTracker listening on http://0.0.0.0:%s/\n", port)
|
||||||
@@ -59,6 +60,10 @@ func autocertListener() net.Listener {
|
|||||||
|
|
||||||
func redirect(w http.ResponseWriter, r *http.Request) {
|
func redirect(w http.ResponseWriter, r *http.Request) {
|
||||||
url := fmt.Sprintf("https://%s/", r.Host)
|
url := fmt.Sprintf("https://%s/", r.Host)
|
||||||
|
if _, icann := publicsuffix.PublicSuffix(r.Host); !icann {
|
||||||
|
// fallback in case we can't get FQDN
|
||||||
|
url = "https://privtracker.com/"
|
||||||
|
}
|
||||||
http.Redirect(w, r, url, http.StatusMovedPermanently)
|
http.Redirect(w, r, url, http.StatusMovedPermanently)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,9 @@ func logRequestMiddleware(next http.Handler) http.Handler {
|
|||||||
|
|
||||||
func headersMiddleware(next http.Handler) http.Handler {
|
func headersMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Strict-Transport-Security", "max-age=31536000") // hsts
|
if r.TLS != nil {
|
||||||
|
w.Header().Set("Strict-Transport-Security", "max-age=31536000") // hsts
|
||||||
|
}
|
||||||
w.Header().Set("Server", "PrivTracker")
|
w.Header().Set("Server", "PrivTracker")
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user