Spaces:
Runtime error
Runtime error
Luis Milke commited on
Commit ·
5b3924a
1
Parent(s): db1cb29
Fix: use base_url config + manual HTTPS proxy for Telegram (bypass HF firewall)
Browse files- data/config.json +1 -0
- server.js +75 -14
data/config.json
CHANGED
|
@@ -19,6 +19,7 @@
|
|
| 19 |
"channels": {
|
| 20 |
"telegram": {
|
| 21 |
"enabled": true,
|
|
|
|
| 22 |
"token": "8146959380:AAElnoCHNhfFuQGvUz_kA3By59LRh4phfCo",
|
| 23 |
"allow_from": [
|
| 24 |
"7261600381"
|
|
|
|
| 19 |
"channels": {
|
| 20 |
"telegram": {
|
| 21 |
"enabled": true,
|
| 22 |
+
"base_url": "http://127.0.0.1:7860/tg",
|
| 23 |
"token": "8146959380:AAElnoCHNhfFuQGvUz_kA3By59LRh4phfCo",
|
| 24 |
"allow_from": [
|
| 25 |
"7261600381"
|
server.js
CHANGED
|
@@ -232,23 +232,84 @@ app.post('/api/chat', async (req, res) => {
|
|
| 232 |
});
|
| 233 |
|
| 234 |
// ==========================================
|
| 235 |
-
// TELEGRAM API PROXY
|
| 236 |
// ==========================================
|
| 237 |
-
app.use('/tg',
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
}
|
| 251 |
-
})
|
| 252 |
|
| 253 |
// ==========================================
|
| 254 |
// WEBHOOK PROXY TO PICOCLAW
|
|
|
|
| 232 |
});
|
| 233 |
|
| 234 |
// ==========================================
|
| 235 |
+
// TELEGRAM API PROXY (manual fetch-based, bypasses HF's HTTPS block)
|
| 236 |
// ==========================================
|
| 237 |
+
app.use('/tg', async (req, res) => {
|
| 238 |
+
// Strip /tg prefix → forward to api.telegram.org
|
| 239 |
+
const targetPath = req.originalUrl.replace(/^\/tg/, '');
|
| 240 |
+
const targetUrl = `https://api.telegram.org${targetPath}`;
|
| 241 |
+
|
| 242 |
+
try {
|
| 243 |
+
// Collect request body — Express may have already parsed it
|
| 244 |
+
let body = undefined;
|
| 245 |
+
if (req.method === 'POST') {
|
| 246 |
+
if (req.body !== undefined && req.body !== null) {
|
| 247 |
+
// Express already parsed the JSON body
|
| 248 |
+
body = Buffer.from(JSON.stringify(req.body));
|
| 249 |
+
} else {
|
| 250 |
+
// Fallback: read raw stream
|
| 251 |
+
body = await new Promise((resolve, reject) => {
|
| 252 |
+
const chunks = [];
|
| 253 |
+
req.on('data', c => chunks.push(c));
|
| 254 |
+
req.on('end', () => resolve(Buffer.concat(chunks)));
|
| 255 |
+
req.on('error', reject);
|
| 256 |
+
});
|
| 257 |
+
}
|
| 258 |
}
|
| 259 |
+
|
| 260 |
+
// Use native https module with our custom DNS-resolving agent
|
| 261 |
+
const result = await new Promise((resolve, reject) => {
|
| 262 |
+
const urlObj = new URL(targetUrl);
|
| 263 |
+
const options = {
|
| 264 |
+
hostname: urlObj.hostname,
|
| 265 |
+
port: 443,
|
| 266 |
+
path: urlObj.pathname + urlObj.search,
|
| 267 |
+
method: req.method,
|
| 268 |
+
agent: proxyAgent,
|
| 269 |
+
headers: {
|
| 270 |
+
'Content-Type': req.headers['content-type'] || 'application/json',
|
| 271 |
+
'Accept': 'application/json',
|
| 272 |
+
...(body ? { 'Content-Length': body.length } : {})
|
| 273 |
+
},
|
| 274 |
+
timeout: 120000 // 2 min for long polling
|
| 275 |
+
};
|
| 276 |
+
|
| 277 |
+
const proxyReq = https.request(options, (proxyRes) => {
|
| 278 |
+
const chunks = [];
|
| 279 |
+
proxyRes.on('data', c => chunks.push(c));
|
| 280 |
+
proxyRes.on('end', () => {
|
| 281 |
+
resolve({
|
| 282 |
+
status: proxyRes.statusCode,
|
| 283 |
+
headers: proxyRes.headers,
|
| 284 |
+
body: Buffer.concat(chunks)
|
| 285 |
+
});
|
| 286 |
+
});
|
| 287 |
+
});
|
| 288 |
+
|
| 289 |
+
proxyReq.on('error', reject);
|
| 290 |
+
proxyReq.on('timeout', () => {
|
| 291 |
+
proxyReq.destroy();
|
| 292 |
+
reject(new Error('Telegram API request timed out'));
|
| 293 |
+
});
|
| 294 |
+
|
| 295 |
+
if (body) proxyReq.write(body);
|
| 296 |
+
proxyReq.end();
|
| 297 |
+
});
|
| 298 |
+
|
| 299 |
+
// Forward response back to Go client
|
| 300 |
+
res.writeHead(result.status, {
|
| 301 |
+
'Content-Type': result.headers['content-type'] || 'application/json',
|
| 302 |
+
'Connection': 'close' // Critical for fasthttp compatibility
|
| 303 |
+
});
|
| 304 |
+
res.end(result.body);
|
| 305 |
+
|
| 306 |
+
} catch (err) {
|
| 307 |
+
console.error('[TG Proxy Error]', err.message);
|
| 308 |
+
addLog('TG-Proxy', `ERROR: ${err.message}`);
|
| 309 |
+
res.writeHead(502, { 'Content-Type': 'application/json', 'Connection': 'close' });
|
| 310 |
+
res.end(JSON.stringify({ ok: false, error_code: 502, description: `Proxy error: ${err.message}` }));
|
| 311 |
}
|
| 312 |
+
});
|
| 313 |
|
| 314 |
// ==========================================
|
| 315 |
// WEBHOOK PROXY TO PICOCLAW
|