@webpodjs/net-worker 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -0
- package/dist/pkg/webpod_net_wasm.d.ts +50 -0
- package/dist/pkg/webpod_net_wasm.js +141 -3
- package/dist/pkg/webpod_net_wasm_bg.wasm +0 -0
- package/dist/pkg/webpod_net_wasm_bg.wasm.d.ts +12 -0
- package/dist/worker.js +223 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -153,6 +153,50 @@ guest reaches another, over any protocol (TCP, UDP, ICMP), with real source addr
|
|
|
153
153
|
none. Replies within connections other members opened always pass, so isolating an untrusted
|
|
154
154
|
pod only needs its own list.
|
|
155
155
|
|
|
156
|
+
## Host sockets
|
|
157
|
+
|
|
158
|
+
Host mode (E4, packages/host-exec) runs node on the page; its sockets come here. The controller gives each host
|
|
159
|
+
process a MessagePort: `{ type: "host-socket", id, port }` (transferred), and `{ type: "host-socket-close", id }` when
|
|
160
|
+
the process is gone (its connections are dropped). A host connection is routed exactly as a guest connection to the
|
|
161
|
+
same address: the same router (routes, `mode`, CORS proxy, relay, deny), sniffing, HTTP proxy and egress, and names
|
|
162
|
+
resolve in the guest's DNS (fake IPs). Loopback is refused (localhost is not the browser's).
|
|
163
|
+
|
|
164
|
+
On the port, the process sends `{ type: "lookup", id, host }` (answered `{ type: "lookup", id, address }`, IPv4),
|
|
165
|
+
`{ type: "connect", id, address, port }` (answered `connected`, or `reset` with a reason), `{ type: "tls", id,
|
|
166
|
+
servername }` before any data (answered `{ type: "tls", id, error? }`), `{ type: "data", id, data }`, `{ type: "end", id }`
|
|
167
|
+
(FIN) and `{ type: "close", id }` (destroy); it acknowledges what it took with `{ type: "ack", id, bytes }` (at most
|
|
168
|
+
1 MiB is unacknowledged). This Worker sends `{ type: "data", id, data }`, `{ type: "end", id }` and
|
|
169
|
+
`{ type: "reset", id, reason }`.
|
|
170
|
+
|
|
171
|
+
**Listening.** The host (not the process: its listen is synchronous and goes through the controller, which also
|
|
172
|
+
asks the guest's agent, docs/agent-protocol.md "Host listen") sends `{ type: "host-listen", req, id, port }` (port 0:
|
|
173
|
+
any free one), answered `{ type: "host-listen", req, port }` or `{ type: "host-listen", req, error: "EADDRINUSE" }`
|
|
174
|
+
when another process listens on it (the same process may listen on it again, at another address: counted), and
|
|
175
|
+
`{ type: "host-unlisten", id, port }`. While the port is listened, the page's connections to it (`inbound`: pod.fetch,
|
|
176
|
+
previews, pod.net.connect, a preview's WebSocket) and the guest's TCP connections to the gateway's address on it
|
|
177
|
+
(the agent's forwarder) go to the process instead of the guest: `{ type: "accept", id, port }` on its port, with an id
|
|
178
|
+
this Worker picks (negative), then the same `data` / `end` / `reset` and `ack` / `close` as a connection of its own.
|
|
179
|
+
Registrations are by the current listener: after an unlisten, new connections go to the guest again (vite 8 listens,
|
|
180
|
+
closes and listens again on its port); accepted ones go on. The gateway serves only UDP itself (DNS 53, DHCP 67), so
|
|
181
|
+
no port of its own is taken by a host listener. `host-socket-close` ends the process's listens too.
|
|
182
|
+
|
|
183
|
+
**The pod's localhost.** A process's `connect` to `127.0.0.1` or `::1` (T95): where a host process listens on that
|
|
184
|
+
port, the two connections are joined here (the listener gets `accept`); otherwise this Worker asks the host for the
|
|
185
|
+
guest's localhost with `{ type: "host-connect-guest", req, port }`, answered `{ type: "host-connect-guest", req, port }`
|
|
186
|
+
(the agent's one-time port, docs/agent-protocol.md HostConnect) or `{ ..., error }`, and dials that port on the
|
|
187
|
+
guest's address from the gateway (a refusal is `reset`). Other loopback addresses stay refused.
|
|
188
|
+
|
|
189
|
+
**The end of a process.** A process's `close` of a connection it ended (`end` first) is close(2) after shutdown: what
|
|
190
|
+
it sent is still delivered, nothing more is reported; without the `end` it is a reset. When it exits it sends
|
|
191
|
+
`{ type: "bye" }` last: `host-socket-close` (which comes on another channel) waits for it (at most a second), so the
|
|
192
|
+
response a server wrote just before exiting is not lost; then the connections it accepted close that way and the
|
|
193
|
+
ones it opened are reset.
|
|
194
|
+
|
|
195
|
+
TLS is not done in the process (design docs/experiments/e4-host-node.md §7.1): after `tls`, the connection carries the
|
|
196
|
+
plaintext of TLS, which is handled as intercepted HTTPS (the browser checks the certificate). TLS that does not carry
|
|
197
|
+
HTTP, TLS whose route is a relay (which needs the TLS bytes) and STARTTLS are refused, the reason starting with
|
|
198
|
+
`unsupported`.
|
|
199
|
+
|
|
156
200
|
## The pod CA
|
|
157
201
|
|
|
158
202
|
Generated on first attach (P-256, valid 10 years) and kept **inside the Worker**: with a `caKey` it
|
|
@@ -23,6 +23,39 @@ export class NetPod {
|
|
|
23
23
|
* Milliseconds until the stack wants to be polled again (-1: no timer).
|
|
24
24
|
*/
|
|
25
25
|
delay(now_ms: number): number;
|
|
26
|
+
host_abort(id: number): void;
|
|
27
|
+
host_ack(id: number, bytes: number): void;
|
|
28
|
+
/**
|
|
29
|
+
* Opens a host connection to an IPv4 address; returns its id, or throws the reason it is
|
|
30
|
+
* refused.
|
|
31
|
+
*/
|
|
32
|
+
host_connect(address: string, port: number): number;
|
|
33
|
+
/**
|
|
34
|
+
* A host connection into the guest: the gateway dials `port` on the guest's address (the
|
|
35
|
+
* agent's HostConnect listener).
|
|
36
|
+
*/
|
|
37
|
+
host_connect_guest(port: number): number;
|
|
38
|
+
/**
|
|
39
|
+
* A host connection to the pod's localhost:port where a host process listens (joined here);
|
|
40
|
+
* throws "connection refused" when none does.
|
|
41
|
+
*/
|
|
42
|
+
host_connect_local(port: number): number;
|
|
43
|
+
host_end(id: number): void;
|
|
44
|
+
/**
|
|
45
|
+
* A host process listens on `port` (0: any free one): returns the port, or throws
|
|
46
|
+
* "EADDRINUSE". Connections to it come as `[id, "accept", port]` host events.
|
|
47
|
+
*/
|
|
48
|
+
host_listen(port: number): number;
|
|
49
|
+
/**
|
|
50
|
+
* The IPv4 address a host process gets for `name` (the guest's DNS answer).
|
|
51
|
+
*/
|
|
52
|
+
host_lookup(name: string): string;
|
|
53
|
+
host_send(id: number, data: Uint8Array | number[]): void;
|
|
54
|
+
/**
|
|
55
|
+
* `tls.connect` on a host connection: returns the reason it cannot, or undefined.
|
|
56
|
+
*/
|
|
57
|
+
host_start_tls(id: number, servername?: string | null): string | undefined;
|
|
58
|
+
host_unlisten(port: number): void;
|
|
26
59
|
/**
|
|
27
60
|
* `network`: `{ mode?: "browser" | "none", mtu?, corsProxy?: { url, token? }, relay?: { url,
|
|
28
61
|
* token? }, routes?: [{ host?, ports?: [lo, hi], protocol?, via, stripHeaders? }] }` (see
|
|
@@ -67,6 +100,11 @@ export class NetPod {
|
|
|
67
100
|
* Guest frames for peers: `[[peerId, Uint8Array], ...]`, to post to their Workers.
|
|
68
101
|
*/
|
|
69
102
|
take_forwarded(): Array<any>;
|
|
103
|
+
/**
|
|
104
|
+
* What happened to host connections since the last call: `[[id, "data", Uint8Array] |
|
|
105
|
+
* [id, "end"] | [id, "reset", reason] | [id, "accept", port], ...]`.
|
|
106
|
+
*/
|
|
107
|
+
take_host_events(): Array<any>;
|
|
70
108
|
}
|
|
71
109
|
|
|
72
110
|
/**
|
|
@@ -84,6 +122,17 @@ export interface InitOutput {
|
|
|
84
122
|
readonly netpod_ca_bundle: (a: number) => [number, number];
|
|
85
123
|
readonly netpod_ca_certificate: (a: number) => [number, number];
|
|
86
124
|
readonly netpod_delay: (a: number, b: number) => number;
|
|
125
|
+
readonly netpod_host_abort: (a: number, b: number) => void;
|
|
126
|
+
readonly netpod_host_ack: (a: number, b: number, c: number) => void;
|
|
127
|
+
readonly netpod_host_connect: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
128
|
+
readonly netpod_host_connect_guest: (a: number, b: number) => number;
|
|
129
|
+
readonly netpod_host_connect_local: (a: number, b: number) => [number, number, number];
|
|
130
|
+
readonly netpod_host_end: (a: number, b: number) => void;
|
|
131
|
+
readonly netpod_host_listen: (a: number, b: number) => [number, number, number];
|
|
132
|
+
readonly netpod_host_lookup: (a: number, b: number, c: number) => [number, number];
|
|
133
|
+
readonly netpod_host_send: (a: number, b: number, c: number, d: number) => void;
|
|
134
|
+
readonly netpod_host_start_tls: (a: number, b: number, c: number, d: number) => [number, number];
|
|
135
|
+
readonly netpod_host_unlisten: (a: number, b: number) => void;
|
|
87
136
|
readonly netpod_new: (a: any, b: number, c: number, d: number, e: number) => [number, number, number];
|
|
88
137
|
readonly netpod_open_inbound: (a: number, b: any) => void;
|
|
89
138
|
readonly netpod_poll: (a: number, b: number) => number;
|
|
@@ -95,6 +144,7 @@ export interface InitOutput {
|
|
|
95
144
|
readonly netpod_stats: (a: number) => any;
|
|
96
145
|
readonly netpod_take: (a: number) => any;
|
|
97
146
|
readonly netpod_take_forwarded: (a: number) => any;
|
|
147
|
+
readonly netpod_take_host_events: (a: number) => any;
|
|
98
148
|
readonly set_logger: (a: any, b: number, c: number) => void;
|
|
99
149
|
readonly ring_core_0_17_14__bn_mul_mont: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
|
|
100
150
|
readonly wasm_bindgen_5a2380350bab5903___convert__closures_____invoke___wasm_bindgen_5a2380350bab5903___JsValue__core_6a51abefd5362b2___result__Result_____wasm_bindgen_5a2380350bab5903___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
|
@@ -68,6 +68,131 @@ export class NetPod {
|
|
|
68
68
|
const ret = wasm.netpod_delay(this.__wbg_ptr, now_ms);
|
|
69
69
|
return ret;
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* @param {number} id
|
|
73
|
+
*/
|
|
74
|
+
host_abort(id) {
|
|
75
|
+
wasm.netpod_host_abort(this.__wbg_ptr, id);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* @param {number} id
|
|
79
|
+
* @param {number} bytes
|
|
80
|
+
*/
|
|
81
|
+
host_ack(id, bytes) {
|
|
82
|
+
wasm.netpod_host_ack(this.__wbg_ptr, id, bytes);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Opens a host connection to an IPv4 address; returns its id, or throws the reason it is
|
|
86
|
+
* refused.
|
|
87
|
+
* @param {string} address
|
|
88
|
+
* @param {number} port
|
|
89
|
+
* @returns {number}
|
|
90
|
+
*/
|
|
91
|
+
host_connect(address, port) {
|
|
92
|
+
const val2 = +port;
|
|
93
|
+
const ptr0 = passStringToWasm0(address, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
94
|
+
const len0 = WASM_VECTOR_LEN;
|
|
95
|
+
const ret = wasm.netpod_host_connect(this.__wbg_ptr, ptr0, len0, val2);
|
|
96
|
+
if (ret[2]) {
|
|
97
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
98
|
+
}
|
|
99
|
+
return ret[0];
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* A host connection into the guest: the gateway dials `port` on the guest's address (the
|
|
103
|
+
* agent's HostConnect listener).
|
|
104
|
+
* @param {number} port
|
|
105
|
+
* @returns {number}
|
|
106
|
+
*/
|
|
107
|
+
host_connect_guest(port) {
|
|
108
|
+
const ret = wasm.netpod_host_connect_guest(this.__wbg_ptr, port);
|
|
109
|
+
return ret;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A host connection to the pod's localhost:port where a host process listens (joined here);
|
|
113
|
+
* throws "connection refused" when none does.
|
|
114
|
+
* @param {number} port
|
|
115
|
+
* @returns {number}
|
|
116
|
+
*/
|
|
117
|
+
host_connect_local(port) {
|
|
118
|
+
const ret = wasm.netpod_host_connect_local(this.__wbg_ptr, port);
|
|
119
|
+
if (ret[2]) {
|
|
120
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
121
|
+
}
|
|
122
|
+
return ret[0];
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* @param {number} id
|
|
126
|
+
*/
|
|
127
|
+
host_end(id) {
|
|
128
|
+
wasm.netpod_host_end(this.__wbg_ptr, id);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* A host process listens on `port` (0: any free one): returns the port, or throws
|
|
132
|
+
* "EADDRINUSE". Connections to it come as `[id, "accept", port]` host events.
|
|
133
|
+
* @param {number} port
|
|
134
|
+
* @returns {number}
|
|
135
|
+
*/
|
|
136
|
+
host_listen(port) {
|
|
137
|
+
const ret = wasm.netpod_host_listen(this.__wbg_ptr, port);
|
|
138
|
+
if (ret[2]) {
|
|
139
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
140
|
+
}
|
|
141
|
+
return ret[0];
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* The IPv4 address a host process gets for `name` (the guest's DNS answer).
|
|
145
|
+
* @param {string} name
|
|
146
|
+
* @returns {string}
|
|
147
|
+
*/
|
|
148
|
+
host_lookup(name) {
|
|
149
|
+
let deferred2_0;
|
|
150
|
+
let deferred2_1;
|
|
151
|
+
try {
|
|
152
|
+
const ptr0 = passStringToWasm0(name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
153
|
+
const len0 = WASM_VECTOR_LEN;
|
|
154
|
+
const ret = wasm.netpod_host_lookup(this.__wbg_ptr, ptr0, len0);
|
|
155
|
+
deferred2_0 = ret[0];
|
|
156
|
+
deferred2_1 = ret[1];
|
|
157
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
158
|
+
} finally {
|
|
159
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* @param {number} id
|
|
164
|
+
* @param {Uint8Array | number[]} data
|
|
165
|
+
*/
|
|
166
|
+
host_send(id, data) {
|
|
167
|
+
const val2 = +id;
|
|
168
|
+
const ptr0 = passArray8ToWasm0(data, wasm.__wbindgen_malloc);
|
|
169
|
+
const len0 = WASM_VECTOR_LEN;
|
|
170
|
+
wasm.netpod_host_send(this.__wbg_ptr, val2, ptr0, len0);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* `tls.connect` on a host connection: returns the reason it cannot, or undefined.
|
|
174
|
+
* @param {number} id
|
|
175
|
+
* @param {string | null} [servername]
|
|
176
|
+
* @returns {string | undefined}
|
|
177
|
+
*/
|
|
178
|
+
host_start_tls(id, servername) {
|
|
179
|
+
const val2 = +id;
|
|
180
|
+
var ptr0 = isLikeNone(servername) ? 0 : passStringToWasm0(servername, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
181
|
+
var len0 = WASM_VECTOR_LEN;
|
|
182
|
+
const ret = wasm.netpod_host_start_tls(this.__wbg_ptr, val2, ptr0, len0);
|
|
183
|
+
let v3;
|
|
184
|
+
if (ret[0] !== 0) {
|
|
185
|
+
v3 = getStringFromWasm0(ret[0], ret[1]);
|
|
186
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
187
|
+
}
|
|
188
|
+
return v3;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* @param {number} port
|
|
192
|
+
*/
|
|
193
|
+
host_unlisten(port) {
|
|
194
|
+
wasm.netpod_host_unlisten(this.__wbg_ptr, port);
|
|
195
|
+
}
|
|
71
196
|
/**
|
|
72
197
|
* `network`: `{ mode?: "browser" | "none", mtu?, corsProxy?: { url, token? }, relay?: { url,
|
|
73
198
|
* token? }, routes?: [{ host?, ports?: [lo, hi], protocol?, via, stripHeaders? }] }` (see
|
|
@@ -172,6 +297,15 @@ export class NetPod {
|
|
|
172
297
|
const ret = wasm.netpod_take_forwarded(this.__wbg_ptr);
|
|
173
298
|
return ret;
|
|
174
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* What happened to host connections since the last call: `[[id, "data", Uint8Array] |
|
|
302
|
+
* [id, "end"] | [id, "reset", reason] | [id, "accept", port], ...]`.
|
|
303
|
+
* @returns {Array<any>}
|
|
304
|
+
*/
|
|
305
|
+
take_host_events() {
|
|
306
|
+
const ret = wasm.netpod_take_host_events(this.__wbg_ptr);
|
|
307
|
+
return ret;
|
|
308
|
+
}
|
|
175
309
|
}
|
|
176
310
|
if (Symbol.dispose) NetPod.prototype[Symbol.dispose] = NetPod.prototype.free;
|
|
177
311
|
|
|
@@ -483,6 +617,10 @@ function __wbg_get_imports() {
|
|
|
483
617
|
const ret = Array.of(arg0);
|
|
484
618
|
return ret;
|
|
485
619
|
},
|
|
620
|
+
__wbg_of_4cc373bef1f715a4: function(arg0, arg1, arg2) {
|
|
621
|
+
const ret = Array.of(arg0, arg1, arg2);
|
|
622
|
+
return ret;
|
|
623
|
+
},
|
|
486
624
|
__wbg_of_e7ea0fcd535c1444: function(arg0, arg1) {
|
|
487
625
|
const ret = Array.of(arg0, arg1);
|
|
488
626
|
return ret;
|
|
@@ -673,17 +811,17 @@ function __wbg_get_imports() {
|
|
|
673
811
|
return ret;
|
|
674
812
|
},
|
|
675
813
|
__wbindgen_generic_0000000000000001: function(arg0, arg1) {
|
|
676
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
814
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 40, ret: Unit, inner_ret: Some(Unit) }, mutable: true }) -> Externref`.
|
|
677
815
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_5a2380350bab5903___convert__closures_____invoke___wasm_bindgen_5a2380350bab5903___JsValue______true_);
|
|
678
816
|
return ret;
|
|
679
817
|
},
|
|
680
818
|
__wbindgen_generic_0000000000000002: function(arg0, arg1) {
|
|
681
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx:
|
|
819
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [Externref], shim_idx: 808, ret: Result(Unit), inner_ret: Some(Result(Unit)) }, mutable: true }) -> Externref`.
|
|
682
820
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_5a2380350bab5903___convert__closures_____invoke___wasm_bindgen_5a2380350bab5903___JsValue__core_6a51abefd5362b2___result__Result_____wasm_bindgen_5a2380350bab5903___JsError___true_);
|
|
683
821
|
return ret;
|
|
684
822
|
},
|
|
685
823
|
__wbindgen_generic_0000000000000003: function(arg0, arg1) {
|
|
686
|
-
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [], shim_idx:
|
|
824
|
+
// Cast intrinsic for `Closure(Closure { owned: true, function: Function { arguments: [], shim_idx: 42, ret: Externref, inner_ret: Some(Externref) }, mutable: true }) -> Externref`.
|
|
687
825
|
const ret = makeMutClosure(arg0, arg1, wasm_bindgen_5a2380350bab5903___convert__closures_____invoke___wasm_bindgen_5a2380350bab5903___JsValue__true_);
|
|
688
826
|
return ret;
|
|
689
827
|
},
|
|
Binary file
|
|
@@ -6,6 +6,17 @@ export const netpod_add_peer: (a: number, b: number, c: number, d: number, e: nu
|
|
|
6
6
|
export const netpod_ca_bundle: (a: number) => [number, number];
|
|
7
7
|
export const netpod_ca_certificate: (a: number) => [number, number];
|
|
8
8
|
export const netpod_delay: (a: number, b: number) => number;
|
|
9
|
+
export const netpod_host_abort: (a: number, b: number) => void;
|
|
10
|
+
export const netpod_host_ack: (a: number, b: number, c: number) => void;
|
|
11
|
+
export const netpod_host_connect: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
12
|
+
export const netpod_host_connect_guest: (a: number, b: number) => number;
|
|
13
|
+
export const netpod_host_connect_local: (a: number, b: number) => [number, number, number];
|
|
14
|
+
export const netpod_host_end: (a: number, b: number) => void;
|
|
15
|
+
export const netpod_host_listen: (a: number, b: number) => [number, number, number];
|
|
16
|
+
export const netpod_host_lookup: (a: number, b: number, c: number) => [number, number];
|
|
17
|
+
export const netpod_host_send: (a: number, b: number, c: number, d: number) => void;
|
|
18
|
+
export const netpod_host_start_tls: (a: number, b: number, c: number, d: number) => [number, number];
|
|
19
|
+
export const netpod_host_unlisten: (a: number, b: number) => void;
|
|
9
20
|
export const netpod_new: (a: any, b: number, c: number, d: number, e: number) => [number, number, number];
|
|
10
21
|
export const netpod_open_inbound: (a: number, b: any) => void;
|
|
11
22
|
export const netpod_poll: (a: number, b: number) => number;
|
|
@@ -17,6 +28,7 @@ export const netpod_set_unix_time: (a: number, b: number) => void;
|
|
|
17
28
|
export const netpod_stats: (a: number) => any;
|
|
18
29
|
export const netpod_take: (a: number) => any;
|
|
19
30
|
export const netpod_take_forwarded: (a: number) => any;
|
|
31
|
+
export const netpod_take_host_events: (a: number) => any;
|
|
20
32
|
export const set_logger: (a: any, b: number, c: number) => void;
|
|
21
33
|
export const ring_core_0_17_14__bn_mul_mont: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
|
|
22
34
|
export const wasm_bindgen_5a2380350bab5903___convert__closures_____invoke___wasm_bindgen_5a2380350bab5903___JsValue__core_6a51abefd5362b2___result__Result_____wasm_bindgen_5a2380350bab5903___JsError___true_: (a: number, b: number, c: any) => [number, number];
|
package/dist/worker.js
CHANGED
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
//
|
|
7
7
|
// host -> worker: attach { memory, toEngine, fromEngine, mac, network?, caKey?, logLevel? }, inbound { port },
|
|
8
8
|
// ca, relay-token { id, token | error }, peer { id, hostname, index, port }, peer-gone { id },
|
|
9
|
-
//
|
|
9
|
+
// host-socket { id, port }, host-socket-close { id }, host-listen { req, id, port },
|
|
10
|
+
// host-unlisten { id, port }, host-connect-guest { req, port | error }, stats, stop
|
|
10
11
|
// worker -> host: ready, ca { pem }, relay-token-request { id }, peer-added { id }, stats { stats },
|
|
11
|
-
// stopped, log, error
|
|
12
|
+
// host-listen { req, port | error }, host-connect-guest { req, port }, stopped, log, error
|
|
12
13
|
import init, { NetPod, set_logger } from "./pkg/webpod_net_wasm.js";
|
|
13
14
|
import { RingConsumer, RingProducer, settleWaits, waitReadable } from "./abi/index.js";
|
|
14
15
|
|
|
@@ -25,6 +26,192 @@ const tokenWaiters = new Map();
|
|
|
25
26
|
let nextToken = 0;
|
|
26
27
|
let wake = null;
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Host sockets (E4 host mode, README.md "Host sockets"): each host process has a MessagePort here; its
|
|
31
|
+
* connections are the stack's host connections, routed like the guest's. id -> { port, conns: local id -> stack id }.
|
|
32
|
+
*/
|
|
33
|
+
const hostSockets = new Map();
|
|
34
|
+
const pendingHostSockets = [];
|
|
35
|
+
/** stack connection id -> [host socket id, local id] */
|
|
36
|
+
const hostConns = new Map();
|
|
37
|
+
/** Ports host processes listen on: port -> { owner: host socket id, count } (a process may listen on several addresses). */
|
|
38
|
+
const hostListeners = new Map();
|
|
39
|
+
/** Local ids of accepted connections are this Worker's: negative, apart from the ids processes pick. */
|
|
40
|
+
let nextAccepted = 0;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The guest's localhost:port for a host process: the host asks the guest's agent (HostConnect), which connects there
|
|
44
|
+
* and offers the connection on a one-time port of the guest's address; resolves with that port.
|
|
45
|
+
*/
|
|
46
|
+
const guestConnects = new Map();
|
|
47
|
+
let nextGuestConnect = 0;
|
|
48
|
+
function guestLocalhost(port) {
|
|
49
|
+
const req = ++nextGuestConnect;
|
|
50
|
+
post({ type: "host-connect-guest", req, port });
|
|
51
|
+
return new Promise((resolve, reject) => {
|
|
52
|
+
// No answer (a page whose SDK does not know the message): the agent's offer lasts 10 s, this a little longer.
|
|
53
|
+
const t = setTimeout(() => {
|
|
54
|
+
if (guestConnects.delete(req)) reject(new Error("connection timed out"));
|
|
55
|
+
}, 15000);
|
|
56
|
+
guestConnects.set(req, { resolve: (p) => (clearTimeout(t), resolve(p)), reject: (e) => (clearTimeout(t), reject(e)) });
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A process listens on port (0: any free one): the port, or throws "EADDRINUSE". */
|
|
61
|
+
function hostListen(id, port) {
|
|
62
|
+
const l = port ? hostListeners.get(port) : undefined;
|
|
63
|
+
if (l) {
|
|
64
|
+
if (l.owner !== id) throw new Error("EADDRINUSE");
|
|
65
|
+
l.count++;
|
|
66
|
+
return port;
|
|
67
|
+
}
|
|
68
|
+
const p = pod.host_listen(port);
|
|
69
|
+
hostListeners.set(p, { owner: id, count: 1 });
|
|
70
|
+
return p;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function hostUnlisten(id, port) {
|
|
74
|
+
const l = hostListeners.get(port);
|
|
75
|
+
if (!l || l.owner !== id || --l.count > 0) return;
|
|
76
|
+
hostListeners.delete(port);
|
|
77
|
+
pod?.host_unlisten(port);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function openHostSocket({ id, port }) {
|
|
81
|
+
closeHostSocket(id);
|
|
82
|
+
const hs = { port, conns: new Map(), bye: false, closing: null };
|
|
83
|
+
hostSockets.set(id, hs);
|
|
84
|
+
const reply = (m, transfer) => port.postMessage(m, transfer);
|
|
85
|
+
port.onmessage = ({ data: m }) => {
|
|
86
|
+
if (!pod) return;
|
|
87
|
+
const conn = hs.conns.get(m.id);
|
|
88
|
+
switch (m.type) {
|
|
89
|
+
// The process ended; all it sent came before this (its connections' last data, end and close).
|
|
90
|
+
case "bye":
|
|
91
|
+
hs.bye = true;
|
|
92
|
+
if (hs.closing) hs.closing();
|
|
93
|
+
break;
|
|
94
|
+
case "lookup":
|
|
95
|
+
reply({ type: "lookup", id: m.id, address: pod.host_lookup(String(m.host)) });
|
|
96
|
+
break;
|
|
97
|
+
case "connect": {
|
|
98
|
+
const attach = (c) => {
|
|
99
|
+
hs.conns.set(m.id, c);
|
|
100
|
+
hostConns.set(c, [id, m.id]);
|
|
101
|
+
reply({ type: "connected", id: m.id });
|
|
102
|
+
wake?.();
|
|
103
|
+
};
|
|
104
|
+
// The pod's localhost (T95): a host process listening there, else the guest's localhost through the agent.
|
|
105
|
+
if (m.address === "127.0.0.1" || m.address === "::1") {
|
|
106
|
+
if (hostListeners.has(m.port)) {
|
|
107
|
+
try {
|
|
108
|
+
attach(pod.host_connect_local(m.port));
|
|
109
|
+
} catch (e) {
|
|
110
|
+
reply({ type: "reset", id: m.id, reason: String(e?.message ?? e) });
|
|
111
|
+
}
|
|
112
|
+
} else
|
|
113
|
+
guestLocalhost(m.port).then(
|
|
114
|
+
(via) => (hostSockets.get(id) === hs && pod ? attach(pod.host_connect_guest(via)) : undefined),
|
|
115
|
+
(e) => reply({ type: "reset", id: m.id, reason: String(e?.message ?? e) }),
|
|
116
|
+
);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
let c;
|
|
120
|
+
try {
|
|
121
|
+
c = pod.host_connect(String(m.address), m.port);
|
|
122
|
+
} catch (e) {
|
|
123
|
+
reply({ type: "reset", id: m.id, reason: String(e?.message ?? e) });
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
attach(c);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case "tls": {
|
|
130
|
+
const error = conn === undefined ? "no such connection" : pod.host_start_tls(conn, m.servername ?? undefined);
|
|
131
|
+
reply({ type: "tls", id: m.id, ...(error ? { error } : {}) });
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case "data":
|
|
135
|
+
if (conn !== undefined) pod.host_send(conn, m.data);
|
|
136
|
+
break;
|
|
137
|
+
case "ack":
|
|
138
|
+
if (conn !== undefined) pod.host_ack(conn, m.bytes);
|
|
139
|
+
break;
|
|
140
|
+
case "end":
|
|
141
|
+
if (conn !== undefined) pod.host_end(conn);
|
|
142
|
+
break;
|
|
143
|
+
case "close":
|
|
144
|
+
if (conn !== undefined) {
|
|
145
|
+
pod.host_abort(conn);
|
|
146
|
+
hs.conns.delete(m.id);
|
|
147
|
+
hostConns.delete(conn);
|
|
148
|
+
}
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
wake?.();
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The process is gone. Its port may still hold what it sent last (another channel than this message): until its
|
|
157
|
+
* "bye" (or a second), then its listens end, the connections it accepted close as a process's exit closes them
|
|
158
|
+
* (what it wrote still goes out), the ones it opened are reset.
|
|
159
|
+
*/
|
|
160
|
+
function closeHostSocketSoon(id) {
|
|
161
|
+
const hs = hostSockets.get(id);
|
|
162
|
+
if (!hs || hs.bye) return closeHostSocket(id);
|
|
163
|
+
if (hs.closing) return;
|
|
164
|
+
const t = setTimeout(() => closeHostSocket(id), 1000);
|
|
165
|
+
hs.closing = () => { clearTimeout(t); closeHostSocket(id); };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function closeHostSocket(id) {
|
|
169
|
+
const hs = hostSockets.get(id);
|
|
170
|
+
if (!hs) return;
|
|
171
|
+
hostSockets.delete(id);
|
|
172
|
+
for (const [p, l] of hostListeners) {
|
|
173
|
+
if (l.owner !== id) continue;
|
|
174
|
+
hostListeners.delete(p);
|
|
175
|
+
pod?.host_unlisten(p);
|
|
176
|
+
}
|
|
177
|
+
for (const [local, c] of hs.conns) {
|
|
178
|
+
if (local < 0) pod?.host_end(c);
|
|
179
|
+
pod?.host_abort(c);
|
|
180
|
+
hostConns.delete(c);
|
|
181
|
+
}
|
|
182
|
+
hs.port.close();
|
|
183
|
+
wake?.();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** The stack's host connection events, to their processes. */
|
|
187
|
+
function deliverHostEvents() {
|
|
188
|
+
for (const [c, kind, payload] of pod.take_host_events()) {
|
|
189
|
+
if (kind === "accept") {
|
|
190
|
+
const owner = hostListeners.get(payload)?.owner;
|
|
191
|
+
const hs = owner === undefined ? undefined : hostSockets.get(owner);
|
|
192
|
+
if (!hs) {
|
|
193
|
+
pod.host_abort(c);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const local = --nextAccepted;
|
|
197
|
+
hs.conns.set(local, c);
|
|
198
|
+
hostConns.set(c, [owner, local]);
|
|
199
|
+
hs.port.postMessage({ type: "accept", id: local, port: payload });
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const owner = hostConns.get(c);
|
|
203
|
+
if (!owner) continue;
|
|
204
|
+
const hs = hostSockets.get(owner[0]);
|
|
205
|
+
if (kind === "data") hs?.port.postMessage({ type: "data", id: owner[1], data: payload }, [payload.buffer]);
|
|
206
|
+
else if (kind === "end") hs?.port.postMessage({ type: "end", id: owner[1] });
|
|
207
|
+
else {
|
|
208
|
+
hs?.port.postMessage({ type: "reset", id: owner[1], reason: payload });
|
|
209
|
+
hs?.conns.delete(owner[1]);
|
|
210
|
+
hostConns.delete(c);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
28
215
|
/** Relay tokens come from the page (getToken() cannot cross to the Worker). */
|
|
29
216
|
function requestToken() {
|
|
30
217
|
const id = ++nextToken;
|
|
@@ -115,6 +302,7 @@ async function pump(memory, fromEngine, toEngine) {
|
|
|
115
302
|
for (const f of batch) pod.push(f);
|
|
116
303
|
const now = performance.now();
|
|
117
304
|
const active = pod.poll(now);
|
|
305
|
+
if (hostConns.size || hostListeners.size) deliverHostEvents();
|
|
118
306
|
const out = pod.take();
|
|
119
307
|
for (const f of out) if (!tx.tryPush(f)) await tx.pushAsync(f);
|
|
120
308
|
for (const [id, f] of pod.take_forwarded()) peers.get(id)?.postMessage(f, [f.buffer]);
|
|
@@ -147,6 +335,7 @@ async function onAttach(m) {
|
|
|
147
335
|
setInterval(() => pod?.set_unix_time(Date.now() / 1000), 60_000);
|
|
148
336
|
for (const p of pendingInbound.splice(0)) pod.open_inbound(p);
|
|
149
337
|
for (const p of pendingPeers.splice(0)) addPeer(p);
|
|
338
|
+
for (const h of pendingHostSockets.splice(0)) openHostSocket(h);
|
|
150
339
|
post({ type: "ready" });
|
|
151
340
|
pump(m.memory, m.fromEngine, m.toEngine);
|
|
152
341
|
}
|
|
@@ -178,6 +367,38 @@ self.onmessage = async ({ data: m }) => {
|
|
|
178
367
|
if (pod) addPeer(m);
|
|
179
368
|
else pendingPeers.push(m);
|
|
180
369
|
break;
|
|
370
|
+
case "host-socket":
|
|
371
|
+
if (pod) openHostSocket(m);
|
|
372
|
+
else pendingHostSockets.push(m);
|
|
373
|
+
break;
|
|
374
|
+
case "host-listen": {
|
|
375
|
+
let reply;
|
|
376
|
+
try {
|
|
377
|
+
if (!pod || !hostSockets.has(m.id)) throw new Error("no such host socket");
|
|
378
|
+
reply = { port: hostListen(m.id, m.port) };
|
|
379
|
+
} catch (e) {
|
|
380
|
+
reply = { error: String(e?.message ?? e) };
|
|
381
|
+
}
|
|
382
|
+
post({ type: "host-listen", req: m.req, ...reply });
|
|
383
|
+
wake?.();
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "host-connect-guest": {
|
|
387
|
+
const w = guestConnects.get(m.req);
|
|
388
|
+
guestConnects.delete(m.req);
|
|
389
|
+
if (typeof m.port === "number") w?.resolve(m.port);
|
|
390
|
+
else w?.reject(new Error(m.error ?? "connection refused"));
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
case "host-unlisten":
|
|
394
|
+
if (pod) hostUnlisten(m.id, m.port);
|
|
395
|
+
break;
|
|
396
|
+
case "host-socket-close": {
|
|
397
|
+
const i = pendingHostSockets.findIndex((h) => h.id === m.id);
|
|
398
|
+
if (i >= 0) pendingHostSockets.splice(i, 1)[0].port.close();
|
|
399
|
+
closeHostSocketSoon(m.id);
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
181
402
|
case "peer-gone":
|
|
182
403
|
peers.get(m.id)?.close();
|
|
183
404
|
peers.delete(m.id);
|