revert: restore proxy routing for Anthropic
This commit is contained in:
23
server.js
23
server.js
@@ -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`;
|
||||||
try {
|
const init = {
|
||||||
const r = await fetch(url, {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }),
|
body: JSON.stringify({ chat_id: TG_CHAT_ID, text: TG_MESSAGE }),
|
||||||
});
|
};
|
||||||
return { ok: r.ok, status: r.status };
|
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' };
|
||||||
} 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 };
|
||||||
|
|||||||
Reference in New Issue
Block a user