y59 commited on
Commit
8b4b8dc
·
verified ·
1 Parent(s): 594bd92

Update proxy.js

Browse files
Files changed (1) hide show
  1. proxy.js +97 -40
proxy.js CHANGED
@@ -1,40 +1,97 @@
1
- const http = require('http');
2
-
3
- const TARGET_PORT = 11470;
4
- const LISTEN_PORT = 7860;
5
-
6
- // Create a proxy server
7
- const proxy = http.createServer((clientReq, clientRes) => {
8
- console.log(`Proxying request to: ${clientReq.url}`);
9
-
10
- // Configure the request options
11
- const options = {
12
- hostname: 'localhost',
13
- port: TARGET_PORT,
14
- path: clientReq.url,
15
- method: clientReq.method,
16
- headers: clientReq.headers
17
- };
18
-
19
- // Create the proxied request
20
- const proxyReq = http.request(options, (proxyRes) => {
21
- clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);
22
- proxyRes.pipe(clientRes, { end: true });
23
- });
24
-
25
- // Handle errors
26
- proxyReq.on('error', (err) => {
27
- console.error('Proxy error:', err);
28
- clientRes.writeHead(500);
29
- clientRes.end('Proxy error connecting to Stremio server');
30
- });
31
-
32
- // Pipe the client request to the proxy request
33
- clientReq.pipe(proxyReq, { end: true });
34
- });
35
-
36
- // Start the proxy server
37
- proxy.listen(LISTEN_PORT, () => {
38
- console.log(`Proxy server running on port ${LISTEN_PORT}`);
39
- console.log(`Forwarding to Stremio server on port ${TARGET_PORT}`);
40
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const http = require('http');
2
+
3
+ const TARGET_PORT = 11470;
4
+ const LISTEN_PORT = 7860;
5
+
6
+ console.log(`===== Stremio Proxy Initializing =====`);
7
+ console.log(`${new Date().toISOString()}: Proxy will forward from port ${LISTEN_PORT} to ${TARGET_PORT}`);
8
+
9
+ // Function to check if Stremio server is running
10
+ function checkStremioServer() {
11
+ return new Promise((resolve) => {
12
+ const req = http.request({
13
+ hostname: 'localhost',
14
+ port: TARGET_PORT,
15
+ path: '/',
16
+ method: 'HEAD',
17
+ timeout: 2000
18
+ }, (res) => {
19
+ console.log(`${new Date().toISOString()}: Stremio server check status: ${res.statusCode}`);
20
+ resolve(true);
21
+ });
22
+
23
+ req.on('error', (err) => {
24
+ console.log(`${new Date().toISOString()}: Stremio server check failed: ${err.message}`);
25
+ resolve(false);
26
+ });
27
+
28
+ req.on('timeout', () => {
29
+ console.log(`${new Date().toISOString()}: Stremio server check timed out`);
30
+ req.destroy();
31
+ resolve(false);
32
+ });
33
+
34
+ req.end();
35
+ });
36
+ }
37
+
38
+ // Create a proxy server
39
+ const proxy = http.createServer((clientReq, clientRes) => {
40
+ const clientIP = clientReq.headers['x-forwarded-for'] || clientReq.socket.remoteAddress;
41
+ console.log(`${new Date().toISOString()}: Proxying request from ${clientIP} to: ${clientReq.url}`);
42
+
43
+ // Configure the request options
44
+ const options = {
45
+ hostname: 'localhost',
46
+ port: TARGET_PORT,
47
+ path: clientReq.url,
48
+ method: clientReq.method,
49
+ headers: {...clientReq.headers, host: `localhost:${TARGET_PORT}`}
50
+ };
51
+
52
+ // Create the proxied request
53
+ const proxyReq = http.request(options, (proxyRes) => {
54
+ console.log(`${new Date().toISOString()}: Got response from Stremio server: ${proxyRes.statusCode}`);
55
+ clientRes.writeHead(proxyRes.statusCode, proxyRes.headers);
56
+ proxyRes.pipe(clientRes, { end: true });
57
+ });
58
+
59
+ // Handle errors
60
+ proxyReq.on('error', (err) => {
61
+ console.error(`${new Date().toISOString()}: Proxy error: ${err.message}`);
62
+ clientRes.writeHead(503);
63
+ clientRes.end(`Service Unavailable: Stremio server not responding. Please check logs and try again later.`);
64
+ });
65
+
66
+ // Add request timeout
67
+ proxyReq.on('timeout', () => {
68
+ console.error(`${new Date().toISOString()}: Request to Stremio server timed out`);
69
+ proxyReq.destroy();
70
+ });
71
+
72
+ // Pipe the client request to the proxy request
73
+ clientReq.pipe(proxyReq, { end: true });
74
+ });
75
+
76
+ // Add error handling for the proxy server
77
+ proxy.on('error', (err) => {
78
+ console.error(`${new Date().toISOString()}: Proxy server error: ${err.message}`);
79
+ });
80
+
81
+ // Start health check then start the proxy server
82
+ (async () => {
83
+ console.log(`${new Date().toISOString()}: Performing initial health check on Stremio server...`);
84
+ const isStremioRunning = await checkStremioServer();
85
+
86
+ if (isStremioRunning) {
87
+ console.log(`${new Date().toISOString()}: Stremio server is responding, starting proxy`);
88
+ } else {
89
+ console.log(`${new Date().toISOString()}: Stremio server is not responding, but starting proxy anyway`);
90
+ }
91
+
92
+ proxy.listen(LISTEN_PORT, () => {
93
+ console.log(`${new Date().toISOString()}: Proxy server running on port ${LISTEN_PORT}`);
94
+ console.log(`${new Date().toISOString()}: Forwarding to Stremio server on port ${TARGET_PORT}`);
95
+ console.log(`===== Stremio Proxy Started Successfully =====`);
96
+ });
97
+ })();