fix: IPv4-direct undici agent, remove VPS proxy dependency

This commit is contained in:
aedes
2026-04-23 06:30:08 +03:00
parent d7f8e3b0d9
commit d471f53b53

View File

@@ -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 };