nx_media_inspect.nx source
↩ module page · 125 lines · 7908 B
1// nx_media_inspect.nx -- DEEP sovereign media inspector CLI. Thin wrapper over nx_media_deepcrawl: crawls a
2// page's resource graph over sovereign TLS (following CSS/JS incl. URLs the JS constructs from constants,
3// reconstructed by nx_js_urlfold WITHOUT executing anything), harvests media from every hop, prints the list,
4// dumps the top page's source, and writes the shared HTML media panel. Never runs page JS -> robust on the ad
5// bundles that segfault a tree-walker. Same crawler the in-browser 'm' key uses.
6// usage: nx_media_inspect <url> [save] -- "save" also DOWNLOADS every media file to knowledge/media/inspect/
7// 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"
12import "nx_media_harvest.nx"
13import "nx_media_deepcrawl.nx"
14const MI_MAGIC_4096: i64 = 4096
15const MI_MAGIC_1024: i64 = 1024
16const MI_MAGIC_1280: i64 = 1280
17const MI_MAGIC_4194304: i64 = 4194304
18
19const MI_HTML: i64 = 12582912
20const MI_FETCH: i64 = 28
21const MI_DEPTH: i64 = 2
22const MI_DLBUF: i64 = 67108864 // 64 MB per-file download buffer (covers videos)
23
24func mi_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 }
25func mi_n(fd: i64, v0: i64) -> i64 { var v: i64=v0; if v<0 { sys_write(fd,"-" as *u8,1); v=0-v } let b: *u8=sys_mmap(24); var k: i64=0; if v==0{b[0]=48 as u8;k=1} while v>0{b[k]=(48+(v%10)) as u8;v=v/10;k=k+1} let o: *u8=sys_mmap(24); var j: i64=0; while j<k{o[j]=b[k-1-j];j=j+1} sys_write(fd,o,k); return 0 }
26func mi_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
27func mi_write_file(path: *u8, buf: *u8, n: i64) -> i64 {
28 let fd: i64 = __syscall(257, 0-100, path as i64, 577, 420, 0, 0)
29 if fd < 0 { return 0 - 1 }
30 var done: i64 = 0
31 while done < n { let w: i64 = sys_write(fd, ((buf as i64)+done) as *u8, n-done); if w <= 0 { done = n } else { done = done + w } }
32 __syscall(3, fd, 0, 0, 0, 0, 0)
33 return n
34}
35func mi_mkdir(path: *u8) -> i64 { return __syscall(258, 0-100, path as i64, 0x1ff, 0, 0, 0) } // mkdirat(AT_FDCWD, 0777)
36func mi_wn_str(out: *u8, o: i64, v: i64) -> i64 { var w: i64=o; if v==0 { out[w]=48 as u8; return w+1 } let b: *u8=sys_mmap(24); var m: i64=v; var k: i64=0; while m>0 { b[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var j: i64=k-1; while j>=0 { out[w]=b[j]; w=w+1; j=j-1 } return w }
37
38// safe "NNN_basename" filename from url[0..ulen) into out (sanitize non [A-Za-z0-9._-] -> '_'). returns len.
39func mi_savename(url: *u8, ulen: i64, idx: i64, out: *u8) -> i64 {
40 var end: i64 = ulen; var i: i64 = 0
41 while i < ulen { let c: i64=url[i] as i64; if c==63 { if end==ulen { end=i } } if c==35 { if end==ulen { end=i } } i=i+1 }
42 var slash: i64 = 0 - 1; var j: i64 = 0
43 while j < end { if url[j]==(47 as u8) { slash=j } j=j+1 }
44 let bs: i64 = slash + 1
45 var o: i64 = mi_wn_str(out, 0, idx)
46 out[o]=95 as u8; o=o+1
47 if bs >= end { out[o]=102 as u8; o=o+1; out[o]=105 as u8; o=o+1; out[o]=108 as u8; o=o+1; out[o]=101 as u8; o=o+1 }
48 var k: i64 = bs
49 while k < end {
50 let c: i64=url[k] as i64
51 var ok: i64=0
52 if c>=48 { if c<=57 { ok=1 } } if c>=65 { if c<=90 { ok=1 } } if c>=97 { if c<=122 { ok=1 } }
53 if c==46 { ok=1 } if c==45 { ok=1 } if c==95 { ok=1 }
54 if ok==1 { out[o]=url[k] } else { out[o]=95 as u8 }
55 o=o+1; k=k+1
56 }
57 out[o]=0 as u8
58 return o
59}
60
61// download every media file in M over sovereign TLS -> knowledge/media/inspect/. returns files saved.
62func mi_save_all(M: *MediaSet, store: *TrustStore) -> i64 {
63 mi_mkdir("knowledge/media" as *u8); mi_mkdir("knowledge/media/inspect" as *u8)
64 let fbuf: *u8 = sys_mmap(MI_DLBUF)
65 let fst: *i64 = sys_mmap(16) as *i64
66 let ub: *u8 = sys_mmap(MI_MAGIC_4096); let nm: *u8 = sys_mmap(MI_MAGIC_1024); let path: *u8 = sys_mmap(MI_MAGIC_1280)
67 var saved: i64 = 0; var total: i64 = 0
68 mi_w(1, " --- downloading media -> knowledge/media/inspect/ ---\n" as *u8)
69 var i: i64 = 0
70 while i < M.cnt {
71 let bp: *u8 = ((M.buf as i64)+M.off[i]) as *u8
72 var z: i64=0; while z < M.len[i] { ub[z]=bp[z]; z=z+1 } ub[M.len[i]]=0 as u8
73 if M.kind[i] == MH_KIND_STREAM { mi_w(1, " skip (HLS/DASH manifest, not a single file) " as *u8); sys_write(1, bp, M.len[i]); mi_w(1, "\n" as *u8) }
74 else {
75 let n: i64 = nx_https_fetch_follow_best(ub, store, fbuf, MI_DLBUF, 6, fst)
76 if fst[0]==200 { if n>0 {
77 mi_savename(ub, M.len[i], i, nm)
78 var po: i64 = mh_ap(path, 0, "knowledge/media/inspect/" as *u8)
79 var pk: i64=0; while nm[pk]!=(0 as u8) { path[po]=nm[pk]; po=po+1; pk=pk+1 } path[po]=0 as u8
80 mi_write_file(path, fbuf, n)
81 mi_w(1, " saved " as *u8); mi_w(1, nm); mi_w(1, " (" as *u8); mi_n(1, n); mi_w(1, " B)\n" as *u8)
82 saved=saved+1; total=total+n
83 } } else { mi_w(1, " skip status=" as *u8); mi_n(1, fst[0]); mi_w(1, " " as *u8); sys_write(1, bp, M.len[i]); mi_w(1, "\n" as *u8) }
84 }
85 i=i+1
86 }
87 mi_w(1, " downloaded " as *u8); mi_n(1, saved); mi_w(1, " files, " as *u8); mi_n(1, total); mi_w(1, " bytes -> C:\\Users\\elder\\nishi-core\\nxc2\\knowledge\\media\\inspect\\\n" as *u8)
88 return saved
89}
90
91func main(argc: i64, argv: *i64) -> i64 {
92 if argc < 2 { mi_w(1, "usage: nx_media_inspect <url> [save]\n" as *u8); return 1 }
93 let arg: *u8 = argv[1] as *u8
94 let al: i64 = mi_slen(arg)
95 let url: *u8 = sys_mmap(MI_MAGIC_4096)
96 var has_scheme: i64 = 0
97 if al >= 4 { if arg[0]==(104 as u8) { if arg[1]==(116 as u8) { if arg[2]==(116 as u8) { if arg[3]==(112 as u8) { has_scheme = 1 } } } } }
98 if has_scheme == 1 { var i: i64=0; while i<al { url[i]=arg[i]; i=i+1 } url[al]=0 as u8 }
99 else { var o: i64 = mh_ap(url, 0, "https://" as *u8); var i2: i64=0; while i2<al { url[o]=arg[i2]; o=o+1; i2=i2+1 } url[o]=0 as u8 }
100 let ul: i64 = mi_slen(url)
101
102 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, MI_MAGIC_4194304)
103 if r <= 0 { mi_w(1, "trust store load failed\n" as *u8); return 2 }
104 let store: *TrustStore = r as *TrustStore
105
106 mi_w(1, "nx_media_inspect (deep crawl): " as *u8); mi_w(1, url); mi_w(1, "\n" as *u8)
107 let M: *MediaSet = nx_mediaset_new()
108 let fetched: i64 = nx_media_deepcrawl(url, ul, store, M, MI_FETCH, MI_DEPTH, 1, "knowledge/status/media_page_raw.html" as *u8)
109
110 var ci: i64=0; var cg: i64=0; var cv: i64=0; var cs: i64=0; var ca: i64=0
111 var t: i64 = 0
112 while t < M.cnt { let kk: i64=M.kind[t]; if kk==MH_KIND_IMAGE {ci=ci+1} if kk==MH_KIND_GIF {cg=cg+1} if kk==MH_KIND_VIDEO {cv=cv+1} if kk==MH_KIND_STREAM {cs=cs+1} if kk==MH_KIND_AUDIO {ca=ca+1} t=t+1 }
113 mi_w(1, " fetched=" as *u8); mi_n(1, fetched); mi_w(1, " media found=" as *u8); mi_n(1, M.cnt)
114 mi_w(1, " (image=" as *u8); mi_n(1, ci); mi_w(1, " gif=" as *u8); mi_n(1, cg); mi_w(1, " video=" as *u8); mi_n(1, cv); mi_w(1, " stream=" as *u8); mi_n(1, cs); mi_w(1, " audio=" as *u8); mi_n(1, ca); mi_w(1, ")\n" as *u8)
115 var d: i64 = 0
116 while d < M.cnt { let bp: *u8=((M.buf as i64)+M.off[d]) as *u8; mi_w(1, " [" as *u8); mi_w(1, nx_media_kind_label(M.kind[d])); mi_w(1, "] " as *u8); sys_write(1, bp, M.len[d]); mi_w(1, "\n" as *u8); d=d+1 }
117
118 let H: *u8 = sys_mmap(MI_HTML)
119 let plen: i64 = nx_media_emit_panel(H, url, ul, M.buf, M.off, M.len, M.kind, M.cnt)
120 if mi_write_file("knowledge/status/media_panel.html" as *u8, H, plen) > 0 { mi_w(1, " panel -> knowledge/status/media_panel.html\n open: C:\\Users\\elder\\nishi-core\\nxc2\\knowledge\\status\\media_panel.html\n" as *u8) }
121
122 // optional: "save" downloads every media file to disk
123 if argc >= 3 { let a2: *u8 = argv[2] as *u8; if a2[0]==(115 as u8) { mi_save_all(M, store) } }
124 return 0
125}