@@ -6,13 +6,11 @@ import (
66 "net/netip"
77 "strconv"
88
9- "github.com/crimist/trakx/config"
109 "github.com/crimist/trakx/pools"
11- "github.com/crimist/trakx/tracker/stats"
12- "github.com/crimist/trakx/tracker/storage"
10+ "github.com/crimist/trakx/storage"
1311)
1412
15- type announceParams struct {
13+ type announceParameters struct {
1614 compact bool
1715 nopeerid bool
1816 noneleft bool
@@ -23,92 +21,75 @@ type announceParams struct {
2321 numwant string
2422}
2523
26- func (t * Tracker ) announce (conn net.Conn , vals * announceParams , ip netip.Addr ) {
27- stats .Announces .Add (1 )
24+ func (tracker * Tracker ) announce (conn net.Conn , parameters * announceParameters , addr netip.Addr ) {
25+ tracker . stats .Announces .Add (1 )
2826
29- // get vars
3027 var hash storage.Hash
3128 var peerid storage.PeerID
3229
33- // hash
34- if len (vals .hash ) != 20 {
35- t .clientError (conn , "Invalid infohash" )
30+ if len (parameters .hash ) != 20 {
31+ writeFailure (conn , "Invalid infohash" )
3632 return
3733 }
38- copy (hash [:], vals .hash )
34+ copy (hash [:], parameters .hash )
3935
40- // peerid
41- if len (vals .peerid ) != 20 {
42- t .clientError (conn , "Invalid peerid" )
36+ if len (parameters .peerid ) != 20 {
37+ writeFailure (conn , "Invalid peerid" )
4338 return
4439 }
45- copy (peerid [:], vals .peerid )
40+ copy (peerid [:], parameters .peerid )
4641
47- // get if stop before continuing
48- if vals .event == "stopped" {
49- t .peerdb .Drop (hash , peerid )
42+ if parameters .event == "stopped" {
43+ tracker .peerdb .PeerRemove (hash , peerid )
5044 conn .Write (httpSuccessBytes )
5145 return
5246 }
5347
54- // port
55- portInt , err := strconv .Atoi (vals .port )
48+ portInt , err := strconv .Atoi (parameters .port )
5649 if err != nil || (portInt > 65535 || portInt < 1 ) {
57- t . clientError (conn , "Invalid port" )
50+ writeFailure (conn , "Invalid port" )
5851 return
5952 }
6053
61- // numwant
62- numwant := config .Config .Numwant .Default
63-
64- if vals .numwant != "" {
65- numwantInt , err := strconv .Atoi (vals .numwant )
54+ numwant := tracker .config .DefaultNumwant
55+ if parameters .numwant != "" {
56+ numwantInt , err := strconv .Atoi (parameters .numwant )
6657 if err != nil || numwantInt < 0 {
67- t . clientError (conn , "Invalid numwant" )
58+ writeFailure (conn , "Invalid numwant" )
6859 return
6960 }
70- numwantUint := uint (numwantInt )
7161
72- // if numwant is within our limit than listen to the client
73- if numwantUint <= config .Config .Numwant .Limit {
74- numwant = numwantUint
75- } else {
76- numwant = config .Config .Numwant .Limit
77- }
62+ numwant = min (uint (numwantInt ), tracker .config .MaximumNumwant )
7863 }
7964
8065 peerComplete := false
81- if vals .event == "completed" || vals .noneleft {
66+ if parameters .event == "completed" || parameters .noneleft {
8267 peerComplete = true
8368 }
8469
85- t .peerdb .Save ( ip , uint16 (portInt ), peerComplete , hash , peerid )
86- complete , incomplete := t .peerdb .HashStats (hash )
70+ tracker .peerdb .PeerAdd ( hash , peerid , addr , uint16 (portInt ), peerComplete )
71+ seeds , leeches := tracker .peerdb .TorrentStats (hash )
8772
88- interval := int64 ( config .Config . Announce . Base . Seconds ())
89- if int32 ( config .Config . Announce . Fuzz . Seconds ()) > 0 {
90- interval += rand .Int63n ( int64 ( config .Config . Announce . Fuzz . Seconds ( )))
73+ interval := tracker . config .Interval
74+ if tracker . config .IntervalVariance > 0 {
75+ interval += uint ( rand .Int31n ( int32 ( tracker . config .IntervalVariance )))
9176 }
9277
9378 dictionary := pools .Dictionaries .Get ()
94- dictionary .Int64 ("interval" , interval )
95- dictionary .Int64 ("complete" , int64 (complete ))
96- dictionary .Int64 ("incomplete" , int64 (incomplete ))
97- if vals .compact {
98- peers4 , peers6 := t .peerdb .PeerListBytes (hash , numwant )
79+ dictionary .Int64 ("interval" , int64 ( interval ) )
80+ dictionary .Int64 ("complete" , int64 (seeds ))
81+ dictionary .Int64 ("incomplete" , int64 (leeches ))
82+ if parameters .compact {
83+ peers4 , peers6 := tracker .peerdb .TorrentPeersCompact (hash , uint ( numwant ), storage . IPv4 | storage . IPv6 )
9984 dictionary .StringBytes ("peers" , peers4 )
10085 dictionary .StringBytes ("peers6" , peers6 )
10186
10287 pools .Peerlists4 .Put (peers4 )
10388 pools .Peerlists6 .Put (peers6 )
10489 } else {
105- dictionary .BytesliceSlice ("peers" , t .peerdb .PeerList (hash , numwant , vals .nopeerid ))
90+ dictionary .BytesliceSlice ("peers" , tracker .peerdb .TorrentPeers (hash , numwant , parameters .nopeerid ))
10691 }
10792
108- // double write no append is more efficient when > ~250 peers in response
109- // conn.Write(httpSuccessBytes)
110- // conn.Write(d.GetBytes())
111-
11293 conn .Write (append (httpSuccessBytes , dictionary .GetBytes ()... ))
11394 pools .Dictionaries .Put (dictionary )
11495}
0 commit comments