Spaces:
Running
Running
Commit
·
0b10343
1
Parent(s):
6cb66da
gave the manager server a console
Browse files- myNodeServer.js +66 -244
myNodeServer.js
CHANGED
@@ -6,6 +6,7 @@ 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 = [];
|
@@ -26,9 +27,10 @@ async function startOmnitoolServer()
|
|
26 |
console.log(`omnitool stdout: ${data}`);
|
27 |
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
28 |
{
|
29 |
-
OMNITOOL_READY = true;
|
30 |
console.log('Omnitool server started successfully');
|
31 |
-
|
|
|
|
|
32 |
}
|
33 |
});
|
34 |
|
@@ -57,9 +59,8 @@ async function startRequestForwardingServer() {
|
|
57 |
|
58 |
async function handleRoutes(req, res)
|
59 |
{
|
60 |
-
if (!OMNITOOL_READY && (req.method === 'GET'))
|
61 |
{
|
62 |
-
console.log(`Handling request: ${req.url}`);
|
63 |
switch (req.url)
|
64 |
{
|
65 |
case '/start-omnitool-server':
|
@@ -97,49 +98,70 @@ async function handleRoutes(req, res)
|
|
97 |
return;
|
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 |
.then(response => response.json())
|
124 |
.then(data => {
|
125 |
-
|
126 |
-
if (data.ready) {
|
127 |
-
document.getElementById('logs').innerText = "Omnitool is ready! Please open the following link in a new tab: https://${localUrl}";
|
128 |
-
clearInterval(interval);
|
129 |
-
//window.location.reload(); // Refresh the page when Omnitool is ready
|
130 |
-
}
|
131 |
});
|
132 |
-
}
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
default:
|
145 |
console.log(`Doing nothing with this request: ${req.url}`);
|
@@ -173,203 +195,3 @@ async function startManagementServer()
|
|
173 |
// Start the servers
|
174 |
startManagementServer();
|
175 |
|
176 |
-
|
177 |
-
/*
|
178 |
-
const fastify = require('fastify')({ logger: true });
|
179 |
-
const { spawn } = require('child_process');
|
180 |
-
const { createProxyMiddleware } = require('http-proxy-middleware');
|
181 |
-
|
182 |
-
let OMNITOOL_READY = false;
|
183 |
-
let ALREADY_STARTING = false;
|
184 |
-
|
185 |
-
//const PROXY_PORT = 4444;
|
186 |
-
//const TARGET_HOST = '0.0.0.0';
|
187 |
-
//const PROXY_TARGET = 'http://127.0.0.1:1688';
|
188 |
-
const VERSION = '0.0.3a';
|
189 |
-
|
190 |
-
|
191 |
-
// ------------
|
192 |
-
const http = require('http');
|
193 |
-
|
194 |
-
const PROXY_PORT = 4444; // Change this to a port different from 1688
|
195 |
-
const TARGET_HOST = '0.0.0.0';
|
196 |
-
const TARGET_PORT = 1688;
|
197 |
-
|
198 |
-
// Function to start the request forwarding server
|
199 |
-
async function startRequestForwardingServer()
|
200 |
-
{
|
201 |
-
|
202 |
-
console.log('Starting request forwarding server...');
|
203 |
-
const server = http.createServer((req, res) =>
|
204 |
-
{
|
205 |
-
const options = {
|
206 |
-
hostname: TARGET_HOST,
|
207 |
-
port: TARGET_PORT,
|
208 |
-
path: req.url,
|
209 |
-
method: req.method,
|
210 |
-
headers: req.headers,
|
211 |
-
};
|
212 |
-
|
213 |
-
const proxy = http.request(options, (proxyRes) =>
|
214 |
-
{
|
215 |
-
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
216 |
-
proxyRes.pipe(res, { end: true });
|
217 |
-
});
|
218 |
-
|
219 |
-
req.pipe(proxy, { end: true });
|
220 |
-
});
|
221 |
-
|
222 |
-
server.listen(PROXY_PORT, '0.0.0.0');
|
223 |
-
console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
|
224 |
-
}
|
225 |
-
|
226 |
-
// ------------
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
231 |
-
let omnitoolLogs = [];
|
232 |
-
|
233 |
-
async function startOmnitoolServer()
|
234 |
-
{
|
235 |
-
if (ALREADY_STARTING) return;
|
236 |
-
ALREADY_STARTING = true;
|
237 |
-
|
238 |
-
console.log('Starting Omnitool Server...');
|
239 |
-
return new Promise((resolve, reject) =>
|
240 |
-
{
|
241 |
-
const omnitoolStartProcess = spawn('./omnitool_start.sh');
|
242 |
-
|
243 |
-
omnitoolStartProcess.stdout.on('data', (data) =>
|
244 |
-
{
|
245 |
-
omnitoolLogs.push(data.toString());
|
246 |
-
console.log(`omnitool stdout: ${data}`);
|
247 |
-
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
248 |
-
{
|
249 |
-
OMNITOOL_READY = true;
|
250 |
-
console.log('Omnitool server started successfully');
|
251 |
-
resolve();
|
252 |
-
}
|
253 |
-
});
|
254 |
-
|
255 |
-
omnitoolStartProcess.stderr.on('data', (data) =>
|
256 |
-
{
|
257 |
-
console.error(`omnitool stderr: ${data}`);
|
258 |
-
});
|
259 |
-
|
260 |
-
omnitoolStartProcess.on('close', (code) =>
|
261 |
-
{
|
262 |
-
console.log(`Omnitool server process exited with code ${code}`);
|
263 |
-
if (!OMNITOOL_READY)
|
264 |
-
{
|
265 |
-
ALREADY_STARTING = false;
|
266 |
-
reject(`Omnitool server did not start within the timeout period.`);
|
267 |
-
}
|
268 |
-
});
|
269 |
-
});
|
270 |
-
}
|
271 |
-
|
272 |
-
|
273 |
-
fastify.get('/', async (request, reply) =>
|
274 |
-
{
|
275 |
-
const localUrl = request.headers['host'];
|
276 |
-
|
277 |
-
if (!OMNITOOL_READY)
|
278 |
-
{
|
279 |
-
let htmlContent = `
|
280 |
-
<html>
|
281 |
-
<head><title>Proxy Server</title></head>
|
282 |
-
<body>
|
283 |
-
<button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
|
284 |
-
<button id="exitIframeButton" onclick="window.open('http://${localUrl}', '_blank')" >GOTO ${localUrl}</button>
|
285 |
-
<div id="logs" style="white-space: pre-wrap;"></div>
|
286 |
-
<script>
|
287 |
-
function startServer() {
|
288 |
-
document.getElementById('startServerButton').disabled = true;
|
289 |
-
fetch('/start-omnitool-server')
|
290 |
-
.then(response => response.json())
|
291 |
-
.then(data => {
|
292 |
-
startLogPolling();
|
293 |
-
});
|
294 |
-
}
|
295 |
-
|
296 |
-
function startLogPolling() {
|
297 |
-
const interval = setInterval(() => {
|
298 |
-
fetch('/omnitool-logs')
|
299 |
-
.then(response => response.json())
|
300 |
-
.then(data => {
|
301 |
-
document.getElementById('logs').innerText = data.logs.join("\\n");
|
302 |
-
if (data.ready) {
|
303 |
-
document.getElementById('logs').innerText = "Omnitool is ready! Please open the following link in a new tab: https://${localUrl}";
|
304 |
-
clearInterval(interval);
|
305 |
-
//window.location.reload(); // Refresh the page when Omnitool is ready
|
306 |
-
}
|
307 |
-
});
|
308 |
-
}, 1000);
|
309 |
-
}
|
310 |
-
|
311 |
-
startLogPolling();
|
312 |
-
</script>
|
313 |
-
</body>
|
314 |
-
</html>
|
315 |
-
`;
|
316 |
-
reply.type('text/html').send(htmlContent);
|
317 |
-
} else
|
318 |
-
{
|
319 |
-
|
320 |
-
console.log('Proxying request as OMNITOOL is ready');
|
321 |
-
|
322 |
-
try
|
323 |
-
{
|
324 |
-
await startRequestForwardingServer();
|
325 |
-
} catch (error)
|
326 |
-
{
|
327 |
-
console.error('Failed to start servers:', error);
|
328 |
-
}
|
329 |
-
}
|
330 |
-
});
|
331 |
-
|
332 |
-
fastify.get('/omnitool-logs', async (request, reply) =>
|
333 |
-
{
|
334 |
-
reply.send({ logs: omnitoolLogs, ready: OMNITOOL_READY });
|
335 |
-
});
|
336 |
-
|
337 |
-
fastify.get('/start-omnitool-server', async (request, reply) =>
|
338 |
-
{
|
339 |
-
if (!OMNITOOL_READY)
|
340 |
-
{
|
341 |
-
if (ALREADY_STARTING)
|
342 |
-
{
|
343 |
-
return { message: "Omnitool server already starting" };
|
344 |
-
}
|
345 |
-
try
|
346 |
-
{
|
347 |
-
await startOmnitoolServer();
|
348 |
-
reply.send({ message: "Omnitool server started successfully" });
|
349 |
-
} catch (error)
|
350 |
-
{
|
351 |
-
console.error(error);
|
352 |
-
reply.send({ message: `Error starting Omnitool server: ${error}` });
|
353 |
-
}
|
354 |
-
} else
|
355 |
-
{
|
356 |
-
reply.send({ message: "Omnitool server already running" });
|
357 |
-
}
|
358 |
-
});
|
359 |
-
|
360 |
-
const start = async () =>
|
361 |
-
{
|
362 |
-
try
|
363 |
-
{
|
364 |
-
await fastify.listen({ port: PROXY_PORT, host: TARGET_HOST });
|
365 |
-
console.log(`Server is listening on http://${TARGET_HOST}:${PROXY_PORT}`);
|
366 |
-
} catch (err)
|
367 |
-
{
|
368 |
-
console.error(`Error starting server: ${err}`);
|
369 |
-
process.exit(1);
|
370 |
-
}
|
371 |
-
};
|
372 |
-
|
373 |
-
start();
|
374 |
-
*/
|
375 |
-
|
|
|
6 |
const TARGET_PORT = 1688;
|
7 |
let OMNITOOL_READY = false;
|
8 |
let ALREADY_STARTING = false;
|
9 |
+
|
10 |
const VERSION = '0.0.4a';
|
11 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
12 |
let omnitoolLogs = [];
|
|
|
27 |
console.log(`omnitool stdout: ${data}`);
|
28 |
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
29 |
{
|
|
|
30 |
console.log('Omnitool server started successfully');
|
31 |
+
setTimeout(() => {
|
32 |
+
OMNITOOL_READY = true;
|
33 |
+
resolve(); }, 1000); // Delay by 1 second
|
34 |
}
|
35 |
});
|
36 |
|
|
|
59 |
|
60 |
async function handleRoutes(req, res)
|
61 |
{
|
62 |
+
if ( !OMNITOOL_READY && (req.method === 'GET'))
|
63 |
{
|
|
|
64 |
switch (req.url)
|
65 |
{
|
66 |
case '/start-omnitool-server':
|
|
|
98 |
return;
|
99 |
}
|
100 |
|
101 |
+
case '/':
|
102 |
+
{
|
103 |
+
const localUrl = req.headers['host'];
|
104 |
+
let htmlContent = `
|
105 |
+
<html>
|
106 |
+
<head>
|
107 |
+
<title>Proxy Server</title>
|
108 |
+
<style>
|
109 |
+
#logContainer {
|
110 |
+
height: 400px; /* Fixed height */
|
111 |
+
overflow-y: scroll; /* Enable vertical scrolling */
|
112 |
+
background-color: black;
|
113 |
+
color: lime;
|
114 |
+
font-family: 'Courier New', Courier, monospace;
|
115 |
+
padding: 10px;
|
116 |
+
white-space: pre-wrap; /* Keep whitespaces */
|
117 |
+
border: 1px solid #ddd;
|
118 |
+
}
|
119 |
+
</style>
|
120 |
+
</head>
|
121 |
+
<body>
|
122 |
+
<button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
|
123 |
+
<button id="exitIframeButton" onclick="window.open('http://${localUrl}', '_blank')">GOTO ${localUrl}</button>
|
124 |
+
<div id="logContainer"></div>
|
125 |
+
<script>
|
126 |
+
function startServer() {
|
127 |
+
document.getElementById('startServerButton').disabled = true;
|
128 |
+
fetch('/start-omnitool-server')
|
129 |
.then(response => response.json())
|
130 |
.then(data => {
|
131 |
+
startLogPolling();
|
|
|
|
|
|
|
|
|
|
|
132 |
});
|
133 |
+
}
|
134 |
+
|
135 |
+
function startLogPolling() {
|
136 |
+
const interval = setInterval(() => {
|
137 |
+
fetch('/omnitool-logs')
|
138 |
+
.then(response => response.json())
|
139 |
+
.then(data => {
|
140 |
+
const logContainer = document.getElementById('logContainer');
|
141 |
+
logContainer.innerText = data.logs.join("\\n");
|
142 |
+
if (data.ready) {
|
143 |
+
logContainer.innerText = "Omnitool is ready! Please open the following link in a new tab: https://${localUrl}";
|
144 |
+
clearInterval(interval);
|
145 |
+
//window.location.reload(); // Optionally refresh the page when Omnitool is ready
|
146 |
+
}
|
147 |
+
scrollToBottom(logContainer);
|
148 |
+
});
|
149 |
+
}, 1000); // Adjusted to 1000ms for efficiency
|
150 |
+
}
|
151 |
+
|
152 |
+
function scrollToBottom(element) {
|
153 |
+
element.scrollTop = element.scrollHeight;
|
154 |
+
}
|
155 |
+
|
156 |
+
startLogPolling();
|
157 |
+
</script>
|
158 |
+
</body>
|
159 |
+
</html>`;
|
160 |
+
res.writeHead(200, { 'Content-Type': 'text/html' });
|
161 |
+
res.end(htmlContent);
|
162 |
+
return;
|
163 |
+
}
|
164 |
+
|
165 |
|
166 |
default:
|
167 |
console.log(`Doing nothing with this request: ${req.url}`);
|
|
|
195 |
// Start the servers
|
196 |
startManagementServer();
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|