@byoky/bridge 0.4.0 → 0.4.1
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/dist/host.js +31 -4
- package/dist/installer.js +11 -8
- package/package.json +1 -1
package/dist/host.js
CHANGED
|
@@ -10,6 +10,8 @@ function handleProxyResponse(msg) {
|
|
|
10
10
|
delete headers["transfer-encoding"];
|
|
11
11
|
delete headers["content-encoding"];
|
|
12
12
|
delete headers["content-length"];
|
|
13
|
+
delete headers["set-cookie"];
|
|
14
|
+
delete headers["set-cookie2"];
|
|
13
15
|
pending.res.writeHead(msg.status, headers);
|
|
14
16
|
} else if (msg.type === "proxy_http_response_chunk") {
|
|
15
17
|
pending.res.write(msg.chunk);
|
|
@@ -46,6 +48,11 @@ function startProxyServer(config) {
|
|
|
46
48
|
res.end(JSON.stringify({ status: "ok", providers }));
|
|
47
49
|
return;
|
|
48
50
|
}
|
|
51
|
+
if ((req.url?.length ?? 0) > MAX_URI_LENGTH) {
|
|
52
|
+
res.writeHead(414, { "Content-Type": "application/json" });
|
|
53
|
+
res.end(JSON.stringify({ error: "URI too long" }));
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
49
56
|
const match = req.url?.match(/^\/([^/]+)(\/.*)?$/);
|
|
50
57
|
if (!match) {
|
|
51
58
|
res.writeHead(404, { "Content-Type": "application/json" });
|
|
@@ -59,6 +66,12 @@ function startProxyServer(config) {
|
|
|
59
66
|
res.end(JSON.stringify({ error: `Provider "${providerId}" not available in this session` }));
|
|
60
67
|
return;
|
|
61
68
|
}
|
|
69
|
+
const declaredLength = parseInt(req.headers["content-length"] || "0", 10);
|
|
70
|
+
if (declaredLength > MAX_BODY_SIZE) {
|
|
71
|
+
res.writeHead(413, { "Content-Type": "application/json" });
|
|
72
|
+
res.end(JSON.stringify({ error: "Payload too large" }));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
62
75
|
const body = await readBody(req);
|
|
63
76
|
const providerUrls = {
|
|
64
77
|
anthropic: "https://api.anthropic.com",
|
|
@@ -85,9 +98,17 @@ function startProxyServer(config) {
|
|
|
85
98
|
}
|
|
86
99
|
const realUrl = `${baseUrl}${path}`;
|
|
87
100
|
const requestId = `proxy-${crypto.randomUUID()}`;
|
|
101
|
+
const STRIP_HEADERS = /* @__PURE__ */ new Set([
|
|
102
|
+
"host",
|
|
103
|
+
"connection",
|
|
104
|
+
"cookie",
|
|
105
|
+
"authorization",
|
|
106
|
+
"proxy-authorization",
|
|
107
|
+
"proxy-connection"
|
|
108
|
+
]);
|
|
88
109
|
const headers = {};
|
|
89
110
|
for (const [key, value] of Object.entries(req.headers)) {
|
|
90
|
-
if (key
|
|
111
|
+
if (STRIP_HEADERS.has(key)) continue;
|
|
91
112
|
if (typeof value === "string") headers[key] = value;
|
|
92
113
|
}
|
|
93
114
|
const proxyMsg = {
|
|
@@ -124,6 +145,7 @@ function startProxyServer(config) {
|
|
|
124
145
|
return server;
|
|
125
146
|
}
|
|
126
147
|
var MAX_BODY_SIZE = 10 * 1024 * 1024;
|
|
148
|
+
var MAX_URI_LENGTH = 8192;
|
|
127
149
|
function readBody(req) {
|
|
128
150
|
return new Promise((resolve, reject) => {
|
|
129
151
|
let body = "";
|
|
@@ -240,11 +262,14 @@ async function handleStreamingFetch(requestId, url, method, headers, body, mode)
|
|
|
240
262
|
};
|
|
241
263
|
const prefix = mode === "bridge" ? "proxy_response" : "proxy_http_response";
|
|
242
264
|
const errorType = mode === "bridge" ? "proxy_error" : "proxy_http_error";
|
|
265
|
+
const controller = new AbortController();
|
|
266
|
+
const fetchTimeout = setTimeout(() => controller.abort(), 12e4);
|
|
243
267
|
try {
|
|
244
268
|
const res = await fetch(url, {
|
|
245
269
|
method,
|
|
246
270
|
headers,
|
|
247
|
-
body: body || void 0
|
|
271
|
+
body: body || void 0,
|
|
272
|
+
signal: controller.signal
|
|
248
273
|
});
|
|
249
274
|
const resHeaders = {};
|
|
250
275
|
res.headers.forEach((v, k) => {
|
|
@@ -262,7 +287,9 @@ async function handleStreamingFetch(requestId, url, method, headers, body, mode)
|
|
|
262
287
|
}
|
|
263
288
|
send({ type: `${prefix}_done`, requestId });
|
|
264
289
|
} catch (e) {
|
|
265
|
-
send({ type: errorType, requestId, error:
|
|
290
|
+
send({ type: errorType, requestId, error: "Fetch request failed" });
|
|
291
|
+
} finally {
|
|
292
|
+
clearTimeout(fetchTimeout);
|
|
266
293
|
}
|
|
267
294
|
}
|
|
268
295
|
function handleStartProxy(req) {
|
|
@@ -281,7 +308,7 @@ function handleStartProxy(req) {
|
|
|
281
308
|
writeMessage({
|
|
282
309
|
type: "proxy_error",
|
|
283
310
|
requestId: "start-proxy",
|
|
284
|
-
error:
|
|
311
|
+
error: "Failed to start proxy server"
|
|
285
312
|
});
|
|
286
313
|
}
|
|
287
314
|
}
|
package/dist/installer.js
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
import { writeFileSync, mkdirSync, unlinkSync, existsSync, chmodSync } from "fs";
|
|
3
3
|
import { dirname, resolve } from "path";
|
|
4
4
|
import { homedir, platform } from "os";
|
|
5
|
-
import {
|
|
5
|
+
import { execFileSync } from "child_process";
|
|
6
6
|
var HOST_NAME = "com.byoky.bridge";
|
|
7
7
|
function getHostPath() {
|
|
8
8
|
try {
|
|
9
|
-
return
|
|
9
|
+
return execFileSync("/usr/bin/which", ["byoky-bridge"], { encoding: "utf-8", timeout: 5e3 }).trim();
|
|
10
10
|
} catch {
|
|
11
11
|
return resolve(dirname(new URL(import.meta.url).pathname), "../bin/byoky-bridge.js");
|
|
12
12
|
}
|
|
@@ -14,16 +14,19 @@ function getHostPath() {
|
|
|
14
14
|
function createNativeWrapper(hostPath, manifestDir) {
|
|
15
15
|
const nodePath = process.execPath;
|
|
16
16
|
const wrapperPath = resolve(manifestDir, "byoky-bridge-host");
|
|
17
|
-
const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const nodeDir = dirname(nodePath);
|
|
18
|
+
const safePath = `${nodeDir}:/usr/local/bin:/usr/bin:/bin`;
|
|
19
|
+
const script = [
|
|
20
|
+
"#!/bin/bash",
|
|
21
|
+
`export PATH='${safePath}'`,
|
|
22
|
+
`exec '${nodePath.replace(/'/g, "'\\''")}' '${hostPath.replace(/'/g, "'\\''")}' host "$@"`,
|
|
23
|
+
""
|
|
24
|
+
].join("\n");
|
|
22
25
|
writeFileSync(wrapperPath, script);
|
|
23
26
|
chmodSync(wrapperPath, 493);
|
|
24
27
|
return wrapperPath;
|
|
25
28
|
}
|
|
26
|
-
var DEFAULT_EXTENSION_ID = "ahhecmfcclkjdgjnmackoacldnmgmipl";
|
|
29
|
+
var DEFAULT_EXTENSION_ID = process.env.BYOKY_EXTENSION_ID || "ahhecmfcclkjdgjnmackoacldnmgmipl";
|
|
27
30
|
function buildManifest(hostPath, browserType, extensionId) {
|
|
28
31
|
const base = {
|
|
29
32
|
name: HOST_NAME,
|