nx_tuya_lan_research_fetch.nx source
↩ module page · 60 lines · 3469 B
1// nx_tuya_lan_research_fetch.nx -- Phase A research: SOVEREIGN fetch of the Tuya LAN (local) control
2// protocol so we control the now-paired devices with ZERO cloud day-to-day. Canonical open impl =
3// jasonacox/tinytuya (core.py: the 0x000055AA framing, AES-ECB/GCM per version 3.1/3.3/3.4/3.5, the
4// CONTROL/DP_QUERY commands, CRC). Save to knowledge/fetched/tlan_*.raw to grep+cite, then port the
5// client in NishiLang (we have nx_aes + sockets). Own TLS-1.3 + Mozilla CA, no curl.
6// expect_exit: 0 license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_x509_trust_store.nx"
9import "nx_trust_store_load_from_certdata.nx"
10import "nx_https_fetch_follow.nx"
11const K_MAGIC_4194304: i64 = 4194304
12const K_MAGIC_8388608: i64 = 8388608
13
14func gf_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
15func gf_putn(v: i64) -> i64 {
16 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
17 var m: i64 = v
18 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
19 let d: *u8 = sys_mmap(24); var k: i64 = 0
20 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
21 var j: i64 = k - 1
22 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
23 return 0
24}
25func have_file(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
26func fetch_save(url: *u8, opath: *u8, store: *TrustStore, out: *u8, cap: i64) -> i64 {
27 if have_file(opath) == 1 { gf_puts(opath); gf_puts(" [have-skip]\n"); return 1 }
28 let status: *i64 = sys_mmap(8) as *i64
29 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status)
30 gf_puts(url); gf_puts(" status="); gf_putn(status[0]); gf_puts(" bytes="); gf_putn(n)
31 if n <= 0 { gf_puts(" FETCH-FAIL\n"); return 0 }
32 var gz: i64 = 0
33 if n >= 2 { if out[0] == 0x1f as u8 { if out[1] == 0x8b as u8 { gz = 1 } } }
34 if gz == 1 { gf_puts(" [GZIP-skip]\n"); return 0 }
35 let fd: i64 = sys_openat_wr(opath, 0x1a4)
36 if fd < 0 { gf_puts(" SAVE-FAIL\n"); return 0 }
37 sys_write(fd, out, n)
38 sys_close(fd)
39 gf_puts(" SAVED\n")
40 return 1
41}
42
43func main() -> i64 {
44 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
45 if r <= 0 { gf_puts("TLAN: certdata load failed\n"); return 1 }
46 let store: *TrustStore = r as *TrustStore
47 gf_puts("CA roots="); gf_putn(trust_store_count(store)); gf_puts("\n")
48 let cap: i64 = K_MAGIC_8388608
49 let out: *u8 = sys_mmap(cap)
50 var ok: i64 = 0
51
52 gf_puts("== tinytuya core subpackage: framing + AES + commands + 3.4/3.5 session ==\n")
53 ok = ok + fetch_save("https://raw.githubusercontent.com/jasonacox/tinytuya/master/tinytuya/core/__init__.py" as *u8, "knowledge/fetched/tlan_coreinit.raw" as *u8, store, out, cap)
54 ok = ok + fetch_save("https://raw.githubusercontent.com/jasonacox/tinytuya/master/tinytuya/core/XenonDevice.py" as *u8, "knowledge/fetched/tlan_xenon2.raw" as *u8, store, out, cap)
55 ok = ok + fetch_save("https://raw.githubusercontent.com/jasonacox/tinytuya/master/tinytuya/core/message_helper.py" as *u8, "knowledge/fetched/tlan_msg2.raw" as *u8, store, out, cap)
56 ok = ok + fetch_save("https://raw.githubusercontent.com/jasonacox/tinytuya/master/tinytuya/core/crypto_helper.py" as *u8, "knowledge/fetched/tlan_crypto2.raw" as *u8, store, out, cap)
57
58 gf_puts("READABLE SOURCES SAVED: "); gf_putn(ok); gf_puts(" / 5\n")
59 return 0
60}