omp-opencode-bridge 0.0.0-stage → 2.6.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/AGENTS.md +253 -0
- package/LICENSE +21 -0
- package/README.md +892 -3
- package/THIRD_PARTY_NOTICES.md +33 -0
- package/anthropic.ts +698 -0
- package/egress-proxy.ts +245 -0
- package/gate-drill.ts +1096 -0
- package/identity.ts +2841 -0
- package/install.sh +708 -0
- package/package.json +79 -4
- package/panel.ts +537 -0
- package/proxy.ts +1508 -0
- package/server.ts +3766 -0
- package/src/index.ts +364 -0
- package/token-budget.ts +116 -0
- package/transport-baseline.json +39 -0
- package/transport-fingerprint.ts +182 -0
- package/tunnel-doctor.sh +171 -0
- package/verify.ts +374 -0
package/egress-proxy.ts
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
/**
|
|
3
|
+
* egress-proxy.ts: a minimal HTTP proxy, for turning a host into one pool entry.
|
|
4
|
+
*
|
|
5
|
+
* The bridge's pool speaks HTTP CONNECT (see proxy.ts), so each pool member
|
|
6
|
+
* needs something that answers CONNECT and forwards bytes — nothing more. This
|
|
7
|
+
* is that: one process, no dependencies, no caching, no inspection. It exists
|
|
8
|
+
* because the alternative is a full squid/nginx install on hosts where the
|
|
9
|
+
* operator has no root, and because a pool entry only has to carry one
|
|
10
|
+
* machine's egress.
|
|
11
|
+
*
|
|
12
|
+
* Deliberately NOT done here: TLS termination, header rewriting, logging of
|
|
13
|
+
* request bodies, and any authentication beyond a shared token. The bridge
|
|
14
|
+
* already tunnels TLS end to end, so this stays a byte pump and cannot see
|
|
15
|
+
* plaintext.
|
|
16
|
+
*
|
|
17
|
+
* Reachability: the proxy binds loopback by default. Reaching it from another
|
|
18
|
+
* host is the tunnel's job (an SSH `-L`), which also carries the authentication,
|
|
19
|
+
* so a token is optional and only meaningful when the listener is exposed.
|
|
20
|
+
*
|
|
21
|
+
* PORT=8888 EGRESS_PROXY_TOKEN=secret bun egress-proxy.ts
|
|
22
|
+
* curl -x http://secret@node:8888 https://api.ipify.org
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const net = require("net");
|
|
26
|
+
const http = require("http");
|
|
27
|
+
const dns = require("dns");
|
|
28
|
+
|
|
29
|
+
// The free-tier gateway refuses IPv6 egress and accepts IPv4 (measured: the
|
|
30
|
+
// same request from the same host answers 200 over IPv4 and
|
|
31
|
+
// `429 FreeUsageLimitError` over IPv6). Bun's default order is `verbatim`,
|
|
32
|
+
// which on a dual-stack host resolves `opencode.ai` to AAAA first, so a pool
|
|
33
|
+
// member would tunnel every request out over the family the gateway refuses.
|
|
34
|
+
// `server.ts` sets the same order for its own fetches; this process dials the
|
|
35
|
+
// upstream itself for every CONNECT, so it needs it too.
|
|
36
|
+
//
|
|
37
|
+
// `ipv6first` only exists from Node 22. Calling setDefaultResultOrder with an
|
|
38
|
+
// order the runtime does not know throws ERR_INVALID_ARG_VALUE, and because
|
|
39
|
+
// this runs at module load the proxy dies before it ever listens — an operator
|
|
40
|
+
// on Node 18 who set DNS_RESULT_ORDER=ipv6first lost the proxy entirely. The
|
|
41
|
+
// preference is honoured where it is understood and announced where it is not.
|
|
42
|
+
const dnsOrder: string | null = (() => {
|
|
43
|
+
const wanted = String(process.env.DNS_RESULT_ORDER || "ipv4first").trim().toLowerCase();
|
|
44
|
+
for (const order of [wanted, "ipv4first", "verbatim"]) {
|
|
45
|
+
try {
|
|
46
|
+
dns.setDefaultResultOrder(order);
|
|
47
|
+
if (order !== wanted) {
|
|
48
|
+
console.warn(
|
|
49
|
+
`[egress-proxy] DNS_RESULT_ORDER=${wanted} is not supported by this Node (${process.versions.node}); using ${order}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
return order;
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
})();
|
|
57
|
+
|
|
58
|
+
const PORT = parseInt(process.env.PORT || "8888", 10);
|
|
59
|
+
const HOST = process.env.HOST || "127.0.0.1";
|
|
60
|
+
const TOKEN = (process.env.EGRESS_PROXY_TOKEN || "").trim();
|
|
61
|
+
const CONNECT_TIMEOUT_MS = parseInt(process.env.CONNECT_TIMEOUT_MS || "15000", 10);
|
|
62
|
+
const IDLE_TIMEOUT_MS = parseInt(process.env.IDLE_TIMEOUT_MS || "120000", 10);
|
|
63
|
+
const MAX_CONNECTIONS = parseInt(process.env.MAX_CONNECTIONS || "512", 10);
|
|
64
|
+
|
|
65
|
+
let active = 0;
|
|
66
|
+
const startedAt = Date.now();
|
|
67
|
+
|
|
68
|
+
function authorized(req: http.IncomingMessage): boolean {
|
|
69
|
+
if (!TOKEN) return true;
|
|
70
|
+
// Proxy-Authorization: Basic base64(user:token). Only the password is
|
|
71
|
+
// checked, because a proxy URL's user part is conventionally free-form.
|
|
72
|
+
const header = req.headers["proxy-authorization"] || "";
|
|
73
|
+
const match = /^Basic\s+(.+)$/i.exec(header);
|
|
74
|
+
if (!match) return false;
|
|
75
|
+
let decoded = "";
|
|
76
|
+
try {
|
|
77
|
+
decoded = Buffer.from(match[1], "base64").toString("utf8");
|
|
78
|
+
} catch {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const password = decoded.slice(decoded.indexOf(":") + 1);
|
|
82
|
+
return password === TOKEN;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function requireAuth(res: http.ServerResponse): void {
|
|
86
|
+
res.writeHead(407, {
|
|
87
|
+
"Proxy-Authenticate": 'Basic realm="egress"',
|
|
88
|
+
"Content-Type": "text/plain",
|
|
89
|
+
});
|
|
90
|
+
res.end("proxy authentication required\n");
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Refuse anything outside the two shapes a pool entry has to carry: CONNECT for
|
|
95
|
+
* TLS, and absolute-URI HTTP for an occasional plaintext probe. A forward proxy
|
|
96
|
+
* that accepts arbitrary methods is a bigger surface than this needs.
|
|
97
|
+
*/
|
|
98
|
+
const server = http.createServer((req, res) => {
|
|
99
|
+
// Health is unauthenticated on purpose: it carries a connection count and an
|
|
100
|
+
// uptime, and a monitor should not need the proxy credential to ask whether
|
|
101
|
+
// the proxy is up. It never reports the token or a target.
|
|
102
|
+
if (req.method === "GET" && (req.url === "/healthz" || req.url === "/")) {
|
|
103
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
104
|
+
res.end(
|
|
105
|
+
JSON.stringify({
|
|
106
|
+
status: "ok",
|
|
107
|
+
role: "egress-proxy",
|
|
108
|
+
activeConnections: active,
|
|
109
|
+
uptimeMs: Date.now() - startedAt,
|
|
110
|
+
})
|
|
111
|
+
);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (!authorized(req)) return requireAuth(res);
|
|
116
|
+
|
|
117
|
+
const target = req.url || "";
|
|
118
|
+
if (!/^http:\/\//i.test(target)) {
|
|
119
|
+
res.writeHead(400, { "Content-Type": "application/json" });
|
|
120
|
+
res.end(JSON.stringify({ error: "Only CONNECT and absolute http:// URLs are proxied." }));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const parsed = new URL(target);
|
|
125
|
+
forwardHttp(req, res, parsed);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/** Plaintext HTTP forwarding: replay the request with origin-form path. */
|
|
129
|
+
function forwardHttp(
|
|
130
|
+
clientReq: http.IncomingMessage,
|
|
131
|
+
clientRes: http.ServerResponse,
|
|
132
|
+
target: URL
|
|
133
|
+
): void {
|
|
134
|
+
if (active >= MAX_CONNECTIONS) {
|
|
135
|
+
clientRes.writeHead(503, { "Content-Type": "application/json" });
|
|
136
|
+
clientRes.end(JSON.stringify({ error: "proxy connection limit reached" }));
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
active += 1;
|
|
140
|
+
|
|
141
|
+
const headers: http.OutgoingHttpHeaders = { ...clientReq.headers };
|
|
142
|
+
delete headers["proxy-authorization"];
|
|
143
|
+
delete headers["proxy-connection"];
|
|
144
|
+
headers.host = target.host;
|
|
145
|
+
|
|
146
|
+
const upstream = http.request(
|
|
147
|
+
{
|
|
148
|
+
host: target.hostname,
|
|
149
|
+
port: target.port || 80,
|
|
150
|
+
method: clientReq.method,
|
|
151
|
+
path: target.pathname + target.search,
|
|
152
|
+
headers,
|
|
153
|
+
},
|
|
154
|
+
(upstreamRes) => {
|
|
155
|
+
clientRes.writeHead(upstreamRes.statusCode || 502, upstreamRes.headers);
|
|
156
|
+
upstreamRes.pipe(clientRes);
|
|
157
|
+
upstreamRes.on("end", () => {
|
|
158
|
+
active -= 1;
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
upstream.setTimeout(CONNECT_TIMEOUT_MS, () => upstream.destroy(new Error("upstream timeout")));
|
|
164
|
+
upstream.on("error", (err) => {
|
|
165
|
+
active -= 1;
|
|
166
|
+
if (!clientRes.headersSent) {
|
|
167
|
+
clientRes.writeHead(502, { "Content-Type": "application/json" });
|
|
168
|
+
clientRes.end(JSON.stringify({ error: `upstream: ${err.message}` }));
|
|
169
|
+
} else {
|
|
170
|
+
clientRes.destroy();
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
clientReq.pipe(upstream);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** CONNECT: open a raw TCP tunnel and join both directions. */
|
|
177
|
+
server.on("connect", (req: http.IncomingMessage, clientSocket: net.Socket, head: Buffer) => {
|
|
178
|
+
if (!authorized(req)) {
|
|
179
|
+
clientSocket.end("HTTP/1.1 407 Proxy Authentication Required\r\nProxy-Authenticate: Basic realm=\"egress\"\r\n\r\n");
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
if (active >= MAX_CONNECTIONS) {
|
|
183
|
+
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\n\r\n");
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const [host, portRaw] = splitHostPort(req.url || "");
|
|
188
|
+
const port = parseInt(portRaw || "443", 10);
|
|
189
|
+
if (!host || !Number.isFinite(port)) {
|
|
190
|
+
clientSocket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
active += 1;
|
|
195
|
+
const upstream = net.connect({ host, port });
|
|
196
|
+
|
|
197
|
+
upstream.setTimeout(IDLE_TIMEOUT_MS, () => upstream.destroy(new Error("idle timeout")));
|
|
198
|
+
|
|
199
|
+
upstream.on("connect", () => {
|
|
200
|
+
clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n");
|
|
201
|
+
if (head && head.length) upstream.write(head);
|
|
202
|
+
upstream.pipe(clientSocket);
|
|
203
|
+
clientSocket.pipe(upstream);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const close = () => {
|
|
207
|
+
active = Math.max(0, active - 1);
|
|
208
|
+
upstream.destroy();
|
|
209
|
+
clientSocket.destroy();
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
upstream.on("error", () => {
|
|
213
|
+
if (!clientSocket.destroyed) clientSocket.end("HTTP/1.1 502 Bad Gateway\r\n\r\n");
|
|
214
|
+
close();
|
|
215
|
+
});
|
|
216
|
+
upstream.on("close", close);
|
|
217
|
+
clientSocket.on("error", close);
|
|
218
|
+
clientSocket.on("close", close);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
function splitHostPort(value: string): [string | null, string | null] {
|
|
222
|
+
if (value.startsWith("[")) {
|
|
223
|
+
const end = value.indexOf("]");
|
|
224
|
+
if (end === -1) return [null, null];
|
|
225
|
+
return [value.slice(1, end), value.slice(end + 1).replace(/^:/, "")];
|
|
226
|
+
}
|
|
227
|
+
const index = value.lastIndexOf(":");
|
|
228
|
+
if (index === -1) return [value, null];
|
|
229
|
+
return [value.slice(0, index), value.slice(index + 1)];
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
server.listen(PORT, HOST, () => {
|
|
233
|
+
console.log(
|
|
234
|
+
`[egress-proxy] listening on ${HOST}:${PORT}` +
|
|
235
|
+
(TOKEN ? " (token required)" : " (no token)")
|
|
236
|
+
);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
for (const signal of ["SIGINT", "SIGTERM"]) {
|
|
240
|
+
process.on(signal, () => {
|
|
241
|
+
console.log(`[egress-proxy] ${signal}: shutting down`);
|
|
242
|
+
server.close(() => process.exit(0));
|
|
243
|
+
setTimeout(() => process.exit(0), 3000).unref();
|
|
244
|
+
});
|
|
245
|
+
}
|