Update server.js
Browse files
server.js
CHANGED
|
@@ -2,22 +2,24 @@ import express from 'express';
|
|
| 2 |
import cors from 'cors';
|
| 3 |
import rateLimit from 'express-rate-limit';
|
| 4 |
import { Readable } from 'stream';
|
|
|
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
const PORT = 7860;
|
| 8 |
|
| 9 |
-
// --- CONFIGURACIÓN DE SEGURIDAD
|
| 10 |
-
// CRÍTICO: Confiar en el proxy es necesario para que req.ip lea la IP real del usuario y no la de Cloudflare/Host
|
| 11 |
app.set('trust proxy', 1);
|
| 12 |
app.disable('x-powered-by');
|
| 13 |
app.use(cors());
|
| 14 |
|
| 15 |
app.use((req, res, next) => {
|
|
|
|
| 16 |
res.setHeader('X-Content-Type-Options', 'nosniff');
|
| 17 |
res.setHeader('X-Frame-Options', 'DENY');
|
| 18 |
res.setHeader('X-XSS-Protection', '1; mode=block');
|
| 19 |
-
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
|
| 20 |
res.setHeader('Referrer-Policy', 'no-referrer');
|
|
|
|
| 21 |
next();
|
| 22 |
});
|
| 23 |
|
|
@@ -25,11 +27,23 @@ app.use(express.json({ limit: '50mb' }));
|
|
| 25 |
app.use(express.urlencoded({ limit: '50mb', extended: true }));
|
| 26 |
|
| 27 |
function logError(providerId, reason) {
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
-
// -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
const PROVIDERS = [
|
| 34 |
{
|
| 35 |
id: "pollinations-ai",
|
|
@@ -41,24 +55,24 @@ const PROVIDERS = [
|
|
| 41 |
url: "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/chat/completions"
|
| 42 |
},
|
| 43 |
{
|
| 44 |
-
id: "voids-api",
|
| 45 |
url: "https://api.voids.top/v1/chat/completions"
|
| 46 |
}
|
| 47 |
];
|
| 48 |
|
| 49 |
-
// Hemos subido el límite drásticamente. Ahora el proxy no será el cuello de botella.
|
| 50 |
-
// El límite real lo dictarán los proveedores según la IP que les reenviemos.
|
| 51 |
const MAX_PER_PROVIDER = 100;
|
| 52 |
const QUEUE_TIMEOUT = 25000;
|
| 53 |
let currentLoad = { "pollinations-ai": 0, "kepler-cloud": 0, "voids-api": 0 };
|
| 54 |
|
| 55 |
-
// --- RATE LIMITING LOCAL
|
|
|
|
|
|
|
| 56 |
const limiter = rateLimit({
|
| 57 |
windowMs: 60 * 1000,
|
| 58 |
-
max: 50,
|
| 59 |
keyGenerator: (req) => req.ip,
|
| 60 |
-
message: { error: { message: "
|
| 61 |
-
standardHeaders:
|
| 62 |
legacyHeaders: false,
|
| 63 |
});
|
| 64 |
|
|
@@ -68,39 +82,35 @@ const AUDIO_KEYWORDS = ["suno", "udio", "music", "audio", "song", "voice", "tts"
|
|
| 68 |
function isImageModel(model) {
|
| 69 |
if (!model) return false;
|
| 70 |
if (model.type === 'image' || model.supports_images === true) return true;
|
| 71 |
-
|
| 72 |
-
return IMAGE_KEYWORDS.some(kw => id.includes(kw));
|
| 73 |
}
|
| 74 |
|
| 75 |
function isAudioModel(model) {
|
| 76 |
if (model.type === 'audio' || model.type === 'music') return true;
|
| 77 |
-
|
| 78 |
-
return AUDIO_KEYWORDS.some(kw => id.includes(kw));
|
| 79 |
}
|
| 80 |
|
| 81 |
async function fetchAllModels() {
|
| 82 |
const fetchPromises = PROVIDERS.map(async (provider) => {
|
| 83 |
const modelsUrl = provider.modelsUrl || provider.url.replace("/chat/completions", "/models");
|
| 84 |
-
const fetchHeaders = { "Content-Type": "application/json" };
|
| 85 |
|
| 86 |
try {
|
| 87 |
-
const resp = await fetch(modelsUrl, {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
if (!resp.ok) return [];
|
| 89 |
|
| 90 |
const json = await resp.json();
|
| 91 |
-
let modelsArray = [];
|
| 92 |
-
|
| 93 |
-
if (Array.isArray(json)) modelsArray = json;
|
| 94 |
-
else if (json && Array.isArray(json.data)) modelsArray = json.data;
|
| 95 |
|
| 96 |
if (modelsArray.length > 0) {
|
| 97 |
return modelsArray
|
| 98 |
.filter(model => !isAudioModel(model))
|
| 99 |
-
.map(model => ({
|
| 100 |
-
...model,
|
| 101 |
-
id: model.id || model.name,
|
| 102 |
-
owned_by: provider.id
|
| 103 |
-
}));
|
| 104 |
}
|
| 105 |
return [];
|
| 106 |
} catch (error) {
|
|
@@ -114,8 +124,6 @@ async function fetchAllModels() {
|
|
| 114 |
if (result.status === "fulfilled") allModels = allModels.concat(result.value);
|
| 115 |
});
|
| 116 |
|
| 117 |
-
// Fallback: Si los proveedores gratuitos fallan al devolver la lista, inyectamos modelos estándar
|
| 118 |
-
// Ahora incluye Gemini explícitamente para que Ventarys AI lo ofrezca en la UI
|
| 119 |
if (allModels.length === 0) {
|
| 120 |
allModels = [
|
| 121 |
{ id: "gpt-4o", object: "model", type: "text", owned_by: "pollinations-ai" },
|
|
@@ -124,17 +132,12 @@ async function fetchAllModels() {
|
|
| 124 |
{ id: "gemini-1.5-flash", object: "model", type: "text", owned_by: "voids-api" }
|
| 125 |
];
|
| 126 |
}
|
| 127 |
-
|
| 128 |
return allModels;
|
| 129 |
}
|
| 130 |
|
| 131 |
app.get('/health', (req, res) => {
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
type: "hf-node-proxy",
|
| 135 |
-
providers: PROVIDERS.map(p => p.id),
|
| 136 |
-
current_load: currentLoad
|
| 137 |
-
});
|
| 138 |
});
|
| 139 |
|
| 140 |
app.get('/v1/models', async (req, res) => {
|
|
@@ -142,15 +145,10 @@ app.get('/v1/models', async (req, res) => {
|
|
| 142 |
const allModels = await fetchAllModels();
|
| 143 |
const textModels = allModels
|
| 144 |
.filter(m => !isImageModel(m) && m.supports_chat !== false)
|
| 145 |
-
.map(m => {
|
| 146 |
-
m.type = 'text';
|
| 147 |
-
return m;
|
| 148 |
-
});
|
| 149 |
-
|
| 150 |
res.json({ object: "list", data: textModels });
|
| 151 |
} catch (error) {
|
| 152 |
-
|
| 153 |
-
res.status(500).json({ error: "No se pudieron recuperar los modelos." });
|
| 154 |
}
|
| 155 |
});
|
| 156 |
|
|
@@ -159,41 +157,35 @@ app.get('/v1/images/models', async (req, res) => {
|
|
| 159 |
const allModels = await fetchAllModels();
|
| 160 |
let imageModels = allModels
|
| 161 |
.filter(m => isImageModel(m))
|
| 162 |
-
.map(m => {
|
| 163 |
-
m.type = 'image';
|
| 164 |
-
return m;
|
| 165 |
-
});
|
| 166 |
|
| 167 |
const baseImages = [
|
| 168 |
-
{ id: "flux", object: "model", type: "image", owned_by: "system"
|
| 169 |
-
{ id: "dall-e-3", object: "model", type: "image", owned_by: "system"
|
| 170 |
];
|
| 171 |
|
| 172 |
baseImages.forEach(baseMod => {
|
| 173 |
-
if (!imageModels.find(m => m.id.toLowerCase() === baseMod.id))
|
| 174 |
-
imageModels.push(baseMod);
|
| 175 |
-
}
|
| 176 |
});
|
| 177 |
|
| 178 |
res.json({ object: "list", data: imageModels });
|
| 179 |
} catch (error) {
|
| 180 |
-
|
| 181 |
-
res.status(500).json({ error: "No se pudieron recuperar los modelos." });
|
| 182 |
}
|
| 183 |
});
|
| 184 |
|
| 185 |
app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req, res) => {
|
| 186 |
const isImage = req.path === '/v1/images/generations';
|
| 187 |
let availableProviders = isImage ? PROVIDERS.filter(p => p.imageUrl) : [...PROVIDERS];
|
| 188 |
-
|
| 189 |
-
// Respaldo por si ningún proveedor declara explícitamente soportar imágenes
|
| 190 |
if (isImage && availableProviders.length === 0) availableProviders = [...PROVIDERS];
|
| 191 |
|
| 192 |
const startTime = Date.now();
|
| 193 |
let responseSent = false;
|
| 194 |
|
| 195 |
-
//
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
|
| 198 |
while (availableProviders.length > 0 && Date.now() - startTime < QUEUE_TIMEOUT) {
|
| 199 |
let selectedProvider = null;
|
|
@@ -223,22 +215,22 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 223 |
try {
|
| 224 |
let targetUrl = isImage && selectedProvider.imageUrl ? selectedProvider.imageUrl : selectedProvider.url;
|
| 225 |
|
| 226 |
-
//
|
|
|
|
| 227 |
const fetchHeaders = {
|
| 228 |
"Content-Type": "application/json",
|
| 229 |
-
"
|
| 230 |
-
"
|
| 231 |
-
"
|
| 232 |
};
|
| 233 |
|
| 234 |
const response = await fetch(targetUrl, {
|
| 235 |
method: "POST",
|
| 236 |
headers: fetchHeaders,
|
| 237 |
-
body:
|
| 238 |
});
|
| 239 |
|
| 240 |
if (!response.ok) {
|
| 241 |
-
logError(selectedProvider.id, `Fallo HTTP ${response.status} (Posible Rate Limit a la IP ${clientIp})`);
|
| 242 |
releaseSlot();
|
| 243 |
availableProviders = availableProviders.filter(p => p.id !== selectedProvider.id);
|
| 244 |
continue;
|
|
@@ -249,6 +241,7 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 249 |
responseHeaders.delete('server');
|
| 250 |
responseHeaders.delete('x-powered-by');
|
| 251 |
responseHeaders.delete('cf-ray');
|
|
|
|
| 252 |
|
| 253 |
if (isImage) {
|
| 254 |
const contentType = responseHeaders.get("content-type") || "";
|
|
@@ -266,13 +259,15 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 266 |
const arrayBuffer = await imgRes.arrayBuffer();
|
| 267 |
item.b64_json = Buffer.from(arrayBuffer).toString('base64');
|
| 268 |
delete item.url;
|
| 269 |
-
} catch (e) {
|
| 270 |
-
logError(selectedProvider.id, `Fallo convitiendo URL a Base64.`);
|
| 271 |
-
}
|
| 272 |
}
|
| 273 |
}
|
| 274 |
releaseSlot();
|
| 275 |
responseSent = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
return res.status(response.status).json({
|
| 277 |
created: Math.floor(Date.now() / 1000),
|
| 278 |
data: dataArray
|
|
@@ -281,14 +276,15 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 281 |
|
| 282 |
releaseSlot();
|
| 283 |
responseSent = true;
|
|
|
|
| 284 |
return res.status(response.status).json(jsonResp);
|
| 285 |
}
|
| 286 |
else if (contentType.includes("image/")) {
|
| 287 |
const arrayBuffer = await response.arrayBuffer();
|
| 288 |
const b64 = Buffer.from(arrayBuffer).toString('base64');
|
| 289 |
releaseSlot();
|
| 290 |
-
|
| 291 |
responseSent = true;
|
|
|
|
| 292 |
return res.status(200).json({
|
| 293 |
created: Math.floor(Date.now() / 1000),
|
| 294 |
data: [{ b64_json: b64 }]
|
|
@@ -298,6 +294,7 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 298 |
const textResp = await response.text();
|
| 299 |
releaseSlot();
|
| 300 |
responseSent = true;
|
|
|
|
| 301 |
return res.status(response.status).type(contentType).send(textResp);
|
| 302 |
}
|
| 303 |
}
|
|
@@ -313,14 +310,21 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 313 |
const stream = Readable.fromWeb(response.body);
|
| 314 |
stream.pipe(res);
|
| 315 |
|
| 316 |
-
stream.on('end',
|
| 317 |
-
stream.on('error', (err) => {
|
| 318 |
-
logError(selectedProvider.id, `Stream interrumpido.`);
|
| 319 |
releaseSlot();
|
|
|
|
| 320 |
});
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
} else {
|
| 323 |
releaseSlot();
|
|
|
|
| 324 |
res.end();
|
| 325 |
}
|
| 326 |
|
|
@@ -328,18 +332,19 @@ app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req
|
|
| 328 |
return;
|
| 329 |
|
| 330 |
} catch (err) {
|
| 331 |
-
logError(selectedProvider.id, `Excepción de red conectando al upstream.`);
|
| 332 |
releaseSlot();
|
| 333 |
availableProviders = availableProviders.filter(p => p.id !== selectedProvider.id);
|
| 334 |
}
|
| 335 |
}
|
| 336 |
|
|
|
|
|
|
|
|
|
|
| 337 |
if (!responseSent) {
|
| 338 |
-
|
| 339 |
-
return res.status(503).json({ error: { message: "El servicio no está disponible temporalmente.", code: 503 } });
|
| 340 |
}
|
| 341 |
});
|
| 342 |
|
| 343 |
app.listen(PORT, '0.0.0.0', () => {
|
| 344 |
-
console.log(`
|
| 345 |
});
|
|
|
|
| 2 |
import cors from 'cors';
|
| 3 |
import rateLimit from 'express-rate-limit';
|
| 4 |
import { Readable } from 'stream';
|
| 5 |
+
import crypto from 'crypto';
|
| 6 |
|
| 7 |
const app = express();
|
| 8 |
const PORT = 7860;
|
| 9 |
|
| 10 |
+
// --- 1. CONFIGURACIÓN DE SEGURIDAD PARANOICA ---
|
|
|
|
| 11 |
app.set('trust proxy', 1);
|
| 12 |
app.disable('x-powered-by');
|
| 13 |
app.use(cors());
|
| 14 |
|
| 15 |
app.use((req, res, next) => {
|
| 16 |
+
// Cabeceras estrictas anti-rastreo y anti-inyección
|
| 17 |
res.setHeader('X-Content-Type-Options', 'nosniff');
|
| 18 |
res.setHeader('X-Frame-Options', 'DENY');
|
| 19 |
res.setHeader('X-XSS-Protection', '1; mode=block');
|
| 20 |
+
res.setHeader('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
|
| 21 |
res.setHeader('Referrer-Policy', 'no-referrer');
|
| 22 |
+
res.setHeader('Content-Security-Policy', "default-src 'none'; frame-ancestors 'none';");
|
| 23 |
next();
|
| 24 |
});
|
| 25 |
|
|
|
|
| 27 |
app.use(express.urlencoded({ limit: '50mb', extended: true }));
|
| 28 |
|
| 29 |
function logError(providerId, reason) {
|
| 30 |
+
// CRÍTICO: No registrar NUNCA fechas exactas al milisegundo, ni IPs, ni el tamaño del prompt.
|
| 31 |
+
// Solo registramos caídas de la infraestructura.
|
| 32 |
+
console.error(`[SYSTEM] Provider Error: ${providerId} | ${reason}`);
|
| 33 |
}
|
| 34 |
|
| 35 |
+
// Generador de User-Agents falsos para ofuscar la huella digital del usuario
|
| 36 |
+
function getRandomUserAgent() {
|
| 37 |
+
const agents = [
|
| 38 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 39 |
+
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15",
|
| 40 |
+
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0",
|
| 41 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
|
| 42 |
+
];
|
| 43 |
+
return agents[Math.floor(Math.random() * agents.length)];
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// --- PROVEEDORES PÚBLICOS ---
|
| 47 |
const PROVIDERS = [
|
| 48 |
{
|
| 49 |
id: "pollinations-ai",
|
|
|
|
| 55 |
url: "https://oai.endpoints.kepler.ai.cloud.ovh.net/v1/chat/completions"
|
| 56 |
},
|
| 57 |
{
|
| 58 |
+
id: "voids-api",
|
| 59 |
url: "https://api.voids.top/v1/chat/completions"
|
| 60 |
}
|
| 61 |
];
|
| 62 |
|
|
|
|
|
|
|
| 63 |
const MAX_PER_PROVIDER = 100;
|
| 64 |
const QUEUE_TIMEOUT = 25000;
|
| 65 |
let currentLoad = { "pollinations-ai": 0, "kepler-cloud": 0, "voids-api": 0 };
|
| 66 |
|
| 67 |
+
// --- RATE LIMITING LOCAL ---
|
| 68 |
+
// Mantenemos el rate limit por IP para que no te tumben EL SERVIDOR,
|
| 69 |
+
// pero esta IP jamás saldrá de aquí hacia los proveedores.
|
| 70 |
const limiter = rateLimit({
|
| 71 |
windowMs: 60 * 1000,
|
| 72 |
+
max: 50,
|
| 73 |
keyGenerator: (req) => req.ip,
|
| 74 |
+
message: { error: { message: "Too many requests.", code: 429 } },
|
| 75 |
+
standardHeaders: false, // Desactivar para no dar pistas del rate limit
|
| 76 |
legacyHeaders: false,
|
| 77 |
});
|
| 78 |
|
|
|
|
| 82 |
function isImageModel(model) {
|
| 83 |
if (!model) return false;
|
| 84 |
if (model.type === 'image' || model.supports_images === true) return true;
|
| 85 |
+
return IMAGE_KEYWORDS.some(kw => (model.id || model.name || "").toLowerCase().includes(kw));
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
function isAudioModel(model) {
|
| 89 |
if (model.type === 'audio' || model.type === 'music') return true;
|
| 90 |
+
return AUDIO_KEYWORDS.some(kw => (model.id || model.name || "").toLowerCase().includes(kw));
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
async function fetchAllModels() {
|
| 94 |
const fetchPromises = PROVIDERS.map(async (provider) => {
|
| 95 |
const modelsUrl = provider.modelsUrl || provider.url.replace("/chat/completions", "/models");
|
|
|
|
| 96 |
|
| 97 |
try {
|
| 98 |
+
const resp = await fetch(modelsUrl, {
|
| 99 |
+
method: "GET",
|
| 100 |
+
headers: {
|
| 101 |
+
"Content-Type": "application/json",
|
| 102 |
+
"User-Agent": getRandomUserAgent() // Ocultar que somos un proxy Node.js
|
| 103 |
+
}
|
| 104 |
+
});
|
| 105 |
if (!resp.ok) return [];
|
| 106 |
|
| 107 |
const json = await resp.json();
|
| 108 |
+
let modelsArray = Array.isArray(json) ? json : (json && Array.isArray(json.data) ? json.data : []);
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if (modelsArray.length > 0) {
|
| 111 |
return modelsArray
|
| 112 |
.filter(model => !isAudioModel(model))
|
| 113 |
+
.map(model => ({ ...model, id: model.id || model.name, owned_by: provider.id }));
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
}
|
| 115 |
return [];
|
| 116 |
} catch (error) {
|
|
|
|
| 124 |
if (result.status === "fulfilled") allModels = allModels.concat(result.value);
|
| 125 |
});
|
| 126 |
|
|
|
|
|
|
|
| 127 |
if (allModels.length === 0) {
|
| 128 |
allModels = [
|
| 129 |
{ id: "gpt-4o", object: "model", type: "text", owned_by: "pollinations-ai" },
|
|
|
|
| 132 |
{ id: "gemini-1.5-flash", object: "model", type: "text", owned_by: "voids-api" }
|
| 133 |
];
|
| 134 |
}
|
|
|
|
| 135 |
return allModels;
|
| 136 |
}
|
| 137 |
|
| 138 |
app.get('/health', (req, res) => {
|
| 139 |
+
// Info mínima para no dar detalles de la infraestructura a atacantes
|
| 140 |
+
res.json({ status: "online" });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
});
|
| 142 |
|
| 143 |
app.get('/v1/models', async (req, res) => {
|
|
|
|
| 145 |
const allModels = await fetchAllModels();
|
| 146 |
const textModels = allModels
|
| 147 |
.filter(m => !isImageModel(m) && m.supports_chat !== false)
|
| 148 |
+
.map(m => { m.type = 'text'; return m; });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
res.json({ object: "list", data: textModels });
|
| 150 |
} catch (error) {
|
| 151 |
+
res.status(500).json({ error: "No models available." });
|
|
|
|
| 152 |
}
|
| 153 |
});
|
| 154 |
|
|
|
|
| 157 |
const allModels = await fetchAllModels();
|
| 158 |
let imageModels = allModels
|
| 159 |
.filter(m => isImageModel(m))
|
| 160 |
+
.map(m => { m.type = 'image'; return m; });
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
const baseImages = [
|
| 163 |
+
{ id: "flux", object: "model", type: "image", owned_by: "system" },
|
| 164 |
+
{ id: "dall-e-3", object: "model", type: "image", owned_by: "system" }
|
| 165 |
];
|
| 166 |
|
| 167 |
baseImages.forEach(baseMod => {
|
| 168 |
+
if (!imageModels.find(m => m.id.toLowerCase() === baseMod.id)) imageModels.push(baseMod);
|
|
|
|
|
|
|
| 169 |
});
|
| 170 |
|
| 171 |
res.json({ object: "list", data: imageModels });
|
| 172 |
} catch (error) {
|
| 173 |
+
res.status(500).json({ error: "No models available." });
|
|
|
|
| 174 |
}
|
| 175 |
});
|
| 176 |
|
| 177 |
app.post(['/v1/chat/completions', '/v1/images/generations'], limiter, async (req, res) => {
|
| 178 |
const isImage = req.path === '/v1/images/generations';
|
| 179 |
let availableProviders = isImage ? PROVIDERS.filter(p => p.imageUrl) : [...PROVIDERS];
|
|
|
|
|
|
|
| 180 |
if (isImage && availableProviders.length === 0) availableProviders = [...PROVIDERS];
|
| 181 |
|
| 182 |
const startTime = Date.now();
|
| 183 |
let responseSent = false;
|
| 184 |
|
| 185 |
+
// --- AMNESIA: Extracción profunda del body ---
|
| 186 |
+
// Clonamos el contenido para enviarlo y borraremos las referencias inmediatamente.
|
| 187 |
+
let payload = JSON.stringify(req.body);
|
| 188 |
+
req.body = null; // Eliminamos el acceso al body de la petición original
|
| 189 |
|
| 190 |
while (availableProviders.length > 0 && Date.now() - startTime < QUEUE_TIMEOUT) {
|
| 191 |
let selectedProvider = null;
|
|
|
|
| 215 |
try {
|
| 216 |
let targetUrl = isImage && selectedProvider.imageUrl ? selectedProvider.imageUrl : selectedProvider.url;
|
| 217 |
|
| 218 |
+
// --- OFUSCACIÓN ABSOLUTA ---
|
| 219 |
+
// Jamás enviamos IPs, Hostnames originales ni Referers.
|
| 220 |
const fetchHeaders = {
|
| 221 |
"Content-Type": "application/json",
|
| 222 |
+
"User-Agent": getRandomUserAgent(),
|
| 223 |
+
"Accept": "*/*",
|
| 224 |
+
"Connection": "keep-alive"
|
| 225 |
};
|
| 226 |
|
| 227 |
const response = await fetch(targetUrl, {
|
| 228 |
method: "POST",
|
| 229 |
headers: fetchHeaders,
|
| 230 |
+
body: payload
|
| 231 |
});
|
| 232 |
|
| 233 |
if (!response.ok) {
|
|
|
|
| 234 |
releaseSlot();
|
| 235 |
availableProviders = availableProviders.filter(p => p.id !== selectedProvider.id);
|
| 236 |
continue;
|
|
|
|
| 241 |
responseHeaders.delete('server');
|
| 242 |
responseHeaders.delete('x-powered-by');
|
| 243 |
responseHeaders.delete('cf-ray');
|
| 244 |
+
responseHeaders.delete('access-control-allow-origin'); // Limpiamos rastros del upstream
|
| 245 |
|
| 246 |
if (isImage) {
|
| 247 |
const contentType = responseHeaders.get("content-type") || "";
|
|
|
|
| 259 |
const arrayBuffer = await imgRes.arrayBuffer();
|
| 260 |
item.b64_json = Buffer.from(arrayBuffer).toString('base64');
|
| 261 |
delete item.url;
|
| 262 |
+
} catch (e) {}
|
|
|
|
|
|
|
| 263 |
}
|
| 264 |
}
|
| 265 |
releaseSlot();
|
| 266 |
responseSent = true;
|
| 267 |
+
|
| 268 |
+
// Purga manual de variables
|
| 269 |
+
payload = null;
|
| 270 |
+
|
| 271 |
return res.status(response.status).json({
|
| 272 |
created: Math.floor(Date.now() / 1000),
|
| 273 |
data: dataArray
|
|
|
|
| 276 |
|
| 277 |
releaseSlot();
|
| 278 |
responseSent = true;
|
| 279 |
+
payload = null;
|
| 280 |
return res.status(response.status).json(jsonResp);
|
| 281 |
}
|
| 282 |
else if (contentType.includes("image/")) {
|
| 283 |
const arrayBuffer = await response.arrayBuffer();
|
| 284 |
const b64 = Buffer.from(arrayBuffer).toString('base64');
|
| 285 |
releaseSlot();
|
|
|
|
| 286 |
responseSent = true;
|
| 287 |
+
payload = null;
|
| 288 |
return res.status(200).json({
|
| 289 |
created: Math.floor(Date.now() / 1000),
|
| 290 |
data: [{ b64_json: b64 }]
|
|
|
|
| 294 |
const textResp = await response.text();
|
| 295 |
releaseSlot();
|
| 296 |
responseSent = true;
|
| 297 |
+
payload = null;
|
| 298 |
return res.status(response.status).type(contentType).send(textResp);
|
| 299 |
}
|
| 300 |
}
|
|
|
|
| 310 |
const stream = Readable.fromWeb(response.body);
|
| 311 |
stream.pipe(res);
|
| 312 |
|
| 313 |
+
stream.on('end', () => {
|
|
|
|
|
|
|
| 314 |
releaseSlot();
|
| 315 |
+
payload = null; // Purga al terminar el stream
|
| 316 |
});
|
| 317 |
+
stream.on('error', () => {
|
| 318 |
+
releaseSlot();
|
| 319 |
+
payload = null;
|
| 320 |
+
});
|
| 321 |
+
req.on('close', () => {
|
| 322 |
+
releaseSlot();
|
| 323 |
+
payload = null;
|
| 324 |
+
});
|
| 325 |
} else {
|
| 326 |
releaseSlot();
|
| 327 |
+
payload = null;
|
| 328 |
res.end();
|
| 329 |
}
|
| 330 |
|
|
|
|
| 332 |
return;
|
| 333 |
|
| 334 |
} catch (err) {
|
|
|
|
| 335 |
releaseSlot();
|
| 336 |
availableProviders = availableProviders.filter(p => p.id !== selectedProvider.id);
|
| 337 |
}
|
| 338 |
}
|
| 339 |
|
| 340 |
+
// Purga final de seguridad si falla todo
|
| 341 |
+
payload = null;
|
| 342 |
+
|
| 343 |
if (!responseSent) {
|
| 344 |
+
return res.status(503).json({ error: { message: "Servicio no disponible.", code: 503 } });
|
|
|
|
| 345 |
}
|
| 346 |
});
|
| 347 |
|
| 348 |
app.listen(PORT, '0.0.0.0', () => {
|
| 349 |
+
console.log(`[SYS] Nivel de cifrado: Tor. Puerto: ${PORT}`);
|
| 350 |
});
|