2015-08-17 17:52:47 +08:00
|
|
|
package tracker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type torrent struct {
|
|
|
|
Leechers int32
|
|
|
|
Seeders int32
|
|
|
|
Peers util.CompactIPv4Peers
|
|
|
|
}
|
|
|
|
|
|
|
|
type server struct {
|
|
|
|
pc net.PacketConn
|
|
|
|
conns map[int64]struct{}
|
|
|
|
t map[[20]byte]torrent
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshal(parts ...interface{}) (ret []byte, err error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
for _, p := range parts {
|
|
|
|
err = binary.Write(&buf, binary.BigEndian, p)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = buf.Bytes()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (s *server) respond(addr net.Addr, rh ResponseHeader, parts ...interface{}) (err error) {
|
2015-08-17 17:52:47 +08:00
|
|
|
b, err := marshal(append([]interface{}{rh}, parts...)...)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
_, err = s.pc.WriteTo(b, addr)
|
2015-08-17 17:52:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (s *server) newConn() (ret int64) {
|
2015-08-17 17:52:47 +08:00
|
|
|
ret = rand.Int63()
|
2016-04-19 12:11:11 +08:00
|
|
|
if s.conns == nil {
|
|
|
|
s.conns = make(map[int64]struct{})
|
2015-08-17 17:52:47 +08:00
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
s.conns[ret] = struct{}{}
|
2015-08-17 17:52:47 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (s *server) serveOne() (err error) {
|
2015-08-17 17:52:47 +08:00
|
|
|
b := make([]byte, 0x10000)
|
2016-04-19 12:11:11 +08:00
|
|
|
n, addr, err := s.pc.ReadFrom(b)
|
2015-08-17 17:52:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r := bytes.NewReader(b[:n])
|
|
|
|
var h RequestHeader
|
|
|
|
err = readBody(r, &h)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch h.Action {
|
2016-02-07 14:16:01 +08:00
|
|
|
case ActionConnect:
|
2015-08-17 17:52:47 +08:00
|
|
|
if h.ConnectionId != connectRequestConnectionId {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
connId := s.newConn()
|
|
|
|
err = s.respond(addr, ResponseHeader{
|
2016-02-07 14:16:01 +08:00
|
|
|
ActionConnect,
|
2015-08-17 17:52:47 +08:00
|
|
|
h.TransactionId,
|
|
|
|
}, ConnectionResponse{
|
|
|
|
connId,
|
|
|
|
})
|
|
|
|
return
|
2016-02-07 14:16:01 +08:00
|
|
|
case ActionAnnounce:
|
2016-04-19 12:11:11 +08:00
|
|
|
if _, ok := s.conns[h.ConnectionId]; !ok {
|
|
|
|
s.respond(addr, ResponseHeader{
|
2015-08-17 17:52:47 +08:00
|
|
|
TransactionId: h.TransactionId,
|
2016-02-07 14:16:01 +08:00
|
|
|
Action: ActionError,
|
2015-08-17 17:52:47 +08:00
|
|
|
}, []byte("not connected"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var ar AnnounceRequest
|
|
|
|
err = readBody(r, &ar)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
t := s.t[ar.InfoHash]
|
2015-08-17 17:52:47 +08:00
|
|
|
b, err = t.Peers.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-04-19 12:11:11 +08:00
|
|
|
err = s.respond(addr, ResponseHeader{
|
2015-08-17 17:52:47 +08:00
|
|
|
TransactionId: h.TransactionId,
|
2016-02-07 14:16:01 +08:00
|
|
|
Action: ActionAnnounce,
|
2015-08-17 17:52:47 +08:00
|
|
|
}, AnnounceResponseHeader{
|
|
|
|
Interval: 900,
|
|
|
|
Leechers: t.Leechers,
|
|
|
|
Seeders: t.Seeders,
|
|
|
|
}, b)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unhandled action: %d", h.Action)
|
2016-04-19 12:11:11 +08:00
|
|
|
s.respond(addr, ResponseHeader{
|
2015-08-17 17:52:47 +08:00
|
|
|
TransactionId: h.TransactionId,
|
2016-02-07 14:16:01 +08:00
|
|
|
Action: ActionError,
|
2015-08-17 17:52:47 +08:00
|
|
|
}, []byte("unhandled action"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|