mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-10-08 10:56:13 -08:00
* feat(easytier-go): accept data plane ABI v4 The core raised DATA_PLANE_ABI_VERSION to 4 inf26c2aa1("feat(wasi): run EasyTier core on Cloudflare Workers and browsers") to advertise the new guest exports easytier_data_plane_tcp_shutdown_write_submit and easytier_data_plane_tcp_shutdown_write_result_take, which let a host half-close guest TCP streams. The Go host has no caller for half-close: net.Conn exposes only Close, which already tears down both directions, so the v3 behavior is preserved and no new plumbing is added. Without this bump, any artifact rebuilt from current core source is rejected at instance creation with "unsupported EasyTier data plane ABI version 4, want 3". * fix(easytier-go): decode socket options wire v3, rebuild core artifact38e2a621("refactor(ohos): 拆分 OHRS 包并按 socket 精细保护 VPN 流量") raised the host socket options wire format from version 2 to 3: a need_protect byte is appended after the purpose byte in TCP connect, UDP bind, and TCP listen options, shifting bind_device one byte later. The Go hostABI decoders were never updated, so every socket operation from a HEAD-built core was rejected as "invalid options" and required listeners failed to start. Bump the accepted wire version to 3 and skip the need_protect byte in all three decoders. The byte requests VPN socket protection, which only Android-style VPN hosts can honor; on every other platform sockets are already protected, so reading and discarding it is correct. Regenerate the embedded core artifact and protobuf bindings from HEAD (599e4eac) so the shipped artifact matches the Go host again. The proto regeneration also picks up schema fields added since the last embed (e.g. prefer_peer_relay). * perf(easytier-go): release compiler garbage after host init wazero's optimizing compiler allocates ~100MB of throwaway state on the Go heap while compiling the embedded core. Go's runtime does not return that memory to the OS after the initiating GC, so the process retained the compilation peak for its entire lifetime: RSS sat at ~164MB before any instance or network activity. Call debug.FreeOSMemory() once after the module is instantiated. NewHost is a one-time initialization path outside the dataplane, so the stop-the-world pass is safe here. Measured RSS after host.New drops from ~164MB to ~64MB; no behavioral change. * chore(easytier-js): bump toolchain dependencies - vitest 2.1.9 -> 3.2.7 (all three packages) - esbuild 0.25.9 -> 0.28.2 (browser bundler) - wrangler 4.114.0 -> 4.134.0 (cloudflare + web example) - @cloudflare/workers-types 5.20260724.1 -> 5.20260917.1 - binaryen 131.0.0 -> 132.0.0 (JS bindings only; the wasm build uses the standalone wasm-opt binary fetched by the build script) Not bumped: typescript stays at 5.9.3 (latest 5.x; 7.0 is a major jump not worth taking for this workspace), vite stays at 5.4.21 (web-example only; 5->8 spans three majors). Verified with pnpm test (28 + 2 + 2 tests across runtime, browser, cloudflare) and pnpm check (tsc + wrangler deploy --dry-run). pnpm-workspace.yaml gained three minimumReleaseAgeExclude entries recorded automatically by pnpm for freshly published versions. * fix(easytier-js): align host wire format with core wire v3 The core socket options wire format moved to version 3 in38e2a621(need_protect byte after purpose). The JS websocket-host still required version 2 and rejected every TCP bind from a HEAD-built core, breaking browser port leases. - websocket-host.ts: accept version 3, minimum length 49. The need_protect byte sits after purpose (offset 43) and is a no-op outside Android VPN hosts, so the decoder just skips it. - websocket-host.test.ts: update the test encoder to emit v3. - binaryen stays at 131.0.0 to match script/build-wasi-core.sh, which intentionally pins binaryen 131 for the Go-side embedded core. The npm binaryen package provides the wasm-opt binary used by build-wasm.mjs, so keeping both sides on the same version avoids divergent optimization output. - pnpm-workspace.yaml: drop two stale minimumReleaseAgeExclude entries for @cloudflare/workers-types versions no longer in the lockfile. * test(easytier-go): cover socket options wire v3 decoders Direct unit tests for decodeTCPConnectOptions, decodeUDPBindOptions, and decodeTCPListenOptions with wire v3 documents. Covers combinations of socket mark, netns, bind device, and local address presence, plus rejection of wire v2. Resolves review feedback on PR #2588.
421 lines
13 KiB
TypeScript
421 lines
13 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { WebSocketHost } from "../src/websocket-host";
|
|
|
|
type HostCall = (...parameters: Array<number | bigint>) => number | bigint;
|
|
|
|
class MockWebSocket {
|
|
readonly sent: Uint8Array[] = [];
|
|
readonly closes: Array<{ code?: number; reason?: string }> = [];
|
|
bufferedAmount = 0;
|
|
|
|
send(message: Uint8Array): void {
|
|
this.sent.push(message.slice());
|
|
}
|
|
|
|
close(code?: number, reason?: string): void {
|
|
this.closes.push({ code, reason });
|
|
}
|
|
}
|
|
|
|
function call(
|
|
host: WebSocketHost,
|
|
name: string,
|
|
...parameters: Array<number | bigint>
|
|
): number | bigint {
|
|
const imported = host.imports[name];
|
|
if (typeof imported !== "function") {
|
|
throw new Error(`missing host import ${name}`);
|
|
}
|
|
return (imported as HostCall)(...parameters);
|
|
}
|
|
|
|
class MockOutboundWebSocket extends MockWebSocket {
|
|
binaryType: "blob" | "arraybuffer" = "blob";
|
|
private readonly listeners = new Map<
|
|
string,
|
|
Array<(event: { data?: string | ArrayBuffer }) => void>
|
|
>();
|
|
|
|
addEventListener(
|
|
type: string,
|
|
listener: (event: { data?: string | ArrayBuffer }) => void,
|
|
): void {
|
|
const listeners = this.listeners.get(type) ?? [];
|
|
listeners.push(listener);
|
|
this.listeners.set(type, listeners);
|
|
}
|
|
|
|
emit(type: string, data?: string | ArrayBuffer): void {
|
|
for (const listener of this.listeners.get(type) ?? []) {
|
|
listener({ data });
|
|
}
|
|
}
|
|
}
|
|
|
|
function fixture(): {
|
|
host: WebSocketHost;
|
|
memory: WebAssembly.Memory;
|
|
socket: MockWebSocket;
|
|
handle: bigint;
|
|
} {
|
|
const host = new WebSocketHost();
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
const socket = new MockWebSocket();
|
|
const handle = host.register(socket as unknown as WebSocket);
|
|
host.transferToGuest(handle);
|
|
return { host, memory, socket, handle };
|
|
}
|
|
|
|
function encodeTcpPortLease(
|
|
memory: WebAssembly.Memory,
|
|
pointer: number,
|
|
port: number,
|
|
purpose = 6,
|
|
): number {
|
|
const options = new Uint8Array(memory.buffer, pointer, 49);
|
|
options.fill(0);
|
|
options[0] = 3;
|
|
options[1] = 4;
|
|
const view = new DataView(memory.buffer, pointer, options.byteLength);
|
|
view.setUint16(18, port, false);
|
|
options[42] = purpose;
|
|
// options[43] is need_protect, left at 0; the browser host is not a VPN.
|
|
return options.byteLength;
|
|
}
|
|
|
|
describe("WebSocketHost", () => {
|
|
it("isolates application event handler failures from the guest", () => {
|
|
const log = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
const host = new WebSocketHost(undefined, () => {
|
|
throw new Error("application callback failed");
|
|
});
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
const kind = new TextEncoder().encode("peer_added");
|
|
const message = new TextEncoder().encode("connected");
|
|
new Uint8Array(memory.buffer, 64, kind.byteLength).set(kind);
|
|
new Uint8Array(memory.buffer, 128, message.byteLength).set(message);
|
|
|
|
expect(
|
|
call(
|
|
host,
|
|
"emit_event",
|
|
2n,
|
|
64,
|
|
kind.byteLength,
|
|
128,
|
|
message.byteLength,
|
|
),
|
|
).toBe(0);
|
|
expect(log).toHaveBeenCalledWith(
|
|
expect.stringContaining("easytier_event_handler_failed"),
|
|
);
|
|
log.mockRestore();
|
|
});
|
|
|
|
it("releases every socket and pending operation on shutdown", () => {
|
|
const { host, socket, handle } = fixture();
|
|
expect(
|
|
call(host, "start_tunnel_receive", handle, 99n, 1024),
|
|
).toBe(0);
|
|
expect(host.health()).toEqual({
|
|
connections: 1,
|
|
queuedBytes: 0,
|
|
pendingOperations: 1,
|
|
});
|
|
|
|
host.shutdown();
|
|
host.shutdown();
|
|
|
|
expect(host.health()).toEqual({
|
|
connections: 0,
|
|
queuedBytes: 0,
|
|
pendingOperations: 0,
|
|
});
|
|
expect(socket.closes).toEqual([
|
|
{ code: 1000, reason: "EasyTier runtime stopped" },
|
|
]);
|
|
});
|
|
|
|
it("opens outbound WebSockets as guest-owned tunnel handles", () => {
|
|
const sockets: MockOutboundWebSocket[] = [];
|
|
const urls: string[] = [];
|
|
const host = new WebSocketHost((url) => {
|
|
urls.push(url);
|
|
const socket = new MockOutboundWebSocket();
|
|
sockets.push(socket);
|
|
return socket as unknown as WebSocket;
|
|
});
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
let wakes = 0;
|
|
host.setWakeGuest(() => {
|
|
wakes += 1;
|
|
});
|
|
const encoded = new TextEncoder().encode("wss://relay.example/");
|
|
new Uint8Array(memory.buffer, 64, encoded.byteLength).set(encoded);
|
|
|
|
expect(
|
|
call(
|
|
host,
|
|
"start_tunnel_connect",
|
|
1n,
|
|
64,
|
|
encoded.byteLength,
|
|
),
|
|
).toBe(0);
|
|
expect(call(host, "take_tunnel_connect", 1n)).toBe(-1n);
|
|
expect(urls).toEqual(["wss://relay.example/"]);
|
|
const socket = sockets[0];
|
|
expect(socket).toBeDefined();
|
|
if (socket === undefined) {
|
|
throw new Error("outbound WebSocket was not created");
|
|
}
|
|
expect(socket.binaryType).toBe("arraybuffer");
|
|
|
|
socket.emit("open");
|
|
expect(wakes).toBe(1);
|
|
const handle = call(host, "take_tunnel_connect", 1n);
|
|
expect(typeof handle).toBe("bigint");
|
|
expect(handle).toBeGreaterThan(0n);
|
|
expect(host.health()).toEqual({
|
|
connections: 1,
|
|
queuedBytes: 0,
|
|
pendingOperations: 0,
|
|
});
|
|
});
|
|
|
|
it("reports outbound WebSocket failure before ownership transfer", () => {
|
|
const socket = new MockOutboundWebSocket();
|
|
const host = new WebSocketHost(
|
|
() => socket as unknown as WebSocket,
|
|
);
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
const encoded = new TextEncoder().encode("ws://relay.example/");
|
|
new Uint8Array(memory.buffer, 32, encoded.byteLength).set(encoded);
|
|
|
|
expect(
|
|
call(
|
|
host,
|
|
"start_tunnel_connect",
|
|
2n,
|
|
32,
|
|
encoded.byteLength,
|
|
),
|
|
).toBe(0);
|
|
socket.emit("error");
|
|
expect(call(host, "take_tunnel_connect", 2n)).toBe(-10n);
|
|
expect(host.health().connections).toBe(0);
|
|
});
|
|
|
|
it("rejects outbound WebSocket requests when no factory is installed", () => {
|
|
const host = new WebSocketHost();
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
const encoded = new TextEncoder().encode("ws://relay.example/");
|
|
new Uint8Array(memory.buffer, 16, encoded.byteLength).set(encoded);
|
|
|
|
expect(
|
|
call(
|
|
host,
|
|
"start_tunnel_connect",
|
|
3n,
|
|
16,
|
|
encoded.byteLength,
|
|
),
|
|
).toBe(-4);
|
|
});
|
|
|
|
it("leases browser-local TCP ports for the smoltcp data plane", () => {
|
|
const host = new WebSocketHost(() => {
|
|
throw new Error("unexpected WebSocket connection");
|
|
});
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
const optionsLength = encodeTcpPortLease(memory, 128, 55_000);
|
|
|
|
expect(call(host, "start_tcp_bind", 4n, 128, optionsLength)).toBe(0);
|
|
expect(call(host, "start_tcp_bind", 4n, 128, optionsLength)).toBe(-3);
|
|
expect(call(host, "take_tcp_bind", 4n, 256, 35)).toBe(0);
|
|
|
|
const result = new Uint8Array(memory.buffer, 256, 35);
|
|
const resultView = new DataView(
|
|
result.buffer,
|
|
result.byteOffset,
|
|
result.byteLength,
|
|
);
|
|
const handle = resultView.getBigUint64(0, false);
|
|
expect(handle).toBeGreaterThan(0n);
|
|
expect(result[8]).toBe(4);
|
|
expect(resultView.getUint16(25, false)).toBe(55_000);
|
|
|
|
expect(call(host, "start_tcp_bind", 5n, 128, optionsLength)).toBe(-3);
|
|
expect(call(host, "close", handle)).toBe(0);
|
|
expect(call(host, "start_tcp_bind", 6n, 128, optionsLength)).toBe(0);
|
|
expect(call(host, "cancel_operation", 6n)).toBe(0);
|
|
});
|
|
|
|
it("rejects host TCP listeners other than browser port leases", () => {
|
|
const host = new WebSocketHost(() => {
|
|
throw new Error("unexpected WebSocket connection");
|
|
});
|
|
const memory = new WebAssembly.Memory({ initial: 1 });
|
|
host.bindMemory(memory);
|
|
|
|
const optionsLength = encodeTcpPortLease(memory, 128, 0, 4);
|
|
expect(call(host, "start_tcp_bind", 7n, 128, optionsLength)).toBe(-4);
|
|
|
|
const inboundOnlyHost = new WebSocketHost();
|
|
inboundOnlyHost.bindMemory(memory);
|
|
encodeTcpPortLease(memory, 128, 0);
|
|
expect(
|
|
call(inboundOnlyHost, "start_tcp_bind", 8n, 128, optionsLength),
|
|
).toBe(-4);
|
|
});
|
|
|
|
it("preserves binary message boundaries", () => {
|
|
const { host, memory, handle } = fixture();
|
|
host.receive(handle, new Uint8Array([1, 2, 3]).buffer);
|
|
host.receive(handle, new Uint8Array([8, 9]).buffer);
|
|
|
|
expect(call(host, "start_tunnel_receive", handle, 10n, 64)).toBe(
|
|
0,
|
|
);
|
|
expect(call(host, "take_tunnel_receive", 10n, 100, 64)).toBe(3);
|
|
expect(new Uint8Array(memory.buffer, 100, 3)).toEqual(
|
|
new Uint8Array([1, 2, 3]),
|
|
);
|
|
|
|
expect(call(host, "start_tunnel_receive", handle, 11n, 64)).toBe(
|
|
0,
|
|
);
|
|
expect(call(host, "take_tunnel_receive", 11n, 200, 64)).toBe(2);
|
|
expect(new Uint8Array(memory.buffer, 200, 2)).toEqual(
|
|
new Uint8Array([8, 9]),
|
|
);
|
|
});
|
|
|
|
it("accounts for a ready receive until its probed message is taken", () => {
|
|
const { host, memory, handle } = fixture();
|
|
expect(call(host, "start_tunnel_receive", handle, 12n, 64)).toBe(
|
|
0,
|
|
);
|
|
|
|
host.receive(handle, new Uint8Array([1, 2, 3]).buffer);
|
|
expect(host.health()).toEqual({
|
|
connections: 1,
|
|
queuedBytes: 3,
|
|
pendingOperations: 1,
|
|
});
|
|
expect(call(host, "take_tunnel_receive", 12n, 0, 0)).toBe(3);
|
|
expect(host.health().queuedBytes).toBe(3);
|
|
|
|
expect(call(host, "take_tunnel_receive", 12n, 500, 3)).toBe(3);
|
|
expect(new Uint8Array(memory.buffer, 500, 3)).toEqual(
|
|
new Uint8Array([1, 2, 3]),
|
|
);
|
|
expect(host.health()).toEqual({
|
|
connections: 1,
|
|
queuedBytes: 0,
|
|
pendingOperations: 0,
|
|
});
|
|
|
|
expect(call(host, "start_tunnel_receive", handle, 13n, 64)).toBe(
|
|
0,
|
|
);
|
|
host.receive(handle, new Uint8Array([4, 5]).buffer);
|
|
expect(call(host, "cancel_operation", 13n)).toBe(0);
|
|
expect(host.health().queuedBytes).toBe(0);
|
|
|
|
expect(call(host, "start_tunnel_receive", handle, 14n, 64)).toBe(
|
|
0,
|
|
);
|
|
host.receive(handle, new ArrayBuffer(0));
|
|
expect(call(host, "take_tunnel_receive", 14n, 0, 0)).toBe(0);
|
|
expect(call(host, "take_tunnel_receive", 14n, 1, 0)).toBe(0);
|
|
expect(host.health().pendingOperations).toBe(0);
|
|
});
|
|
|
|
it("copies outbound data before returning from submit", () => {
|
|
const { host, memory, socket, handle } = fixture();
|
|
const source = new Uint8Array(memory.buffer, 300, 3);
|
|
source.set([4, 5, 6]);
|
|
|
|
expect(call(host, "start_tunnel_send", handle, 20n, 300, 3)).toBe(
|
|
0,
|
|
);
|
|
source.fill(9);
|
|
expect(call(host, "take_tunnel_send", 20n)).toBe(0);
|
|
expect(socket.sent).toEqual([new Uint8Array([4, 5, 6])]);
|
|
});
|
|
|
|
it("counts a ready receive against the per-connection byte limit", () => {
|
|
const { host, socket, handle } = fixture();
|
|
const megabyte = 1024 * 1024;
|
|
expect(
|
|
call(
|
|
host,
|
|
"start_tunnel_receive",
|
|
handle,
|
|
21n,
|
|
megabyte,
|
|
),
|
|
).toBe(0);
|
|
|
|
host.receive(handle, new ArrayBuffer(megabyte));
|
|
host.receive(handle, new ArrayBuffer(megabyte));
|
|
expect(host.health().queuedBytes).toBe(2 * megabyte);
|
|
|
|
host.receive(handle, new ArrayBuffer(1));
|
|
expect(socket.closes).toContainEqual({
|
|
code: 1009,
|
|
reason: "receive queue limit",
|
|
});
|
|
expect(host.health().queuedBytes).toBe(2 * megabyte);
|
|
|
|
expect(call(host, "close", handle)).toBe(0);
|
|
expect(host.health().queuedBytes).toBe(0);
|
|
});
|
|
|
|
it("rejects text before exposing the tunnel payload ABI", () => {
|
|
const { host, socket, handle } = fixture();
|
|
host.receive(handle, "not a packet");
|
|
expect(socket.closes).toContainEqual({
|
|
code: 1003,
|
|
reason: "binary tunnel payload required",
|
|
});
|
|
expect(call(host, "start_tunnel_receive", handle, 30n, 64)).toBe(0);
|
|
expect(call(host, "take_tunnel_receive", 30n, 400, 64)).toBe(-3);
|
|
});
|
|
|
|
it("reports remote close as tunnel EOF", () => {
|
|
const { host, handle } = fixture();
|
|
expect(call(host, "start_tunnel_receive", handle, 31n, 64)).toBe(0);
|
|
host.remoteClose(handle);
|
|
expect(call(host, "take_tunnel_receive", 31n, 400, 64)).toBe(
|
|
-10,
|
|
);
|
|
});
|
|
|
|
it("closes resources and cancellation idempotently", () => {
|
|
const { host, socket, handle } = fixture();
|
|
expect(call(host, "start_tunnel_receive", handle, 40n, 64)).toBe(
|
|
0,
|
|
);
|
|
expect(call(host, "cancel_operation", 40n)).toBe(0);
|
|
expect(call(host, "cancel_operation", 40n)).toBe(0);
|
|
expect(call(host, "close", handle)).toBe(0);
|
|
expect(call(host, "close", handle)).toBe(0);
|
|
|
|
expect(socket.closes).toHaveLength(1);
|
|
expect(host.health()).toEqual({
|
|
connections: 0,
|
|
queuedBytes: 0,
|
|
pendingOperations: 0,
|
|
});
|
|
});
|
|
});
|