Add AllowIPSpoofing configuration

This commit is contained in:
Justin Li 2014-07-23 13:08:52 -04:00
parent bd1fa3eb24
commit 14843b9e89
5 changed files with 19 additions and 14 deletions

View file

@ -50,6 +50,7 @@ type Config struct {
Freeleech bool `json:"freeleech"` Freeleech bool `json:"freeleech"`
Whitelist bool `json:"whitelist"` Whitelist bool `json:"whitelist"`
PurgeInactiveTorrents bool `json:"purge_inactive_torrents"` PurgeInactiveTorrents bool `json:"purge_inactive_torrents"`
AllowIPSpoofing bool `json:"allow_ip_spoofing"`
Announce Duration `json:"announce"` Announce Duration `json:"announce"`
MinAnnounce Duration `json:"min_announce"` MinAnnounce Duration `json:"min_announce"`
@ -77,6 +78,7 @@ var DefaultConfig = Config{
Freeleech: false, Freeleech: false,
Whitelist: false, Whitelist: false,
PurgeInactiveTorrents: true, PurgeInactiveTorrents: true,
AllowIPSpoofing: true,
Announce: Duration{30 * time.Minute}, Announce: Duration{30 * time.Minute},
MinAnnounce: Duration{15 * time.Minute}, MinAnnounce: Duration{15 * time.Minute},

View file

@ -14,6 +14,7 @@
"freeleech": false, "freeleech": false,
"whitelist": false, "whitelist": false,
"purge_inactive_torrents": true, "purge_inactive_torrents": true,
"allow_ip_spoofing": true,
"announce": "30m", "announce": "30m",
"min_announce": "15m", "min_announce": "15m",

View file

@ -122,7 +122,8 @@ func (q Query) RequestedPeerCount(fallback int) int {
} }
// RequestedIP returns the requested IP address from a Query. // RequestedIP returns the requested IP address from a Query.
func (q Query) RequestedIP(r *http.Request) (net.IP, error) { func (q Query) RequestedIP(r *http.Request, allowSpoofing bool) (net.IP, error) {
if allowSpoofing {
if ipstr, ok := q.Params["ip"]; ok { if ipstr, ok := q.Params["ip"]; ok {
if ip := net.ParseIP(ipstr); ip != nil { if ip := net.ParseIP(ipstr); ip != nil {
return ip, nil return ip, nil
@ -140,6 +141,7 @@ func (q Query) RequestedIP(r *http.Request) (net.IP, error) {
return ip, nil return ip, nil
} }
} }
}
if xRealIPs, ok := q.Params["x-real-ip"]; ok { if xRealIPs, ok := q.Params["x-real-ip"]; ok {
if ip := net.ParseIP(string(xRealIPs[0])); ip != nil { if ip := net.ParseIP(string(xRealIPs[0])); ip != nil {

View file

@ -35,7 +35,7 @@ func NewAnnounce(cfg *config.Config, r *http.Request, p httprouter.Params) (*mod
return nil, models.ErrMalformedRequest return nil, models.ErrMalformedRequest
} }
ip, err := q.RequestedIP(r) ip, err := q.RequestedIP(r, cfg.AllowIPSpoofing)
if err != nil { if err != nil {
return nil, models.ErrMalformedRequest return nil, models.ErrMalformedRequest
} }

View file

@ -48,7 +48,7 @@ func (w *Writer) WriteAnnounce(res *models.AnnounceResponse) error {
} }
} }
} else if res.IPv4Peers != nil || res.IPv6Peers != nil { } 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) bencoder := bencode.NewEncoder(w)