Spaces:
Sleeping
Sleeping
Update proxy.js
Browse files
proxy.js
CHANGED
|
@@ -1,78 +1,109 @@
|
|
| 1 |
const http = require('http');
|
| 2 |
const httpProxy = require('http-proxy');
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
const APP_URL = process.env.MAIN_URL || "http://localhost:3000";
|
|
|
|
| 5 |
|
| 6 |
-
/
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
* -------------------------------------------------------------------
|
| 10 |
-
*/
|
| 11 |
const proxyOptions = {
|
| 12 |
ws: true,
|
| 13 |
-
xfwd: true,
|
| 14 |
-
cookieDomainRewrite: "",
|
| 15 |
-
secure: false,
|
| 16 |
-
changeOrigin: true
|
| 17 |
};
|
| 18 |
|
| 19 |
const backendProxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:4000', ...proxyOptions });
|
| 20 |
const frontendProxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:4200', ...proxyOptions });
|
| 21 |
|
| 22 |
-
/
|
| 23 |
-
|
| 24 |
-
|
| 25 |
const errorHandler = (err, req, res) => {
|
| 26 |
-
|
| 27 |
if (!res.headersSent) {
|
| 28 |
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
| 29 |
-
res.end('MVEMBA_SYSTEM: Service temporarily unavailable.
|
| 30 |
}
|
| 31 |
};
|
| 32 |
|
| 33 |
backendProxy.on('error', errorHandler);
|
| 34 |
frontendProxy.on('error', errorHandler);
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
if (req.url === '/tiktokQDQMgLkzRU9aDBUTgDacXi8GWwyCR9Cn.txt') {
|
| 42 |
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
| 43 |
res.end('tiktok-developers-site-verification=QDQMgLkzRU9aDBUTgDacXi8GWwyCR9Cn');
|
| 44 |
-
return;
|
| 45 |
}
|
| 46 |
|
| 47 |
-
/
|
| 48 |
-
|
| 49 |
-
|
| 50 |
req.headers['x-forwarded-proto'] = 'https';
|
| 51 |
req.headers['x-forwarded-port'] = '443';
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
-
/
|
| 58 |
-
|
| 59 |
-
|
| 60 |
if (req.url.startsWith('/api') || req.url.startsWith('/public') || req.url.startsWith('/webhooks') || req.url.startsWith('/v1')) {
|
| 61 |
-
|
| 62 |
-
// Normalizing request paths for Backend service compatibility
|
| 63 |
-
if (req.url.startsWith('/api')) {
|
| 64 |
-
req.url = req.url.replace(/^\/api/, '') || '/';
|
| 65 |
-
}
|
| 66 |
backendProxy.web(req, res);
|
| 67 |
} else {
|
| 68 |
-
// Directing primary traffic to Frontend service
|
| 69 |
frontendProxy.web(req, res);
|
| 70 |
}
|
| 71 |
});
|
| 72 |
|
| 73 |
-
/
|
| 74 |
-
|
| 75 |
-
|
| 76 |
server.on('upgrade', (req, socket, head) => {
|
| 77 |
if (req.url.startsWith('/api') || req.url.startsWith('/socket.io')) {
|
| 78 |
if (req.url.startsWith('/api')) req.url = req.url.replace(/^\/api/, '') || '/';
|
|
@@ -82,12 +113,13 @@ server.on('upgrade', (req, socket, head) => {
|
|
| 82 |
}
|
| 83 |
});
|
| 84 |
|
| 85 |
-
/
|
| 86 |
-
|
| 87 |
-
|
| 88 |
server.listen(3000, () => {
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 93 |
});
|
|
|
|
| 1 |
const http = require('http');
|
| 2 |
const httpProxy = require('http-proxy');
|
| 3 |
+
const fs = require('fs');
|
| 4 |
+
const path = require('path');
|
| 5 |
+
const crypto = require('crypto');
|
| 6 |
|
| 7 |
const APP_URL = process.env.MAIN_URL || "http://localhost:3000";
|
| 8 |
+
const UPLOAD_DIR = process.env.UPLOAD_DIRECTORY || "/app/uploads";
|
| 9 |
|
| 10 |
+
// -------------------------------------------------------------------
|
| 11 |
+
// MVEMBA SYSTEM CONFIGURATION: PROXY v5
|
| 12 |
+
// -------------------------------------------------------------------
|
|
|
|
|
|
|
| 13 |
const proxyOptions = {
|
| 14 |
ws: true,
|
| 15 |
+
xfwd: true,
|
| 16 |
+
cookieDomainRewrite: "",
|
| 17 |
+
secure: false,
|
| 18 |
+
changeOrigin: true
|
| 19 |
};
|
| 20 |
|
| 21 |
const backendProxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:4000', ...proxyOptions });
|
| 22 |
const frontendProxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:4200', ...proxyOptions });
|
| 23 |
|
| 24 |
+
// -------------------------------------------------------------------
|
| 25 |
+
// ERROR HANDLER: DIAGNOSTIC LAYER
|
| 26 |
+
// -------------------------------------------------------------------
|
| 27 |
const errorHandler = (err, req, res) => {
|
| 28 |
+
console.error(`[CRITICAL_SYSTEM_ERROR] ${new Date().toISOString()} : ${err.message}`);
|
| 29 |
if (!res.headersSent) {
|
| 30 |
res.writeHead(500, { 'Content-Type': 'text/plain' });
|
| 31 |
+
res.end('MVEMBA_SYSTEM: Service temporarily unavailable.');
|
| 32 |
}
|
| 33 |
};
|
| 34 |
|
| 35 |
backendProxy.on('error', errorHandler);
|
| 36 |
frontendProxy.on('error', errorHandler);
|
| 37 |
|
| 38 |
+
// -------------------------------------------------------------------
|
| 39 |
+
// UTILITY: Cache Headers pour streaming fichiers
|
| 40 |
+
// -------------------------------------------------------------------
|
| 41 |
+
const setCacheHeaders = (res, ext) => {
|
| 42 |
+
const oneDay = 24 * 60 * 60; // secondes
|
| 43 |
+
if (['.mp4', '.webm', '.mov'].includes(ext)) {
|
| 44 |
+
res.setHeader('Cache-Control', `public, max-age=${oneDay}`);
|
| 45 |
+
} else {
|
| 46 |
+
res.setHeader('Cache-Control', 'no-cache');
|
| 47 |
+
}
|
| 48 |
+
};
|
| 49 |
|
| 50 |
+
// -------------------------------------------------------------------
|
| 51 |
+
// HTTP SERVER
|
| 52 |
+
// -------------------------------------------------------------------
|
| 53 |
+
const server = http.createServer((req, res) => {
|
| 54 |
+
|
| 55 |
+
// -------------------------------
|
| 56 |
+
// TikTok Domain Verification
|
| 57 |
+
// -------------------------------
|
| 58 |
if (req.url === '/tiktokQDQMgLkzRU9aDBUTgDacXi8GWwyCR9Cn.txt') {
|
| 59 |
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
| 60 |
res.end('tiktok-developers-site-verification=QDQMgLkzRU9aDBUTgDacXi8GWwyCR9Cn');
|
| 61 |
+
return;
|
| 62 |
}
|
| 63 |
|
| 64 |
+
// -------------------------------
|
| 65 |
+
// PROTOCOL ENFORCEMENT HTTPS
|
| 66 |
+
// -------------------------------
|
| 67 |
req.headers['x-forwarded-proto'] = 'https';
|
| 68 |
req.headers['x-forwarded-port'] = '443';
|
| 69 |
+
if (req.headers.host) req.headers['x-forwarded-host'] = req.headers.host;
|
| 70 |
+
|
| 71 |
+
// -------------------------------
|
| 72 |
+
// PUBLIC UPLOADS (Streaming + Cache)
|
| 73 |
+
// -------------------------------
|
| 74 |
+
if (req.url.startsWith('/uploads/')) {
|
| 75 |
+
const filePath = path.join(UPLOAD_DIR, decodeURIComponent(req.url.replace('/uploads/', '')));
|
| 76 |
+
if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
|
| 77 |
+
const ext = path.extname(filePath);
|
| 78 |
+
setCacheHeaders(res, ext);
|
| 79 |
+
const stream = fs.createReadStream(filePath);
|
| 80 |
+
stream.pipe(res);
|
| 81 |
+
stream.on('error', err => {
|
| 82 |
+
console.error(`[UPLOAD_STREAM_ERROR] ${err.message}`);
|
| 83 |
+
res.writeHead(500);
|
| 84 |
+
res.end('Internal Server Error');
|
| 85 |
+
});
|
| 86 |
+
} else {
|
| 87 |
+
res.writeHead(404);
|
| 88 |
+
res.end('File not found');
|
| 89 |
+
}
|
| 90 |
+
return;
|
| 91 |
}
|
| 92 |
|
| 93 |
+
// -------------------------------
|
| 94 |
+
// TRAFFIC ROUTING
|
| 95 |
+
// -------------------------------
|
| 96 |
if (req.url.startsWith('/api') || req.url.startsWith('/public') || req.url.startsWith('/webhooks') || req.url.startsWith('/v1')) {
|
| 97 |
+
if (req.url.startsWith('/api')) req.url = req.url.replace(/^\/api/, '') || '/';
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
backendProxy.web(req, res);
|
| 99 |
} else {
|
|
|
|
| 100 |
frontendProxy.web(req, res);
|
| 101 |
}
|
| 102 |
});
|
| 103 |
|
| 104 |
+
// -------------------------------------------------------------------
|
| 105 |
+
// WEBSOCKET UPGRADE
|
| 106 |
+
// -------------------------------------------------------------------
|
| 107 |
server.on('upgrade', (req, socket, head) => {
|
| 108 |
if (req.url.startsWith('/api') || req.url.startsWith('/socket.io')) {
|
| 109 |
if (req.url.startsWith('/api')) req.url = req.url.replace(/^\/api/, '') || '/';
|
|
|
|
| 113 |
}
|
| 114 |
});
|
| 115 |
|
| 116 |
+
// -------------------------------------------------------------------
|
| 117 |
+
// START SERVER
|
| 118 |
+
// -------------------------------------------------------------------
|
| 119 |
server.listen(3000, () => {
|
| 120 |
+
console.log('------------------------------------------------------------');
|
| 121 |
+
console.log('[STATUS] MVEMBA SECURE PROXY ENGINE v5.0.0 DEPLOYED');
|
| 122 |
+
console.log(`[TARGET] ${APP_URL}`);
|
| 123 |
+
console.log(`[UPLOADS] Public path: /uploads -> ${UPLOAD_DIR}`);
|
| 124 |
+
console.log('------------------------------------------------------------');
|
| 125 |
});
|