code wiki / _hdl_build / nx_galx_bridge.nx
nx_galx_bridge.nx source
↩ module page · 123 lines · 6120 B
1// nx_galx_bridge.nx -- SOVEREIGN reverse-tunnel bridging the WSL-workstation gallery to the always-on NAS.
2// The gallery runs on the workstation (WSL2-NAT'd -> NOT LAN-reachable). Instead of netsh/ssh, the workstation
3// DIALS OUT to the NAS (outbound traverses the NAT); the NAS routes nishifamily.com/gallery requests back down
4// those pooled connections to the workstation, which forwards them to the local OPAQUE gateway -> gallery.
5// Pure sockets (sys_socket/connect/bind/listen/accept/read/write/fork) -- NO sh, NO netsh, NO ssh.
6// NAS mode: nx_galx_bridge nas <tunnel_port> <front_port>
7// listen tunnel_port (workstation dials IN -> conns wait in the accept backlog = the pool) + front_port
8// (the NAS TLS front sends /gallery requests here, loopback). Per front request: fork -> accept one pooled
9// worker -> forward request -> relay worker->front until close.
10// WS mode: nx_galx_bridge ws <a> <b> <c> <d> <tunnel_port> <gateway_port> <pool>
11// fork <pool> dialers; each loops: connect <a.b.c.d>:tunnel_port -> read the routed request -> connect
12// 127.0.0.1:gateway_port (the OPAQUE gateway) -> forward -> relay gateway->tunnel until close -> reconnect.
13import "nx_syscalls.nx"
14import "nx_connect.nx" // bounded connect: a raw sys_connect hangs ~127s on a black-holed host
15const K_MAGIC_65536: i64 = 65536
16const K_MAGIC_1000000000: i64 = 1000000000
17const K_MAGIC_131072: i64 = 131072
18const K_MAGIC_3600: i64 = 3600
19
20func gb_atoi(s: *u8) -> i64 { var v: i64=0; var i: i64=0; while s[i]!=(0 as u8){ let c: i64=s[i] as i64; if c<48{return v} if c>57{return v} v=v*10+(c-48); i=i+1 } return v }
21
22// connect to a.b.c.d:port -> fd (>=0) or negative
23func gb_connect(a: i64, b: i64, c: i64, d: i64, port: i64) -> i64 {
24 let fd: i64 = sys_socket(2, 1, 0); if fd < 0 { return 0-1 }
25 let ad: *u8 = sys_mmap(16)
26 ad[0]=2 as u8; ad[1]=0 as u8; ad[2]=((port>>8)&0xff) as u8; ad[3]=(port&0xff) as u8
27 ad[4]=a as u8; ad[5]=b as u8; ad[6]=c as u8; ad[7]=d as u8
28 var z: i64=8; while z<16 { ad[z]=0 as u8; z=z+1 }
29 if nx_connect_bounded(fd, ad, 16, NX_CONN_DEFAULT_MS) != 0 { sys_close(fd); return 0-2 }
30 return fd
31}
32// bind+listen 0.0.0.0:port -> lfd (>=0) or negative
33func gb_listen(port: i64) -> i64 {
34 let ad: *u8 = sys_mmap(16)
35 ad[0]=2 as u8; ad[1]=0 as u8; ad[2]=((port>>8)&0xff) as u8; ad[3]=(port&0xff) as u8
36 ad[4]=0 as u8; ad[5]=0 as u8; ad[6]=0 as u8; ad[7]=0 as u8
37 var z: i64=8; while z<16 { ad[z]=0 as u8; z=z+1 }
38 let lfd: i64 = sys_socket(2, 1, 0); if lfd < 0 { return 0-1 }
39 let ov: *u8 = sys_mmap(4); ov[0]=1 as u8
40 sys_setsockopt(lfd, 1, 2, ov, 4)
41 if sys_bind(lfd, ad, 16) < 0 { sys_close(lfd); return 0-2 }
42 if sys_listen(lfd, 64) < 0 { sys_close(lfd); return 0-3 }
43 return lfd
44}
45// relay: read src -> write dst until src read<=0
46func gb_relay(src: i64, dst: i64) -> i64 {
47 let buf: *u8 = sys_mmap(K_MAGIC_65536); var tot: i64=0; var go: i64=1
48 while go==1 { let r: i64 = sys_read(src, buf, K_MAGIC_65536); if r<=0 {go=0} else { sys_write(dst, buf, r); tot=tot+r } }
49 return tot
50}
51
52func main(argc: i64, argv: *i64) -> i64 {
53 if argc < 3 {
54 sys_write(1, "usage: nx_galx_bridge nas <tport> <fport> | ws <a> <b> <c> <d> <tport> <gport> <pool>\n" as *u8, 86)
55 sys_exit(2); return 2
56 }
57 let mode: *u8 = argv[1] as *u8
58
59 if mode[0] == (110 as u8) { // 'n' -> NAS server
60 let tport: i64 = gb_atoi(argv[2] as *u8)
61 let fport: i64 = gb_atoi(argv[3] as *u8)
62 let lt: i64 = gb_listen(tport); if lt < 0 { sys_write(1, "BR-NAS-TLISTEN-FAIL\n" as *u8, 20); sys_exit(1); return 1 }
63 let lf: i64 = gb_listen(fport); if lf < 0 { sys_write(1, "BR-NAS-FLISTEN-FAIL\n" as *u8, 20); sys_exit(1); return 1 }
64 sys_write(1, "GALX-BRIDGE-NAS-UP\n" as *u8, 19)
65 let st: *i64 = sys_mmap(16) as *i64
66 var served: i64 = 0
67 while served < K_MAGIC_1000000000 {
68 var dummy: i64 = 0
69 while sys_wait4(0-1, st, 1) > 0 { dummy = dummy + 1 }
70 let cfd: i64 = sys_accept(lf)
71 if cfd >= 0 {
72 let pid: i64 = sys_fork()
73 if pid == 0 {
74 sys_close(lf)
75 let wfd: i64 = sys_accept(lt)
76 if wfd >= 0 {
77 sys_set_socket_timeout(cfd, 30); sys_set_socket_timeout(wfd, 30)
78 let buf: *u8 = sys_mmap(K_MAGIC_131072)
79 let r: i64 = sys_read(cfd, buf, K_MAGIC_131072)
80 if r > 0 { sys_write(wfd, buf, r) }
81 gb_relay(wfd, cfd)
82 sys_close(wfd)
83 }
84 sys_close(cfd); sys_exit(0)
85 }
86 sys_close(cfd)
87 }
88 served = served + 1
89 }
90 sys_close(lt); sys_close(lf); sys_exit(0); return 0
91 }
92
93 // WS mode (workstation dialer pool)
94 let a: i64 = gb_atoi(argv[2] as *u8); let b: i64 = gb_atoi(argv[3] as *u8)
95 let c: i64 = gb_atoi(argv[4] as *u8); let d: i64 = gb_atoi(argv[5] as *u8)
96 let tport: i64 = gb_atoi(argv[6] as *u8)
97 let gport: i64 = gb_atoi(argv[7] as *u8)
98 var pool: i64 = gb_atoi(argv[8] as *u8)
99 if pool < 1 { pool = 1 }
100 sys_write(1, "GALX-BRIDGE-WS-UP\n" as *u8, 18)
101 var k: i64 = 1
102 while k < pool { let p: i64 = sys_fork(); if p == 0 { k = pool } else { k = k + 1 } }
103 var go: i64 = 1
104 while go == 1 {
105 let wfd: i64 = gb_connect(a, b, c, d, tport)
106 if wfd < 0 { sys_sleep_ms(500) } else {
107 sys_set_socket_timeout(wfd, K_MAGIC_3600)
108 let buf: *u8 = sys_mmap(K_MAGIC_131072)
109 let r: i64 = sys_read(wfd, buf, K_MAGIC_131072)
110 if r > 0 {
111 let gfd: i64 = gb_connect(127, 0, 0, 1, gport)
112 if gfd >= 0 {
113 sys_set_socket_timeout(gfd, 30)
114 sys_write(gfd, buf, r)
115 gb_relay(gfd, wfd)
116 sys_close(gfd)
117 }
118 }
119 sys_close(wfd)
120 }
121 }
122 sys_exit(0); return 0
123}