Spaces:
Running
Running
Commit
·
db9a6dd
1
Parent(s):
41eddce
only creating proxy once
Browse files- myNodeServer.js +58 -28
myNodeServer.js
CHANGED
@@ -3,6 +3,7 @@
|
|
3 |
* All rights reserved.
|
4 |
*/
|
5 |
//@ts-check
|
|
|
6 |
|
7 |
const express = require('express');
|
8 |
const http = require('http');
|
@@ -14,7 +15,7 @@ const { createProxyMiddleware } = require('http-proxy-middleware');
|
|
14 |
const app = express();
|
15 |
|
16 |
const CONTAINER_HOST = '127.0.0.1';
|
17 |
-
|
18 |
const PROXY_PORT_OMNITOOL = 4444;
|
19 |
const CONTAINER_PORT_OMNITOOL = 1688;
|
20 |
|
@@ -28,10 +29,12 @@ global.ALREADY_STARTING = false;
|
|
28 |
global.LOCAL_URL = "";
|
29 |
global.PING_URL = "";
|
30 |
global.PROTOCOL = "";
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
const VERSION = '0.0.8a';
|
33 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
34 |
-
let omnitoolLogs = [];
|
35 |
|
36 |
// HTML templates and constants
|
37 |
const LOG_CONTAINER_HTML = `<div id="logContainer"></div>`;
|
@@ -69,10 +72,9 @@ async function startOmnitoolServer()
|
|
69 |
return new Promise((resolve, reject) =>
|
70 |
{
|
71 |
const omnitoolStartProcess = spawn('./omnitool_start.sh');
|
72 |
-
|
73 |
omnitoolStartProcess.stdout.on('data', (data) =>
|
74 |
{
|
75 |
-
|
76 |
console.log(`omnitool stdout: ${data}`);
|
77 |
|
78 |
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
@@ -86,14 +88,14 @@ async function startOmnitoolServer()
|
|
86 |
omnitoolStartProcess.stderr.on('data', (data) =>
|
87 |
{
|
88 |
console.error(`omnitool stderr: ${data}`);
|
89 |
-
|
90 |
});
|
91 |
|
92 |
omnitoolStartProcess.on('close', (code) =>
|
93 |
{
|
94 |
const message = `Omnitool server process exited with code: ${code}`;
|
95 |
console.log(message);
|
96 |
-
|
97 |
|
98 |
global.ALREADY_STARTING = false;
|
99 |
if (code === 0)
|
@@ -181,10 +183,11 @@ async function handleGetStartOmnitoolServer(req, res)
|
|
181 |
async function handleGetOmnitoolLogs(req, res)
|
182 |
{
|
183 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
184 |
-
const reply = { logs:
|
185 |
res.end(JSON.stringify(reply));
|
186 |
return;
|
187 |
}
|
|
|
188 |
async function handleGetBurstIframe(req, res)
|
189 |
{
|
190 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
@@ -197,6 +200,11 @@ async function handleGetBurstIframe(req, res)
|
|
197 |
|
198 |
async function proxyRequest(req, res)
|
199 |
{
|
|
|
|
|
|
|
|
|
|
|
200 |
// Proxy logic...
|
201 |
const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
|
202 |
const proxy = http.request(options, (proxyRes) =>
|
@@ -208,7 +216,7 @@ async function proxyRequest(req, res)
|
|
208 |
return;
|
209 |
}
|
210 |
|
211 |
-
function getButtonsString(buttonsHTML
|
212 |
{
|
213 |
|
214 |
return `
|
@@ -231,7 +239,7 @@ function getButtonsString(buttonsHTML, gotoUrl)
|
|
231 |
.then(response => response.json())
|
232 |
.then(data => startLogPolling());
|
233 |
|
234 |
-
|
235 |
}
|
236 |
|
237 |
function exitIframe() {
|
@@ -247,12 +255,12 @@ function getButtonsString(buttonsHTML, gotoUrl)
|
|
247 |
.then(response => response.json())
|
248 |
.then(data => {
|
249 |
const logContainer = document.getElementById('logContainer');
|
250 |
-
logContainer.innerText = data.logs.join("
|
251 |
if (data.ready) {
|
252 |
clearInterval(interval);
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
}
|
257 |
else {
|
258 |
scrollToBottom(logContainer);
|
@@ -264,7 +272,7 @@ function getButtonsString(buttonsHTML, gotoUrl)
|
|
264 |
function scrollToBottom(element) {
|
265 |
element.scrollTop = element.scrollHeight;
|
266 |
}
|
267 |
-
|
268 |
startLogPolling();
|
269 |
</script>
|
270 |
</body>
|
@@ -285,9 +293,14 @@ async function handleGetRoot(req, res)
|
|
285 |
if (!global.OMNITOOL_RUNNING)
|
286 |
{
|
287 |
console.log('Omnitool server is not running')
|
288 |
-
let startButtonClass = '
|
|
|
|
|
|
|
289 |
let buttonsHTML = `<button id="startServerButton" class="${startButtonClass}" onclick="startServer()">Start Omnitool Server</button>`;
|
290 |
-
|
|
|
|
|
291 |
res.writeHead(200, { 'Content-Type': 'text/html' });
|
292 |
res.end(html);
|
293 |
|
@@ -299,7 +312,7 @@ async function handleGetRoot(req, res)
|
|
299 |
console.log('First time visitor')
|
300 |
const gotoButtonClass = 'highlight-button';
|
301 |
const buttonsHTML = `<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>`;
|
302 |
-
const html = getButtonsString(buttonsHTML
|
303 |
res.writeHead(200, { 'Content-Type': 'text/html' });
|
304 |
res.end(html);
|
305 |
|
@@ -324,16 +337,13 @@ function setGlobals(req)
|
|
324 |
async function startServer()
|
325 |
{
|
326 |
// Start server
|
327 |
-
|
328 |
-
http.createServer(app).listen(PORT, () =>
|
329 |
{
|
330 |
-
console.log(`Server running on port ${
|
331 |
});
|
332 |
|
333 |
}
|
334 |
|
335 |
-
const OMNI_URL = 'http://127.0.0.1:1688'; // URL of the OMNITOOL service
|
336 |
-
|
337 |
function omnitoolProxyMiddleware(req, res, next) {
|
338 |
if (global.OMNITOOL_RUNNING && req.session.isVisited) {
|
339 |
// Proxy all requests to OMNITOOL when conditions are met
|
@@ -361,7 +371,16 @@ async function main(app)
|
|
361 |
resave: false,
|
362 |
saveUninitialized: true
|
363 |
}));
|
364 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
365 |
|
366 |
app.use(omnitoolProxyMiddleware);
|
367 |
|
@@ -376,13 +395,24 @@ async function main(app)
|
|
376 |
await checkOmnitoolStatus();
|
377 |
}, CHECK_OMNI_INTERVAL);
|
378 |
|
|
|
379 |
|
380 |
-
|
381 |
-
|
|
|
|
|
|
|
|
|
382 |
|
383 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
}
|
385 |
-
|
386 |
main(app);
|
387 |
|
388 |
|
|
|
3 |
* All rights reserved.
|
4 |
*/
|
5 |
//@ts-check
|
6 |
+
const VERSION = '0.0.8d';
|
7 |
|
8 |
const express = require('express');
|
9 |
const http = require('http');
|
|
|
15 |
const app = express();
|
16 |
|
17 |
const CONTAINER_HOST = '127.0.0.1';
|
18 |
+
const OMNI_URL = 'http://127.0.0.1:1688'; // URL of the OMNITOOL service
|
19 |
const PROXY_PORT_OMNITOOL = 4444;
|
20 |
const CONTAINER_PORT_OMNITOOL = 1688;
|
21 |
|
|
|
29 |
global.LOCAL_URL = "";
|
30 |
global.PING_URL = "";
|
31 |
global.PROTOCOL = "";
|
32 |
+
global.PROXY_STARTED = false;
|
33 |
+
global.logs = [];
|
34 |
+
global.OMNITOOL_PROXY = null;
|
35 |
+
|
36 |
|
|
|
37 |
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
|
|
|
38 |
|
39 |
// HTML templates and constants
|
40 |
const LOG_CONTAINER_HTML = `<div id="logContainer"></div>`;
|
|
|
72 |
return new Promise((resolve, reject) =>
|
73 |
{
|
74 |
const omnitoolStartProcess = spawn('./omnitool_start.sh');
|
|
|
75 |
omnitoolStartProcess.stdout.on('data', (data) =>
|
76 |
{
|
77 |
+
global.logs.push(data.toString());
|
78 |
console.log(`omnitool stdout: ${data}`);
|
79 |
|
80 |
if (data.toString().includes(`Server has started and is ready to accept connections`))
|
|
|
88 |
omnitoolStartProcess.stderr.on('data', (data) =>
|
89 |
{
|
90 |
console.error(`omnitool stderr: ${data}`);
|
91 |
+
global.logs.push(data.toString());
|
92 |
});
|
93 |
|
94 |
omnitoolStartProcess.on('close', (code) =>
|
95 |
{
|
96 |
const message = `Omnitool server process exited with code: ${code}`;
|
97 |
console.log(message);
|
98 |
+
global.logs.push(message);
|
99 |
|
100 |
global.ALREADY_STARTING = false;
|
101 |
if (code === 0)
|
|
|
183 |
async function handleGetOmnitoolLogs(req, res)
|
184 |
{
|
185 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
186 |
+
const reply = { logs: global.logs, ready:global.OMNITOOL_READY };
|
187 |
res.end(JSON.stringify(reply));
|
188 |
return;
|
189 |
}
|
190 |
+
|
191 |
async function handleGetBurstIframe(req, res)
|
192 |
{
|
193 |
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
|
200 |
|
201 |
async function proxyRequest(req, res)
|
202 |
{
|
203 |
+
|
204 |
+
console.log('Proxying request');
|
205 |
+
if (global.PROXY_STARTED) return;
|
206 |
+
global.PROXY_STARTED = true;
|
207 |
+
|
208 |
// Proxy logic...
|
209 |
const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
|
210 |
const proxy = http.request(options, (proxyRes) =>
|
|
|
216 |
return;
|
217 |
}
|
218 |
|
219 |
+
function getButtonsString(buttonsHTML)
|
220 |
{
|
221 |
|
222 |
return `
|
|
|
239 |
.then(response => response.json())
|
240 |
.then(data => startLogPolling());
|
241 |
|
242 |
+
|
243 |
}
|
244 |
|
245 |
function exitIframe() {
|
|
|
255 |
.then(response => response.json())
|
256 |
.then(data => {
|
257 |
const logContainer = document.getElementById('logContainer');
|
258 |
+
logContainer.innerText = data.logs.join("");
|
259 |
if (data.ready) {
|
260 |
clearInterval(interval);
|
261 |
+
document.getElementById('exitIframeButton').classList.add('highlight-button');
|
262 |
+
|
263 |
+
|
264 |
}
|
265 |
else {
|
266 |
scrollToBottom(logContainer);
|
|
|
272 |
function scrollToBottom(element) {
|
273 |
element.scrollTop = element.scrollHeight;
|
274 |
}
|
275 |
+
|
276 |
startLogPolling();
|
277 |
</script>
|
278 |
</body>
|
|
|
293 |
if (!global.OMNITOOL_RUNNING)
|
294 |
{
|
295 |
console.log('Omnitool server is not running')
|
296 |
+
let startButtonClass = '';
|
297 |
+
if (!global.ALREADY_STARTING) startButtonClass = 'highlight-button';
|
298 |
+
const gotoButtonClass = '';
|
299 |
+
|
300 |
let buttonsHTML = `<button id="startServerButton" class="${startButtonClass}" onclick="startServer()">Start Omnitool Server</button>`;
|
301 |
+
buttonsHTML += `<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>`;
|
302 |
+
|
303 |
+
const html = getButtonsString(buttonsHTML);
|
304 |
res.writeHead(200, { 'Content-Type': 'text/html' });
|
305 |
res.end(html);
|
306 |
|
|
|
312 |
console.log('First time visitor')
|
313 |
const gotoButtonClass = 'highlight-button';
|
314 |
const buttonsHTML = `<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>`;
|
315 |
+
const html = getButtonsString(buttonsHTML);
|
316 |
res.writeHead(200, { 'Content-Type': 'text/html' });
|
317 |
res.end(html);
|
318 |
|
|
|
337 |
async function startServer()
|
338 |
{
|
339 |
// Start server
|
340 |
+
http.createServer(app).listen(PROXY_PORT_OMNITOOL, () =>
|
|
|
341 |
{
|
342 |
+
console.log(`Server running on port ${PROXY_PORT_OMNITOOL}`);
|
343 |
});
|
344 |
|
345 |
}
|
346 |
|
|
|
|
|
347 |
function omnitoolProxyMiddleware(req, res, next) {
|
348 |
if (global.OMNITOOL_RUNNING && req.session.isVisited) {
|
349 |
// Proxy all requests to OMNITOOL when conditions are met
|
|
|
371 |
resave: false,
|
372 |
saveUninitialized: true
|
373 |
}));
|
374 |
+
try
|
375 |
+
{
|
376 |
+
await startServer();
|
377 |
+
}
|
378 |
+
catch (error)
|
379 |
+
{
|
380 |
+
console.error(`There was an error starting the server: ${error}`);
|
381 |
+
return;
|
382 |
+
}
|
383 |
+
|
384 |
|
385 |
app.use(omnitoolProxyMiddleware);
|
386 |
|
|
|
395 |
await checkOmnitoolStatus();
|
396 |
}, CHECK_OMNI_INTERVAL);
|
397 |
|
398 |
+
}
|
399 |
|
400 |
+
// Define the proxy middleware outside the function
|
401 |
+
global.OMNITOOL_PROXY = createProxyMiddleware({
|
402 |
+
target: OMNI_URL,
|
403 |
+
changeOrigin: true,
|
404 |
+
ws: true // if you need WebSocket support
|
405 |
+
});
|
406 |
|
407 |
+
function omnitoolProxyMiddleware(req, res, next) {
|
408 |
+
if (global.OMNITOOL_RUNNING && req.session.isVisited) {
|
409 |
+
// Use the predefined proxy middleware
|
410 |
+
return global.OMNITOOL_PROXY(req, res, next);
|
411 |
+
} else {
|
412 |
+
// Continue with normal processing for other cases
|
413 |
+
next();
|
414 |
+
}
|
415 |
}
|
|
|
416 |
main(app);
|
417 |
|
418 |
|