From d471f53b5317fc425a5bdc36073a7ec75273d406 Mon Sep 17 00:00:00 2001 From: aedes Date: Thu, 23 Apr 2026 06:30:08 +0300 Subject: [PATCH] fix: IPv4-direct undici agent, remove VPS proxy dependency --- server.js | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index 31673b0..e45753c 100644 --- a/server.js +++ b/server.js @@ -2,13 +2,15 @@ const express = require('express'); const path = require('path'); const fs = require('fs'); const os = require('os'); -const { ProxyAgent, Agent, setGlobalDispatcher } = require('undici'); +const { Agent, setGlobalDispatcher } = require('undici'); +const dns = require('dns'); -const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY; -const directAgent = new Agent(); -const anthropicAgent = proxyUrl ? new ProxyAgent(proxyUrl) : directAgent; +// 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; setGlobalDispatcher(directAgent); -if (proxyUrl) console.log(`✓ Anthropic proxy: ${proxyUrl}`); +console.log('✓ IPv4-direct mode'); const app = express(); const PUBLIC_DIR = path.join(__dirname, 'public'); @@ -327,17 +329,14 @@ 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 freshProxy = proxyUrl ? new ProxyAgent(proxyUrl) : undefined; - const r = await fetch(url, { ...init, dispatcher: freshProxy }); - return { ok: r.ok, status: r.status, via: 'proxy-fresh' }; + 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 }; } catch (e) { const details = e.cause ? `${e.message} | cause: ${e.cause.message || e.cause.code}` : e.message; return { ok: false, error: details };