Fix bug Tunnel

This commit is contained in:
decolua
2026-02-22 21:44:11 +07:00
parent a5eb5a864e
commit d21f7aaadc
9 changed files with 734 additions and 237 deletions
+14 -10
View File
@@ -23,10 +23,16 @@ if (!API_KEY) {
// Load SSL certificates
const certDir = path.join(os.homedir(), ".9router", "mitm");
const sslOptions = {
key: fs.readFileSync(path.join(certDir, "server.key")),
cert: fs.readFileSync(path.join(certDir, "server.crt"))
};
let sslOptions;
try {
sslOptions = {
key: fs.readFileSync(path.join(certDir, "server.key")),
cert: fs.readFileSync(path.join(certDir, "server.crt"))
};
} catch (e) {
console.error(`❌ SSL cert not found in ${certDir}: ${e.message}`);
process.exit(1);
}
// Chat endpoints that should be intercepted
const CHAT_URL_PATTERNS = [":generateContent", ":streamGenerateContent"];
@@ -146,12 +152,10 @@ async function intercept(req, res, bodyBuffer, mappedModel) {
throw new Error(`9Router ${response.status}: ${errText}`);
}
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no"
});
const ct = response.headers.get("content-type") || "application/json";
const resHeaders = { "Content-Type": ct, "Cache-Control": "no-cache", "Connection": "keep-alive" };
if (ct.includes("text/event-stream")) resHeaders["X-Accel-Buffering"] = "no";
res.writeHead(200, resHeaders);
const reader = response.body.getReader();
const decoder = new TextDecoder();