Skip to content

Commit

Permalink
refactor everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna-devv committed Apr 15, 2023
1 parent 1f6b1a5 commit 7719e8c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 40 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
12 changes: 8 additions & 4 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
File renamed without changes.
15 changes: 15 additions & 0 deletions src/handlers/error.ts
Original file line number Diff line number Diff line change
@@ -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 };
};
47 changes: 14 additions & 33 deletions src/server.ts → src/handlers/http.ts
Original file line number Diff line number Diff line change
@@ -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') {
Expand Down Expand Up @@ -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]}` : '')

Expand Down Expand Up @@ -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');
Expand All @@ -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 };
};
12 changes: 12 additions & 0 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit 7719e8c

Please sign in to comment.