From 7719e8ca9780c233bdb77d7766291d2a04534f43 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 15 Apr 2023 23:07:29 +0200 Subject: [PATCH] refactor everything --- package.json | 9 ++++-- src/app.ts | 12 +++++--- src/{errors.ts => constants.ts} | 0 src/handlers/error.ts | 15 +++++++++ src/{server.ts => handlers/http.ts} | 47 +++++++++-------------------- src/handlers/index.ts | 12 ++++++++ 6 files changed, 55 insertions(+), 40 deletions(-) rename src/{errors.ts => constants.ts} (100%) create mode 100644 src/handlers/error.ts rename src/{server.ts => handlers/http.ts} (87%) create mode 100644 src/handlers/index.ts diff --git a/package.json b/package.json index f62cc32..6069aa9 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,11 @@ }, "dependencies": { "@types/node": "^18.6.5", - "typescript": "^4.7.4", + "http-proxy": "^1.18.1", "tsc": "^2.0.4", - "http-proxy": "^1.18.1" + "typescript": "^4.7.4" + }, + "devDependencies": { + "@types/http-proxy": "^1.17.10" } -} \ No newline at end of file +} diff --git a/src/app.ts b/src/app.ts index d298a33..960721e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,10 +1,14 @@ -import http from 'node:http'; +import node_proxy from 'node:http'; import { Config } from './config'; -import { requestManager, proxy, onError } from './server'; +import { http, ws, error, proxy } from './handlers'; -proxy.on('error', onError); -http.createServer(requestManager).listen(Config.port.http, () => { +proxy.on('error', error.send); +const server = node_proxy.createServer(http.handle); + +server.on('upgrade', (req, socket, head) => ws.handle); + +server.listen(Config.port.http, () => { console.log(`\x1b[33mHTTP (server) running on port ${Config.port.http}\x1b[0m`); console.log(`\x1b[33mProxy (server) running on port ${Config.port.proxy}\x1b[0m`); }); \ No newline at end of file diff --git a/src/errors.ts b/src/constants.ts similarity index 100% rename from src/errors.ts rename to src/constants.ts diff --git a/src/handlers/error.ts b/src/handlers/error.ts new file mode 100644 index 0000000..95b7eb3 --- /dev/null +++ b/src/handlers/error.ts @@ -0,0 +1,15 @@ +import node_proxy, { Server } from 'node:http'; +import path from 'node:path'; +import fs from 'node:fs'; +import { Socket } from 'node:net'; + +export function send(err: Error | string, req: node_proxy.IncomingMessage, res: node_proxy.ServerResponse | Socket, e?: any) { + if (!res) return; + try { + (res as node_proxy.ServerResponse).writeHead?.(500, { + 'Content-Type': 'text/html' + }); + const html_500 = fs.readFileSync(path.join(__dirname, '../html/500.html'), 'utf-8') + return res.end(html_500.replace(/{host}/g, req.headers.host || '')); + } catch { return }; +}; \ No newline at end of file diff --git a/src/server.ts b/src/handlers/http.ts similarity index 87% rename from src/server.ts rename to src/handlers/http.ts index 7b50610..930461c 100644 --- a/src/server.ts +++ b/src/handlers/http.ts @@ -1,18 +1,19 @@ -import http_proxy from 'http-proxy'; -import fs from 'node:fs'; +import node_proxy from 'node:http'; import path from 'node:path'; +import fs from 'node:fs'; -import { Errors } from './errors'; -import { Config, Host } from './config'; -import hosts from './hosts'; +import { proxy, error } from '.'; +import { Errors } from '../constants'; +import { Host } from '../config'; +import hosts from '../hosts'; -export const proxy = http_proxy.createProxyServer({ - proxyTimeout: Config.outTimeout, - timeout: Config.inTimeout, - ws: true -}).listen(Config.port.proxy); +export async function handle(req: node_proxy.IncomingMessage, res: node_proxy.ServerResponse) { + if (hosts[req.headers.host || ''].type === 'WS') return; -export async function requestManager(req, res) { + // Return error if no host header or url is present + if (!req.headers.host || !req.url) { + return res.end(JSON.stringify({ success: false, message: "No host" })); + } // Return success for uptime bots if (req.url === '/__http_proxy_status') { @@ -70,10 +71,10 @@ export async function requestManager(req, res) { if (overwrite.target.includes('{path}')) process.emitWarning(Errors.PLACEHOLDER_DEPRECATION.replace('![[DEPRICATED_PLACEHOLDER]]', '{path}').replace('![[NEW_PLACHOLDER]]', '{total_path}'), 'DeprecationWarning'); // Redirections - if (typeof overwrite.target === 'number') return onError('Redirect target cannot be number', req, res); + if (typeof overwrite.target === 'number') return error.send('Redirect target cannot be number', req, res); res.writeHead(302, { 'Location': overwrite.target - .replace(/{after_path}/g, (req.url.includes('?') ? req.url.split('?')[0] : req.url).split(overwrite.path.slice(0, -1))[1]) + .replace(/{after_path}/g, (req.url.includes('?') ? req.url.split('?')[0] : req.url).split(overwrite.path.toString().slice(0, -1))[1]) .replace(/{total_path}/g, (req.url.includes('?') ? req.url.split('?')[0] : req.url).slice(1)) .replace(/{query}/g, req.url.split('?')[1] ? `?${req.url.split('?')[1]}` : '') @@ -108,15 +109,6 @@ export async function requestManager(req, res) { }); break; - // WebSocket requests - case "WS": - if (typeof target !== 'number') return process.emitWarning(Errors.INVALID_TYPE.replace('![[INVALID_TYPE]]', typeof target), 'INVALID_TYPE'); - - proxy.ws(req, res.socket, { - target: `ws://${ip || '127.0.0.1'}:${target}` - }); - break; - // Redirections case "REDIRECT": if (typeof target !== 'string') return process.emitWarning(Errors.INVALID_TYPE.replace('![[INVALID_TYPE]]', typeof target), 'INVALID_TYPE'); @@ -136,15 +128,4 @@ export async function requestManager(req, res) { res.end(); break; }; -}; - -export async function onError(err, req, res) { - if (!res) return; - try { - res.writeHead(500, { - 'Content-Type': 'text/html' - }); - const html_500 = fs.readFileSync(path.join(__dirname, '../html/500.html'), 'utf-8') - return res.end(html_500.replace(/{host}/g, req.headers.host)); - } catch { return }; }; \ No newline at end of file diff --git a/src/handlers/index.ts b/src/handlers/index.ts new file mode 100644 index 0000000..bda951c --- /dev/null +++ b/src/handlers/index.ts @@ -0,0 +1,12 @@ +import node_proxy from 'http-proxy'; +import { Config } from '../config'; + +export const proxy = node_proxy.createProxyServer({ + proxyTimeout: Config.outTimeout, + timeout: Config.inTimeout, + ws: true +}).listen(Config.port.proxy); + +export * as error from './error'; +export * as http from './http'; +export * as ws from './ws'; \ No newline at end of file