File size: 5,105 Bytes
b2934e9
 
bebfcb2
 
b2934e9
 
bebfcb2
0d58140
6ad7d21
 
 
17c41d9
 
d71e775
 
 
17c41d9
 
6ad7d21
d71e775
6ad7d21
 
 
 
 
 
 
d71e775
 
 
6ad7d21
b2934e9
bebfcb2
6ad7d21
bebfcb2
7ad7949
b2934e9
fa73a90
b2934e9
 
7ad7949
b2934e9
 
bebfcb2
b2934e9
 
 
 
 
 
bebfcb2
7ad7949
bebfcb2
 
 
 
 
 
b2934e9
7ad7949
aa75e29
7ad7949
 
 
0d58140
7ad7949
 
 
 
 
bebfcb2
 
 
 
7ad7949
0d58140
4e5218a
b2934e9
4e5218a
bebfcb2
b2934e9
 
6ad7d21
7ad7949
 
 
 
b2934e9
 
bebfcb2
 
 
 
 
7ad7949
 
 
bebfcb2
 
 
7ad7949
bebfcb2
b2934e9
 
 
bebfcb2
b2934e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bebfcb2
6ad7d21
 
 
 
 
 
bebfcb2
 
b2934e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const http = require('http');
const httpProxy = require('http-proxy');
const fs = require('fs');
const path = require('path');

const APP_URL = process.env.MAIN_URL || "http://localhost:3000";
const UPLOAD_DIR = process.env.UPLOAD_DIRECTORY || "/app/uploads";

// -------------------------------------------------------------------
// DÉTECTION DYNAMIQUE DU DOSSIER STATIC (Vérifié pour Hugging Face)
// -------------------------------------------------------------------
const possiblePaths = [
    path.join(__dirname, 'app', 'static'),
    path.join(__dirname, 'static'),
    '/app/app/static',
    '/app/static'
];

let foundPath = '';
for (const p of possiblePaths) {
    try {
        if (fs.existsSync(p) && fs.readdirSync(p).includes('terms.html')) {
            foundPath = p;
            break;
        }
    } catch (e) {
        // Dossier non accessible ou n'existe pas
    }
}

const STATIC_DIR = foundPath || possiblePaths[0];

// -------------------------------------------------------------------
// MVEMBA SYSTEM CONFIGURATION: PROXY v5.2.1 (Fix Double Declaration)
// -------------------------------------------------------------------
const proxyOptions = { ws: true, xfwd: true, cookieDomainRewrite: "", secure: false, changeOrigin: true };
const backendProxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:4000', ...proxyOptions });
const frontendProxy = httpProxy.createProxyServer({ target: 'http://127.0.0.1:4200', ...proxyOptions });

const errorHandler = (err, req, res) => {
    console.error(`[ERROR] ${new Date().toISOString()} : ${err.message}`);
    if (!res.headersSent) {
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('MVEMBA_SYSTEM: Service temporarily unavailable.');
    }
};

backendProxy.on('error', errorHandler);
frontendProxy.on('error', errorHandler);

const setCacheHeaders = (res, ext) => {
    const oneDay = 24 * 60 * 60;
    if (['.mp4', '.webm', '.mov'].includes(ext)) {
        res.setHeader('Cache-Control', `public, max-age=${oneDay}`);
    } else {
        res.setHeader('Cache-Control', 'no-cache');
    }
};

const serveStaticFile = (res, filePath, contentType = 'text/html') => {
    if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
        res.writeHead(200, { 'Content-Type': contentType });
        fs.createReadStream(filePath).pipe(res);
    } else {
        console.error(`[404] File not found at: ${filePath}`);
        res.writeHead(404);
        res.end('File not found');
    }
};

// -------------------------------------------------------------------
// HTTP SERVER
// -------------------------------------------------------------------
const server = http.createServer((req, res) => {

    // TikTok Domain Verification
    if (req.url === '/tiktokPNM40yS7EF2dkzSNfJclFifdnpalsOMm.txt') {
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        res.end('tiktok-developers-site-verification=PNM40yS7EF2dkzSNfJclFifdnpalsOMm');
        return;
    }

    // Static pages
    if (req.url === '/privacy') return serveStaticFile(res, path.join(STATIC_DIR, 'privacy.html'));
    if (req.url === '/terms') return serveStaticFile(res, path.join(STATIC_DIR, 'terms.html'));
    if (req.url === '/deletion') return serveStaticFile(res, path.join(STATIC_DIR, 'deletion.html'));

    req.headers['x-forwarded-proto'] = 'https';
    req.headers['x-forwarded-port'] = '443';
    if (req.headers.host) req.headers['x-forwarded-host'] = req.headers.host;

    if (req.url.startsWith('/uploads/')) {
        const filePath = path.join(UPLOAD_DIR, decodeURIComponent(req.url.replace('/uploads/', '')));
        if (fs.existsSync(filePath) && fs.statSync(filePath).isFile()) {
            setCacheHeaders(res, path.extname(filePath));
            fs.createReadStream(filePath).pipe(res);
            return;
        } else {
            res.writeHead(404);
            res.end('File not found');
            return;
        }
    }

    if (req.url.startsWith('/api') || req.url.startsWith('/public') || req.url.startsWith('/webhooks') || req.url.startsWith('/v1')) {
        if (req.url.startsWith('/api')) req.url = req.url.replace(/^\/api/, '') || '/';
        backendProxy.web(req, res);
    } else {
        frontendProxy.web(req, res);
    }
});

server.on('upgrade', (req, socket, head) => {
    if (req.url.startsWith('/api') || req.url.startsWith('/socket.io')) {
        if (req.url.startsWith('/api')) req.url = req.url.replace(/^\/api/, '') || '/';
        backendProxy.ws(req, socket, head);
    } else {
        frontendProxy.ws(req, socket, head);
    }
});

server.listen(3000, () => {
    console.log('------------------------------------------------------------');
    console.log('[STATUS] MVEMBA SECURE PROXY ENGINE v5.2.1 DEPLOYED');
    if (foundPath) {
        console.log(`[SUCCESS] Static files matched at: ${STATIC_DIR}`);
    } else {
        console.log(`[WARNING] Static directory not confirmed, using: ${STATIC_DIR}`);
    }
    console.log(`[TARGET] ${APP_URL}`);
    console.log('------------------------------------------------------------');
});