code wiki / (root) / nx_tracker_refresh.nx

nx_tracker_refresh.nx source

↩ module page · 72 lines · 5138 B

1// nx_tracker_refresh.nx -- GROW the tracker list (operator: "trackers list needs to grow, not be static"). 2// Fetches live public tracker lists over the sovereign TLS-1.3 stack (nx_https_fetch_follow + Mozilla CA), 3// keeps only http/https announce URLs (UDP goes via cl_announce; tg_wide_to_pex reads http/https only), and 4// MERGES them into <trackers.txt> (dedup by exact URL, preserving our curated header + entries). Re-run daily 5// (hostctl trackerrefresh / cron) => the set grows as the public lists update. Fail-safe: any fetch/CA failure 6// leaves the existing file untouched (no-op), so a network blip never empties our trackers. 7// argv[1] = trackers.txt path (default /volume1/ai/torrent/trackers.txt). license_tier: ORIGINAL 8import "nx_syscalls.nx" 9import "nx_x509_trust_store.nx" 10import "nx_trust_store_load_from_certdata.nx" 11import "nx_https_fetch_follow.nx" 12const K_MAGIC_4194304: i64 = 4194304 13const K_MAGIC_1048576: i64 = 1048576 14const K_MAGIC_262144: i64 = 262144 15const K_MAGIC_262143: i64 = 262143 16 17func tr_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 18func tr_putn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(28); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } while k>0 { k=k-1; sys_write(1,(((t as i64)+k) as *u8),1) } return 0 } 19func tr_is_http(buf: *u8, ls: i64, le: i64) -> i64 { let h: *u8="http" as *u8; var i: i64=0; while i<4 { if ls+i>=le { return 0 } if buf[ls+i]!=h[i] { return 0 } i=i+1 } return 1 } 20func tr_has(hay: *u8, hlen: i64, ndl: *u8, nlen: i64) -> i64 { 21 if nlen<=0 { return 0 } 22 var s: i64=0; while s+nlen<=hlen { var m: i64=1; var k: i64=0; while k<nlen { if hay[s+k]!=ndl[k] { m=0; k=nlen } else { k=k+1 } } if m==1 { return 1 } s=s+1 } 23 return 0 24} 25// fetch `url`, extract http/https announce lines from the body, append the NEW ones to ex (dedup). Returns new exn. 26func tr_fetch_merge(url: *u8, store: *TrustStore, out: *u8, cap: i64, ex: *u8, exn: i64) -> i64 { 27 let status: *i64 = sys_mmap(8) as *i64 28 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status) 29 tr_puts(" fetch "); tr_puts(url); tr_puts(" status="); tr_putn(status[0]); tr_puts(" bytes="); tr_putn(n); tr_puts("\n" as *u8) 30 if n <= 0 { return exn } 31 var body: i64=0; var bi: i64=0; var fnd: i64=0 32 while fnd==0 { if bi+3>=n { fnd=1 } else { if out[bi]==(13 as u8) { if out[bi+1]==(10 as u8) { if out[bi+2]==(13 as u8) { if out[bi+3]==(10 as u8) { body=bi+4; fnd=1 } } } } if fnd==0 { bi=bi+1 } } } 33 var added: i64=0; var ls: i64=body; var e: i64=exn 34 while ls < n { 35 var le: i64=ls; var eol: i64=0 36 while eol==0 { if le>=n { eol=1 } else { if out[le]==(10 as u8) { eol=1 } else { le=le+1 } } } 37 var e2: i64=le; if e2>ls { if out[e2-1]==(13 as u8) { e2=e2-1 } } 38 let llen: i64=e2-ls 39 if llen>=8 { if tr_is_http(out,ls,e2)==1 { 40 if tr_has(ex, e, (((out as i64)+ls) as *u8), llen)==0 { 41 var c: i64=0; while c<llen { ex[e]=out[ls+c]; e=e+1; c=c+1 } ex[e]=10 as u8; e=e+1 42 added=added+1 43 } 44 } } 45 ls=le+1 46 } 47 tr_puts(" +"); tr_putn(added); tr_puts(" new http/https tracker(s)\n" as *u8) 48 return e 49} 50 51func main(argc: i64, argv: *i64) -> i64 { 52 var tpath: *u8 = "/volume1/ai/torrent/trackers.txt" as *u8 53 if argc>1 { tpath = argv[1] as *u8 } 54 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 55 if r <= 0 { tr_puts("tracker-refresh: CA load failed -> keeping existing list (no-op)\n" as *u8); sys_exit(0); return 0 } 56 let store: *TrustStore = r as *TrustStore 57 let cap: i64 = K_MAGIC_1048576; let out: *u8 = sys_mmap(cap) 58 let ex: *u8 = sys_mmap(K_MAGIC_262144); var exn: i64 = 0 59 let efd: i64 = sys_openat_rd(tpath) 60 if efd>=0 { var rr: i64=1; while rr>0 { rr=sys_read(efd, (((ex as i64)+exn) as *u8), K_MAGIC_262143-exn); if rr>0 { exn=exn+rr } } sys_close(efd) } 61 if exn==0 { let hdr: *u8="# tracker list (auto-grown by nx_tracker_refresh; http/https only)\n" as *u8; var h: i64=0; while hdr[h]!=(0 as u8){ex[exn]=hdr[h];exn=exn+1;h=h+1} } 62 if exn>0 { if ex[exn-1]!=(10 as u8) { ex[exn]=10 as u8; exn=exn+1 } } 63 let before: i64 = exn 64 exn = tr_fetch_merge("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_best.txt" as *u8, store, out, cap, ex, exn) 65 exn = tr_fetch_merge("https://raw.githubusercontent.com/ngosang/trackerslist/master/trackers_all_http.txt" as *u8, store, out, cap, ex, exn) 66 if exn <= before { tr_puts("tracker-refresh: no new trackers (list unchanged)\n" as *u8); sys_exit(0); return 0 } 67 let wfd: i64 = sys_openat_wr(tpath, 0x1a4) 68 if wfd<0 { tr_puts(" cannot write "); tr_puts(tpath); tr_puts(" (no-op)\n" as *u8); sys_exit(0); return 0 } 69 sys_write(wfd, ex, exn); sys_close(wfd) 70 tr_puts("tracker-refresh: "); tr_puts(tpath); tr_puts(" grew "); tr_putn(before); tr_puts(" -> "); tr_putn(exn); tr_puts(" bytes\n" as *u8) 71 sys_exit(0); return 0 72}