mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-27 01:48:09 -07:00
(untested) Merge commit e56ad81 from https://github.com/jzelinskie/chihaya
* rename/replace redis keys
This commit is contained in:
+10
-16
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
@@ -282,12 +283,12 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps http
|
||||
if f.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
var af *bittorrent.AddressFamily
|
||||
var addr netip.Addr
|
||||
defer func() {
|
||||
if f.EnableRequestTiming {
|
||||
recordResponseDuration("announce", af, err, time.Since(start))
|
||||
recordResponseDuration("announce", addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration("announce", af, err, time.Duration(0))
|
||||
recordResponseDuration("announce", addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -296,8 +297,7 @@ func (f *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, ps http
|
||||
WriteError(w, err)
|
||||
return
|
||||
}
|
||||
af = new(bittorrent.AddressFamily)
|
||||
*af = req.IP.AddressFamily
|
||||
addr = req.AddrPort.Addr()
|
||||
|
||||
ctx := injectRouteParamsToContext(context.Background(), ps)
|
||||
ctx, resp, err := f.logic.HandleAnnounce(ctx, req)
|
||||
@@ -323,12 +323,12 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
if f.EnableRequestTiming {
|
||||
start = time.Now()
|
||||
}
|
||||
var af *bittorrent.AddressFamily
|
||||
var addr netip.Addr
|
||||
defer func() {
|
||||
if f.EnableRequestTiming {
|
||||
recordResponseDuration("scrape", af, err, time.Since(start))
|
||||
recordResponseDuration("scrape", addr, err, time.Since(start))
|
||||
} else {
|
||||
recordResponseDuration("scrape", af, err, time.Duration(0))
|
||||
recordResponseDuration("scrape", addr, err, time.Duration(0))
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -345,18 +345,12 @@ func (f *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, ps httpro
|
||||
return
|
||||
}
|
||||
|
||||
reqIP := net.ParseIP(host)
|
||||
if reqIP.To4() != nil {
|
||||
req.AddressFamily = bittorrent.IPv4
|
||||
} else if len(reqIP) == net.IPv6len { // implies reqIP.To4() == nil
|
||||
req.AddressFamily = bittorrent.IPv6
|
||||
} else {
|
||||
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
|
||||
}
|
||||
af = new(bittorrent.AddressFamily)
|
||||
*af = req.AddressFamily
|
||||
|
||||
ctx := injectRouteParamsToContext(context.Background(), ps)
|
||||
ctx, resp, err := f.logic.HandleScrape(ctx, req)
|
||||
|
||||
+44
-32
@@ -2,8 +2,8 @@ package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
)
|
||||
@@ -28,6 +28,16 @@ const (
|
||||
defaultMaxScrapeInfoHashes = 50
|
||||
)
|
||||
|
||||
var (
|
||||
errNoInfoHash = bittorrent.ClientError("no info hash supplied")
|
||||
errMultipleInfoHashes = bittorrent.ClientError("multiple info hashes supplied")
|
||||
errInvalidPeerID = bittorrent.ClientError("peer ID invalid or not provided")
|
||||
errInvalidParameterLeft = bittorrent.ClientError("parameter 'left' invalid or not provided")
|
||||
errInvalidParameterDownloaded = bittorrent.ClientError("parameter 'downloaded' invalid or not provided")
|
||||
errInvalidParameterUploaded = bittorrent.ClientError("parameter 'uploaded' invalid or not provided")
|
||||
errInvalidParameterNumWant = bittorrent.ClientError("parameter 'num want' invalid or not provided")
|
||||
)
|
||||
|
||||
// ParseAnnounce parses an bittorrent.AnnounceRequest from an http.Request.
|
||||
func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequest, error) {
|
||||
qp, err := bittorrent.ParseURLData(r.RequestURI)
|
||||
@@ -41,9 +51,8 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
var eventStr string
|
||||
eventStr, request.EventProvided = qp.String("event")
|
||||
if request.EventProvided {
|
||||
request.Event, err = bittorrent.NewEvent(eventStr)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to provide valid client event")
|
||||
if request.Event, err = bittorrent.NewEvent(eventStr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
request.Event = bittorrent.None
|
||||
@@ -56,17 +65,17 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
// Parse the infohash from the request.
|
||||
infoHashes := qp.InfoHashes()
|
||||
if len(infoHashes) < 1 {
|
||||
return nil, bittorrent.ClientError("no info_hash parameter supplied")
|
||||
return nil, errNoInfoHash
|
||||
}
|
||||
if len(infoHashes) > 1 {
|
||||
return nil, bittorrent.ClientError("multiple info_hash parameters supplied")
|
||||
return nil, errMultipleInfoHashes
|
||||
}
|
||||
request.InfoHash = infoHashes[0]
|
||||
|
||||
// Parse the PeerID from the request.
|
||||
peerID, ok := qp.String("peer_id")
|
||||
if !ok {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: peer_id")
|
||||
return nil, errInvalidPeerID
|
||||
}
|
||||
request.Peer.ID, err = bittorrent.NewPeerID([]byte(peerID))
|
||||
if err != nil {
|
||||
@@ -75,25 +84,25 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
// Determine the number of remaining bytes for the client.
|
||||
request.Left, err = qp.Uint("left", 64)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: left")
|
||||
return nil, errInvalidParameterLeft
|
||||
}
|
||||
|
||||
// Determine the number of bytes downloaded by the client.
|
||||
request.Downloaded, err = qp.Uint("downloaded", 64)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: downloaded")
|
||||
return nil, errInvalidParameterDownloaded
|
||||
}
|
||||
|
||||
// Determine the number of bytes shared by the client.
|
||||
request.Uploaded, err = qp.Uint("uploaded", 64)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: uploaded")
|
||||
return nil, errInvalidParameterUploaded
|
||||
}
|
||||
|
||||
// Determine the number of peers the client wants in the response.
|
||||
numwant, err := qp.Uint("numwant", 32)
|
||||
if err != nil && !errors.Is(err, bittorrent.ErrKeyNotFound) {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: numwant")
|
||||
return nil, errInvalidParameterNumWant
|
||||
}
|
||||
// If there were no errors, the user actually provided the numwant.
|
||||
request.NumWantProvided = err == nil
|
||||
@@ -102,21 +111,22 @@ func ParseAnnounce(r *http.Request, opts ParseOptions) (*bittorrent.AnnounceRequ
|
||||
// Parse the port where the client is listening.
|
||||
port, err := qp.Uint("port", 16)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ClientError("failed to parse parameter: port")
|
||||
return nil, bittorrent.ErrInvalidPort
|
||||
}
|
||||
request.Peer.Port = uint16(port)
|
||||
|
||||
// Parse the IP address where the client is listening.
|
||||
request.Peer.IP.IP, request.IPProvided = requestedIP(r, qp, opts)
|
||||
if request.Peer.IP.IP == nil {
|
||||
return nil, bittorrent.ClientError("failed to parse peer IP address")
|
||||
ip, spoofed, err := requestedIP(r, qp, opts)
|
||||
if err != nil {
|
||||
return nil, bittorrent.ErrInvalidIP
|
||||
}
|
||||
request.Peer.AddrPort = netip.AddrPortFrom(ip, uint16(port))
|
||||
request.IPProvided = spoofed
|
||||
|
||||
if err = bittorrent.SanitizeAnnounce(request, opts.MaxNumWant, opts.DefaultNumWant); err != nil {
|
||||
request = nil
|
||||
}
|
||||
|
||||
if err := bittorrent.SanitizeAnnounce(request, opts.MaxNumWant, opts.DefaultNumWant); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return request, nil
|
||||
return request, err
|
||||
}
|
||||
|
||||
// ParseScrape parses an bittorrent.ScrapeRequest from an http.Request.
|
||||
@@ -128,7 +138,7 @@ func ParseScrape(r *http.Request, opts ParseOptions) (*bittorrent.ScrapeRequest,
|
||||
|
||||
infoHashes := qp.InfoHashes()
|
||||
if len(infoHashes) < 1 {
|
||||
return nil, bittorrent.ClientError("no info_hash parameter supplied")
|
||||
return nil, errNoInfoHash
|
||||
}
|
||||
|
||||
request := &bittorrent.ScrapeRequest{
|
||||
@@ -144,27 +154,29 @@ 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) (ip net.IP, provided bool) {
|
||||
func requestedIP(r *http.Request, p bittorrent.Params, opts ParseOptions) (netip.Addr, bool, error) {
|
||||
if opts.AllowIPSpoofing {
|
||||
if ipstr, ok := p.String("ip"); ok {
|
||||
return net.ParseIP(ipstr), true
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
|
||||
if ipstr, ok := p.String("ipv4"); ok {
|
||||
return net.ParseIP(ipstr), true
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
|
||||
if ipstr, ok := p.String("ipv6"); ok {
|
||||
return net.ParseIP(ipstr), true
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, true, err
|
||||
}
|
||||
}
|
||||
|
||||
if opts.RealIPHeader != "" {
|
||||
if ip := r.Header.Get(opts.RealIPHeader); ip != "" {
|
||||
return net.ParseIP(ip), false
|
||||
}
|
||||
if ipstr := r.Header.Get(opts.RealIPHeader); ipstr != "" && opts.RealIPHeader != "" {
|
||||
addr, err := netip.ParseAddr(ipstr)
|
||||
return addr, false, err
|
||||
}
|
||||
|
||||
host, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||||
return net.ParseIP(host), false
|
||||
addrPort, err := netip.ParseAddrPort(r.RemoteAddr)
|
||||
return addrPort.Addr(), false, err
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package http
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
"github.com/sot-tech/mochi/bittorrent"
|
||||
"github.com/sot-tech/mochi/pkg/metrics"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -24,7 +26,7 @@ var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
|
||||
|
||||
// recordResponseDuration records the duration of time to respond to a Request
|
||||
// in milliseconds.
|
||||
func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) {
|
||||
func recordResponseDuration(action string, addr netip.Addr, err error, duration time.Duration) {
|
||||
var errString string
|
||||
if err != nil {
|
||||
var clientErr bittorrent.ClientError
|
||||
@@ -35,16 +37,7 @@ func recordResponseDuration(action string, af *bittorrent.AddressFamily, err err
|
||||
}
|
||||
}
|
||||
|
||||
var afString string
|
||||
if af == nil {
|
||||
afString = "Unknown"
|
||||
} else if *af == bittorrent.IPv4 {
|
||||
afString = "IPv4"
|
||||
} else if *af == bittorrent.IPv6 {
|
||||
afString = "IPv6"
|
||||
}
|
||||
|
||||
promResponseDurationMilliseconds.
|
||||
WithLabelValues(action, afString, errString).
|
||||
WithLabelValues(action, metrics.AddressFamily(addr), errString).
|
||||
Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond))
|
||||
}
|
||||
|
||||
+10
-14
@@ -98,29 +98,25 @@ func WriteScrapeResponse(w http.ResponseWriter, resp *bittorrent.ScrapeResponse)
|
||||
}
|
||||
|
||||
func compact4(peer bittorrent.Peer) (buf []byte) {
|
||||
if ip := peer.IP.To4(); ip == nil {
|
||||
panic("non-IPv4 IP for Peer in IPv4Peers")
|
||||
} else {
|
||||
buf = ip
|
||||
}
|
||||
buf = append(buf, byte(peer.Port>>8), byte(peer.Port))
|
||||
ip := peer.AddrPort.Addr().As4()
|
||||
buf = append(buf, ip[:]...)
|
||||
port := peer.AddrPort.Port()
|
||||
buf = append(buf, byte(port>>8), byte(port&0xff))
|
||||
return
|
||||
}
|
||||
|
||||
func compact6(peer bittorrent.Peer) (buf []byte) {
|
||||
if ip := peer.IP.To16(); ip == nil {
|
||||
panic("non-IPv6 IP for Peer in IPv6Peers")
|
||||
} else {
|
||||
buf = ip
|
||||
}
|
||||
buf = append(buf, byte(peer.Port>>8), byte(peer.Port))
|
||||
ip := peer.AddrPort.Addr().As16()
|
||||
buf = append(buf, ip[:]...)
|
||||
port := peer.AddrPort.Port()
|
||||
buf = append(buf, byte(port>>8), byte(port&0xff))
|
||||
return
|
||||
}
|
||||
|
||||
func dict(peer bittorrent.Peer) map[string]any {
|
||||
return map[string]any{
|
||||
"peer id": string(peer.ID[:]),
|
||||
"ip": peer.IP.String(),
|
||||
"port": peer.Port,
|
||||
"ip": peer.Addr(),
|
||||
"port": peer.Port(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user