Spaces:
Running
Running
File size: 8,478 Bytes
d44e3b5 0b10343 d44e3b5 6cb66da d44e3b5 6cb66da d44e3b5 0b10343 d44e3b5 6cb66da d44e3b5 6cb66da 0b10343 6cb66da 0b10343 6cb66da 0b10343 6cb66da 0b10343 6cb66da d44e3b5 6cb66da d44e3b5 6cb66da d44e3b5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
const http = require('http');
const { spawn } = require('child_process');
const PROXY_PORT = 4444; // Change this to a port different from 1688
const TARGET_HOST = '127.0.0.1';
const TARGET_PORT = 1688;
let OMNITOOL_READY = false;
let ALREADY_STARTING = false;
const VERSION = '0.0.4a';
console.log(`************ Omnitool Proxy Server v${VERSION} ************`);
let omnitoolLogs = [];
async function startOmnitoolServer()
{
if (ALREADY_STARTING) return;
ALREADY_STARTING = true;
console.log('Starting Omnitool Server...');
return new Promise((resolve, reject) =>
{
const omnitoolStartProcess = spawn('./omnitool_start.sh');
omnitoolStartProcess.stdout.on('data', (data) =>
{
omnitoolLogs.push(data.toString());
console.log(`omnitool stdout: ${data}`);
if (data.toString().includes(`Server has started and is ready to accept connections`))
{
console.log('Omnitool server started successfully');
setTimeout(() => {
OMNITOOL_READY = true;
resolve(); }, 1000); // Delay by 1 second
}
});
omnitoolStartProcess.stderr.on('data', (data) =>
{
console.error(`omnitool stderr: ${data}`);
});
omnitoolStartProcess.on('close', (code) =>
{
console.log(`Omnitool server process exited with code ${code}`);
if (!OMNITOOL_READY)
{
ALREADY_STARTING = false;
reject(`Omnitool server did not start within the timeout period.`);
}
});
});
}
async function startRequestForwardingServer() {
const server = http.createServer((req, res) => handleRoutes(req, res));
server.listen(PROXY_PORT, '0.0.0.0');
console.log(`Request forwarding server listening on port ${PROXY_PORT}`);
}
async function handleRoutes(req, res)
{
if ( !OMNITOOL_READY && (req.method === 'GET'))
{
switch (req.url)
{
case '/start-omnitool-server':
{
console.log(`Omnitool Server:ALREADY_STARTING = ${ALREADY_STARTING}`)
if (ALREADY_STARTING)
{
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end("Omnitool server already starting");
return;
}
try
{
await startOmnitoolServer();
res.writeHead(200, { 'Content-Type': 'text/html'});
res.end("Omnitool server started successfully");
}
catch (error)
{
console.error(error);
ALREADY_STARTING = false;
res.writeHead(500, { 'Content-Type': 'text/html'});
res.end(`Error starting Omnitool server: ${error}`);
}
return;
}
case '/omnitool-logs':
{
res.writeHead(200, { 'Content-Type': 'application/json'});
const reply = { logs: omnitoolLogs, ready: OMNITOOL_READY };
res.end(JSON.stringify(reply));
return;
}
case '/':
{
const localUrl = req.headers['host'];
let htmlContent = `
<html>
<head>
<title>Proxy Server</title>
<style>
#logContainer {
height: 400px; /* Fixed height */
overflow-y: scroll; /* Enable vertical scrolling */
background-color: black;
color: lime;
font-family: 'Courier New', Courier, monospace;
padding: 10px;
white-space: pre-wrap; /* Keep whitespaces */
border: 1px solid #ddd;
}
</style>
</head>
<body>
<button id="startServerButton" onclick="startServer()">Start Omnitool Server</button>
<button id="exitIframeButton" onclick="window.open('http://${localUrl}', '_blank')">GOTO ${localUrl}</button>
<div id="logContainer"></div>
<script>
function startServer() {
document.getElementById('startServerButton').disabled = true;
fetch('/start-omnitool-server')
.then(response => response.json())
.then(data => {
startLogPolling();
});
}
function startLogPolling() {
const interval = setInterval(() => {
fetch('/omnitool-logs')
.then(response => response.json())
.then(data => {
const logContainer = document.getElementById('logContainer');
logContainer.innerText = data.logs.join("\\n");
if (data.ready) {
logContainer.innerText = "Omnitool is ready! Please open the following link in a new tab: https://${localUrl}";
clearInterval(interval);
//window.location.reload(); // Optionally refresh the page when Omnitool is ready
}
scrollToBottom(logContainer);
});
}, 1000); // Adjusted to 1000ms for efficiency
}
function scrollToBottom(element) {
element.scrollTop = element.scrollHeight;
}
startLogPolling();
</script>
</body>
</html>`;
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(htmlContent);
return;
}
default:
console.log(`Doing nothing with this request: ${req.url}`);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(`Doing nothing with this request: ${req.url}`);
return;
}
}
const options = { hostname: TARGET_HOST, port: TARGET_PORT, path: req.url, method: req.method, headers: req.headers, };
const proxy = http.request(options, (proxyRes) =>
{
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
req.pipe(proxy, { end: true });
return;
}
// Main function to start everything
async function startManagementServer()
{
try
{
await startRequestForwardingServer();
} catch (error)
{
console.error('Failed to start servers:', error);
}
}
// Start the servers
startManagementServer();
|