code wiki / (root) / nx_tracker_list.nx

nx_tracker_list.nx source

↩ module page · 83 lines · 5342 B

1// nx_tracker_list.nx -- DATA-DRIVEN tracker list parser (Cardinal 11: the tracker SET is config in 2// <bundle>/trackers.txt, not hardcoded magic). One announce URL per line; '#' comment lines + blank / 3// whitespace-only lines are skipped; a trailing CR (CRLF files) is trimmed. The worker feeds each 4// extracted URL to ht_announce_url (HTTP/HTTPS over TCP -- bypasses this network's UDP-tracker block) to 5// WIDEN peer discovery => better odds of finding the one seed holding a rare tail piece (the "stuck at 6// 99%" fix for poorly-seeded swarms). Pure logic here (gateable); the network + detached-fork glue lives 7// in the worker (nx_torrent_get). main() is the self-gate. 8// license_tier: ORIGINAL layer: peer-discovery config module: nishi-core.torrent.tracker_list 9// depends: nishi-core.syscalls 10import "nx_syscalls.nx" 11 12// Extract the next USABLE line from buf[pos..n) into out (NUL-terminated): leading spaces/tabs and a 13// trailing CR are trimmed; '#' comment lines + blank lines are skipped. Returns the resume position 14// (>=0, with out holding the line) or -1 when the buffer is exhausted. Caller loop: 15// var p = tl_next_line(buf,n,0,line); while p>=0 { use line; p = tl_next_line(buf,n,p,line) } 16func tl_next_line(buf: *u8, n: i64, pos: i64, out: *u8) -> i64 { 17 var i: i64 = pos 18 while i < n { 19 var eol: i64 = i; var f: i64 = 0 20 while f == 0 { if eol >= n { f = 1 } else { if (buf[eol] as i64) == 10 { f = 1 } else { eol = eol + 1 } } } 21 var s: i64 = i; var sf: i64 = 0 22 while sf == 0 { if s >= eol { sf = 1 } else { let c0: i64 = buf[s] as i64; if c0 == 32 { s = s + 1 } else { if c0 == 9 { s = s + 1 } else { sf = 1 } } } } 23 var k: i64 = 0; var c: i64 = s 24 while c < eol { if k < 511 { out[k] = buf[c]; k = k + 1 } c = c + 1 } 25 if k > 0 { if (out[k-1] as i64) == 13 { k = k - 1 } } 26 out[k] = 0 as u8 27 let nexti: i64 = eol + 1 28 var good: i64 = 0 29 if k > 0 { if (out[0] as i64) != 35 { good = 1 } } 30 if good == 1 { return nexti } 31 i = nexti 32 } 33 return 0 - 1 34} 35 36// ---- self-gate ----------------------------------------------------------------------------------- 37func tl_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 38func tl_pn(v: i64) -> i64 { 39 let b: *u8 = sys_mmap(28); var m: i64 = v; let t: *u8 = sys_mmap(28); var k: i64 = 0 40 if m == 0 { t[0] = 48 as u8; k = 1 } 41 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 42 var i: i64 = 0; while i < k { b[i] = t[k-1-i]; i = i + 1 } 43 sys_write(1, b, k); return 0 44} 45func tl_streq(a: *u8, b: *u8) -> i64 { var i: i64 = 0; while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } if b[i] != (0 as u8) { return 0 } return 1 } 46func tl_report(label: *u8, ok: i64) -> i64 { if ok == 1 { tl_puts(" [PASS] " as *u8) } else { tl_puts(" [FAIL] " as *u8) } tl_puts(label); tl_puts("\n" as *u8); return 0 } 47 48func main() -> i64 { 49 var pass: i64 = 0; var tot: i64 = 0 50 // a trackers.txt-shaped buffer: comment, URL, blank, whitespace-only, CRLF URL, mid-list comment, URL 51 let src: *u8 = "# nishi trackers\nhttp://a.test:1/announce\n\n \nhttps://b.test:2/announce\r\n#disabled://x\nhttp://c.test:3/announce" as *u8 52 var n: i64 = 0; while src[n] != (0 as u8) { n = n + 1 } 53 let line: *u8 = sys_mmap(512) 54 let g0: *u8 = sys_mmap(512); let g1: *u8 = sys_mmap(512); let g2: *u8 = sys_mmap(512) 55 var cnt: i64 = 0 56 var p: i64 = tl_next_line(src, n, 0, line) 57 while p >= 0 { 58 if cnt == 0 { var z: i64=0; while line[z]!=(0 as u8){g0[z]=line[z];z=z+1} g0[z]=0 as u8 } 59 if cnt == 1 { var z: i64=0; while line[z]!=(0 as u8){g1[z]=line[z];z=z+1} g1[z]=0 as u8 } 60 if cnt == 2 { var z: i64=0; while line[z]!=(0 as u8){g2[z]=line[z];z=z+1} g2[z]=0 as u8 } 61 cnt = cnt + 1 62 p = tl_next_line(src, n, p, line) 63 } 64 var ok1: i64 = 0; if cnt == 3 { ok1 = 1 } 65 tot=tot+1; if ok1==1 { pass=pass+1 } tl_report("extracts exactly 3 URLs (skips comment/blank/whitespace/#)" as *u8, ok1) 66 let ok2: i64 = tl_streq(g0, "http://a.test:1/announce" as *u8) 67 tot=tot+1; if ok2==1 { pass=pass+1 } tl_report("URL[0] = http://a.test:1/announce" as *u8, ok2) 68 let ok3: i64 = tl_streq(g1, "https://b.test:2/announce" as *u8) 69 tot=tot+1; if ok3==1 { pass=pass+1 } tl_report("URL[1] CRLF trimmed = https://b.test:2/announce" as *u8, ok3) 70 let ok4: i64 = tl_streq(g2, "http://c.test:3/announce" as *u8) 71 tot=tot+1; if ok4==1 { pass=pass+1 } tl_report("URL[2] after mid-list comment = http://c.test:3/announce" as *u8, ok4) 72 // all-comment/blank buffer -> 0 usable lines 73 let src2: *u8 = "# only\n\n \n#x" as *u8 74 var n2: i64 = 0; while src2[n2] != (0 as u8) { n2 = n2 + 1 } 75 let l2: *u8 = sys_mmap(512); var c2: i64 = 0; var p2: i64 = tl_next_line(src2, n2, 0, l2) 76 while p2 >= 0 { c2 = c2 + 1; p2 = tl_next_line(src2, n2, p2, l2) } 77 var ok5: i64 = 0; if c2 == 0 { ok5 = 1 } 78 tot=tot+1; if ok5==1 { pass=pass+1 } tl_report("all-comment/blank buffer -> 0 URLs" as *u8, ok5) 79 80 tl_puts("TRACKER-LIST-GATE pass=" as *u8); tl_pn(pass); tl_puts("/" as *u8); tl_pn(tot) 81 if pass == tot { tl_puts(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 82 tl_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1 83}