nx_iot_pair_research_fetch.nx source
↩ module page · 72 lines · 4123 B
1// nx_iot_pair_research_fetch.nx -- SOVEREIGN web-fetch: HOW TUYA/Smart-Life PAIRS A DEVICE ON ANDROID
2// (operator 2026-06-23: "how does tuya do it on android"). The point: Tuya's primary mode is EZ /
3// SmartConfig -- the phone, WHILE on the home Wi-Fi, BROADCASTS the SSID+password encoded in packet
4// lengths; the device in pairing mode SNIFFS the air + decodes (NO SoftAP join). If true + replicable,
5// that's a SOVEREIGN onboard that sidesteps the Wi-Fi-client gap. Mirrors nx_idam_research_fetch (own
6// TLS-1.3 + Mozilla CA, saves to knowledge/fetched/tpair_*.raw to grep+cite). 100% sovereign, no curl.
7// expect_exit: 0 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_8388608: i64 = 8388608
14
15func 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 }
16func gf_putn(v: i64) -> i64 {
17 if v == 0 { sys_write(1, "0" as *u8, 1); return 0 }
18 var m: i64 = v
19 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m }
20 let d: *u8 = sys_mmap(24); var k: i64 = 0
21 while m > 0 { d[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
22 var j: i64 = k - 1
23 while j >= 0 { sys_write(1, ((d as i64)+j) as *u8, 1); j = j - 1 }
24 return 0
25}
26func have_file(path: *u8) -> i64 {
27 let fd: i64 = sys_openat_rd(path)
28 if fd < 0 { return 0 }
29 sys_close(fd)
30 return 1
31}
32func fetch_save(url: *u8, opath: *u8, store: *TrustStore, out: *u8, cap: i64) -> i64 {
33 if have_file(opath) == 1 { gf_puts(opath); gf_puts(" [have-skip]\n"); return 1 }
34 let status: *i64 = sys_mmap(8) as *i64
35 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status)
36 gf_puts(url); gf_puts(" status="); gf_putn(status[0]); gf_puts(" bytes="); gf_putn(n)
37 if n <= 0 { gf_puts(" FETCH-FAIL\n"); return 0 }
38 var gz: i64 = 0
39 if n >= 2 { if out[0] == 0x1f as u8 { if out[1] == 0x8b as u8 { gz = 1 } } }
40 if gz == 1 { gf_puts(" [GZIP-skip]\n"); return 0 }
41 let fd: i64 = sys_openat_wr(opath, 0x1a4)
42 if fd < 0 { gf_puts(" SAVE-FAIL\n"); return 0 }
43 sys_write(fd, out, n)
44 sys_close(fd)
45 gf_puts(" SAVED\n")
46 return 1
47}
48
49func main() -> i64 {
50 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
51 if r <= 0 { gf_puts("TPAIR: certdata load failed\n"); return 1 }
52 let store: *TrustStore = r as *TrustStore
53 gf_puts("CA roots="); gf_putn(trust_store_count(store)); gf_puts("\n")
54 let cap: i64 = K_MAGIC_8388608
55 let out: *u8 = sys_mmap(cap)
56 var ok: i64 = 0
57
58 gf_puts("== A. SOVEREIGN replication of Tuya pairing (intercept the EZ/AP flow) ==\n")
59 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/README.md" as *u8, "knowledge/fetched/tpair_tuyaconvert.raw" as *u8, store, out, cap)
60 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/docs/PROTOCOL.md" as *u8, "knowledge/fetched/tpair_tuyaconvert_proto.raw" as *u8, store, out, cap)
61
62 gf_puts("== B. SmartConfig / EZ underlying tech (Espressif ESP-Touch = what Tuya/ESP devices use) ==\n")
63 ok = ok + fetch_save("https://raw.githubusercontent.com/EspressifApp/EsptouchForAndroid/master/README.md" as *u8, "knowledge/fetched/tpair_esptouch.raw" as *u8, store, out, cap)
64
65 gf_puts("== C. Tuya LOCAL protocol + device key (tinytuya / tuyapi) ==\n")
66 ok = ok + fetch_save("https://raw.githubusercontent.com/jasonacox/tinytuya/master/README.md" as *u8, "knowledge/fetched/tpair_tinytuya.raw" as *u8, store, out, cap)
67 ok = ok + fetch_save("https://raw.githubusercontent.com/codetheweb/tuyapi/master/README.md" as *u8, "knowledge/fetched/tpair_tuyapi.raw" as *u8, store, out, cap)
68 ok = ok + fetch_save("https://raw.githubusercontent.com/codetheweb/tuyapi/master/docs/SETUP.md" as *u8, "knowledge/fetched/tpair_tuyapi_setup.raw" as *u8, store, out, cap)
69
70 gf_puts("READABLE SOURCES SAVED: "); gf_putn(ok); gf_puts(" / 6\n")
71 return 0
72}