manu-sapiens commited on
Commit
bb5f1a6
·
1 Parent(s): 82641f8

now using two proxies, one on 1688 and one on 4444, with the understanding that 1688 is outside the iFrame while 4444 may or may not be in the iFrame

Browse files
Files changed (1) hide show
  1. myNodeServer.js +317 -137
myNodeServer.js CHANGED
@@ -1,14 +1,16 @@
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 OMNITOOL_PRE_READY = false;
9
  let ALREADY_STARTING = false;
10
 
11
- const VERSION = '0.0.4a';
12
  console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
13
  let omnitoolLogs = [];
14
 
@@ -55,165 +57,342 @@ async function startOmnitoolServer()
55
  });
56
  }
57
 
58
- async function startRequestForwardingServer()
59
  {
60
- const server = http.createServer((req, res) => handleRoutes(req, res));
61
- server.listen(PROXY_PORT, '0.0.0.0');
62
- console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
63
  }
64
 
65
- async function handleRoutes(req, res)
66
  {
67
- if (!OMNITOOL_READY && (req.method === 'GET'))
 
 
 
 
 
 
 
 
68
  {
69
- switch (req.url)
70
  {
71
- case '/start-omnitool-server':
72
- {
73
 
74
- console.log(`Omnitool Server:ALREADY_STARTING = ${ALREADY_STARTING}`);
75
- if (ALREADY_STARTING)
 
76
  {
77
- res.writeHead(200, { 'Content-Type': 'text/html' });
78
- res.end("Omnitool server already starting");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  return;
80
  }
81
- try
 
82
  {
83
- await startOmnitoolServer();
84
- res.writeHead(200, { 'Content-Type': 'text/html' });
85
- res.end("Omnitool server started successfully");
 
86
  }
87
- catch (error)
 
88
  {
89
- console.error(error);
90
- ALREADY_STARTING = false;
91
- res.writeHead(500, { 'Content-Type': 'text/html' });
92
- res.end(`Error starting Omnitool server: ${error}`);
93
- }
94
 
95
- return;
96
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
- case '/omnitool-logs':
99
- {
100
- res.writeHead(200, { 'Content-Type': 'application/json' });
101
- const reply = { logs: omnitoolLogs, ready: OMNITOOL_PRE_READY };
102
- res.end(JSON.stringify(reply));
103
- return;
104
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- case '/':
107
- {
108
- const localUrl = req.headers['host'];
109
- let htmlContent = `
110
- <html>
111
- <head>
112
- <title>Proxy Server</title>
113
- <style>
114
- #logContainer {
115
- height: 400px; /* Fixed height */
116
- overflow-y: scroll; /* Enable vertical scrolling */
117
- background-color: black;
118
- color: lime;
119
- font-family: 'Courier New', Courier, monospace;
120
- padding: 10px;
121
- white-space: pre-wrap; /* Keep whitespaces */
122
- border: 1px solid #ddd;
123
- }
124
- .highlight-button {
125
- animation: pulseAnimation 1s infinite;
126
- background-color: yellow;
127
- color: black;
128
- font-weight: bold;
129
- }
130
- @keyframes pulseAnimation {
131
- 0% { transform: scale(1); }
132
- 50% { transform: scale(1.1); }
133
- 100% { transform: scale(1); }
134
- }
135
- </style>
136
- </style>
137
- </head>
138
- <body>
139
- <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
140
- <button id="exitIframeButton" onclick="window.open('http://${localUrl}', '_blank')">GOTO ${localUrl}</button>
141
- <div id="logContainer"></div>
142
- <script>
143
- function startServer() {
144
- document.getElementById('startServerButton').disabled = true;
145
- fetch('/start-omnitool-server')
146
- .then(response => response.json())
147
- .then(data => {
148
- startLogPolling();
149
- });
150
- }
151
-
152
- function highlightExitButton() {
153
- const exitButton = document.getElementById('exitIframeButton');
154
- exitButton.classList.add('highlight-button');
155
- }
156
-
157
- function startLogPolling() {
158
- const interval = setInterval(() => {
159
- fetch('/omnitool-logs')
160
- .then(response => response.json())
161
- .then(data => {
162
- const logContainer = document.getElementById('logContainer');
163
- logContainer.innerText = data.logs.join("\\n");
164
- if (data.ready) {
165
- logContainer.innerText = "Omnitool is ready! Please open the following link in a new tab: https://${localUrl}";
166
- clearInterval(interval);
167
- highlightExitButton();
168
- }
169
- scrollToBottom(logContainer);
170
- });
171
- }, 1000); // Adjusted to 1000ms for efficiency
172
- }
173
-
174
- function scrollToBottom(element) {
175
- element.scrollTop = element.scrollHeight;
176
- }
177
-
178
- startLogPolling();
179
- </script>
180
- </body>
181
- </html>`;
182
  res.writeHead(200, { 'Content-Type': 'text/html' });
183
- res.end(htmlContent);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  return;
185
- }
186
-
187
-
188
- default:
189
- console.log(`Doing nothing with this request: ${req.url}`);
190
- res.writeHead(200, { 'Content-Type': 'text/html' });
191
- res.end(`
192
- <html>
193
- <head>
194
- <title>Page Refresh</title>
195
- <script>
196
- setTimeout(function() {
197
- window.location.reload(1);
198
- }, 5000); // Refreshes after 5 seconds
199
- </script>
200
- </head>
201
- <body>
202
- <p>Doing nothing with this request: ${req.url}</p>
203
- <p>The page will refresh automatically in 5 seconds.</p>
204
- </body>
205
- </html>`);
206
- return;
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  }
209
  }
210
- const options = { hostname: TARGET_HOST, port: TARGET_PORT, path: req.url, method: req.method, headers: req.headers, };
 
 
211
  const proxy = http.request(options, (proxyRes) =>
212
  {
213
  res.writeHead(proxyRes.statusCode, proxyRes.headers);
214
  proxyRes.pipe(res, { end: true });
215
  });
216
  req.pipe(proxy, { end: true });
 
217
  return;
218
  }
219
 
@@ -222,7 +401,8 @@ async function startManagementServer()
222
  {
223
  try
224
  {
225
- await startRequestForwardingServer();
 
226
  } catch (error)
227
  {
228
  console.error('Failed to start servers:', error);
 
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 = 1688; // 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
 
 
57
  });
58
  }
59
 
60
+ async function startRequestForwardingServer(proxy_port)
61
  {
62
+ const server = http.createServer((req, res) => handleRoutes(req, res, proxy_port));
63
+ server.listen(proxy_port, '0.0.0.0');
64
+ console.log(`Request forwarding server listening on port ${proxy_port}`);
65
  }
66
 
67
+ async function handleRoutes(req, res, proxy_port)
68
  {
69
+ const localUrl = req.headers['host'];
70
+ let htmlContent = '';
71
+ const hostname = localUrl.split(':')[0];
72
+ const hostport = localUrl.split(':')[1] || PROXY_PORT_HUGGINGFACE;
73
+ const newUrl = `${PROTOCOL}://${hostname}:${PROXY_PORT_OMNITOOL}`;
74
+
75
+ console.log(`hostname = ${hostname}, hostport = ${hostport}, newUrl = ${newUrl}`);
76
+
77
+ if (!OMNITOOL_READY)
78
  {
79
+ if (req.method === 'GET')
80
  {
81
+
 
82
 
83
+ switch (req.url)
84
+ {
85
+ case '/start-omnitool-server':
86
  {
87
+
88
+ console.log(`Omnitool Server:ALREADY_STARTING = ${ALREADY_STARTING}`);
89
+ if (ALREADY_STARTING)
90
+ {
91
+ res.writeHead(200, { 'Content-Type': 'text/html' });
92
+ res.end("Omnitool server already starting");
93
+ return;
94
+ }
95
+ try
96
+ {
97
+ await startOmnitoolServer();
98
+ res.writeHead(200, { 'Content-Type': 'text/html' });
99
+ res.end("Omnitool server started successfully");
100
+ }
101
+ catch (error)
102
+ {
103
+ console.error(error);
104
+ ALREADY_STARTING = false;
105
+ res.writeHead(500, { 'Content-Type': 'text/html' });
106
+ res.end(`Error starting Omnitool server: ${error}`);
107
+ }
108
+
109
  return;
110
  }
111
+
112
+ case '/omnitool-logs':
113
  {
114
+ res.writeHead(200, { 'Content-Type': 'application/json' });
115
+ const reply = { logs: omnitoolLogs, ready: OMNITOOL_PRE_READY };
116
+ res.end(JSON.stringify(reply));
117
+ return;
118
  }
119
+
120
+ case '/':
121
  {
 
 
 
 
 
122
 
123
+ if (hostport === PROXY_PORT_OMNITOOL.toString())
124
+ {
125
+ console.log(`Serving the page with START OMNITOOL SERVER button`);
126
+ htmlContent = `
127
+ <html>
128
+ <head>
129
+ <title>Proxy Server</title>
130
+ <style>
131
+ #logContainer {
132
+ height: 400px; /* Fixed height */
133
+ overflow-y: scroll; /* Enable vertical scrolling */
134
+ background-color: black;
135
+ color: lime;
136
+ font-family: 'Courier New', Courier, monospace;
137
+ padding: 10px;
138
+ white-space: pre-wrap; /* Keep whitespaces */
139
+ border: 1px solid #ddd;
140
+ }
141
+ </style>
142
+ </head>
143
+ <body>
144
+ <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
145
+ <div id="logContainer"></div>
146
+ <script>
147
 
148
+ function startServer() {
149
+ document.getElementById('startServerButton').disabled = true;
150
+ fetch('/start-omnitool-server')
151
+ .then(response => response.json())
152
+ .then(data => {
153
+ startLogPolling();
154
+ });
155
+ }
156
+
157
+ function startLogPolling() {
158
+ const interval = setInterval(() => {
159
+ fetch('/omnitool-logs')
160
+ .then(response => response.json())
161
+ .then(data => {
162
+ const logContainer = document.getElementById('logContainer');
163
+ logContainer.innerText = data.logs.join("\\n");
164
+ if (data.ready) {
165
+ logContainer.innerText = "Omnitool is ready! REFRESHING PAGE...";
166
+ clearInterval(interval);
167
+ setTimeout(function() {
168
+ window.location.reload(1);
169
+ }, 1500); // Refreshes after 1.5 seconds
170
+ }
171
+ scrollToBottom(logContainer);
172
+ });
173
+ }, 500); // Adjust to 500 for efficiency
174
+ }
175
+
176
+ function scrollToBottom(element) {
177
+ element.scrollTop = element.scrollHeight;
178
+ }
179
+
180
+ startLogPolling();
181
+ </script>
182
+ </body>
183
+ </html>`;
184
+ }
185
+ else
186
+ {
187
+ console.log(`Serving the page with START OMNITOOL SERVER button and GOTO ${newUrl} button`);
188
+ htmlContent = `
189
+ <html>
190
+ <head>
191
+ <title>Proxy Server</title>
192
+ <style>
193
+ #logContainer {
194
+ height: 400px; /* Fixed height */
195
+ overflow-y: scroll; /* Enable vertical scrolling */
196
+ background-color: black;
197
+ color: lime;
198
+ font-family: 'Courier New', Courier, monospace;
199
+ padding: 10px;
200
+ white-space: pre-wrap; /* Keep whitespaces */
201
+ border: 1px solid #ddd;
202
+ }
203
+ .highlight-button {
204
+ animation: pulseAnimation 1s infinite;
205
+ background-color: yellow;
206
+ color: black;
207
+ font-weight: bold;
208
+ }
209
+ @keyframes pulseAnimation {
210
+ 0% { transform: scale(1); }
211
+ 50% { transform: scale(1.1); }
212
+ 100% { transform: scale(1); }
213
+ }
214
+ </style>
215
+ </head>
216
+ <body>
217
+ <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
218
+ <button id="exitIframeButton" onclick="exitIframe()">GOTO ${newUrl}</button>
219
+ <div id="logContainer"></div>
220
+ <script>
221
+ function exitIframe() {
222
+ window.open('${newUrl}', '_blank');
223
+ }
224
+
225
+ function startServer() {
226
+ document.getElementById('startServerButton').disabled = true;
227
+ fetch('/start-omnitool-server')
228
+ .then(response => response.json())
229
+ .then(data => {
230
+ startLogPolling();
231
+ });
232
+ }
233
+
234
+ function highlightExitButton() {
235
+ const exitButton = document.getElementById('exitIframeButton');
236
+ exitButton.classList.add('highlight-button');
237
+ }
238
+
239
+ function startLogPolling() {
240
+ const interval = setInterval(() => {
241
+ fetch('/omnitool-logs')
242
+ .then(response => response.json())
243
+ .then(data => {
244
+ const logContainer = document.getElementById('logContainer');
245
+ logContainer.innerText = data.logs.join("\\n");
246
+ if (data.ready) {
247
+ logContainer.innerText = "Omnitool is ready! Please press the flashing button to exit the Iframe...";
248
+ clearInterval(interval);
249
+ highlightExitButton();
250
+ }
251
+ scrollToBottom(logContainer);
252
+ });
253
+ }, 250); // Adjust to 500 for efficiency
254
+ }
255
+
256
+ function scrollToBottom(element) {
257
+ element.scrollTop = element.scrollHeight;
258
+ }
259
+
260
+ startLogPolling();
261
+ </script>
262
+ </body>
263
+ </html>`;
264
 
265
+ }
266
+ res.writeHead(200, { 'Content-Type': 'text/html' });
267
+ res.end(htmlContent);
268
+ return;
269
+
270
+ }
271
+
272
+
273
+ default:
274
+ console.log(`Doing nothing with this request: ${req.url}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
  res.writeHead(200, { 'Content-Type': 'text/html' });
276
+ res.end(`
277
+ <html>
278
+ <head>
279
+ <title>Page Refresh</title>
280
+ <script>
281
+ setTimeout(function() {
282
+ window.location.reload(1);
283
+ }, 5000); // Refreshes after 5 seconds
284
+ </script>
285
+ </head>
286
+ <body>
287
+ <p>Doing nothing with this request: ${req.url}</p>
288
+ <p>The page will refresh automatically in 5 seconds.</p>
289
+ </body>
290
+ </html>`);
291
  return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
 
293
+ }
294
+ }
295
+
296
+ // we are dropping anything that isn't a GET request
297
+ res.writeHead(200, { 'Content-Type': 'text/html' });
298
+ res.end(`
299
+ <html>
300
+ <head>
301
+ <title>Page Refresh</title>
302
+ <script>
303
+ setTimeout(function() {
304
+ window.location.reload(1);
305
+ }, 1000); // Refreshes after 1 seconds
306
+ </script>
307
+ </head>
308
+ <body>
309
+ <p>Doing nothing with this request: ${req.url}</p>
310
+ <p>The page will refresh automatically in 1 seconds.</p>
311
+ </body>
312
+ </html>`);
313
+ return;
314
+ }
315
+ if (OMNITOOL_READY)
316
+ {
317
+
318
+ if (proxy_port === PROXY_PORT_HUGGINGFACE)
319
+ {
320
+ htmlContent = `
321
+ <html>
322
+ <head>
323
+ <title>Proxy Server</title>
324
+ <style>
325
+ #logContainer {
326
+ height: 400px; /* Fixed height */
327
+ overflow-y: scroll; /* Enable vertical scrolling */
328
+ background-color: black;
329
+ color: lime;
330
+ font-family: 'Courier New', Courier, monospace;
331
+ padding: 10px;
332
+ white-space: pre-wrap; /* Keep whitespaces */
333
+ border: 1px solid #ddd;
334
+ }
335
+ .highlight-button {
336
+ animation: pulseAnimation 1s infinite;
337
+ background-color: yellow;
338
+ color: black;
339
+ font-weight: bold;
340
+ }
341
+ @keyframes pulseAnimation {
342
+ 0% { transform: scale(1); }
343
+ 50% { transform: scale(1.1); }
344
+ 100% { transform: scale(1); }
345
+ }
346
+ </style>
347
+ </head>
348
+ <body>
349
+ <button id="exitIframeButton" onclick="exitIframe()">GOTO ${newUrl}</button>
350
+ <div id="logContainer"></div>
351
+ <script>
352
+ function exitIframe() {
353
+ window.open('${newUrl}', '_blank');
354
+ }
355
+
356
+ function highlightExitButton() {
357
+ const exitButton = document.getElementById('exitIframeButton');
358
+ exitButton.classList.add('highlight-button');
359
+ }
360
+
361
+ highlightExitButton();
362
+ element.scrollTop = element.scrollHeight;
363
+
364
+ </script>
365
+ </body>
366
+ </html>`;
367
+ res.writeHead(200, { 'Content-Type': 'text/html' });
368
+ res.end(htmlContent);
369
+ return;
370
+ }
371
+
372
+
373
+ if (proxy_port === PROXY_PORT_OMNITOOL)
374
+ {
375
+ // Proxy logic...
376
+ const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
377
+ const proxy = http.request(options, (proxyRes) =>
378
+ {
379
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
380
+ proxyRes.pipe(res, { end: true });
381
+ });
382
+ req.pipe(proxy, { end: true });
383
+ return;
384
  }
385
  }
386
+ /*
387
+ // Proxy logic...
388
+ const options = { hostname: CONTAINER_HOST, port: CONTAINER_PORT_OMNITOOL, path: req.url, method: req.method, headers: req.headers, };
389
  const proxy = http.request(options, (proxyRes) =>
390
  {
391
  res.writeHead(proxyRes.statusCode, proxyRes.headers);
392
  proxyRes.pipe(res, { end: true });
393
  });
394
  req.pipe(proxy, { end: true });
395
+ */
396
  return;
397
  }
398
 
 
401
  {
402
  try
403
  {
404
+ await startRequestForwardingServer(PROXY_PORT_HUGGINGFACE);
405
+ await startRequestForwardingServer(PROXY_PORT_OMNITOOL);
406
  } catch (error)
407
  {
408
  console.error('Failed to start servers:', error);