File size: 1,351 Bytes
b39afbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const fastify = require('fastify')({ logger: true });
const http = require('http');
const { spawn } = require('child_process');

const PROXY_PORT = 7860; // Change this to a port different from 1688
const TARGET_HOST = '127.0.0.1';
const TARGET_PORT = 1688;

// Function to start the request forwarding server
async function startRequestForwardingServer() {
  const server = http.createServer((req, res) => {
    const options = {
      hostname: TARGET_HOST,
      port: TARGET_PORT,
      path: req.url,
      method: req.method,
      headers: req.headers,
    };

    const proxy = http.request(options, (proxyRes) => {
      res.writeHead(proxyRes.statusCode, proxyRes.headers);
      proxyRes.pipe(res, { end: true });
    });

    req.pipe(proxy, { end: true });
  });

  server.listen(PROXY_PORT, '0.0.0.0');
  console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
}

// Function to start the background process
function startYarnStartProcess() {
  const child = spawn('yarn', ['start'], {
    detached: true,
    stdio: 'ignore'
  });

  child.unref();
}

// Main function to start everything
async function startServers() {
  try {
    startYarnStartProcess();
    await startRequestForwardingServer();
  } catch (error) {
    console.error('Failed to start servers:', error);
  }
}

// Start the servers
startServers();