| import express from 'express'; |
| import cors from 'cors'; |
| import rateLimit from 'express-rate-limit'; |
| import { Readable } from 'stream'; |
|
|
| const app = express(); |
| const PORT = 7860; |
|
|
| |
| app.set('trust proxy', 1); |
| app.disable('x-powered-by'); |
| app.use(cors()); |
|
|
| app.use((req, res, next) => { |
| res.setHeader('X-Content-Type-Options', 'nosniff'); |
| res.setHeader('X-Frame-Options', 'DENY'); |
| res.setHeader('X-XSS-Protection', '1; mode=block'); |
| res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload'); |
| res.setHeader('Referrer-Policy', 'no-referrer'); |
| next(); |
| }); |
|
|
| app.use(express.json({ limit: '50mb' })); |
| app.use(express.urlencoded({ limit: '50mb', extended: true })); |
|
|
| |
| |
| const limiter = rateLimit({ |
| windowMs: 60 * 1000, |
| max: 500, |
| keyGenerator: (req) => req.ip, |
| message: { error: { message: "Servidor a máxima capacidad. Intenta en unos segundos.", code: 429 } }, |
| standardHeaders: false, |
| legacyHeaders: false, |
| }); |
|
|
| app.get('/health', (req, res) => { |
| res.json({ status: "online", type: "High Concurrency Proxy", max_capacity: 500 }); |
| }); |
|
|
| |
| app.get('/v1/models', (req, res) => { |
| res.json({ |
| object: "list", |
| data: [ |
| { id: "gpt-4o", object: "model", type: "text", owned_by: "system" }, |
| { id: "claude-3-5-sonnet", object: "model", type: "text", owned_by: "system" }, |
| { id: "gemini-1.5-pro", object: "model", type: "text", owned_by: "system" } |
| ] |
| }); |
| }); |
|
|
| |
| |
| const PROVIDERS = [ |
| "https://api.llm7.io/v1/chat/completions" |
| ]; |
|
|
| let providerIndex = 0; |
|
|
| |
| function getNextProvider() { |
| const url = PROVIDERS[providerIndex]; |
| providerIndex = (providerIndex + 1) % PROVIDERS.length; |
| return url; |
| } |
|
|
| |
| app.post(['/v1/chat/completions'], limiter, async (req, res) => { |
| const clientIp = req.ip || req.headers['x-forwarded-for'] || req.socket.remoteAddress; |
| |
| |
| const controller = new AbortController(); |
| const timeoutId = setTimeout(() => controller.abort(), 45000); |
|
|
| |
| let attempts = 0; |
| let responseSent = false; |
| let lastError = null; |
|
|
| while (attempts < PROVIDERS.length && !responseSent) { |
| const targetUrl = getNextProvider(); |
| attempts++; |
|
|
| try { |
| const fetchHeaders = { |
| "Content-Type": "application/json", |
| "Accept": "*/*", |
| "X-Forwarded-For": clientIp, |
| "Authorization": "Bearer sk-dummy-key-12345" |
| }; |
| |
| const response = await fetch(targetUrl, { |
| method: "POST", |
| headers: fetchHeaders, |
| body: JSON.stringify(req.body), |
| signal: controller.signal |
| }); |
| |
| clearTimeout(timeoutId); |
|
|
| if (!response.ok) { |
| lastError = `HTTP ${response.status} de ${targetUrl}`; |
| continue; |
| } |
| |
| const responseHeaders = new Headers(response.headers); |
| res.writeHead(response.status, { |
| 'Content-Type': responseHeaders.get('content-type') || 'application/json', |
| 'Cache-Control': 'no-cache', |
| 'Connection': 'keep-alive' |
| }); |
| |
| if (response.body) { |
| const stream = Readable.fromWeb(response.body); |
| stream.pipe(res); |
| |
| req.on('close', () => { |
| stream.destroy(); |
| }); |
| } else { |
| res.end(); |
| } |
| |
| responseSent = true; |
| |
| } catch (err) { |
| lastError = err.name === 'AbortError' ? 'Timeout excedido' : err.message; |
| |
| } |
| } |
|
|
| if (!responseSent) { |
| clearTimeout(timeoutId); |
| if (!res.headersSent) { |
| res.status(503).json({ |
| error: { |
| message: "Todos los proveedores base rechazaron la conexión simultánea. Intenta reducir la concurrencia.", |
| details: lastError, |
| code: 503 |
| } |
| }); |
| } |
| } |
| }); |
|
|
| app.listen(PORT, '0.0.0.0', () => { |
| console.log(`[SYS] Ventarys API v2 corriendo en puerto: ${PORT}`); |
| console.log(`[SYS] Capacidad: 500 req/min. Conectado a: api.llm7.io`); |
| }); |