manu-sapiens commited on
Commit
6cb66da
·
1 Parent(s): d44e3b5

double button management server - working on local

Browse files
Files changed (1) hide show
  1. myNodeServer.js +121 -36
myNodeServer.js CHANGED
@@ -10,32 +10,7 @@ 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;
@@ -43,7 +18,8 @@ function startYarnStartProcess()
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());
@@ -71,22 +47,131 @@ function startYarnStartProcess()
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
  /*
 
10
  console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
11
  let omnitoolLogs = [];
12
 
13
+ async function startOmnitoolServer()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  {
15
  if (ALREADY_STARTING) return;
16
  ALREADY_STARTING = true;
 
18
  console.log('Starting Omnitool Server...');
19
  return new Promise((resolve, reject) =>
20
  {
21
+ const omnitoolStartProcess = spawn('./omnitool_start.sh');
22
+
23
  omnitoolStartProcess.stdout.on('data', (data) =>
24
  {
25
  omnitoolLogs.push(data.toString());
 
47
  }
48
  });
49
  });
50
+ }
51
 
52
+ async function startRequestForwardingServer() {
53
+ const server = http.createServer((req, res) => handleRoutes(req, res));
54
+ server.listen(PROXY_PORT, '0.0.0.0');
55
+ console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
56
+ }
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':
66
+ {
67
+
68
+ console.log(`Omnitool Server:ALREADY_STARTING = ${ALREADY_STARTING}`)
69
+ if (ALREADY_STARTING)
70
+ {
71
+ res.writeHead(200, { 'Content-Type': 'text/html'});
72
+ res.end("Omnitool server already starting");
73
+ return;
74
+ }
75
+ try
76
+ {
77
+ await startOmnitoolServer();
78
+ res.writeHead(200, { 'Content-Type': 'text/html'});
79
+ res.end("Omnitool server started successfully");
80
+ }
81
+ catch (error)
82
+ {
83
+ console.error(error);
84
+ ALREADY_STARTING = false;
85
+ res.writeHead(500, { 'Content-Type': 'text/html'});
86
+ res.end(`Error starting Omnitool server: ${error}`);
87
+ }
88
+
89
+ return;
90
+ }
91
+
92
+ case '/omnitool-logs':
93
+ {
94
+ res.writeHead(200, { 'Content-Type': 'application/json'});
95
+ const reply = { logs: omnitoolLogs, ready: OMNITOOL_READY };
96
+ res.end(JSON.stringify(reply));
97
+ return;
98
+ }
99
+
100
+ case '/':
101
+ {
102
+ const localUrl = req.headers['host'];
103
+ let htmlContent = `
104
+ <html>
105
+ <head><title>Proxy Server</title></head>
106
+ <body>
107
+ <button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
108
+ <button id="exitIframeButton" onclick="window.open('http://${localUrl}', '_blank')" >GOTO ${localUrl}</button>
109
+ <div id="logs" style="white-space: pre-wrap;"></div>
110
+ <script>
111
+ function startServer() {
112
+ document.getElementById('startServerButton').disabled = true;
113
+ fetch('/start-omnitool-server')
114
+ .then(response => response.json())
115
+ .then(data => {
116
+ startLogPolling();
117
+ });
118
+ }
119
+
120
+ function startLogPolling() {
121
+ const interval = setInterval(() => {
122
+ fetch('/omnitool-logs')
123
+ .then(response => response.json())
124
+ .then(data => {
125
+ document.getElementById('logs').innerText = data.logs.join("\\n");
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
+ }, 1000);
133
+ }
134
+
135
+ startLogPolling();
136
+ </script>
137
+ </body>
138
+ </html>`;
139
+ res.writeHead(200, { 'Content-Type': 'text/html' });
140
+ res.end(htmlContent);
141
+ return;
142
+ }
143
+
144
+ default:
145
+ console.log(`Doing nothing with this request: ${req.url}`);
146
+ res.writeHead(200, { 'Content-Type': 'text/html' });
147
+ res.end(`Doing nothing with this request: ${req.url}`);
148
+ return;
149
+ }
150
+ }
151
+ const options = { hostname: TARGET_HOST, port: TARGET_PORT, path: req.url, method: req.method, headers: req.headers, };
152
+ const proxy = http.request(options, (proxyRes) =>
153
+ {
154
+ res.writeHead(proxyRes.statusCode, proxyRes.headers);
155
+ proxyRes.pipe(res, { end: true });
156
+ });
157
+ req.pipe(proxy, { end: true });
158
+ return;
159
  }
160
 
161
  // Main function to start everything
162
+ async function startManagementServer()
163
+ {
164
+ try
165
+ {
166
+ await startRequestForwardingServer();
167
+ } catch (error)
168
+ {
169
+ console.error('Failed to start servers:', error);
170
+ }
171
  }
172
 
173
  // Start the servers
174
+ startManagementServer();
175
 
176
 
177
  /*