revert: restore proxy routing for Anthropic

This commit is contained in:
aedes
2026-04-23 06:31:54 +03:00
parent d471f53b53
commit 18c758e52d

View File

@@ -2,15 +2,13 @@ const express = require('express');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const os = require('os'); const os = require('os');
const { Agent, setGlobalDispatcher } = require('undici'); const { ProxyAgent, Agent, setGlobalDispatcher } = require('undici');
const dns = require('dns');
// Force IPv4 for all outbound connections — container DNS may resolve to IPv6 const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
const ipv4Lookup = (host, opts, cb) => dns.lookup(host, { family: 4, ...opts }, cb); const directAgent = new Agent();
const directAgent = new Agent({ connect: { lookup: ipv4Lookup } }); const anthropicAgent = proxyUrl ? new ProxyAgent(proxyUrl) : directAgent;
const anthropicAgent = directAgent;
setGlobalDispatcher(directAgent); setGlobalDispatcher(directAgent);
console.log('✓ IPv4-direct mode'); if (proxyUrl) console.log(`✓ Anthropic proxy: ${proxyUrl}`);
const app = express(); const app = express();
const PUBLIC_DIR = path.join(__dirname, 'public'); const PUBLIC_DIR = path.join(__dirname, 'public');
@@ -329,14 +327,17 @@ const TG_CHAT_ID = '833411630';
const TG_MESSAGE = 'начинай решать через 5 мин'; const TG_MESSAGE = 'начинай решать через 5 мин';
async function tgSend() { async function tgSend() {
// Fresh ProxyAgent per call to avoid stale connections
const url = `https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`; const url = `https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage`;
const init = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }),
};
try { try {
const r = await fetch(url, { const freshProxy = proxyUrl ? new ProxyAgent(proxyUrl) : undefined;
method: 'POST', const r = await fetch(url, { ...init, dispatcher: freshProxy });
headers: { 'Content-Type': 'application/json' }, return { ok: r.ok, status: r.status, via: 'proxy-fresh' };
body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }),
});
return { ok: r.ok, status: r.status };
} catch (e) { } catch (e) {
const details = e.cause ? `${e.message} | cause: ${e.cause.message || e.cause.code}` : e.message; const details = e.cause ? `${e.message} | cause: ${e.cause.message || e.cause.code}` : e.message;
return { ok: false, error: details }; return { ok: false, error: details };