y59 commited on
Commit
0323105
·
verified ·
1 Parent(s): a495c66

Update proxy.js

Browse files
Files changed (1) hide show
  1. proxy.js +43 -80
proxy.js CHANGED
@@ -1,97 +1,60 @@
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
- })();
 
1
  const http = require('http');
2
 
3
+ // Configuration
4
  const TARGET_PORT = 11470;
5
  const LISTEN_PORT = 7860;
6
 
7
+ console.log('===== Simple Stremio Proxy Starting =====');
 
8
 
9
+ // Create a proxy server with minimal complexity
10
+ const server = http.createServer((req, res) => {
11
+ const timestamp = new Date().toISOString();
12
+
13
+ // Special route for health checks
14
+ if (req.url === '/health' || req.url === '/') {
15
+ console.log(`${timestamp}: Health check request received`);
16
+ res.writeHead(200, { 'Content-Type': 'application/json' });
17
+ res.end(JSON.stringify({
18
+ status: 'up',
19
+ message: 'Stremio proxy is running',
20
+ timestamp: timestamp
21
+ }));
22
+ return;
23
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
+ console.log(`${timestamp}: Proxying request to: ${req.url}`);
26
+
27
+ // Forward request to Stremio server
28
+ const proxyReq = http.request({
29
  hostname: 'localhost',
30
  port: TARGET_PORT,
31
+ path: req.url,
32
+ method: req.method,
33
+ headers: req.headers
34
+ }, (proxyRes) => {
35
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
36
+ proxyRes.pipe(res);
 
 
 
 
37
  });
38
+
39
  // Handle errors
40
+ proxyReq.on('error', (e) => {
41
+ console.error(`${timestamp}: Proxy error: ${e.message}`);
42
+ res.writeHead(503);
43
+ res.end('Stremio server is not responding');
 
 
 
 
 
 
44
  });
45
 
46
+ // Pipe original request to proxy request
47
+ req.pipe(proxyReq);
48
  });
49
 
50
+ // Handle server errors
51
+ server.on('error', (e) => {
52
+ console.error(`Server error: ${e.message}`);
53
  });
54
 
55
+ // Start the server
56
+ server.listen(LISTEN_PORT, () => {
57
+ console.log(`Proxy server listening on port ${LISTEN_PORT}`);
58
+ console.log(`Forwarding requests to port ${TARGET_PORT}`);
59
+ console.log('===== Proxy Server Started =====');
60
+ });