Spaces:
Running
Running
Commit
·
d44e3b5
1
Parent(s):
b79d62f
basic, working implementation
Browse files- myNodeServer.js +187 -30
myNodeServer.js
CHANGED
@@ -1,3 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
const fastify = require('fastify')({ logger: true });
|
2 |
const { spawn } = require('child_process');
|
3 |
const { createProxyMiddleware } = require('http-proxy-middleware');
|
@@ -5,39 +97,86 @@ const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
5 |
let OMNITOOL_READY = false;
|
6 |
let ALREADY_STARTING = false;
|
7 |
|
8 |
-
const PROXY_PORT = 4444;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
const TARGET_HOST = '0.0.0.0';
|
10 |
-
const
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
14 |
let omnitoolLogs = [];
|
15 |
|
16 |
-
async function startOmnitoolServer()
|
|
|
17 |
if (ALREADY_STARTING) return;
|
18 |
ALREADY_STARTING = true;
|
19 |
|
20 |
console.log('Starting Omnitool Server...');
|
21 |
-
return new Promise((resolve, reject) =>
|
|
|
22 |
const omnitoolStartProcess = spawn('./omnitool_start.sh');
|
23 |
|
24 |
-
omnitoolStartProcess.stdout.on('data', (data) =>
|
|
|
25 |
omnitoolLogs.push(data.toString());
|
26 |
console.log(`omnitool stdout: ${data}`);
|
27 |
-
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
|
|
28 |
OMNITOOL_READY = true;
|
29 |
console.log('Omnitool server started successfully');
|
30 |
resolve();
|
31 |
}
|
32 |
});
|
33 |
|
34 |
-
omnitoolStartProcess.stderr.on('data', (data) =>
|
|
|
35 |
console.error(`omnitool stderr: ${data}`);
|
36 |
});
|
37 |
|
38 |
-
omnitoolStartProcess.on('close', (code) =>
|
|
|
39 |
console.log(`Omnitool server process exited with code ${code}`);
|
40 |
-
if (!OMNITOOL_READY)
|
|
|
41 |
ALREADY_STARTING = false;
|
42 |
reject(`Omnitool server did not start within the timeout period.`);
|
43 |
}
|
@@ -45,10 +184,13 @@ async function startOmnitoolServer() {
|
|
45 |
});
|
46 |
}
|
47 |
|
48 |
-
|
|
|
|
|
49 |
const localUrl = request.headers['host'];
|
50 |
|
51 |
-
if (!OMNITOOL_READY)
|
|
|
52 |
let htmlContent = `
|
53 |
<html>
|
54 |
<head><title>Proxy Server</title></head>
|
@@ -87,47 +229,62 @@ fastify.get('/', async (request, reply) => {
|
|
87 |
</html>
|
88 |
`;
|
89 |
reply.type('text/html').send(htmlContent);
|
90 |
-
} else
|
|
|
|
|
91 |
console.log('Proxying request as OMNITOOL is ready');
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
99 |
}
|
100 |
});
|
101 |
|
102 |
-
fastify.get('/omnitool-logs', async (request, reply) =>
|
|
|
103 |
reply.send({ logs: omnitoolLogs, ready: OMNITOOL_READY });
|
104 |
});
|
105 |
|
106 |
-
fastify.get('/start-omnitool-server', async (request, reply) =>
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
109 |
return { message: "Omnitool server already starting" };
|
110 |
}
|
111 |
-
try
|
|
|
112 |
await startOmnitoolServer();
|
113 |
reply.send({ message: "Omnitool server started successfully" });
|
114 |
-
} catch (error)
|
|
|
115 |
console.error(error);
|
116 |
reply.send({ message: `Error starting Omnitool server: ${error}` });
|
117 |
}
|
118 |
-
} else
|
|
|
119 |
reply.send({ message: "Omnitool server already running" });
|
120 |
}
|
121 |
});
|
122 |
|
123 |
-
const start = async () =>
|
124 |
-
|
|
|
|
|
125 |
await fastify.listen({ port: PROXY_PORT, host: TARGET_HOST });
|
126 |
console.log(`Server is listening on http://${TARGET_HOST}:${PROXY_PORT}`);
|
127 |
-
} catch (err)
|
|
|
128 |
console.error(`Error starting server: ${err}`);
|
129 |
process.exit(1);
|
130 |
}
|
131 |
};
|
132 |
|
133 |
start();
|
|
|
|
|
|
1 |
+
const http = require('http');
|
2 |
+
const { spawn } = require('child_process');
|
3 |
+
|
4 |
+
const PROXY_PORT = 4444; // Change this to a port different from 1688
|
5 |
+
const TARGET_HOST = '127.0.0.1';
|
6 |
+
const TARGET_PORT = 1688;
|
7 |
+
let OMNITOOL_READY = false;
|
8 |
+
let ALREADY_STARTING = false;
|
9 |
+
const VERSION = '0.0.4a';
|
10 |
+
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
11 |
+
let omnitoolLogs = [];
|
12 |
+
|
13 |
+
|
14 |
+
// Function to start the request forwarding server
|
15 |
+
async function startRequestForwardingServer() {
|
16 |
+
const server = http.createServer((req, res) => {
|
17 |
+
const options = {
|
18 |
+
hostname: TARGET_HOST,
|
19 |
+
port: TARGET_PORT,
|
20 |
+
path: req.url,
|
21 |
+
method: req.method,
|
22 |
+
headers: req.headers,
|
23 |
+
};
|
24 |
+
|
25 |
+
const proxy = http.request(options, (proxyRes) => {
|
26 |
+
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
27 |
+
proxyRes.pipe(res, { end: true });
|
28 |
+
});
|
29 |
+
|
30 |
+
req.pipe(proxy, { end: true });
|
31 |
+
});
|
32 |
+
|
33 |
+
server.listen(PROXY_PORT, '0.0.0.0');
|
34 |
+
console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
|
35 |
+
}
|
36 |
+
|
37 |
+
// Function to start the background process
|
38 |
+
function startYarnStartProcess()
|
39 |
+
{
|
40 |
+
if (ALREADY_STARTING) return;
|
41 |
+
ALREADY_STARTING = true;
|
42 |
+
|
43 |
+
console.log('Starting Omnitool Server...');
|
44 |
+
return new Promise((resolve, reject) =>
|
45 |
+
{
|
46 |
+
const omnitoolStartProcess = spawn('./omnitool_start.sh', {detached: true});
|
47 |
+
omnitoolStartProcess.stdout.on('data', (data) =>
|
48 |
+
{
|
49 |
+
omnitoolLogs.push(data.toString());
|
50 |
+
console.log(`omnitool stdout: ${data}`);
|
51 |
+
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
52 |
+
{
|
53 |
+
OMNITOOL_READY = true;
|
54 |
+
console.log('Omnitool server started successfully');
|
55 |
+
resolve();
|
56 |
+
}
|
57 |
+
});
|
58 |
+
|
59 |
+
omnitoolStartProcess.stderr.on('data', (data) =>
|
60 |
+
{
|
61 |
+
console.error(`omnitool stderr: ${data}`);
|
62 |
+
});
|
63 |
+
|
64 |
+
omnitoolStartProcess.on('close', (code) =>
|
65 |
+
{
|
66 |
+
console.log(`Omnitool server process exited with code ${code}`);
|
67 |
+
if (!OMNITOOL_READY)
|
68 |
+
{
|
69 |
+
ALREADY_STARTING = false;
|
70 |
+
reject(`Omnitool server did not start within the timeout period.`);
|
71 |
+
}
|
72 |
+
});
|
73 |
+
});
|
74 |
+
|
75 |
+
omnitoolStartProcess.unref();
|
76 |
+
}
|
77 |
+
|
78 |
+
// Main function to start everything
|
79 |
+
async function startServers() {
|
80 |
+
try {
|
81 |
+
startYarnStartProcess();
|
82 |
+
await startRequestForwardingServer();
|
83 |
+
} catch (error) {
|
84 |
+
console.error('Failed to start servers:', error);
|
85 |
+
}
|
86 |
+
}
|
87 |
+
|
88 |
+
// Start the servers
|
89 |
+
startServers();
|
90 |
+
|
91 |
+
|
92 |
+
/*
|
93 |
const fastify = require('fastify')({ logger: true });
|
94 |
const { spawn } = require('child_process');
|
95 |
const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
|
97 |
let OMNITOOL_READY = false;
|
98 |
let ALREADY_STARTING = false;
|
99 |
|
100 |
+
//const PROXY_PORT = 4444;
|
101 |
+
//const TARGET_HOST = '0.0.0.0';
|
102 |
+
//const PROXY_TARGET = 'http://127.0.0.1:1688';
|
103 |
+
const VERSION = '0.0.3a';
|
104 |
+
|
105 |
+
|
106 |
+
// ------------
|
107 |
+
const http = require('http');
|
108 |
+
|
109 |
+
const PROXY_PORT = 4444; // Change this to a port different from 1688
|
110 |
const TARGET_HOST = '0.0.0.0';
|
111 |
+
const TARGET_PORT = 1688;
|
112 |
+
|
113 |
+
// Function to start the request forwarding server
|
114 |
+
async function startRequestForwardingServer()
|
115 |
+
{
|
116 |
+
|
117 |
+
console.log('Starting request forwarding server...');
|
118 |
+
const server = http.createServer((req, res) =>
|
119 |
+
{
|
120 |
+
const options = {
|
121 |
+
hostname: TARGET_HOST,
|
122 |
+
port: TARGET_PORT,
|
123 |
+
path: req.url,
|
124 |
+
method: req.method,
|
125 |
+
headers: req.headers,
|
126 |
+
};
|
127 |
+
|
128 |
+
const proxy = http.request(options, (proxyRes) =>
|
129 |
+
{
|
130 |
+
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
131 |
+
proxyRes.pipe(res, { end: true });
|
132 |
+
});
|
133 |
+
|
134 |
+
req.pipe(proxy, { end: true });
|
135 |
+
});
|
136 |
+
|
137 |
+
server.listen(PROXY_PORT, '0.0.0.0');
|
138 |
+
console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
|
139 |
+
}
|
140 |
+
|
141 |
+
// ------------
|
142 |
+
|
143 |
+
|
144 |
|
145 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
146 |
let omnitoolLogs = [];
|
147 |
|
148 |
+
async function startOmnitoolServer()
|
149 |
+
{
|
150 |
if (ALREADY_STARTING) return;
|
151 |
ALREADY_STARTING = true;
|
152 |
|
153 |
console.log('Starting Omnitool Server...');
|
154 |
+
return new Promise((resolve, reject) =>
|
155 |
+
{
|
156 |
const omnitoolStartProcess = spawn('./omnitool_start.sh');
|
157 |
|
158 |
+
omnitoolStartProcess.stdout.on('data', (data) =>
|
159 |
+
{
|
160 |
omnitoolLogs.push(data.toString());
|
161 |
console.log(`omnitool stdout: ${data}`);
|
162 |
+
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
163 |
+
{
|
164 |
OMNITOOL_READY = true;
|
165 |
console.log('Omnitool server started successfully');
|
166 |
resolve();
|
167 |
}
|
168 |
});
|
169 |
|
170 |
+
omnitoolStartProcess.stderr.on('data', (data) =>
|
171 |
+
{
|
172 |
console.error(`omnitool stderr: ${data}`);
|
173 |
});
|
174 |
|
175 |
+
omnitoolStartProcess.on('close', (code) =>
|
176 |
+
{
|
177 |
console.log(`Omnitool server process exited with code ${code}`);
|
178 |
+
if (!OMNITOOL_READY)
|
179 |
+
{
|
180 |
ALREADY_STARTING = false;
|
181 |
reject(`Omnitool server did not start within the timeout period.`);
|
182 |
}
|
|
|
184 |
});
|
185 |
}
|
186 |
|
187 |
+
|
188 |
+
fastify.get('/', async (request, reply) =>
|
189 |
+
{
|
190 |
const localUrl = request.headers['host'];
|
191 |
|
192 |
+
if (!OMNITOOL_READY)
|
193 |
+
{
|
194 |
let htmlContent = `
|
195 |
<html>
|
196 |
<head><title>Proxy Server</title></head>
|
|
|
229 |
</html>
|
230 |
`;
|
231 |
reply.type('text/html').send(htmlContent);
|
232 |
+
} else
|
233 |
+
{
|
234 |
+
|
235 |
console.log('Proxying request as OMNITOOL is ready');
|
236 |
+
|
237 |
+
try
|
238 |
+
{
|
239 |
+
await startRequestForwardingServer();
|
240 |
+
} catch (error)
|
241 |
+
{
|
242 |
+
console.error('Failed to start servers:', error);
|
243 |
+
}
|
244 |
}
|
245 |
});
|
246 |
|
247 |
+
fastify.get('/omnitool-logs', async (request, reply) =>
|
248 |
+
{
|
249 |
reply.send({ logs: omnitoolLogs, ready: OMNITOOL_READY });
|
250 |
});
|
251 |
|
252 |
+
fastify.get('/start-omnitool-server', async (request, reply) =>
|
253 |
+
{
|
254 |
+
if (!OMNITOOL_READY)
|
255 |
+
{
|
256 |
+
if (ALREADY_STARTING)
|
257 |
+
{
|
258 |
return { message: "Omnitool server already starting" };
|
259 |
}
|
260 |
+
try
|
261 |
+
{
|
262 |
await startOmnitoolServer();
|
263 |
reply.send({ message: "Omnitool server started successfully" });
|
264 |
+
} catch (error)
|
265 |
+
{
|
266 |
console.error(error);
|
267 |
reply.send({ message: `Error starting Omnitool server: ${error}` });
|
268 |
}
|
269 |
+
} else
|
270 |
+
{
|
271 |
reply.send({ message: "Omnitool server already running" });
|
272 |
}
|
273 |
});
|
274 |
|
275 |
+
const start = async () =>
|
276 |
+
{
|
277 |
+
try
|
278 |
+
{
|
279 |
await fastify.listen({ port: PROXY_PORT, host: TARGET_HOST });
|
280 |
console.log(`Server is listening on http://${TARGET_HOST}:${PROXY_PORT}`);
|
281 |
+
} catch (err)
|
282 |
+
{
|
283 |
console.error(`Error starting server: ${err}`);
|
284 |
process.exit(1);
|
285 |
}
|
286 |
};
|
287 |
|
288 |
start();
|
289 |
+
*/
|
290 |
+
|