@seedprotocol/gateway-hyper 0.7.10-next.1353.g74aeeed → 0.7.10
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 +24 -2
- package/dist/bin.js +164 -48
- package/dist/bin.js.map +1 -1
- package/dist/cli.d.ts +5 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/keys.d.ts +16 -0
- package/dist/keys.d.ts.map +1 -1
- package/dist/src-DHEkxp5F.js +943 -0
- package/dist/src-DHEkxp5F.js.map +1 -0
- package/dist/tunnel/allowlist.d.ts +45 -0
- package/dist/tunnel/allowlist.d.ts.map +1 -0
- package/dist/tunnel/connect.d.ts.map +1 -1
- package/dist/tunnel/events.d.ts +131 -0
- package/dist/tunnel/events.d.ts.map +1 -0
- package/dist/tunnel/proxy.d.ts +11 -0
- package/dist/tunnel/proxy.d.ts.map +1 -1
- package/dist/tunnel/proxyApp.d.ts.map +1 -1
- package/dist/tunnel/rawConnect.d.ts +14 -0
- package/dist/tunnel/rawConnect.d.ts.map +1 -0
- package/dist/tunnel/rawPipe.d.ts +35 -0
- package/dist/tunnel/rawPipe.d.ts.map +1 -0
- package/dist/tunnel/rawServe.d.ts +18 -0
- package/dist/tunnel/rawServe.d.ts.map +1 -0
- package/dist/tunnel/serve.d.ts +11 -0
- package/dist/tunnel/serve.d.ts.map +1 -1
- package/dist/tunnel/session.d.ts +15 -1
- package/dist/tunnel/session.d.ts.map +1 -1
- package/dist/types.d.ts +125 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/src-qIlmrs1H.js +0 -573
- package/dist/src-qIlmrs1H.js.map +0 -1
|
@@ -0,0 +1,943 @@
|
|
|
1
|
+
import e from "node:fs";
|
|
2
|
+
import t from "node:path";
|
|
3
|
+
import n from "hyperdht";
|
|
4
|
+
import r from "hypercore-id-encoding";
|
|
5
|
+
import i from "node:http";
|
|
6
|
+
import { URL as a } from "node:url";
|
|
7
|
+
import o from "node:net";
|
|
8
|
+
//#region src/keys.ts
|
|
9
|
+
function s(e) {
|
|
10
|
+
return e.toString("hex");
|
|
11
|
+
}
|
|
12
|
+
function c(e) {
|
|
13
|
+
return Buffer.from(e, "hex");
|
|
14
|
+
}
|
|
15
|
+
function l(e) {
|
|
16
|
+
return r.encode(e);
|
|
17
|
+
}
|
|
18
|
+
function u(e) {
|
|
19
|
+
let t = e.trim();
|
|
20
|
+
if (!t) throw Error("Gateway Hyper key is empty");
|
|
21
|
+
try {
|
|
22
|
+
return Buffer.from(r.decode(t));
|
|
23
|
+
} catch {
|
|
24
|
+
if (/^[a-fA-F0-9]{64}$/.test(t)) return c(t);
|
|
25
|
+
throw Error(`Invalid Gateway Hyper key (expected z32 or 64-char hex): ${t.slice(0, 12)}…`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function d(r) {
|
|
29
|
+
let i = t.resolve(r);
|
|
30
|
+
if (e.existsSync(i)) {
|
|
31
|
+
let t = e.readFileSync(i, "utf8"), n = JSON.parse(t);
|
|
32
|
+
if (!n.publicKey || !n.secretKey) throw Error(`Invalid key file at ${i}: missing publicKey or secretKey`);
|
|
33
|
+
let r = c(n.publicKey);
|
|
34
|
+
return {
|
|
35
|
+
publicKey: r,
|
|
36
|
+
secretKey: c(n.secretKey),
|
|
37
|
+
z32: l(r)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
let a = t.dirname(i);
|
|
41
|
+
e.mkdirSync(a, { recursive: !0 });
|
|
42
|
+
let o = n.keyPair(), u = {
|
|
43
|
+
publicKey: s(o.publicKey),
|
|
44
|
+
secretKey: s(o.secretKey),
|
|
45
|
+
encoding: "hex"
|
|
46
|
+
};
|
|
47
|
+
return e.writeFileSync(i, `${JSON.stringify(u, null, 2)}\n`, { mode: 384 }), {
|
|
48
|
+
publicKey: o.publicKey,
|
|
49
|
+
secretKey: o.secretKey,
|
|
50
|
+
z32: l(o.publicKey)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function f(e) {
|
|
54
|
+
let { publicKey: t, secretKey: n } = e;
|
|
55
|
+
if (t?.length !== 32 || n?.length !== 64) throw Error("Invalid keyPair: expected a 32-byte publicKey and a 64-byte secretKey");
|
|
56
|
+
if (!Buffer.from(n.subarray(32)).equals(Buffer.from(t))) throw Error("Invalid keyPair: publicKey does not belong to secretKey");
|
|
57
|
+
return {
|
|
58
|
+
publicKey: t,
|
|
59
|
+
secretKey: n,
|
|
60
|
+
z32: l(t)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function p(e) {
|
|
64
|
+
if (e.keyFile && e.keyPair) throw Error("Pass keyFile or keyPair, not both");
|
|
65
|
+
return e.keyPair ? f(e.keyPair) : e.keyFile ? d(e.keyFile) : m();
|
|
66
|
+
}
|
|
67
|
+
function m() {
|
|
68
|
+
let e = n.keyPair();
|
|
69
|
+
return {
|
|
70
|
+
publicKey: e.publicKey,
|
|
71
|
+
secretKey: e.secretKey,
|
|
72
|
+
z32: l(e.publicKey)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/tunnel/protocol.ts
|
|
77
|
+
var h = 67108864, g = 1048576, _ = class {
|
|
78
|
+
buffer = Buffer.alloc(0);
|
|
79
|
+
waiters = [];
|
|
80
|
+
failure = null;
|
|
81
|
+
constructor(e) {
|
|
82
|
+
e.on("data", (e) => {
|
|
83
|
+
this.buffer = Buffer.concat([this.buffer, e]), this.drain();
|
|
84
|
+
}), e.on("error", (e) => this.fail(e)), e.on("end", () => this.fail(/* @__PURE__ */ Error("Unexpected end of stream"))), e.on("close", () => this.fail(/* @__PURE__ */ Error("Stream closed")));
|
|
85
|
+
}
|
|
86
|
+
readExact(e) {
|
|
87
|
+
return new Promise((t, n) => {
|
|
88
|
+
e === 0 ? t(Buffer.alloc(0)) : (this.waiters.push({
|
|
89
|
+
length: e,
|
|
90
|
+
resolve: t,
|
|
91
|
+
reject: n
|
|
92
|
+
}), this.drain(), this.rejectWaiters());
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
fail(e) {
|
|
96
|
+
this.failure ??= e, this.drain(), this.rejectWaiters();
|
|
97
|
+
}
|
|
98
|
+
rejectWaiters() {
|
|
99
|
+
if (!this.failure || this.waiters.length === 0) return;
|
|
100
|
+
let e = this.waiters.splice(0);
|
|
101
|
+
for (let t of e) t.reject(/* @__PURE__ */ Error(`${this.failure.message} (expected ${t.length} bytes, got ${this.buffer.length})`));
|
|
102
|
+
}
|
|
103
|
+
drain() {
|
|
104
|
+
for (; this.waiters.length > 0;) {
|
|
105
|
+
let e = this.waiters[0];
|
|
106
|
+
if (!e || this.buffer.length < e.length) break;
|
|
107
|
+
this.waiters.shift();
|
|
108
|
+
let t = this.buffer.subarray(0, e.length);
|
|
109
|
+
this.buffer = this.buffer.subarray(e.length), e.resolve(t);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}, v = /* @__PURE__ */ new WeakMap();
|
|
113
|
+
function y(e) {
|
|
114
|
+
let t = v.get(e);
|
|
115
|
+
return t || (t = new _(e), v.set(e, t)), t;
|
|
116
|
+
}
|
|
117
|
+
function b(e, t) {
|
|
118
|
+
return y(e).readExact(t);
|
|
119
|
+
}
|
|
120
|
+
function x(e, t) {
|
|
121
|
+
return new Promise((n, r) => {
|
|
122
|
+
if (e.destroyed) r(/* @__PURE__ */ Error("Stream closed"));
|
|
123
|
+
else try {
|
|
124
|
+
if (e.write(t)) {
|
|
125
|
+
n();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
let i = () => {
|
|
129
|
+
e.off("drain", a), e.off("error", o), e.off("close", s);
|
|
130
|
+
}, a = () => {
|
|
131
|
+
i(), n();
|
|
132
|
+
}, o = (e) => {
|
|
133
|
+
i(), r(e);
|
|
134
|
+
}, s = () => {
|
|
135
|
+
i(), r(/* @__PURE__ */ Error("Stream closed"));
|
|
136
|
+
};
|
|
137
|
+
e.once("drain", a), e.once("error", o), e.once("close", s);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
r(e instanceof Error ? e : Error(String(e)));
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
function S(e) {
|
|
144
|
+
if (e > 67108864) throw Error(`Frame too large: ${e} bytes (max ${h})`);
|
|
145
|
+
}
|
|
146
|
+
async function C(e, t) {
|
|
147
|
+
S(t.length);
|
|
148
|
+
let n = Buffer.alloc(4);
|
|
149
|
+
n.writeUInt32BE(t.length, 0);
|
|
150
|
+
let r = t.subarray(0, g);
|
|
151
|
+
await x(e, Buffer.concat([n, r]));
|
|
152
|
+
for (let n = r.length; n < t.length; n += g) await x(e, t.subarray(n, n + g));
|
|
153
|
+
}
|
|
154
|
+
async function w(e) {
|
|
155
|
+
let t = (await b(e, 4)).readUInt32BE(0);
|
|
156
|
+
return t === 0 ? Buffer.alloc(0) : (S(t), b(e, t));
|
|
157
|
+
}
|
|
158
|
+
async function T(e, t, n) {
|
|
159
|
+
S(n.length), await C(e, Buffer.from(JSON.stringify(t), "utf8")), await C(e, n);
|
|
160
|
+
}
|
|
161
|
+
async function E(e) {
|
|
162
|
+
let t = await w(e), n = JSON.parse(t.toString("utf8"));
|
|
163
|
+
if (!n.method || !n.path) throw Error("Invalid tunnel request meta: missing method or path");
|
|
164
|
+
return n.headers = n.headers ?? {}, {
|
|
165
|
+
meta: n,
|
|
166
|
+
body: await w(e)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async function D(e, t, n) {
|
|
170
|
+
S(n.length), await C(e, Buffer.from(JSON.stringify(t), "utf8")), await C(e, n);
|
|
171
|
+
}
|
|
172
|
+
async function O(e) {
|
|
173
|
+
let t = await w(e), n = JSON.parse(t.toString("utf8"));
|
|
174
|
+
if (typeof n.status != "number") throw Error("Invalid tunnel response meta: missing status");
|
|
175
|
+
return n.headers = n.headers ?? {}, {
|
|
176
|
+
meta: n,
|
|
177
|
+
body: await w(e)
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function k(e) {
|
|
181
|
+
let t = {};
|
|
182
|
+
for (let [n, r] of Object.entries(e)) r != null && (t[n] = Array.isArray(r) ? r.join(", ") : r);
|
|
183
|
+
return t;
|
|
184
|
+
}
|
|
185
|
+
function A(e) {
|
|
186
|
+
let t = /* @__PURE__ */ new Set([
|
|
187
|
+
"connection",
|
|
188
|
+
"keep-alive",
|
|
189
|
+
"transfer-encoding",
|
|
190
|
+
"upgrade"
|
|
191
|
+
]), n = {};
|
|
192
|
+
for (let [r, i] of Object.entries(e)) t.has(r.toLowerCase()) || i != null && (n[r] = Array.isArray(i) ? i.join(", ") : i);
|
|
193
|
+
return n;
|
|
194
|
+
}
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/tunnel/errors.ts
|
|
197
|
+
var j = class extends Error {
|
|
198
|
+
timeoutMs;
|
|
199
|
+
code = "TUNNEL_TIMEOUT";
|
|
200
|
+
constructor(e) {
|
|
201
|
+
super(`No bytes sent or received on the tunnel for ${e} ms`), this.timeoutMs = e, this.name = "TunnelTimeoutError";
|
|
202
|
+
}
|
|
203
|
+
}, M = class extends Error {
|
|
204
|
+
timeoutMs;
|
|
205
|
+
code = "TUNNEL_CONNECT_TIMEOUT";
|
|
206
|
+
constructor(e) {
|
|
207
|
+
super(`Could not connect to the operator within ${e} ms`), this.timeoutMs = e, this.name = "TunnelConnectTimeoutError";
|
|
208
|
+
}
|
|
209
|
+
}, N = class extends Error {
|
|
210
|
+
timeoutMs;
|
|
211
|
+
code = "UPSTREAM_TIMEOUT";
|
|
212
|
+
constructor(e) {
|
|
213
|
+
super(`No response from upstream for ${e} ms`), this.timeoutMs = e, this.name = "UpstreamTimeoutError";
|
|
214
|
+
}
|
|
215
|
+
}, ee = 3e4;
|
|
216
|
+
function te(e, t) {
|
|
217
|
+
if (!t) return e;
|
|
218
|
+
let n = new Set(Object.keys(t).map((e) => e.toLowerCase())), r = {};
|
|
219
|
+
for (let [t, i] of Object.entries(e)) n.has(t.toLowerCase()) || (r[t] = i);
|
|
220
|
+
return {
|
|
221
|
+
...r,
|
|
222
|
+
...t
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
var ne = /* @__PURE__ */ new Set([
|
|
226
|
+
"connection",
|
|
227
|
+
"keep-alive",
|
|
228
|
+
"proxy-connection",
|
|
229
|
+
"te",
|
|
230
|
+
"trailer",
|
|
231
|
+
"transfer-encoding",
|
|
232
|
+
"upgrade"
|
|
233
|
+
]);
|
|
234
|
+
function P(e) {
|
|
235
|
+
let t = new Set((Object.entries(e).find(([e]) => e.toLowerCase() === "connection")?.[1] ?? "").split(",").map((e) => e.trim().toLowerCase()).filter(Boolean)), n = {}, r;
|
|
236
|
+
for (let [i, a] of Object.entries(e)) {
|
|
237
|
+
let e = i.toLowerCase();
|
|
238
|
+
e === "host" ? r = a : e === "content-length" || ne.has(e) || t.has(e) || (n[i] = a);
|
|
239
|
+
}
|
|
240
|
+
return r && !Object.keys(n).some((e) => e.toLowerCase() === "x-forwarded-host") && (n["x-forwarded-host"] = r), n;
|
|
241
|
+
}
|
|
242
|
+
async function re(e, t, n, r = {}) {
|
|
243
|
+
let o = e.replace(/\/$/, ""), s = new a(t.path, `${o}/`), c = r.timeoutMs ?? 3e4, { observer: l } = r;
|
|
244
|
+
return new Promise((e, a) => {
|
|
245
|
+
l?.onPhase?.("send");
|
|
246
|
+
let o = i.request(s, {
|
|
247
|
+
method: t.method,
|
|
248
|
+
headers: te(P(t.headers), r.extraHeaders),
|
|
249
|
+
signal: r.signal
|
|
250
|
+
}, (t) => {
|
|
251
|
+
l && (l.onPhase?.("body"), l.onResponse?.(t.statusCode ?? 502));
|
|
252
|
+
let n = [], r = 0;
|
|
253
|
+
t.on("data", (e) => {
|
|
254
|
+
r += e.length, r > 67108864 ? o.destroy(/* @__PURE__ */ Error(`Upstream response exceeds ${h} bytes, the tunnel's limit`)) : n.push(e);
|
|
255
|
+
}), t.on("end", () => {
|
|
256
|
+
l?.onDone?.(t.statusCode ?? 502, r), e({
|
|
257
|
+
status: t.statusCode ?? 502,
|
|
258
|
+
headers: A(t.headers),
|
|
259
|
+
body: Buffer.concat(n)
|
|
260
|
+
});
|
|
261
|
+
}), t.on("error", a);
|
|
262
|
+
});
|
|
263
|
+
c > 0 && o.setTimeout(c, () => o.destroy(new N(c))), o.on("error", a), l?.onPhase && o.once("finish", () => l.onPhase?.("wait")), n.length > 0 ? o.end(n) : o.end();
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
//#endregion
|
|
267
|
+
//#region src/tunnel/allowlist.ts
|
|
268
|
+
function F(e) {
|
|
269
|
+
return o.isIPv4(e) ? "ipv4" : o.isIPv6(e) ? "ipv6" : null;
|
|
270
|
+
}
|
|
271
|
+
function ie(e) {
|
|
272
|
+
let t = new o.BlockList();
|
|
273
|
+
for (let n of e) {
|
|
274
|
+
let [e = "", r, i] = n.split("/"), a = F(e);
|
|
275
|
+
if (!a || i !== void 0) throw Error(`Invalid allowlist IP (expected an address or CIDR): ${n}`);
|
|
276
|
+
if (r === void 0) {
|
|
277
|
+
t.addAddress(e, a);
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
let o = Number(r), s = a === "ipv4" ? 32 : 128;
|
|
281
|
+
if (!/^\d+$/.test(r) || o > s) throw Error(`Invalid allowlist CIDR prefix (expected 0–${s}): ${n}`);
|
|
282
|
+
t.addSubnet(e, o, a);
|
|
283
|
+
}
|
|
284
|
+
return t;
|
|
285
|
+
}
|
|
286
|
+
function ae(e) {
|
|
287
|
+
let t = /* @__PURE__ */ new Set();
|
|
288
|
+
for (let n of e) t.add(u(n).toString("hex"));
|
|
289
|
+
return t;
|
|
290
|
+
}
|
|
291
|
+
function I(e) {
|
|
292
|
+
if (!e) return null;
|
|
293
|
+
let t = e.toLowerCase().startsWith("::ffff:") ? e.slice(7) : null;
|
|
294
|
+
return t && o.isIPv4(t) ? t : e;
|
|
295
|
+
}
|
|
296
|
+
function L(e) {
|
|
297
|
+
return (e ?? []).map((e) => e.trim()).filter(Boolean);
|
|
298
|
+
}
|
|
299
|
+
function R(e = {}) {
|
|
300
|
+
let t = L(e.keys), n = L(e.ips), r = ae(t), i = ie(n), a = r.size > 0 || n.length > 0, o = (e) => e != null && r.has(Buffer.from(e).toString("hex")), s = (e) => {
|
|
301
|
+
if (!e) return !1;
|
|
302
|
+
let t = F(e);
|
|
303
|
+
return t !== null && i.check(e, t);
|
|
304
|
+
};
|
|
305
|
+
return {
|
|
306
|
+
enabled: a,
|
|
307
|
+
summary: a ? `keys=${r.size} ips=${n.join(",") || "none"}` : "off",
|
|
308
|
+
firewall: (e) => !a || n.length > 0 ? !1 : !o(e),
|
|
309
|
+
check: (e) => {
|
|
310
|
+
let t = I(e.rawStream?.remoteHost);
|
|
311
|
+
return a ? o(e.remotePublicKey) ? {
|
|
312
|
+
ok: !0,
|
|
313
|
+
host: t,
|
|
314
|
+
by: "key"
|
|
315
|
+
} : s(t) ? {
|
|
316
|
+
ok: !0,
|
|
317
|
+
host: t,
|
|
318
|
+
by: "ip"
|
|
319
|
+
} : {
|
|
320
|
+
ok: !1,
|
|
321
|
+
host: t,
|
|
322
|
+
by: "none"
|
|
323
|
+
} : {
|
|
324
|
+
ok: !0,
|
|
325
|
+
host: t,
|
|
326
|
+
by: "open"
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
//#endregion
|
|
332
|
+
//#region src/tunnel/events.ts
|
|
333
|
+
function z(e) {
|
|
334
|
+
if (e) return (t) => {
|
|
335
|
+
try {
|
|
336
|
+
e(t);
|
|
337
|
+
} catch {}
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
function B(e) {
|
|
341
|
+
let t = e instanceof Error ? e.message : String(e), n = e?.code;
|
|
342
|
+
return typeof n == "string" ? {
|
|
343
|
+
error: t,
|
|
344
|
+
code: n
|
|
345
|
+
} : { error: t };
|
|
346
|
+
}
|
|
347
|
+
function V() {
|
|
348
|
+
let e = Date.now().toString(36), t = 0, n = 0;
|
|
349
|
+
return {
|
|
350
|
+
stream: () => `s${++t}`,
|
|
351
|
+
request: () => `${e}-r${++n}`
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
//#endregion
|
|
355
|
+
//#region src/tunnel/serve.ts
|
|
356
|
+
function H(e, t) {
|
|
357
|
+
return {
|
|
358
|
+
status: e,
|
|
359
|
+
headers: { "content-type": "text/plain; charset=utf-8" },
|
|
360
|
+
body: Buffer.from(t, "utf8")
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
async function oe(e, t, n = {}) {
|
|
364
|
+
let r = z(n.onEvent), { requestIdHeader: i } = n, a = n.streamId ?? "s0", o = r || i ? n.nextRequestId ?? V().request : null, s = new AbortController(), c = () => s.abort();
|
|
365
|
+
e.once("end", c), e.once("close", c), e.on("error", c);
|
|
366
|
+
let l = performance.now(), u = 0, d = 0, f = null, p = null;
|
|
367
|
+
r && (e.on("data", (e) => {
|
|
368
|
+
d += e.length, f ??= performance.now();
|
|
369
|
+
}), e.on("error", (e) => r({
|
|
370
|
+
name: "stream.error",
|
|
371
|
+
stream: a,
|
|
372
|
+
...B(e)
|
|
373
|
+
})), e.once("close", () => r({
|
|
374
|
+
name: "stream.close",
|
|
375
|
+
stream: a,
|
|
376
|
+
requests: u,
|
|
377
|
+
bytesIn: d,
|
|
378
|
+
durationMs: Math.round(performance.now() - l),
|
|
379
|
+
inflight: p
|
|
380
|
+
})));
|
|
381
|
+
let m = E(e);
|
|
382
|
+
try {
|
|
383
|
+
for (;;) {
|
|
384
|
+
let { meta: c, body: l } = await m, d = o?.() ?? null, h = f ?? performance.now();
|
|
385
|
+
f = null;
|
|
386
|
+
let g = 0, _ = "send";
|
|
387
|
+
r && d && (u++, p = d, r({
|
|
388
|
+
name: "req.recv",
|
|
389
|
+
request: d,
|
|
390
|
+
stream: a,
|
|
391
|
+
method: c.method,
|
|
392
|
+
path: c.path,
|
|
393
|
+
bodyBytes: l.length,
|
|
394
|
+
receiveMs: Math.round(performance.now() - h)
|
|
395
|
+
}), g = performance.now());
|
|
396
|
+
let v = () => Math.round(performance.now() - g), y;
|
|
397
|
+
try {
|
|
398
|
+
y = await re(t, c, l, {
|
|
399
|
+
timeoutMs: n.upstreamTimeoutMs,
|
|
400
|
+
signal: s.signal,
|
|
401
|
+
extraHeaders: i && d ? { [i]: d } : void 0,
|
|
402
|
+
observer: r && d ? {
|
|
403
|
+
onPhase: (e) => {
|
|
404
|
+
(e !== "wait" || _ !== "body") && (_ = e);
|
|
405
|
+
},
|
|
406
|
+
onResponse: (e) => r({
|
|
407
|
+
name: "up.response",
|
|
408
|
+
request: d,
|
|
409
|
+
status: e,
|
|
410
|
+
upstreamMs: v()
|
|
411
|
+
}),
|
|
412
|
+
onDone: (e, t) => r({
|
|
413
|
+
name: "up.done",
|
|
414
|
+
request: d,
|
|
415
|
+
status: e,
|
|
416
|
+
bytes: t,
|
|
417
|
+
upstreamMs: v()
|
|
418
|
+
})
|
|
419
|
+
} : void 0
|
|
420
|
+
});
|
|
421
|
+
} catch (e) {
|
|
422
|
+
if (r && d && r(e instanceof N && !s.signal.aborted ? {
|
|
423
|
+
name: "up.timeout",
|
|
424
|
+
request: d,
|
|
425
|
+
phase: _,
|
|
426
|
+
timeoutMs: e.timeoutMs
|
|
427
|
+
} : {
|
|
428
|
+
name: "up.error",
|
|
429
|
+
request: d,
|
|
430
|
+
phase: _,
|
|
431
|
+
...B(e),
|
|
432
|
+
aborted: s.signal.aborted
|
|
433
|
+
}), s.signal.aborted) return;
|
|
434
|
+
let t = e instanceof Error ? e.message : String(e);
|
|
435
|
+
y = e instanceof N ? H(504, `Gateway Hyper upstream timeout: ${t}`) : H(502, `Gateway Hyper upstream error: ${t}`);
|
|
436
|
+
}
|
|
437
|
+
await D(e, {
|
|
438
|
+
status: y.status,
|
|
439
|
+
headers: y.headers
|
|
440
|
+
}, y.body), r && d && (p = null, r({
|
|
441
|
+
name: "res.sent",
|
|
442
|
+
request: d,
|
|
443
|
+
status: y.status,
|
|
444
|
+
bytes: y.body.length,
|
|
445
|
+
totalMs: Math.round(performance.now() - h)
|
|
446
|
+
})), m = E(e);
|
|
447
|
+
}
|
|
448
|
+
} catch {
|
|
449
|
+
e.destroy();
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
async function se(e) {
|
|
453
|
+
let t = e.upstream.replace(/\/$/, ""), r = e.keyFile ?? ".seed/gateway-tunnel/operator.key.json", i = R(e.allowlist), a = d(r), o = z(e.onEvent), s = V(), c = new n(e.bootstrap ? { bootstrap: e.bootstrap } : {}), u = c.createServer({ firewall: (e) => {
|
|
454
|
+
let t = i.firewall(e);
|
|
455
|
+
return t && o && o({
|
|
456
|
+
name: "stream.refused",
|
|
457
|
+
remoteKey: l(e),
|
|
458
|
+
stage: "handshake",
|
|
459
|
+
host: null
|
|
460
|
+
}), t;
|
|
461
|
+
} }, (n) => {
|
|
462
|
+
let r = i.check(n);
|
|
463
|
+
if (!r.ok) {
|
|
464
|
+
o?.({
|
|
465
|
+
name: "stream.refused",
|
|
466
|
+
remoteKey: l(n.remotePublicKey),
|
|
467
|
+
stage: "connection",
|
|
468
|
+
host: r.host
|
|
469
|
+
}), n.on("error", () => {}), n.destroy();
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
let a = s.stream();
|
|
473
|
+
o?.({
|
|
474
|
+
name: "stream.open",
|
|
475
|
+
stream: a,
|
|
476
|
+
remoteKey: l(n.remotePublicKey),
|
|
477
|
+
host: r.host,
|
|
478
|
+
by: r.by
|
|
479
|
+
}), oe(n, t, {
|
|
480
|
+
upstreamTimeoutMs: e.upstreamTimeoutMs,
|
|
481
|
+
onEvent: e.onEvent,
|
|
482
|
+
requestIdHeader: e.requestIdHeader,
|
|
483
|
+
streamId: a,
|
|
484
|
+
nextRequestId: s.request
|
|
485
|
+
});
|
|
486
|
+
});
|
|
487
|
+
return await u.listen(a), {
|
|
488
|
+
key: a.z32,
|
|
489
|
+
upstream: t,
|
|
490
|
+
close: async () => {
|
|
491
|
+
await u.close(), await c.destroy();
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
//#endregion
|
|
496
|
+
//#region src/tunnel/rawPipe.ts
|
|
497
|
+
var ce = 3e4;
|
|
498
|
+
function U(e, t) {
|
|
499
|
+
let n = t?.code, r = t instanceof Error ? t.message : String(t);
|
|
500
|
+
return `${e}:${typeof n == "string" ? n : r}`;
|
|
501
|
+
}
|
|
502
|
+
function W(e, t, n = {}) {
|
|
503
|
+
let r = n.graceMs ?? 3e4, i = performance.now(), a = null, o = 0;
|
|
504
|
+
e.on("data", (n) => {
|
|
505
|
+
t.write(n) || e.pause();
|
|
506
|
+
}), t.on("drain", () => e.resume()), e.on("end", () => t.end()), t.pipe(e), e.on("error", (e) => {
|
|
507
|
+
a ??= U("hyper", e);
|
|
508
|
+
}), t.on("error", (e) => {
|
|
509
|
+
a ??= U("tcp", e);
|
|
510
|
+
});
|
|
511
|
+
let s = (e) => () => {
|
|
512
|
+
e.destroyed || (a ? e.destroy() : (e.end(), setTimeout(() => e.destroy(), r).unref()));
|
|
513
|
+
}, c = () => {
|
|
514
|
+
++o < 2 || n.onClose?.({
|
|
515
|
+
tcpBytesRead: t.bytesRead,
|
|
516
|
+
tcpBytesWritten: t.bytesWritten,
|
|
517
|
+
durationMs: Math.round(performance.now() - i),
|
|
518
|
+
error: a
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
return e.on("close", s(t)), t.on("close", s(e)), e.on("close", c), t.on("close", c), { fail: (e) => {
|
|
522
|
+
a ??= e;
|
|
523
|
+
} };
|
|
524
|
+
}
|
|
525
|
+
//#endregion
|
|
526
|
+
//#region src/tunnel/rawServe.ts
|
|
527
|
+
var le = ".seed/gateway-tunnel/raw-operator.key.json";
|
|
528
|
+
function ue(e) {
|
|
529
|
+
let t = e.trim(), n = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(t) ? t : `tcp://${t}`), r = n.protocol === "http:" ? 80 : n.protocol === "https:" ? 443 : NaN, i = n.port ? Number(n.port) : r, a = n.hostname.replace(/^\[(.*)\]$/, "$1");
|
|
530
|
+
if (!a || !Number.isInteger(i)) throw Error(`Invalid raw upstream (expected http://host:port or host:port): ${e}`);
|
|
531
|
+
return {
|
|
532
|
+
host: a,
|
|
533
|
+
port: i
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
async function G(e) {
|
|
537
|
+
let t = ue(e.upstream), r = R(e.allowlist), i = e.keyPair ? f(e.keyPair) : d(e.keyFile ?? ".seed/gateway-tunnel/raw-operator.key.json"), a = z(e.onEvent), s = /* @__PURE__ */ new Set(), c = 0, u = new n(e.bootstrap ? { bootstrap: e.bootstrap } : {}), p = u.createServer({ firewall: (e) => {
|
|
538
|
+
let t = r.firewall(e);
|
|
539
|
+
return t && a && a({
|
|
540
|
+
name: "stream.refused",
|
|
541
|
+
remoteKey: l(e),
|
|
542
|
+
stage: "handshake",
|
|
543
|
+
host: null
|
|
544
|
+
}), t;
|
|
545
|
+
} }, (n) => {
|
|
546
|
+
let i = r.check(n);
|
|
547
|
+
if (!i.ok) {
|
|
548
|
+
a?.({
|
|
549
|
+
name: "stream.refused",
|
|
550
|
+
remoteKey: l(n.remotePublicKey),
|
|
551
|
+
stage: "connection",
|
|
552
|
+
host: i.host
|
|
553
|
+
}), n.on("error", () => {}), n.destroy();
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
let u = `c${++c}`;
|
|
557
|
+
a?.({
|
|
558
|
+
name: "conn.open",
|
|
559
|
+
conn: u,
|
|
560
|
+
remoteKey: l(n.remotePublicKey),
|
|
561
|
+
host: i.host,
|
|
562
|
+
by: i.by
|
|
563
|
+
});
|
|
564
|
+
let d = o.connect({
|
|
565
|
+
host: t.host,
|
|
566
|
+
port: t.port,
|
|
567
|
+
allowHalfOpen: !0
|
|
568
|
+
});
|
|
569
|
+
d.setNoDelay(!0), s.add(n), s.add(d), W(n, d, {
|
|
570
|
+
graceMs: e.closeGraceMs,
|
|
571
|
+
onClose: (e) => {
|
|
572
|
+
s.delete(n), s.delete(d), a?.({
|
|
573
|
+
name: "conn.close",
|
|
574
|
+
conn: u,
|
|
575
|
+
bytesUp: e.tcpBytesWritten,
|
|
576
|
+
bytesDown: e.tcpBytesRead,
|
|
577
|
+
durationMs: e.durationMs,
|
|
578
|
+
error: e.error
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
});
|
|
583
|
+
return await p.listen(i), {
|
|
584
|
+
key: i.z32,
|
|
585
|
+
upstream: `${t.host}:${t.port}`,
|
|
586
|
+
close: async () => {
|
|
587
|
+
for (let e of s) e.destroy();
|
|
588
|
+
await p.close(), await u.destroy();
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region src/tunnel/session.ts
|
|
594
|
+
var K = 6e4, de = 3e4, fe = 4;
|
|
595
|
+
function q(e) {
|
|
596
|
+
let t = e.reason;
|
|
597
|
+
if (t instanceof Error) return t;
|
|
598
|
+
let n = /* @__PURE__ */ Error("The request was aborted");
|
|
599
|
+
return n.name = "AbortError", n;
|
|
600
|
+
}
|
|
601
|
+
function pe(e, t) {
|
|
602
|
+
return t.aborted ? Promise.reject(q(t)) : new Promise((n, r) => {
|
|
603
|
+
let i = () => r(q(t));
|
|
604
|
+
t.addEventListener("abort", i, { once: !0 }), e.then((e) => {
|
|
605
|
+
t.removeEventListener("abort", i), n(e);
|
|
606
|
+
}, (e) => {
|
|
607
|
+
t.removeEventListener("abort", i), r(e);
|
|
608
|
+
});
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
function me(e, t = K) {
|
|
612
|
+
let n = async (n, r) => {
|
|
613
|
+
let { signal: i } = r, a = r.timeoutMs ?? t;
|
|
614
|
+
if (i?.aborted) throw q(i);
|
|
615
|
+
let o = await e();
|
|
616
|
+
if (i?.aborted) throw q(i);
|
|
617
|
+
return new Promise((e, t) => {
|
|
618
|
+
let r = !1, s = null, c = () => {
|
|
619
|
+
a <= 0 || (s && clearTimeout(s), s = setTimeout(() => u(new j(a)), a));
|
|
620
|
+
}, l = () => {
|
|
621
|
+
s && clearTimeout(s), o.off("data", c), o.off("drain", c), i?.removeEventListener("abort", d);
|
|
622
|
+
}, u = (e) => {
|
|
623
|
+
r || (r = !0, l(), o.destroy(), t(e));
|
|
624
|
+
}, d = () => u(q(i));
|
|
625
|
+
c(), o.on("data", c), o.on("drain", c), i?.addEventListener("abort", d, { once: !0 }), n(o).then((t) => {
|
|
626
|
+
r || (r = !0, l(), e(t));
|
|
627
|
+
}, u);
|
|
628
|
+
});
|
|
629
|
+
}, r = Promise.resolve();
|
|
630
|
+
return (e, t = {}) => {
|
|
631
|
+
let i = r.then(() => n(e, t));
|
|
632
|
+
return r = i.catch(() => {}), t.signal ? pe(i, t.signal) : i;
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
function J() {
|
|
636
|
+
return /* @__PURE__ */ Error("Tunnel session is closed");
|
|
637
|
+
}
|
|
638
|
+
function he(e, t = {}) {
|
|
639
|
+
let n = Math.max(1, Math.floor(t.maxStreams ?? 4)), r = t.requestTimeoutMs ?? 6e4, i = [], a = [], o = !1, s = () => {
|
|
640
|
+
let t = {
|
|
641
|
+
run: null,
|
|
642
|
+
socket: null,
|
|
643
|
+
busy: !1
|
|
644
|
+
};
|
|
645
|
+
return t.run = me(async () => {
|
|
646
|
+
if (o) throw J();
|
|
647
|
+
if (t.socket && !t.socket.destroyed) return t.socket;
|
|
648
|
+
let n = await e();
|
|
649
|
+
if (o) throw n.destroy(), J();
|
|
650
|
+
return t.socket = n, n;
|
|
651
|
+
}, r), i.push(t), t;
|
|
652
|
+
}, c = () => {
|
|
653
|
+
let e = i.filter((e) => !e.busy), t = e.find((e) => e.socket !== null && !e.socket.destroyed) ?? e[0] ?? (i.length < n ? s() : void 0);
|
|
654
|
+
return t ? (t.busy = !0, t) : null;
|
|
655
|
+
}, l = (e) => {
|
|
656
|
+
let t = a.shift();
|
|
657
|
+
t ? t.resolve(e) : e.busy = !1;
|
|
658
|
+
}, u = (e) => {
|
|
659
|
+
let t = c();
|
|
660
|
+
return t ? Promise.resolve(t) : new Promise((t, n) => {
|
|
661
|
+
let r = () => {
|
|
662
|
+
let t = a.indexOf(i);
|
|
663
|
+
t >= 0 && a.splice(t, 1), n(q(e));
|
|
664
|
+
}, i = {
|
|
665
|
+
resolve: (n) => {
|
|
666
|
+
e?.removeEventListener("abort", r), t(n);
|
|
667
|
+
},
|
|
668
|
+
reject: (t) => {
|
|
669
|
+
e?.removeEventListener("abort", r), n(t);
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
a.push(i), e?.addEventListener("abort", r, { once: !0 });
|
|
673
|
+
});
|
|
674
|
+
};
|
|
675
|
+
return {
|
|
676
|
+
runOnTunnel: async (e, t = {}) => {
|
|
677
|
+
if (t.signal?.aborted) throw q(t.signal);
|
|
678
|
+
if (o) throw J();
|
|
679
|
+
let n = await u(t.signal);
|
|
680
|
+
try {
|
|
681
|
+
return await n.run(e, t);
|
|
682
|
+
} finally {
|
|
683
|
+
l(n);
|
|
684
|
+
}
|
|
685
|
+
},
|
|
686
|
+
close: () => {
|
|
687
|
+
o = !0;
|
|
688
|
+
for (let e of a.splice(0)) e.reject(J());
|
|
689
|
+
for (let e of i) e.socket?.destroy();
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
async function Y(e) {
|
|
694
|
+
let t = u(e.key), r = p(e), i = e.connectTimeoutMs ?? 3e4, a = new n(e.bootstrap ? { bootstrap: e.bootstrap } : {}), o = he(() => new Promise((e, n) => {
|
|
695
|
+
let o = a.connect(t, { keyPair: {
|
|
696
|
+
publicKey: r.publicKey,
|
|
697
|
+
secretKey: r.secretKey
|
|
698
|
+
} }), s = !1, c = i > 0 ? setTimeout(() => l(new M(i)), i) : null, l = (e) => {
|
|
699
|
+
s || (s = !0, c && clearTimeout(c), o.destroy(), n(e));
|
|
700
|
+
}, u = () => {
|
|
701
|
+
s || (s = !0, c && clearTimeout(c), e(o));
|
|
702
|
+
};
|
|
703
|
+
o.on("error", (e) => l(e)), o.once("close", () => l(/* @__PURE__ */ Error("Tunnel connection closed before it opened"))), o.once("open", u), o.once("connect", u);
|
|
704
|
+
}), {
|
|
705
|
+
maxStreams: e.maxStreams,
|
|
706
|
+
requestTimeoutMs: e.requestTimeoutMs
|
|
707
|
+
}), { runOnTunnel: s } = o;
|
|
708
|
+
return {
|
|
709
|
+
key: e.key,
|
|
710
|
+
clientKey: r.z32,
|
|
711
|
+
runOnTunnel: s,
|
|
712
|
+
forwardHttp: async (e, t, n) => s(async (n) => (await T(n, e, t), O(n)), n),
|
|
713
|
+
close: async () => {
|
|
714
|
+
o.close(), await a.destroy();
|
|
715
|
+
}
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
//#endregion
|
|
719
|
+
//#region src/tunnel/rawConnect.ts
|
|
720
|
+
var ge = 1985, _e = 64;
|
|
721
|
+
async function ve(e) {
|
|
722
|
+
let t = u(e.key), r = p(e), i = e.host ?? "127.0.0.1", a = e.maxConnections ?? 64, s = e.connectTimeoutMs ?? 3e4, c = z(e.onEvent), l = /* @__PURE__ */ new Set(), d = 0, f = new n(e.bootstrap ? { bootstrap: e.bootstrap } : {}), m = o.createServer({ allowHalfOpen: !0 }, (n) => {
|
|
723
|
+
if (l.size >= a) {
|
|
724
|
+
c?.({
|
|
725
|
+
name: "conn.refused",
|
|
726
|
+
open: l.size,
|
|
727
|
+
max: a
|
|
728
|
+
}), n.destroy();
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
let i = `c${++d}`, o = performance.now();
|
|
732
|
+
l.add(n), n.setNoDelay(!0);
|
|
733
|
+
let u = f.connect(t, { keyPair: {
|
|
734
|
+
publicKey: r.publicKey,
|
|
735
|
+
secretKey: r.secretKey
|
|
736
|
+
} }), p = null, m = W(u, n, {
|
|
737
|
+
graceMs: e.closeGraceMs,
|
|
738
|
+
onClose: (e) => {
|
|
739
|
+
p && clearTimeout(p), l.delete(n), c?.({
|
|
740
|
+
name: "conn.close",
|
|
741
|
+
conn: i,
|
|
742
|
+
bytesUp: e.tcpBytesRead,
|
|
743
|
+
bytesDown: e.tcpBytesWritten,
|
|
744
|
+
durationMs: e.durationMs,
|
|
745
|
+
error: e.error
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
});
|
|
749
|
+
s > 0 && (p = setTimeout(() => {
|
|
750
|
+
m.fail("connect-timeout"), u.destroy(new M(s));
|
|
751
|
+
}, s)), u.once("open", () => {
|
|
752
|
+
p && clearTimeout(p), c?.({
|
|
753
|
+
name: "conn.open",
|
|
754
|
+
conn: i,
|
|
755
|
+
connectMs: Math.round(performance.now() - o)
|
|
756
|
+
});
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
await new Promise((t, n) => {
|
|
760
|
+
m.once("error", n), m.listen(e.port ?? 1985, i, () => t());
|
|
761
|
+
});
|
|
762
|
+
let h = m.address(), g = h && typeof h == "object" ? h.port : e.port ?? 1985;
|
|
763
|
+
return {
|
|
764
|
+
key: e.key,
|
|
765
|
+
clientKey: r.z32,
|
|
766
|
+
host: i,
|
|
767
|
+
port: g,
|
|
768
|
+
close: async () => {
|
|
769
|
+
for (let e of l) e.destroy();
|
|
770
|
+
await new Promise((e) => m.close(() => e())), await f.destroy();
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
//#endregion
|
|
775
|
+
//#region src/tunnel/mountPath.ts
|
|
776
|
+
function X(e, t) {
|
|
777
|
+
let n = e || "/", r = n.indexOf("?"), i = r === -1 ? n : n.slice(0, r), a = r === -1 ? "" : n.slice(r), o = (t || "").replace(/\/$/, "");
|
|
778
|
+
if (!o || o === "/") return `${i.startsWith("/") ? i : `/${i}`}${a}` || "/";
|
|
779
|
+
if (i === o || i.startsWith(`${o}/`)) {
|
|
780
|
+
let e = i.slice(o.length);
|
|
781
|
+
return `${e.startsWith("/") ? e : e ? `/${e}` : "/"}${a}`;
|
|
782
|
+
}
|
|
783
|
+
return `${i.startsWith("/") ? i : `/${i}`}${a}`;
|
|
784
|
+
}
|
|
785
|
+
//#endregion
|
|
786
|
+
//#region src/tunnel/forward.ts
|
|
787
|
+
async function ye(e) {
|
|
788
|
+
let t = [];
|
|
789
|
+
for await (let n of e) t.push(Buffer.isBuffer(n) ? n : Buffer.from(n));
|
|
790
|
+
return Buffer.concat(t);
|
|
791
|
+
}
|
|
792
|
+
function Z(e) {
|
|
793
|
+
return e instanceof j ? 504 : 502;
|
|
794
|
+
}
|
|
795
|
+
function Q(e) {
|
|
796
|
+
return `Gateway Hyper tunnel error: ${e instanceof Error ? e.message : String(e)}`;
|
|
797
|
+
}
|
|
798
|
+
async function $(e, t, n, r) {
|
|
799
|
+
let i = new AbortController(), a = () => {
|
|
800
|
+
n.writableEnded || i.abort();
|
|
801
|
+
};
|
|
802
|
+
n.once("close", a);
|
|
803
|
+
try {
|
|
804
|
+
let a = await ye(t), o = X(t.url ?? "/", r), s = {
|
|
805
|
+
method: t.method ?? "GET",
|
|
806
|
+
path: o,
|
|
807
|
+
headers: k(t.headers)
|
|
808
|
+
}, { meta: c, body: l } = await e.forwardHttp(s, a, { signal: i.signal });
|
|
809
|
+
be(n, c, l);
|
|
810
|
+
} catch (e) {
|
|
811
|
+
if (i.signal.aborted) return;
|
|
812
|
+
n.headersSent || n.writeHead(Z(e), { "Content-Type": "text/plain; charset=utf-8" }), n.end(Q(e));
|
|
813
|
+
} finally {
|
|
814
|
+
n.off("close", a);
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function be(e, t, n) {
|
|
818
|
+
e.writeHead(t.status, A(t.headers)), e.end(n);
|
|
819
|
+
}
|
|
820
|
+
async function xe(e, t, n) {
|
|
821
|
+
try {
|
|
822
|
+
let r = new URL(t.url), i = X(`${r.pathname}${r.search}`, n), a = Buffer.from(await t.arrayBuffer()), o = {};
|
|
823
|
+
t.headers.forEach((e, t) => {
|
|
824
|
+
o[t] = e;
|
|
825
|
+
});
|
|
826
|
+
let s = {
|
|
827
|
+
method: t.method || "GET",
|
|
828
|
+
path: i,
|
|
829
|
+
headers: k(o)
|
|
830
|
+
}, { meta: c, body: l } = await e.forwardHttp(s, a, { signal: t.signal });
|
|
831
|
+
return new Response(l, {
|
|
832
|
+
status: c.status,
|
|
833
|
+
headers: A(c.headers)
|
|
834
|
+
});
|
|
835
|
+
} catch (e) {
|
|
836
|
+
return new Response(Q(e), {
|
|
837
|
+
status: Z(e),
|
|
838
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
//#endregion
|
|
843
|
+
//#region src/tunnel/connect.ts
|
|
844
|
+
async function Se(e) {
|
|
845
|
+
let t = e.host ?? "127.0.0.1", n = e.port ?? 1984, r = await Y({
|
|
846
|
+
key: e.key,
|
|
847
|
+
keyFile: e.keyFile,
|
|
848
|
+
keyPair: e.keyPair,
|
|
849
|
+
storePath: e.storePath,
|
|
850
|
+
bootstrap: e.bootstrap,
|
|
851
|
+
requestTimeoutMs: e.requestTimeoutMs,
|
|
852
|
+
connectTimeoutMs: e.connectTimeoutMs,
|
|
853
|
+
maxStreams: e.maxStreams
|
|
854
|
+
}), a = i.createServer((e, t) => {
|
|
855
|
+
$(r, e, t, "");
|
|
856
|
+
});
|
|
857
|
+
await new Promise((e, r) => {
|
|
858
|
+
a.once("error", r), a.listen(n, t, () => e());
|
|
859
|
+
});
|
|
860
|
+
let o = a.address(), s = o && typeof o == "object" ? o.port : n, c = `http://${t}:${s}`;
|
|
861
|
+
return {
|
|
862
|
+
key: e.key,
|
|
863
|
+
clientKey: r.clientKey,
|
|
864
|
+
host: t,
|
|
865
|
+
port: s,
|
|
866
|
+
baseUrl: c,
|
|
867
|
+
close: async () => {
|
|
868
|
+
await new Promise((e) => a.close(() => e())), await r.close();
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
function Ce(e, t = "") {
|
|
873
|
+
let n = e.replace(/\/$/, "");
|
|
874
|
+
return t ? t.startsWith("/") ? `${n}${t}` : `${n}/${t}` : n;
|
|
875
|
+
}
|
|
876
|
+
//#endregion
|
|
877
|
+
//#region src/tunnel/proxyApp.ts
|
|
878
|
+
function we(e) {
|
|
879
|
+
let t = e.key?.trim() || (typeof process < "u" ? process.env.SEED_GATEWAY_HYPER_KEY?.trim() : "") || "";
|
|
880
|
+
if (!t) throw Error("createGatewayProxy requires options.key or SEED_GATEWAY_HYPER_KEY environment variable");
|
|
881
|
+
return t;
|
|
882
|
+
}
|
|
883
|
+
function Te(e = {}) {
|
|
884
|
+
let t = e.mountPath?.trim() || "/api/seed-gateway", n = we(e), r = null, i = null, a = async () => {
|
|
885
|
+
if (r) return r;
|
|
886
|
+
if (i) return i;
|
|
887
|
+
i = Y({
|
|
888
|
+
key: n,
|
|
889
|
+
keyFile: e.keyFile,
|
|
890
|
+
keyPair: e.keyPair,
|
|
891
|
+
storePath: e.storePath,
|
|
892
|
+
bootstrap: e.bootstrap,
|
|
893
|
+
requestTimeoutMs: e.requestTimeoutMs,
|
|
894
|
+
connectTimeoutMs: e.connectTimeoutMs,
|
|
895
|
+
maxStreams: e.maxStreams
|
|
896
|
+
}).then((e) => (r = e, i = null, e));
|
|
897
|
+
try {
|
|
898
|
+
return await i;
|
|
899
|
+
} catch (e) {
|
|
900
|
+
throw i = null, e;
|
|
901
|
+
}
|
|
902
|
+
}, o = {
|
|
903
|
+
mountPath: t,
|
|
904
|
+
start: async () => {
|
|
905
|
+
await a();
|
|
906
|
+
},
|
|
907
|
+
close: async () => {
|
|
908
|
+
if (i) try {
|
|
909
|
+
await i;
|
|
910
|
+
} catch {}
|
|
911
|
+
r &&= (await r.close(), null);
|
|
912
|
+
},
|
|
913
|
+
handleNode: (e, n) => {
|
|
914
|
+
(async () => {
|
|
915
|
+
try {
|
|
916
|
+
await $(await a(), e, n, t);
|
|
917
|
+
} catch (e) {
|
|
918
|
+
let t = e instanceof Error ? e.message : String(e);
|
|
919
|
+
n.headersSent || n.writeHead(502, { "Content-Type": "text/plain; charset=utf-8" }), n.end(`Gateway Hyper tunnel error: ${t}`);
|
|
920
|
+
}
|
|
921
|
+
})();
|
|
922
|
+
},
|
|
923
|
+
handleFetch: async (e) => {
|
|
924
|
+
try {
|
|
925
|
+
return await xe(await a(), e, t);
|
|
926
|
+
} catch (e) {
|
|
927
|
+
let t = e instanceof Error ? e.message : String(e);
|
|
928
|
+
return new Response(`Gateway Hyper tunnel error: ${t}`, {
|
|
929
|
+
status: 502,
|
|
930
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
};
|
|
935
|
+
return e.eager && o.start().catch(() => {}), o;
|
|
936
|
+
}
|
|
937
|
+
//#endregion
|
|
938
|
+
//#region src/constants.ts
|
|
939
|
+
var Ee = "";
|
|
940
|
+
//#endregion
|
|
941
|
+
export { T as A, h as C, E as D, w as E, u as M, l as N, O, d as P, N as S, k as T, R as _, X as a, M as b, ve as c, K as d, Y as f, se as g, ce as h, Ce as i, D as j, C as k, de as l, G as m, Te as n, _e as o, le as p, Se as r, ge as s, Ee as t, fe as u, ee as v, A as w, j as x, P as y };
|
|
942
|
+
|
|
943
|
+
//# sourceMappingURL=src-DHEkxp5F.js.map
|