Spaces:
Running
Running
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 = []; | |
// Function to start the request forwarding server | |
async function startRequestForwardingServer() { | |
const server = http.createServer((req, res) => { | |
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 }); | |
}); | |
server.listen(PROXY_PORT, '0.0.0.0'); | |
console.log(`Request forwarding server listening on port ${PROXY_PORT}`); | |
} | |
// Function to start the background process | |
function startYarnStartProcess() | |
{ | |
if (ALREADY_STARTING) return; | |
ALREADY_STARTING = true; | |
console.log('Starting Omnitool Server...'); | |
return new Promise((resolve, reject) => | |
{ | |
const omnitoolStartProcess = spawn('./omnitool_start.sh', {detached: true}); | |
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`)) | |
{ | |
OMNITOOL_READY = true; | |
console.log('Omnitool server started successfully'); | |
resolve(); | |
} | |
}); | |
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.`); | |
} | |
}); | |
}); | |
omnitoolStartProcess.unref(); | |
} | |
// Main function to start everything | |
async function startServers() { | |
try { | |
startYarnStartProcess(); | |
await startRequestForwardingServer(); | |
} catch (error) { | |
console.error('Failed to start servers:', error); | |
} | |
} | |
// Start the servers | |
startServers(); | |
/* | |
const fastify = require('fastify')({ logger: true }); | |
const { spawn } = require('child_process'); | |
const { createProxyMiddleware } = require('http-proxy-middleware'); | |
let OMNITOOL_READY = false; | |
let ALREADY_STARTING = false; | |
//const PROXY_PORT = 4444; | |
//const TARGET_HOST = '0.0.0.0'; | |
//const PROXY_TARGET = 'http://127.0.0.1:1688'; | |
const VERSION = '0.0.3a'; | |
// ------------ | |
const http = require('http'); | |
const PROXY_PORT = 4444; // Change this to a port different from 1688 | |
const TARGET_HOST = '0.0.0.0'; | |
const TARGET_PORT = 1688; | |
// Function to start the request forwarding server | |
async function startRequestForwardingServer() | |
{ | |
console.log('Starting request forwarding server...'); | |
const server = http.createServer((req, res) => | |
{ | |
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 }); | |
}); | |
server.listen(PROXY_PORT, '0.0.0.0'); | |
console.log(`Request forwarding server listening on port ${PROXY_PORT}`); | |
} | |
// ------------ | |
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`)) | |
{ | |
OMNITOOL_READY = true; | |
console.log('Omnitool server started successfully'); | |
resolve(); | |
} | |
}); | |
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.`); | |
} | |
}); | |
}); | |
} | |
fastify.get('/', async (request, reply) => | |
{ | |
const localUrl = request.headers['host']; | |
if (!OMNITOOL_READY) | |
{ | |
let htmlContent = ` | |
<html> | |
<head><title>Proxy Server</title></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="logs" style="white-space: pre-wrap;"></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 => { | |
document.getElementById('logs').innerText = data.logs.join("\\n"); | |
if (data.ready) { | |
document.getElementById('logs').innerText = "Omnitool is ready! Please open the following link in a new tab: https://${localUrl}"; | |
clearInterval(interval); | |
//window.location.reload(); // Refresh the page when Omnitool is ready | |
} | |
}); | |
}, 1000); | |
} | |
startLogPolling(); | |
</script> | |
</body> | |
</html> | |
`; | |
reply.type('text/html').send(htmlContent); | |
} else | |
{ | |
console.log('Proxying request as OMNITOOL is ready'); | |
try | |
{ | |
await startRequestForwardingServer(); | |
} catch (error) | |
{ | |
console.error('Failed to start servers:', error); | |
} | |
} | |
}); | |
fastify.get('/omnitool-logs', async (request, reply) => | |
{ | |
reply.send({ logs: omnitoolLogs, ready: OMNITOOL_READY }); | |
}); | |
fastify.get('/start-omnitool-server', async (request, reply) => | |
{ | |
if (!OMNITOOL_READY) | |
{ | |
if (ALREADY_STARTING) | |
{ | |
return { message: "Omnitool server already starting" }; | |
} | |
try | |
{ | |
await startOmnitoolServer(); | |
reply.send({ message: "Omnitool server started successfully" }); | |
} catch (error) | |
{ | |
console.error(error); | |
reply.send({ message: `Error starting Omnitool server: ${error}` }); | |
} | |
} else | |
{ | |
reply.send({ message: "Omnitool server already running" }); | |
} | |
}); | |
const start = async () => | |
{ | |
try | |
{ | |
await fastify.listen({ port: PROXY_PORT, host: TARGET_HOST }); | |
console.log(`Server is listening on http://${TARGET_HOST}:${PROXY_PORT}`); | |
} catch (err) | |
{ | |
console.error(`Error starting server: ${err}`); | |
process.exit(1); | |
} | |
}; | |
start(); | |
*/ | |