mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-26 17:38:14 -07:00
(untested) sanitize code
* remove peer argument from scrape swarm storage call * replace Peer field with netip.Addr in ScrapeRequest * add man for keydb storage * update readme
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"time"
|
||||
@@ -290,9 +289,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps http
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
start = time.Now()
|
||||
defer func() {
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
recordResponseDuration("announce", addr, err, time.Since(start))
|
||||
}
|
||||
recordResponseDuration("announce", addr, err, time.Since(start))
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -328,9 +325,7 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
start = time.Now()
|
||||
defer func() {
|
||||
if f.EnableRequestTiming && metrics.Enabled() {
|
||||
recordResponseDuration("scrape", addr, err, time.Since(start))
|
||||
}
|
||||
recordResponseDuration("scrape", addr, err, time.Since(start))
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -339,20 +334,7 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
host, _, err := net.SplitHostPort(r.RemoteAddr)
|
||||
if err != nil {
|
||||
log.Error("http: unable to determine remote address for scrape", log.Err(err))
|
||||
WriteError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
addr, err = netip.ParseAddr(host)
|
||||
if err != nil || addr.IsUnspecified() {
|
||||
log.Error("http: invalid IP: neither v4 nor v6", log.Fields{"remoteAddr": r.RemoteAddr})
|
||||
WriteError(w, bittorrent.ErrInvalidIP)
|
||||
return
|
||||
}
|
||||
addr = req.Addr
|
||||
|
||||
ctx := injectRouteParamsToContext(context.Background(), ps)
|
||||
ctx, resp, err := f.logic.HandleScrape(ctx, req)
|
||||
|
||||
+16
-23
@@ -115,10 +115,7 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
}
|
||||
|
||||
// Parse the IP address where the client is listening.
|
||||
ip, spoofed, err := requestedIP(r, qp, opts)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ErrInvalidIP
|
||||
}
|
||||
ip, spoofed := requestedIP(r, qp, opts)
|
||||
request.Peer.AddrPort = netip.AddrPortFrom(ip, uint16(port))
|
||||
request.IPProvided = spoofed
|
||||
|
||||
@@ -141,9 +138,12 @@ func ParseScrape(r *http.Request, opts ParseOptions) (*bittorrent.ScrapeRequest,
|
||||
return nil, errNoInfoHash
|
||||
}
|
||||
|
||||
ip, _ := requestedIP(r, qp, opts)
|
||||
|
||||
request := &bittorrent.ScrapeRequest{
|
||||
InfoHashes: infoHashes,
|
||||
Params: qp,
|
||||
Addr: ip,
|
||||
}
|
||||
|
||||
if err := bittorrent.SanitizeScrape(request, opts.MaxScrapeInfoHashes); err != nil {
|
||||
@@ -154,29 +154,22 @@ func ParseScrape(r *http.Request, opts ParseOptions) (*bittorrent.ScrapeRequest,
|
||||
}
|
||||
|
||||
// requestedIP determines the IP address for a BitTorrent client request.
|
||||
func requestedIP(r *http.Request, p bittorrent.Params, opts ParseOptions) (netip.Addr, bool, error) {
|
||||
func requestedIP(r *http.Request, p bittorrent.Params, opts ParseOptions) (ip netip.Addr, spoofed bool) {
|
||||
if opts.AllowIPSpoofing {
|
||||
if ipstr, ok := p.String("ip"); ok {
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
|
||||
if ipstr, ok := p.String("ipv4"); ok {
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
|
||||
if ipstr, ok := p.String("ipv6"); ok {
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
for _, f := range []string{"ip", "ipv4", "ipv6"} {
|
||||
if ipStr, ok := p.String(f); ok {
|
||||
spoofed = true
|
||||
ip, _ = netip.ParseAddr(ipStr)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ipstr := r.Header.Get(opts.RealIPHeader); ipstr != "" && opts.RealIPHeader != "" {
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, false, err
|
||||
if ipStr := r.Header.Get(opts.RealIPHeader); ipStr != "" && opts.RealIPHeader != "" {
|
||||
ip, _ = netip.ParseAddr(ipStr)
|
||||
}
|
||||
|
||||
addrPort, err := netip.ParseAddrPort(r.RemoteAddr)
|
||||
return addrPort.Addr(), false, err
|
||||
addrPort, _ := netip.ParseAddrPort(r.RemoteAddr)
|
||||
ip = addrPort.Addr()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user