code wiki / (root) / nx_tuya_pair_research_fetch.nx

nx_tuya_pair_research_fetch.nx source

↩ module page · 66 lines · 4235 B

1// nx_tuya_pair_research_fetch.nx -- PATH B research: SOVEREIGN fetch of the EXACT Tuya pairing 2// protocol (the operator's devices are Tuya: "easy on android"). Tuya EZ mode is Tuya's OWN 3// length-encoding (NOT Espressif ESP-Touch v1 in nx_iot_smartconfig); Tuya AP mode is a local 4// HTTP/UDP config. Fetch the canonical open-source implementation (ct-Open-Source/tuya-convert's 5// smartconfig encoder) + a couple of references, save to knowledge/fetched/tuya_*.raw to grep+cite, 6// then port the encoder. Mirrors nx_smartconfig_research_fetch (own TLS-1.3 + Mozilla CA, 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 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 } 27func fetch_save(url: *u8, opath: *u8, store: *TrustStore, out: *u8, cap: i64) -> i64 { 28 if have_file(opath) == 1 { gf_puts(opath); gf_puts(" [have-skip]\n"); return 1 } 29 let status: *i64 = sys_mmap(8) as *i64 30 let n: i64 = nx_https_fetch_follow(url, store, out, cap, 6, status) 31 gf_puts(url); gf_puts(" status="); gf_putn(status[0]); gf_puts(" bytes="); gf_putn(n) 32 if n <= 0 { gf_puts(" FETCH-FAIL\n"); return 0 } 33 var gz: i64 = 0 34 if n >= 2 { if out[0] == 0x1f as u8 { if out[1] == 0x8b as u8 { gz = 1 } } } 35 if gz == 1 { gf_puts(" [GZIP-skip]\n"); return 0 } 36 let fd: i64 = sys_openat_wr(opath, 0x1a4) 37 if fd < 0 { gf_puts(" SAVE-FAIL\n"); return 0 } 38 sys_write(fd, out, n) 39 sys_close(fd) 40 gf_puts(" SAVED\n") 41 return 1 42} 43 44func main() -> i64 { 45 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 46 if r <= 0 { gf_puts("TUYA: certdata load failed\n"); return 1 } 47 let store: *TrustStore = r as *TrustStore 48 gf_puts("CA roots="); gf_putn(trust_store_count(store)); gf_puts("\n") 49 let cap: i64 = K_MAGIC_8388608 50 let out: *u8 = sys_mmap(cap) 51 var ok: i64 = 0 52 53 gf_puts("== tuya-convert smartconfig encoder (the canonical open Tuya EZ impl) ==\n") 54 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/scripts/smartconfig/smartconfig.py" as *u8, "knowledge/fetched/tuya_smartconfig.raw" as *u8, store, out, cap) 55 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/scripts/smartconfig/__main__.py" as *u8, "knowledge/fetched/tuya_main.raw" as *u8, store, out, cap) 56 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/scripts/smartconfig/multicast.py" as *u8, "knowledge/fetched/tuya_multicast.raw" as *u8, store, out, cap) 57 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/scripts/smartconfig/broadcast.py" as *u8, "knowledge/fetched/tuya_broadcast.raw" as *u8, store, out, cap) 58 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/scripts/smartconfig/crc.py" as *u8, "knowledge/fetched/tuya_crc.raw" as *u8, store, out, cap) 59 60 gf_puts("== references (AP-mode local config + protocol notes) ==\n") 61 ok = ok + fetch_save("https://raw.githubusercontent.com/ct-Open-Source/tuya-convert/master/README.md" as *u8, "knowledge/fetched/tuya_readme.raw" as *u8, store, out, cap) 62 ok = ok + fetch_save("https://raw.githubusercontent.com/jasonacox/tinytuya/master/README.md" as *u8, "knowledge/fetched/tuya_tinytuya_readme.raw" as *u8, store, out, cap) 63 64 gf_puts("READABLE SOURCES SAVED: "); gf_putn(ok); gf_puts(" / 7\n") 65 return 0 66}