manu-sapiens commited on
Commit
a3113b3
·
1 Parent(s): 4b4847d

now detecting omnitool status via polling and requiring a single proxy on 4444 pointing at 1688

Browse files
Files changed (4) hide show
  1. Dockerfile +0 -1
  2. myNodeServer.js +246 -329
  3. package.json +2 -1
  4. yarn.lock +1 -0
Dockerfile CHANGED
@@ -25,7 +25,6 @@ COPY --chown=node . $HOME/app
25
  RUN yarn install
26
 
27
  EXPOSE 4444
28
- EXPOSE 1689
29
 
30
  #CMD ["bash", "run.sh"]
31
  CMD ["node", "myNodeServer.js"]
 
25
  RUN yarn install
26
 
27
  EXPOSE 4444
 
28
 
29
  #CMD ["bash", "run.sh"]
30
  CMD ["node", "myNodeServer.js"]
myNodeServer.js CHANGED
@@ -1,19 +1,119 @@
1
  const http = require('http');
2
  const { spawn } = require('child_process');
3
 
4
- const PROXY_PORT_HUGGINGFACE = 4444; // Change this to a port different from 1688
5
- const PROXY_PORT_OMNITOOL = 1689; // Change this to a port different from 1688
6
  const CONTAINER_HOST = '127.0.0.1';
7
  const CONTAINER_PORT_OMNITOOL = 1688;
8
- const PROTOCOL = 'https';
9
  let OMNITOOL_READY = false;
10
  let OMNITOOL_PRE_READY = false;
11
  let ALREADY_STARTING = false;
 
 
12
 
13
- const VERSION = '0.0.5';
14
  console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
15
  let omnitoolLogs = [];
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  async function startOmnitoolServer()
18
  {
19
  if (ALREADY_STARTING) return;
@@ -28,23 +128,12 @@ async function startOmnitoolServer()
28
  {
29
  omnitoolLogs.push(data.toString());
30
  console.log(`omnitool stdout: ${data}`);
31
- if (data.toString().includes(`Server has started and is ready to accept connections`))
32
- {
33
- console.log('Omnitool server started successfully');
34
- OMNITOOL_PRE_READY = true;
35
- setTimeout(() =>
36
- {
37
- OMNITOOL_READY = true;
38
- resolve();
39
- }, 1000); // Delay by 1 second
40
- }
41
  });
42
 
43
  omnitoolStartProcess.stderr.on('data', (data) =>
44
  {
45
  console.error(`omnitool stderr: ${data}`);
46
  omnitoolLogs.push(data.toString());
47
-
48
  });
49
 
50
  omnitoolStartProcess.on('close', (code) =>
@@ -53,370 +142,198 @@ async function startOmnitoolServer()
53
  console.log(message);
54
  omnitoolLogs.push(message);
55
 
56
- if (!OMNITOOL_READY)
 
 
 
 
57
  {
58
- ALREADY_STARTING = false;
59
- reject(`Omnitool server did not start within the timeout period.`);
60
  }
61
  });
62
  });
63
  }
64
 
65
- async function startRequestForwardingServer(proxy_port)
66
  {
67
- const server = http.createServer((req, res) => handleRoutes(req, res, proxy_port));
68
- server.listen(proxy_port, '0.0.0.0');
69
- console.log(`Request forwarding server listening on port ${proxy_port}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  }
71
 
72
- async function handleRoutes(req, res, proxy_port)
 
73
  {
74
- const localUrl = req.headers['host'];
75
- let htmlContent = '';
76
- const hostname = localUrl.split(':')[0];
77
- const hostport = localUrl.split(':')[1] || PROXY_PORT_HUGGINGFACE;
78
- const newUrl = `${PROTOCOL}://${hostname}:${PROXY_PORT_OMNITOOL}`;
79
 
80
- //console.log(`hostname = ${hostname}, hostport = ${hostport}, newUrl = ${newUrl}`);
81
 
82
- if (!OMNITOOL_READY)
83
  {
84
- if (req.method === 'GET')
85
- {
86
-
 
 
 
87
 
88
- switch (req.url)
 
 
89
  {
90
- case '/start-omnitool-server':
91
- {
92
-
93
- console.log(`Omnitool Server:ALREADY_STARTING = ${ALREADY_STARTING}`);
94
- if (ALREADY_STARTING)
95
- {
96
- res.writeHead(200, { 'Content-Type': 'text/html' });
97
- res.end("Omnitool server already starting");
98
- return;
99
- }
100
- try
101
- {
102
- await startOmnitoolServer();
103
- res.writeHead(200, { 'Content-Type': 'text/html' });
104
- res.end("Omnitool server started successfully");
105
- }
106
- catch (error)
107
- {
108
- console.error(error);
109
- ALREADY_STARTING = false;
110
- res.writeHead(500, { 'Content-Type': 'text/html' });
111
- res.end(`Error starting Omnitool server: ${error}`);
112
- }
113
 
114
- return;
115
- }
 
 
 
 
 
116
 
117
- case '/omnitool-logs':
118
- {
119
- res.writeHead(200, { 'Content-Type': 'application/json' });
120
- const reply = { logs: omnitoolLogs, ready: OMNITOOL_PRE_READY };
121
- res.end(JSON.stringify(reply));
122
- return;
123
- }
124
 
125
- case '/':
126
- {
127
 
128
- if (hostport === PROXY_PORT_OMNITOOL.toString())
129
- {
130
- console.log(`Serving the page with START OMNITOOL SERVER button`);
131
- htmlContent = `
132
- <html>
133
- <head>
134
- <title>Proxy Server</title>
135
- <style>
136
- #logContainer {
137
- height: 400px; /* Fixed height */
138
- overflow-y: scroll; /* Enable vertical scrolling */
139
- background-color: black;
140
- color: lime;
141
- font-family: 'Courier New', Courier, monospace;
142
- padding: 10px;
143
- white-space: pre-wrap; /* Keep whitespaces */
144
- border: 1px solid #ddd;
145
- }
146
- </style>
147
- </head>
148
- <body>
149
- <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
150
- <div id="logContainer"></div>
151
- <script>
152
-
153
- function startServer() {
154
- document.getElementById('startServerButton').disabled = true;
155
- fetch('/start-omnitool-server')
156
- .then(response => response.json())
157
- .then(data => {
158
- startLogPolling();
159
- });
160
- }
161
-
162
- function startLogPolling() {
163
- const interval = setInterval(() => {
164
- fetch('/omnitool-logs')
165
- .then(response => response.json())
166
- .then(data => {
167
- const logContainer = document.getElementById('logContainer');
168
- logContainer.innerText = data.logs.join("");
169
- if (data.ready) {
170
- data.logs.push("Omnitool is ready! REFRESHING PAGE...");
171
- logContainer.innerText = data.logs.join("");
172
- clearInterval(interval);
173
- setTimeout(function() {
174
- window.location.reload(1);
175
- }, 1500); // Refreshes after 1.5 seconds
176
- }
177
- scrollToBottom(logContainer);
178
- });
179
- }, 500); // Adjust to 500 for efficiency
180
- }
181
-
182
- function scrollToBottom(element) {
183
- element.scrollTop = element.scrollHeight;
184
- }
185
-
186
- startLogPolling();
187
- </script>
188
- </body>
189
- </html>`;
190
- }
191
- else
192
- {
193
- console.log(`Serving the page with START OMNITOOL SERVER button and GOTO ${newUrl} button`);
194
- htmlContent = `
195
- <html>
196
- <head>
197
- <title>Proxy Server</title>
198
- <style>
199
- #logContainer {
200
- height: 400px; /* Fixed height */
201
- overflow-y: scroll; /* Enable vertical scrolling */
202
- background-color: black;
203
- color: lime;
204
- font-family: 'Courier New', Courier, monospace;
205
- padding: 10px;
206
- white-space: pre-wrap; /* Keep whitespaces */
207
- border: 1px solid #ddd;
208
- }
209
- .highlight-button {
210
- animation: pulseAnimation 1s infinite;
211
- background-color: yellow;
212
- color: black;
213
- font-weight: bold;
214
- }
215
- @keyframes pulseAnimation {
216
- 0% { transform: scale(1); }
217
- 50% { transform: scale(1.1); }
218
- 100% { transform: scale(1); }
219
- }
220
- </style>
221
- </head>
222
- <body>
223
- <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
224
- <button id="exitIframeButton" onclick="exitIframe()">GOTO ${newUrl}</button>
225
- <div id="logContainer"></div>
226
- <script>
227
- function exitIframe() {
228
- window.open('${newUrl}', '_blank');
229
- }
230
-
231
- function startServer() {
232
- document.getElementById('startServerButton').disabled = true;
233
- fetch('/start-omnitool-server')
234
- .then(response => response.json())
235
- .then(data => {
236
- startLogPolling();
237
- });
238
- }
239
-
240
- function highlightExitButton() {
241
- const exitButton = document.getElementById('exitIframeButton');
242
- exitButton.classList.add('highlight-button');
243
- }
244
-
245
- function startLogPolling() {
246
- const interval = setInterval(() => {
247
- fetch('/omnitool-logs')
248
- .then(response => response.json())
249
- .then(data => {
250
- const logContainer = document.getElementById('logContainer');
251
- logContainer.innerText = data.logs.join("");
252
- if (data.ready) {
253
- data.logs.push("Omnitool is ready! REFRESHING PAGE...");
254
- logContainer.innerText = data.logs.join("");
255
- clearInterval(interval);
256
- highlightExitButton();
257
- }
258
- scrollToBottom(logContainer);
259
- });
260
- }, 250); // Adjust to 500 for efficiency
261
- }
262
-
263
- function scrollToBottom(element) {
264
- element.scrollTop = element.scrollHeight;
265
- }
266
-
267
- startLogPolling();
268
- </script>
269
- </body>
270
- </html>`;
271
 
272
- }
273
- res.writeHead(200, { 'Content-Type': 'text/html' });
274
- res.end(htmlContent);
275
- return;
276
 
277
- }
278
 
 
 
 
 
279
 
280
- default:
281
- console.log(`Doing nothing with this request: ${req.url}`);
282
- res.writeHead(200, { 'Content-Type': 'text/html' });
283
- res.end(`
284
- <html>
285
- <head>
286
- <title>Page Refresh</title>
287
- <script>
288
- setTimeout(function() {
289
- window.location.reload(1);
290
- }, 5000); // Refreshes after 5 seconds
291
- </script>
292
- </head>
293
- <body>
294
- <p>Doing nothing with this request: ${req.url}</p>
295
- <p>The page will refresh automatically in 5 seconds.</p>
296
- </body>
297
- </html>`);
298
- return;
299
 
300
- }
301
- }
 
 
 
 
 
 
 
 
302
 
303
- // we are dropping anything that isn't a GET request
304
- res.writeHead(200, { 'Content-Type': 'text/html' });
305
- res.end(`
306
- <html>
307
- <head>
308
- <title>Page Refresh</title>
309
- <script>
310
- setTimeout(function() {
311
- window.location.reload(1);
312
- }, 1000); // Refreshes after 1 seconds
313
- </script>
314
- </head>
315
- <body>
316
- <p>Doing nothing with this request: ${req.url}</p>
317
- <p>The page will refresh automatically in 1 seconds.</p>
318
- </body>
319
- </html>`);
320
- return;
321
- }
322
- if (OMNITOOL_READY)
323
- {
324
 
325
- if (proxy_port === PROXY_PORT_HUGGINGFACE)
326
- {
327
- htmlContent = `
328
- <html>
329
- <head>
330
- <title>Proxy Server</title>
331
- <style>
332
- #logContainer {
333
- height: 400px; /* Fixed height */
334
- overflow-y: scroll; /* Enable vertical scrolling */
335
- background-color: black;
336
- color: lime;
337
- font-family: 'Courier New', Courier, monospace;
338
- padding: 10px;
339
- white-space: pre-wrap; /* Keep whitespaces */
340
- border: 1px solid #ddd;
341
- }
342
- .highlight-button {
343
- animation: pulseAnimation 1s infinite;
344
- background-color: yellow;
345
- color: black;
346
- font-weight: bold;
347
- }
348
- @keyframes pulseAnimation {
349
- 0% { transform: scale(1); }
350
- 50% { transform: scale(1.1); }
351
- 100% { transform: scale(1); }
352
- }
353
- </style>
354
- </head>
355
- <body>
356
- <button id="exitIframeButton" onclick="exitIframe()">GOTO ${newUrl}</button>
357
- <div id="logContainer"></div>
358
- <script>
359
- function exitIframe() {
360
- window.open('${newUrl}', '_blank');
361
  }
362
-
363
- function highlightExitButton() {
364
- const exitButton = document.getElementById('exitIframeButton');
365
- exitButton.classList.add('highlight-button');
 
 
 
366
  }
367
-
368
- highlightExitButton();
369
- element.scrollTop = element.scrollHeight;
370
-
371
- </script>
372
- </body>
373
- </html>`;
374
- res.writeHead(200, { 'Content-Type': 'text/html' });
375
- res.end(htmlContent);
376
- return;
377
- }
378
 
 
 
 
 
 
 
379
 
380
- if (proxy_port === PROXY_PORT_OMNITOOL)
 
 
 
 
 
 
 
 
 
 
 
 
 
381
  {
382
  // Proxy logic...
383
- const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
384
  const proxy = http.request(options, (proxyRes) =>
385
  {
386
  res.writeHead(proxyRes.statusCode, proxyRes.headers);
387
  proxyRes.pipe(res, { end: true });
388
  });
389
  req.pipe(proxy, { end: true });
390
- return;
391
  }
392
- }
393
- /*
394
- // Proxy logic...
395
- const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
396
- const proxy = http.request(options, (proxyRes) =>
397
- {
398
- res.writeHead(proxyRes.statusCode, proxyRes.headers);
399
- proxyRes.pipe(res, { end: true });
400
  });
401
- req.pipe(proxy, { end: true });
402
- */
403
- return;
404
  }
405
 
406
- // Main function to start everything
407
  async function startManagementServer()
408
  {
409
  try
410
  {
411
- await startRequestForwardingServer(PROXY_PORT_HUGGINGFACE);
412
  await startRequestForwardingServer(PROXY_PORT_OMNITOOL);
 
 
 
 
 
413
  } catch (error)
414
  {
415
  console.error('Failed to start servers:', error);
416
  }
417
  }
418
 
419
- // main
420
- // Start the servers
421
- startManagementServer();
422
-
 
1
  const http = require('http');
2
  const { spawn } = require('child_process');
3
 
4
+ const PROXY_PORT_OMNITOOL = 4444;
 
5
  const CONTAINER_HOST = '127.0.0.1';
6
  const CONTAINER_PORT_OMNITOOL = 1688;
7
+ const PROTOCOL = 'http';
8
  let OMNITOOL_READY = false;
9
  let OMNITOOL_PRE_READY = false;
10
  let ALREADY_STARTING = false;
11
+ let IFRAME = false;
12
+ const OMNITOOL_STATUS_CHECK_INTERVAL = 2000; // 2 seconds
13
 
14
+ const VERSION = '0.0.7a';
15
  console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
16
  let omnitoolLogs = [];
17
 
18
+ // HTML templates and constants
19
+ const START_SERVER_BUTTON_HTML = `<button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>`;
20
+ const GOTO_OMNITOOL_BUTTON_HTML = `<button id="exitIframeButton" onclick="exitIframe()">GOTO OMNITOOL</button>`;
21
+ const LOG_CONTAINER_HTML = `<div id="logContainer"></div>`;
22
+ const COMMON_STYLES = `
23
+ <style>
24
+ #logContainer {
25
+ height: 400px;
26
+ overflow-y: scroll;
27
+ background-color: black;
28
+ color: lime;
29
+ font-family: 'Courier New', Courier, monospace;
30
+ padding: 10px;
31
+ white-space: pre-wrap;
32
+ border: 1px solid #ddd;
33
+ }
34
+ .highlight-button {
35
+ animation: pulseAnimation 1s infinite;
36
+ background-color: yellow;
37
+ color: black;
38
+ font-weight: bold;
39
+ }
40
+ @keyframes pulseAnimation {
41
+ 0% { transform: scale(1); }
42
+ 50% { transform: scale(1.1); }
43
+ 100% { transform: scale(1); }
44
+ }
45
+ </style>`;
46
+
47
+ // Function to generate HTML content based on server status
48
+ function generateHTMLContent(newUrl) {
49
+ let startButtonDisabled = OMNITOOL_PRE_READY || OMNITOOL_READY || ALREADY_STARTING ? 'disabled' : '';
50
+ let startButtonClass = !startButtonDisabled ? 'highlight-button' : '';
51
+ let gotoButtonClass = IFRAME ? 'highlight-button' : '';
52
+
53
+ // set the startButton to disabled if OMNITOOL_PRE_READY or OMNITOOL_READY or ALREADY_STARTING are true
54
+ let buttonsHTML = `<button id="startServerButton" class="${startButtonClass}" onclick="startServer()" ${startButtonDisabled}>Start Omnitool Server</button>`;
55
+
56
+ if (IFRAME)
57
+ {
58
+ buttonsHTML += `<button id="exitIframeButton" class="${gotoButtonClass}" onclick="exitIframe()">GOTO OMNITOOL</button>`;
59
+ }
60
+
61
+ return `
62
+ <html>
63
+ <head>
64
+ <title>Proxy Server</title>
65
+ ${COMMON_STYLES}
66
+ </head>
67
+ <body>
68
+ ${buttonsHTML}
69
+ ${LOG_CONTAINER_HTML}
70
+ <script>
71
+ function startServer() {
72
+ document.getElementById('startServerButton').classList.remove
73
+ ('highlight-button');
74
+ document.getElementById('startServerButton').disabled = true;
75
+
76
+
77
+ fetch('/start-omnitool-server')
78
+ .then(response => response.json())
79
+ .then(data => startLogPolling());
80
+
81
+
82
+ }
83
+
84
+ function exitIframe() {
85
+ window.open('${newUrl}', '_blank');
86
+ }
87
+
88
+ function startLogPolling() {
89
+
90
+ const interval = setInterval(() => {
91
+ fetch('/omnitool-logs')
92
+ .then(response => response.json())
93
+ .then(data => {
94
+ const logContainer = document.getElementById('logContainer');
95
+ logContainer.innerText = data.logs.join("\\n");
96
+ if (data.ready) {
97
+ clearInterval(interval);
98
+ }
99
+ else {
100
+ scrollToBottom(logContainer);
101
+ }
102
+ });
103
+ }, 500);
104
+ }
105
+
106
+ function scrollToBottom(element) {
107
+ element.scrollTop = element.scrollHeight;
108
+ }
109
+
110
+ startLogPolling();
111
+ </script>
112
+ </body>
113
+ </html>`;
114
+ }
115
+
116
+
117
  async function startOmnitoolServer()
118
  {
119
  if (ALREADY_STARTING) return;
 
128
  {
129
  omnitoolLogs.push(data.toString());
130
  console.log(`omnitool stdout: ${data}`);
 
 
 
 
 
 
 
 
 
 
131
  });
132
 
133
  omnitoolStartProcess.stderr.on('data', (data) =>
134
  {
135
  console.error(`omnitool stderr: ${data}`);
136
  omnitoolLogs.push(data.toString());
 
137
  });
138
 
139
  omnitoolStartProcess.on('close', (code) =>
 
142
  console.log(message);
143
  omnitoolLogs.push(message);
144
 
145
+ ALREADY_STARTING = false;
146
+ if (code === 0)
147
+ {
148
+ resolve();
149
+ } else
150
  {
151
+ reject(`Omnitool server did not start properly.`);
 
152
  }
153
  });
154
  });
155
  }
156
 
157
+ function setOmnitoolReady()
158
  {
159
+ if (OMNITOOL_PRE_READY === false)
160
+ {
161
+ OMNITOOL_PRE_READY = true;
162
+ console.log('Omnitool server is (PRE) ready! ');
163
+ setTimeout(() =>
164
+ {
165
+ console.log('Omnitool server is (NOW) ready! ');
166
+ OMNITOOL_READY = true;
167
+ }, 2000); // Delay by 2 second
168
+ }
169
+ }
170
+
171
+ async function periodicCheckOmnitoolStatus() {
172
+ console.log("Starting async operation...");
173
+ try
174
+ {
175
+ await checkOmnitoolStatus();
176
+ }
177
+ catch (error)
178
+ {
179
+ console.log(`Omnitool not ready: ${error} (IFRAME = ${IFRAME}`);
180
+ OMNITOOL_READY = false;
181
+ OMNITOOL_PRE_READY = false;
182
+ }
183
+ // Schedule the next execution
184
+ setTimeout(periodicCheckOmnitoolStatus, OMNITOOL_STATUS_CHECK_INTERVAL);
185
  }
186
 
187
+
188
+ async function checkOmnitoolStatus()
189
  {
 
 
 
 
 
190
 
191
+ console.log(`BEFORE: Checking Omnitool status: PRE_READY = ${OMNITOOL_PRE_READY}, READY = ${OMNITOOL_READY}, IFRAME = ${IFRAME}, ALREADY_STARTING = ${ALREADY_STARTING}`);
192
 
193
+ return new Promise((resolve, reject) =>
194
  {
195
+ const options = {
196
+ hostname: CONTAINER_HOST,
197
+ port: CONTAINER_PORT_OMNITOOL,
198
+ path: '/',
199
+ method: 'GET'
200
+ };
201
 
202
+ const req = http.request(options, (res) =>
203
+ {
204
+ if (res.statusCode === 200)
205
  {
206
+ IFRAME = false;
207
+ setOmnitoolReady();
208
+ }
209
+ else
210
+ {
211
+ OMNITOOL_READY = false;
212
+ OMNITOOL_PRE_READY = false;
213
+ }
214
+ resolve();
215
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
217
+ req.on('error', (error) =>
218
+ {
219
+ if (error.code === 'ECONNREFUSED')
220
+ {
221
+ IFRAME = true;
222
+ }
223
+ });
224
 
225
+ req.end();
226
+ });
 
 
 
 
 
227
 
 
 
228
 
229
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
 
 
 
 
231
 
 
232
 
233
+ async function startRequestForwardingServer(proxy_port)
234
+ {
235
+ const server = http.createServer(async (req, res) =>
236
+ {
237
 
238
+ console.log("------------------");
239
+ const localUrl = req.headers['host'];
240
+ const hostname = localUrl.split(':')[0];
241
+ const newUrl = `${PROTOCOL}://${hostname}:${PROXY_PORT_OMNITOOL}`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
+ if (!OMNITOOL_READY || IFRAME)
244
+ {
245
+ // Handle GET requests here
246
+ console.log(`Omnitool Server:OMNITOOL_READY = ${OMNITOOL_READY}`);
247
+ if (req.method === 'GET')
248
+ {
249
+ switch (req.url)
250
+ {
251
+ case '/start-omnitool-server':
252
+ {
253
 
254
+ console.log(`Omnitool Server:ALREADY_STARTING = ${ALREADY_STARTING}`);
255
+ if (ALREADY_STARTING)
256
+ {
257
+ res.writeHead(200, { 'Content-Type': 'text/html' });
258
+ res.end("Omnitool server already starting");
259
+ return;
260
+ }
261
+ try
262
+ {
263
+ await startOmnitoolServer();
264
+ res.writeHead(200, { 'Content-Type': 'text/html' });
265
+ res.end("Omnitool server started successfully");
266
+ }
267
+ catch (error)
268
+ {
269
+ console.error(error);
270
+ ALREADY_STARTING = false;
271
+ res.writeHead(500, { 'Content-Type': 'text/html' });
272
+ res.end(`Error starting Omnitool server: ${error}`);
273
+ }
 
274
 
275
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  }
277
+
278
+ case '/omnitool-logs':
279
+ {
280
+ res.writeHead(200, { 'Content-Type': 'application/json' });
281
+ const reply = { logs: omnitoolLogs, ready: OMNITOOL_PRE_READY };
282
+ res.end(JSON.stringify(reply));
283
+ return;
284
  }
 
 
 
 
 
 
 
 
 
 
 
285
 
286
+ case '/':
287
+ {
288
+ res.writeHead(200, { 'Content-Type': 'text/html' });
289
+ res.end(generateHTMLContent(newUrl));
290
+ return;
291
+ }
292
 
293
+ default:
294
+ res.writeHead(404, { 'Content-Type': 'text/html' });
295
+ res.end(`<html><body><h1>404 Not Found</h1><p>The requested URL ${req.url} was not found on this server.</p></body></html>`);
296
+ return;
297
+ }
298
+ }
299
+ else
300
+ {
301
+ // Handle non-GET requests here
302
+ res.writeHead(405, { 'Content-Type': 'text/html' });
303
+ res.end(`<html><body><h1>405 Method Not Allowed</h1><p>The request method ${req.method} is not allowed for the URL ${req.url}.</p></body></html>`);
304
+ }
305
+ }
306
+ else
307
  {
308
  // Proxy logic...
309
+ const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers };
310
  const proxy = http.request(options, (proxyRes) =>
311
  {
312
  res.writeHead(proxyRes.statusCode, proxyRes.headers);
313
  proxyRes.pipe(res, { end: true });
314
  });
315
  req.pipe(proxy, { end: true });
 
316
  }
 
 
 
 
 
 
 
 
317
  });
318
+
319
+ server.listen(proxy_port, '0.0.0.0');
320
+ console.log(`Request forwarding server listening on port ${proxy_port}`);
321
  }
322
 
 
323
  async function startManagementServer()
324
  {
325
  try
326
  {
 
327
  await startRequestForwardingServer(PROXY_PORT_OMNITOOL);
328
+
329
+ // Set the interval in milliseconds (e.g., 2000 milliseconds = 2 seconds)
330
+
331
+ // Use setInterval to schedule the function
332
+ setInterval(checkOmnitoolStatus, OMNITOOL_STATUS_CHECK_INTERVAL);
333
  } catch (error)
334
  {
335
  console.error('Failed to start servers:', error);
336
  }
337
  }
338
 
339
+ startManagementServer();
 
 
 
package.json CHANGED
@@ -56,7 +56,8 @@
56
  "compare-versions": "^6.1.0",
57
  "cross-env": "^7.0.3",
58
  "fs-extra": "^11.1.1",
59
- "http-proxy-middleware": "^2.0.6"
 
60
  },
61
  "devDependencies": {
62
  "eslint": "^8.53.0",
 
56
  "compare-versions": "^6.1.0",
57
  "cross-env": "^7.0.3",
58
  "fs-extra": "^11.1.1",
59
+ "http-proxy-middleware": "^2.0.6",
60
+ "node-fetch": "^3.3.2"
61
  },
62
  "devDependencies": {
63
  "eslint": "^8.53.0",
yarn.lock CHANGED
@@ -10218,6 +10218,7 @@ __metadata:
10218
  eslint-plugin-prettier: ^5.0.1
10219
  fs-extra: ^11.1.1
10220
  http-proxy-middleware: ^2.0.6
 
10221
  prettier: ^3.0.3
10222
  dependenciesMeta:
10223
 
10218
  eslint-plugin-prettier: ^5.0.1
10219
  fs-extra: ^11.1.1
10220
  http-proxy-middleware: ^2.0.6
10221
+ node-fetch: ^3.3.2
10222
  prettier: ^3.0.3
10223
  dependenciesMeta:
10224