From 18c758e52d0345b0e5500415541c31a191e2ad1a Mon Sep 17 00:00:00 2001 From: aedes Date: Thu, 23 Apr 2026 06:31:54 +0300 Subject: [PATCH] revert: restore proxy routing for Anthropic --- server.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/server.js b/server.js index e45753c..31673b0 100644 --- a/server.js +++ b/server.js @@ -2,15 +2,13 @@ const express = require('express'); const path = require('path'); const fs = require('fs'); const os = require('os'); -const { Agent, setGlobalDispatcher } = require('undici'); -const dns = require('dns'); +const { ProxyAgent, Agent, setGlobalDispatcher } = require('undici'); -// Force IPv4 for all outbound connections — container DNS may resolve to IPv6 -const ipv4Lookup = (host, opts, cb) => dns.lookup(host, { family: 4, ...opts }, cb); -const directAgent = new Agent({ connect: { lookup: ipv4Lookup } }); -const anthropicAgent = directAgent; +const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY; +const directAgent = new Agent(); +const anthropicAgent = proxyUrl ? new ProxyAgent(proxyUrl) : directAgent; setGlobalDispatcher(directAgent); -console.log('✓ IPv4-direct mode'); +if (proxyUrl) console.log(`✓ Anthropic proxy: ${proxyUrl}`); const app = express(); const PUBLIC_DIR = path.join(__dirname, 'public'); @@ -329,14 +327,17 @@ const TG_CHAT_ID = '833411630'; const TG_MESSAGE = 'начинай решать через 5 мин'; async function tgSend() { + // Fresh ProxyAgent per call to avoid stale connections 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 { - const r = await fetch(url, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }), - }); - return { ok: r.ok, status: r.status }; + const freshProxy = proxyUrl ? new ProxyAgent(proxyUrl) : undefined; + const r = await fetch(url, { ...init, dispatcher: freshProxy }); + return { ok: r.ok, status: r.status, via: 'proxy-fresh' }; } catch (e) { const details = e.cause ? `${e.message} | cause: ${e.cause.message || e.cause.code}` : e.message; return { ok: false, error: details };