@bman654/clodex 2.11.2 → 2.11.3

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/cli.js CHANGED
@@ -382,7 +382,7 @@ import { join } from "path";
382
382
  // package.json
383
383
  var package_default = {
384
384
  name: "@bman654/clodex",
385
- version: "2.11.2",
385
+ version: "2.11.3",
386
386
  publishConfig: {
387
387
  access: "public"
388
388
  },
@@ -18389,6 +18389,7 @@ import * as p9 from "@clack/prompts";
18389
18389
  import * as http2 from "http";
18390
18390
  import * as https from "https";
18391
18391
  import * as net from "net";
18392
+ import { PassThrough } from "stream";
18392
18393
  import { randomUUID as randomUUID6 } from "crypto";
18393
18394
  import { URL as URL2 } from "url";
18394
18395
  import { createBrotliDecompress, createGunzip, createInflate } from "zlib";
@@ -19076,6 +19077,97 @@ function forwardToAdapter(req, res, rawBody, adapter, adapterRequest = http2.req
19076
19077
  upstream.end(rawBody);
19077
19078
  });
19078
19079
  }
19080
+ function forwardAnthropicUpgrade(req, clientSocket, head, origin, rejectUnauthorized, agent, sockets) {
19081
+ if (clientSocket._httpMessage) {
19082
+ clientSocket.destroy();
19083
+ return;
19084
+ }
19085
+ const clientData = new PassThrough();
19086
+ clientSocket.pipe(clientData);
19087
+ let responseStarted = false;
19088
+ let upgradedSocket;
19089
+ const upstream = https.request({
19090
+ protocol: "https:",
19091
+ hostname: origin.hostname,
19092
+ port: origin.port || 443,
19093
+ method: req.method,
19094
+ path: req.url,
19095
+ headers: requestHeadersWithoutProxyHeaders(req),
19096
+ servername: net.isIP(origin.hostname) ? void 0 : origin.hostname,
19097
+ rejectUnauthorized,
19098
+ agent
19099
+ });
19100
+ const fail = () => {
19101
+ if (clientSocket.destroyed) return;
19102
+ if (responseStarted) {
19103
+ clientSocket.destroy();
19104
+ return;
19105
+ }
19106
+ responseStarted = true;
19107
+ clientSocket.end(
19108
+ "HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\nContent-Length: 0\r\n\r\n",
19109
+ () => clientSocket.destroy()
19110
+ );
19111
+ };
19112
+ clientSocket.once("error", () => clientSocket.destroy());
19113
+ clientSocket.once("end", () => {
19114
+ if (!responseStarted) clientSocket.destroy();
19115
+ });
19116
+ clientSocket.once("close", () => {
19117
+ clientData.destroy();
19118
+ upstream.destroy();
19119
+ upgradedSocket?.destroy();
19120
+ });
19121
+ upstream.once("socket", (socket) => {
19122
+ if (clientSocket.destroyed) socket.destroy();
19123
+ });
19124
+ upstream.once("error", fail);
19125
+ upstream.once("close", () => {
19126
+ if (!responseStarted) fail();
19127
+ });
19128
+ upstream.once("response", (upstreamRes) => {
19129
+ if (clientSocket.destroyed) {
19130
+ upstreamRes.destroy();
19131
+ return;
19132
+ }
19133
+ responseStarted = true;
19134
+ const response = new http2.ServerResponse(req);
19135
+ response.shouldKeepAlive = false;
19136
+ response.assignSocket(clientSocket);
19137
+ clientSocket.on("drain", () => response.emit("drain"));
19138
+ response.once("error", () => clientSocket.destroy());
19139
+ response.once("finish", () => clientSocket.end(() => clientSocket.destroy()));
19140
+ copyResponse(upstreamRes, response);
19141
+ });
19142
+ upstream.once("upgrade", (upstreamRes, socket, upstreamHead) => {
19143
+ if (clientSocket.destroyed) {
19144
+ socket.destroy();
19145
+ return;
19146
+ }
19147
+ responseStarted = true;
19148
+ upgradedSocket = socket;
19149
+ sockets.add(socket);
19150
+ socket.once("error", () => clientSocket.destroy());
19151
+ socket.once("close", () => {
19152
+ sockets.delete(socket);
19153
+ clientSocket.destroy();
19154
+ });
19155
+ const headers = [
19156
+ `HTTP/${upstreamRes.httpVersion} ${upstreamRes.statusCode} ${upstreamRes.statusMessage}`
19157
+ ];
19158
+ for (let i = 0; i < upstreamRes.rawHeaders.length; i += 2) {
19159
+ headers.push(`${upstreamRes.rawHeaders[i]}: ${upstreamRes.rawHeaders[i + 1]}`);
19160
+ }
19161
+ clientSocket.write(Buffer.from(`${headers.join("\r\n")}\r
19162
+ \r
19163
+ `, "latin1"));
19164
+ if (upstreamHead.length > 0) clientSocket.write(upstreamHead);
19165
+ if (head.length > 0) socket.write(head);
19166
+ clientData.pipe(socket);
19167
+ socket.pipe(clientSocket);
19168
+ });
19169
+ upstream.end();
19170
+ }
19079
19171
  function forwardPlainHttp(req, res) {
19080
19172
  let target;
19081
19173
  try {
@@ -19292,6 +19384,17 @@ async function startHttpProxy(options) {
19292
19384
  );
19293
19385
  });
19294
19386
  const sockets = /* @__PURE__ */ new Set();
19387
+ mitmServer.on("upgrade", (req, socket, head) => {
19388
+ forwardAnthropicUpgrade(
19389
+ req,
19390
+ socket,
19391
+ head,
19392
+ anthropicOrigin,
19393
+ options.anthropicRejectUnauthorized ?? true,
19394
+ anthropicAgent,
19395
+ sockets
19396
+ );
19397
+ });
19295
19398
  const proxyServer = http2.createServer(forwardPlainHttp);
19296
19399
  proxyServer.on("connection", (socket) => {
19297
19400
  sockets.add(socket);