nx_tracker_live.nx source
↩ module page · 97 lines · 6395 B
1// nx_tracker_live.nx -- LIVE BEP-15 tracker announce (X-TORRENT-LIVE-001 L2): resolve real trackers
2// (nx_dns_a) + UDP connect+announce a real info_hash -> REAL peers. Tries a LIST of public UDP
3// trackers (like a real client) and reports each, succeeding on the first that returns peers. Test
4// target: Big Buck Bunny (the standard legal/well-seeded CC test torrent). GATE = LIVE: any tracker
5// -> >=1 peer. Composes nx_dns_a_resolve + nx_udp_tracker build/parse. license_tier: ORIGINAL
6//
7// module: nishi-core.torrent.tracker_live
8// depends: nishi-core.torrent.dns_a, nishi-core.torrent.udp_tracker
9import "nx_dns_a.nx"
10import "nx_udp_tracker.nx"
11const K_MAGIC_305419896: i64 = 305419896
12const K_MAGIC_1000000000: i64 = 1000000000
13const K_MAGIC_6881: i64 = 6881
14const K_MAGIC_2048: i64 = 2048
15const K_MAGIC_1337: i64 = 1337
16const K_MAGIC_6969: i64 = 6969
17
18func tl_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
19func tl_wn(fd: i64, v: i64) -> i64 {
20 let t: *u8=sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}
21 var k: i64=0; if m==0 {t[0]=48 as u8;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}
22 let b: *u8=sys_mmap(28); var i: i64=0; while i<k {b[i]=t[k-1-i]; i=i+1}; sys_write(fd,b,k); return 0
23}
24func tl_sockaddr(sa: *u8, port: i64, i0: i64, i1: i64, i2: i64, i3: i64) -> i64 {
25 sa[0]=2 as u8; sa[1]=0 as u8
26 sa[2]=((port>>8)&0xff) as u8; sa[3]=(port&0xff) as u8
27 sa[4]=i0 as u8; sa[5]=i1 as u8; sa[6]=i2 as u8; sa[7]=i3 as u8
28 var k: i64=8; while k<16 { sa[k]=0 as u8; k=k+1 }
29 return 0
30}
31func tl_ip(a: i64) -> i64 { return a }
32
33// try one tracker: returns peer count (>=0), -1 connect-timeout, -2 dns-fail. fills ip4 for report.
34func tl_try(host: *u8, port: i64, ih: *u8, pid: *u8, ip: *u8, peer0: *i64) -> i64 {
35 ip[0]=0 as u8; ip[1]=0 as u8; ip[2]=0 as u8; ip[3]=0 as u8
36 if nx_dns_a_resolve(host, ip) != 1 { return 0 - 2 }
37 let fd: i64 = sys_socket(AF_INET, SOCK_DGRAM, 0)
38 if fd < 0 { return 0 - 1 }
39 sys_set_socket_timeout(fd, 3)
40 let dest: *u8 = sys_mmap(16); tl_sockaddr(dest, port, ip[0] as i64, ip[1] as i64, ip[2] as i64, ip[3] as i64)
41 let txid: i64 = K_MAGIC_305419896
42 let creq: *u8 = sys_mmap(32); nx_udp_build_connect(txid, creq); sys_sendto(fd, creq, 16, 0, dest, 16)
43 let cresp: *u8 = sys_mmap(64); let cn: i64 = sys_recvfrom(fd, cresp, 64, 0, 0 as *u8, 0 as *i64)
44 let oc: *i64 = sys_mmap(16) as *i64
45 if cn < 16 { sys_close(fd); return 0 - 1 }
46 if nx_udp_parse_connect_resp(cresp, cn, txid, oc) != 1 { sys_close(fd); return 0 - 1 }
47 let areq: *u8 = sys_mmap(128); nx_udp_build_announce(oc[0], txid, ih, pid, 0, K_MAGIC_1000000000, 0, 2, 50, K_MAGIC_6881, areq)
48 sys_sendto(fd, areq, 98, 0, dest, 16)
49 let aresp: *u8 = sys_mmap(K_MAGIC_2048); let an: i64 = sys_recvfrom(fd, aresp, K_MAGIC_2048, 0, 0 as *u8, 0 as *i64)
50 sys_close(fd)
51 if an < 20 { return 0 - 1 }
52 let oiv: *i64 = sys_mmap(16) as *i64; let osd: *i64 = sys_mmap(16) as *i64; let olc: *i64 = sys_mmap(16) as *i64
53 let ips: *i64 = sys_mmap(8*64) as *i64; let ports: *i64 = sys_mmap(8*64) as *i64
54 let np: i64 = nx_udp_parse_announce_resp(aresp, an, txid, oiv, osd, olc, ips, ports, 64)
55 if np >= 1 { peer0[0] = (ips[0] << 16) | ports[0] } // pack ip(approx)/port for a quick report
56 if np >= 1 { peer0[1] = ips[0]; peer0[2] = ports[0] }
57 return np
58}
59
60func tl_report(host: *u8, ip: *u8, rc: i64, peer0: *i64) -> i64 {
61 tl_w(1, " " as *u8); tl_w(1, host); tl_w(1, " ip=" as *u8)
62 tl_wn(1, ip[0] as i64); tl_w(1, "." as *u8); tl_wn(1, ip[1] as i64); tl_w(1, "." as *u8); tl_wn(1, ip[2] as i64); tl_w(1, "." as *u8); tl_wn(1, ip[3] as i64)
63 if rc == 0 - 2 { tl_w(1, " -> dns-fail\n" as *u8) }
64 if rc == 0 - 1 { tl_w(1, " -> no-response (down/blocked)\n" as *u8) }
65 if rc >= 0 { tl_w(1, " -> peers=" as *u8); tl_wn(1, rc)
66 if rc >= 1 { let a: i64 = peer0[1]; tl_w(1, " peer0=" as *u8); tl_wn(1, (a>>24)&0xff); tl_w(1, "." as *u8); tl_wn(1, (a>>16)&0xff); tl_w(1, "." as *u8); tl_wn(1, (a>>8)&0xff); tl_w(1, "." as *u8); tl_wn(1, a&0xff); tl_w(1, ":" as *u8); tl_wn(1, peer0[2]) }
67 tl_w(1, "\n" as *u8) }
68 return 0
69}
70
71func main() -> i64 {
72 let ih: *u8 = sys_mmap(20)
73 ih[0]=221 as u8; ih[1]=130 as u8; ih[2]=85 as u8; ih[3]=236 as u8; ih[4]=220 as u8
74 ih[5]=124 as u8; ih[6]=165 as u8; ih[7]=95 as u8; ih[8]=176 as u8; ih[9]=187 as u8
75 ih[10]=248 as u8; ih[11]=19 as u8; ih[12]=35 as u8; ih[13]=216 as u8; ih[14]=112 as u8
76 ih[15]=98 as u8; ih[16]=219 as u8; ih[17]=31 as u8; ih[18]=109 as u8; ih[19]=28 as u8
77 let pid: *u8 = sys_mmap(20); let ps: *u8 = "-NX0001-000000000000" as *u8; var z: i64 = 0; while z < 20 { pid[z] = ps[z]; z = z + 1 }
78
79 tl_w(1, "TRACKERLIVE info_hash=BigBuckBunny trying public UDP trackers:\n" as *u8)
80 let ip: *u8 = sys_mmap(4); let p0: *i64 = sys_mmap(64) as *i64
81 var best: i64 = 0
82 var got_peer0: i64 = 0; var got_port: i64 = 0
83
84 let r1: i64 = tl_try("tracker.opentrackr.org" as *u8, K_MAGIC_1337, ih, pid, ip, p0); tl_report("tracker.opentrackr.org" as *u8, ip, r1, p0); if r1 > best { best = r1; got_peer0 = p0[1]; got_port = p0[2] }
85 let r2: i64 = tl_try("open.demonii.com" as *u8, K_MAGIC_1337, ih, pid, ip, p0); tl_report("open.demonii.com" as *u8, ip, r2, p0); if r2 > best { best = r2; got_peer0 = p0[1]; got_port = p0[2] }
86 let r3: i64 = tl_try("tracker.openbittorrent.com" as *u8, K_MAGIC_6969, ih, pid, ip, p0); tl_report("tracker.openbittorrent.com" as *u8, ip, r3, p0); if r3 > best { best = r3; got_peer0 = p0[1]; got_port = p0[2] }
87 let r4: i64 = tl_try("exodus.desync.com" as *u8, K_MAGIC_6969, ih, pid, ip, p0); tl_report("exodus.desync.com" as *u8, ip, r4, p0); if r4 > best { best = r4; got_peer0 = p0[1]; got_port = p0[2] }
88 let r5: i64 = tl_try("open.stealth.si" as *u8, 80, ih, pid, ip, p0); tl_report("open.stealth.si" as *u8, ip, r5, p0); if r5 > best { best = r5; got_peer0 = p0[1]; got_port = p0[2] }
89
90 if best >= 1 {
91 tl_w(1, "TRACKERLIVE verdict=GREEN best_peers=" as *u8); tl_wn(1, best)
92 tl_w(1, " (REAL peers from a REAL tracker)\n" as *u8); sys_exit(0); return 0
93 }
94 tl_w(1, "TRACKERLIVE verdict=RED (no tracker returned peers -- if ALL no-response, UDP egress to tracker ports may be blocked; next try HTTP/TCP trackers)\n" as *u8)
95 sys_exit(1)
96 return 1
97}