From 14843b9e8916ef6a22279c993dc1122070140882 Mon Sep 17 00:00:00 2001 From: Justin Li Date: Wed, 23 Jul 2014 13:08:52 -0400 Subject: [PATCH] Add AllowIPSpoofing configuration --- config/config.go | 2 ++ example.json | 1 + http/query/query.go | 26 ++++++++++++++------------ http/tracker.go | 2 +- http/writer.go | 2 +- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/config/config.go b/config/config.go index 60b7b9c..58d422f 100644 --- a/config/config.go +++ b/config/config.go @@ -50,6 +50,7 @@ type Config struct { Freeleech bool `json:"freeleech"` Whitelist bool `json:"whitelist"` PurgeInactiveTorrents bool `json:"purge_inactive_torrents"` + AllowIPSpoofing bool `json:"allow_ip_spoofing"` Announce Duration `json:"announce"` MinAnnounce Duration `json:"min_announce"` @@ -77,6 +78,7 @@ var DefaultConfig = Config{ Freeleech: false, Whitelist: false, PurgeInactiveTorrents: true, + AllowIPSpoofing: true, Announce: Duration{30 * time.Minute}, MinAnnounce: Duration{15 * time.Minute}, diff --git a/example.json b/example.json index 802e658..103abac 100644 --- a/example.json +++ b/example.json @@ -14,6 +14,7 @@ "freeleech": false, "whitelist": false, "purge_inactive_torrents": true, + "allow_ip_spoofing": true, "announce": "30m", "min_announce": "15m", diff --git a/http/query/query.go b/http/query/query.go index 973afd1..793d9b3 100644 --- a/http/query/query.go +++ b/http/query/query.go @@ -122,22 +122,24 @@ func (q Query) RequestedPeerCount(fallback int) int { } // RequestedIP returns the requested IP address from a Query. -func (q Query) RequestedIP(r *http.Request) (net.IP, error) { - if ipstr, ok := q.Params["ip"]; ok { - if ip := net.ParseIP(ipstr); ip != nil { - return ip, nil +func (q Query) RequestedIP(r *http.Request, allowSpoofing bool) (net.IP, error) { + if allowSpoofing { + if ipstr, ok := q.Params["ip"]; ok { + if ip := net.ParseIP(ipstr); ip != nil { + return ip, nil + } } - } - if ipstr, ok := q.Params["ipv4"]; ok { - if ip := net.ParseIP(ipstr); ip != nil { - return ip, nil + if ipstr, ok := q.Params["ipv4"]; ok { + if ip := net.ParseIP(ipstr); ip != nil { + return ip, nil + } } - } - if ipstr, ok := q.Params["ipv6"]; ok { - if ip := net.ParseIP(ipstr); ip != nil { - return ip, nil + if ipstr, ok := q.Params["ipv6"]; ok { + if ip := net.ParseIP(ipstr); ip != nil { + return ip, nil + } } } diff --git a/http/tracker.go b/http/tracker.go index 3762bc9..fafe6b7 100644 --- a/http/tracker.go +++ b/http/tracker.go @@ -35,7 +35,7 @@ func NewAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*mod return nil, models.ErrMalformedRequest } - ip, err := q.RequestedIP(r) + ip, err := q.RequestedIP(r, cfg.AllowIPSpoofing) if err != nil { return nil, models.ErrMalformedRequest } diff --git a/http/writer.go b/http/writer.go index 66a179d..8cce11c 100644 --- a/http/writer.go +++ b/http/writer.go @@ -48,7 +48,7 @@ func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error { } } } else if res.IPv4Peers != nil || res.IPv6Peers != nil { - dict["peers"] = peersList(res.IPv6Peers, res.IPv4Peers) + dict["peers"] = peersList(res.IPv4Peers, res.IPv6Peers) } bencoder := bencode.NewEncoder(w)