2016-08-03 09:11:52 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
|
2016-08-17 03:42:08 +02:00
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ParseAnnounce parses an bittorrent.AnnounceRequest from an http.Request.
|
|
|
|
//
|
|
|
|
// If allowIPSpoofing is true, IPs provided via params will be used.
|
|
|
|
// If realIPHeader is not empty string, the first value of the HTTP Header with
|
|
|
|
// that name will be used.
|
|
|
|
func ParseAnnounce(r *http.Request, realIPHeader string, allowIPSpoofing bool) (*bittorrent.AnnounceRequest, error) {
|
|
|
|
qp, err := NewQueryParams(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
request := &bittorrent.AnnounceRequest{Params: qp}
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
eventStr, _ := qp.String("event")
|
2016-08-03 09:11:52 +02:00
|
|
|
request.Event, err = bittorrent.NewEvent(eventStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to provide valid client event")
|
|
|
|
}
|
|
|
|
|
|
|
|
compactStr, _ := qp.String("compact")
|
|
|
|
request.Compact = compactStr != "" && compactStr != "0"
|
|
|
|
|
|
|
|
infoHashes := qp.InfoHashes()
|
|
|
|
if len(infoHashes) < 1 {
|
|
|
|
return nil, bittorrent.ClientError("no info_hash parameter supplied")
|
|
|
|
}
|
|
|
|
if len(infoHashes) > 1 {
|
|
|
|
return nil, bittorrent.ClientError("multiple info_hash parameters supplied")
|
|
|
|
}
|
|
|
|
request.InfoHash = infoHashes[0]
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
peerID, ok := qp.String("peer_id")
|
|
|
|
if !ok {
|
2016-08-03 09:11:52 +02:00
|
|
|
return nil, bittorrent.ClientError("failed to parse parameter: peer_id")
|
|
|
|
}
|
|
|
|
if len(peerID) != 20 {
|
|
|
|
return nil, bittorrent.ClientError("failed to provide valid peer_id")
|
|
|
|
}
|
2016-08-05 07:47:04 +02:00
|
|
|
request.Peer.ID = bittorrent.PeerIDFromString(peerID)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
request.Left, err = qp.Uint64("left")
|
|
|
|
if err != nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to parse parameter: left")
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Downloaded, err = qp.Uint64("downloaded")
|
|
|
|
if err != nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to parse parameter: downloaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Uploaded, err = qp.Uint64("uploaded")
|
|
|
|
if err != nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to parse parameter: uploaded")
|
|
|
|
}
|
|
|
|
|
2016-08-04 22:26:44 +02:00
|
|
|
numwant, err := qp.Uint64("numwant")
|
|
|
|
if err != nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to parse parameter: numwant")
|
|
|
|
}
|
2016-08-05 07:47:04 +02:00
|
|
|
request.NumWant = uint32(numwant)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
|
|
|
port, err := qp.Uint64("port")
|
|
|
|
if err != nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to parse parameter: port")
|
|
|
|
}
|
2016-08-05 07:47:04 +02:00
|
|
|
request.Peer.Port = uint16(port)
|
2016-08-03 09:11:52 +02:00
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
request.Peer.IP = requestedIP(r, qp, realIPHeader, allowIPSpoofing)
|
|
|
|
if request.Peer.IP == nil {
|
|
|
|
return nil, bittorrent.ClientError("failed to parse peer IP address")
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseScrape parses an bittorrent.ScrapeRequest from an http.Request.
|
2016-08-05 07:47:04 +02:00
|
|
|
func ParseScrape(r *http.Request) (*bittorrent.ScrapeRequest, error) {
|
2016-08-03 09:11:52 +02:00
|
|
|
qp, err := NewQueryParams(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
infoHashes := qp.InfoHashes()
|
|
|
|
if len(infoHashes) < 1 {
|
|
|
|
return nil, bittorrent.ClientError("no info_hash parameter supplied")
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &bittorrent.ScrapeRequest{
|
|
|
|
InfoHashes: infoHashes,
|
2016-08-05 07:47:04 +02:00
|
|
|
Params: qp,
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return request, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// requestedIP determines the IP address for a BitTorrent client request.
|
|
|
|
//
|
|
|
|
// If allowIPSpoofing is true, IPs provided via params will be used.
|
|
|
|
// If realIPHeader is not empty string, the first value of the HTTP Header with
|
|
|
|
// that name will be used.
|
2016-08-05 07:47:04 +02:00
|
|
|
func requestedIP(r *http.Request, p bittorrent.Params, realIPHeader string, allowIPSpoofing bool) net.IP {
|
2016-08-03 09:11:52 +02:00
|
|
|
if allowIPSpoofing {
|
2016-08-05 07:47:04 +02:00
|
|
|
if ipstr, ok := p.String("ip"); ok {
|
|
|
|
ip := net.ParseIP(ipstr)
|
|
|
|
return ip
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
if ipstr, ok := p.String("ipv4"); ok {
|
|
|
|
ip := net.ParseIP(ipstr)
|
|
|
|
return ip
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
if ipstr, ok := p.String("ipv6"); ok {
|
|
|
|
ip := net.ParseIP(ipstr)
|
|
|
|
return ip
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if realIPHeader != "" {
|
|
|
|
if ips, ok := r.Header[realIPHeader]; ok && len(ips) > 0 {
|
2016-08-05 07:47:04 +02:00
|
|
|
ip := net.ParseIP(ips[0])
|
|
|
|
return ip
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-05 07:47:04 +02:00
|
|
|
host, _, _ := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
return net.ParseIP(host)
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|