2015-01-01 18:02:25 +01:00
|
|
|
// Copyright 2015 The Chihaya Authors. All rights reserved.
|
2014-07-17 06:54:53 +02:00
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2014-07-23 21:56:15 +02:00
|
|
|
"errors"
|
|
|
|
"net"
|
2014-07-17 06:54:53 +02:00
|
|
|
"net/http"
|
2014-07-23 21:56:15 +02:00
|
|
|
"strconv"
|
2014-07-17 06:54:53 +02:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/config"
|
2014-07-17 07:00:26 +02:00
|
|
|
"github.com/chihaya/chihaya/http/query"
|
2014-07-17 06:54:53 +02:00
|
|
|
"github.com/chihaya/chihaya/tracker/models"
|
|
|
|
)
|
|
|
|
|
2015-02-20 07:36:49 +01:00
|
|
|
// newAnnounce parses an HTTP request and generates a models.Announce.
|
2015-02-20 08:06:44 +01:00
|
|
|
func (s *Server) newAnnounce(r *http.Request, p httprouter.Params) (*models.Announce, error) {
|
2014-07-17 06:54:53 +02:00
|
|
|
q, err := query.New(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
compact := q.Params["compact"] != "0"
|
|
|
|
event, _ := q.Params["event"]
|
2015-02-20 08:06:44 +01:00
|
|
|
numWant := requestedPeerCount(q, s.config.NumWantFallback)
|
2014-07-17 06:54:53 +02:00
|
|
|
|
|
|
|
infohash, exists := q.Params["info_hash"]
|
|
|
|
if !exists {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
peerID, exists := q.Params["peer_id"]
|
|
|
|
if !exists {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
2015-03-29 09:03:19 +02:00
|
|
|
port, err := q.Uint64("port")
|
2014-07-17 06:54:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
2015-03-29 09:03:19 +02:00
|
|
|
left, err := q.Uint64("left")
|
2014-07-17 06:54:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
2015-08-20 06:17:00 +02:00
|
|
|
ipv4, ipv6, err := requestedIP(q, r, &s.config.NetConfig)
|
2014-07-17 06:54:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
2015-08-20 06:17:00 +02:00
|
|
|
ipv4Endpoint := models.Endpoint{ipv4, uint16(port)}
|
|
|
|
ipv6Endpoint := models.Endpoint{ipv6, uint16(port)}
|
|
|
|
|
2014-07-17 06:54:53 +02:00
|
|
|
downloaded, err := q.Uint64("downloaded")
|
|
|
|
if err != nil {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
uploaded, err := q.Uint64("uploaded")
|
|
|
|
if err != nil {
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.Announce{
|
2015-02-20 08:06:44 +01:00
|
|
|
Config: s.config,
|
2014-07-17 06:54:53 +02:00
|
|
|
Compact: compact,
|
|
|
|
Downloaded: downloaded,
|
|
|
|
Event: event,
|
2015-03-29 09:03:19 +02:00
|
|
|
IPv4: ipv4Endpoint,
|
|
|
|
IPv6: ipv6Endpoint,
|
2014-07-17 06:54:53 +02:00
|
|
|
Infohash: infohash,
|
|
|
|
Left: left,
|
|
|
|
NumWant: numWant,
|
|
|
|
Passkey: p.ByName("passkey"),
|
|
|
|
PeerID: peerID,
|
|
|
|
Uploaded: uploaded,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:36:49 +01:00
|
|
|
// newScrape parses an HTTP request and generates a models.Scrape.
|
2015-02-20 08:06:44 +01:00
|
|
|
func (s *Server) newScrape(r *http.Request, p httprouter.Params) (*models.Scrape, error) {
|
2014-07-17 06:54:53 +02:00
|
|
|
q, err := query.New(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.Infohashes == nil {
|
|
|
|
if _, exists := q.Params["info_hash"]; !exists {
|
|
|
|
// There aren't any infohashes.
|
|
|
|
return nil, models.ErrMalformedRequest
|
|
|
|
}
|
|
|
|
q.Infohashes = []string{q.Params["info_hash"]}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &models.Scrape{
|
2015-02-20 08:06:44 +01:00
|
|
|
Config: s.config,
|
2014-07-17 06:54:53 +02:00
|
|
|
|
|
|
|
Passkey: p.ByName("passkey"),
|
|
|
|
Infohashes: q.Infohashes,
|
|
|
|
}, nil
|
|
|
|
}
|
2014-07-23 21:56:15 +02:00
|
|
|
|
|
|
|
// requestedPeerCount returns the wanted peer count or the provided fallback.
|
|
|
|
func requestedPeerCount(q *query.Query, fallback int) int {
|
|
|
|
if numWantStr, exists := q.Params["numwant"]; exists {
|
|
|
|
numWant, err := strconv.Atoi(numWantStr)
|
|
|
|
if err != nil {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
return numWant
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
2015-08-20 06:17:00 +02:00
|
|
|
// requestedIP returns the IP address for a request. If there are multiple in
|
|
|
|
// the request, one IPv4 and one IPv6 will be returned.
|
|
|
|
func requestedIP(q *query.Query, r *http.Request, cfg *config.NetConfig) (v4, v6 net.IP, err error) {
|
2014-07-23 21:56:15 +02:00
|
|
|
var done bool
|
|
|
|
|
|
|
|
if cfg.AllowIPSpoofing {
|
|
|
|
if str, ok := q.Params["ip"]; ok {
|
2015-08-20 06:17:00 +02:00
|
|
|
if v4, v6, done = getIPs(str, v4, v6, cfg); done {
|
2014-07-23 21:56:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if str, ok := q.Params["ipv4"]; ok {
|
2015-08-20 06:17:00 +02:00
|
|
|
if v4, v6, done = getIPs(str, v4, v6, cfg); done {
|
2014-07-23 21:56:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if str, ok := q.Params["ipv6"]; ok {
|
2015-08-20 06:17:00 +02:00
|
|
|
if v4, v6, done = getIPs(str, v4, v6, cfg); done {
|
2014-07-23 21:56:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.RealIPHeader != "" {
|
2014-07-23 21:59:07 +02:00
|
|
|
if xRealIPs, ok := r.Header[cfg.RealIPHeader]; ok {
|
2015-08-20 06:17:00 +02:00
|
|
|
if v4, v6, done = getIPs(string(xRealIPs[0]), v4, v6, cfg); done {
|
2014-07-23 21:56:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2015-03-29 09:03:19 +02:00
|
|
|
if r.RemoteAddr == "" && v4 == nil {
|
2015-08-20 06:17:00 +02:00
|
|
|
if v4, v6, done = getIPs("127.0.0.1", v4, v6, cfg); done {
|
2015-03-29 09:03:19 +02:00
|
|
|
return
|
2014-07-23 21:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 06:17:00 +02:00
|
|
|
if v4, v6, done = getIPs(r.RemoteAddr, v4, v6, cfg); done {
|
2015-03-29 09:03:19 +02:00
|
|
|
return
|
2014-07-23 21:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v4 == nil && v6 == nil {
|
|
|
|
err = errors.New("failed to parse IP address")
|
|
|
|
}
|
2015-03-29 09:03:19 +02:00
|
|
|
|
2014-07-23 21:56:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-20 06:17:00 +02:00
|
|
|
func getIPs(ipstr string, ipv4, ipv6 net.IP, cfg *config.NetConfig) (net.IP, net.IP, bool) {
|
|
|
|
host, _, err := net.SplitHostPort(ipstr)
|
2015-03-29 09:03:19 +02:00
|
|
|
if err != nil {
|
|
|
|
host = ipstr
|
|
|
|
}
|
2014-07-23 21:56:15 +02:00
|
|
|
|
2015-03-29 09:03:19 +02:00
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
ipTo4 := ip.To4()
|
|
|
|
if ipv4 == nil && ipTo4 != nil {
|
2015-08-20 06:17:00 +02:00
|
|
|
ipv4 = ipTo4
|
2015-03-29 09:03:19 +02:00
|
|
|
} else if ipv6 == nil && ipTo4 == nil {
|
2015-08-20 06:17:00 +02:00
|
|
|
ipv6 = ip
|
2014-07-23 21:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-29 09:03:19 +02:00
|
|
|
var done bool
|
2014-07-23 21:56:15 +02:00
|
|
|
if cfg.DualStackedPeers {
|
|
|
|
done = ipv4 != nil && ipv6 != nil
|
|
|
|
} else {
|
|
|
|
done = ipv4 != nil || ipv6 != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipv4, ipv6, done
|
|
|
|
}
|