diff --git a/package-lock.json b/package-lock.json index d7762b0..49b6df8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,13 +9,14 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@types/http-proxy": "^1.17.13", "cors": "^2.8.5", "express": "^4.18.2", "http-proxy": "^1.18.1" }, "devDependencies": { + "@types/cors": "^2.8.17", "@types/express": "^4.17.20", + "@types/http-proxy": "^1.17.13", "typescript": "^5.2.2" } }, @@ -38,6 +39,15 @@ "@types/node": "*" } }, + "node_modules/@types/cors": { + "version": "2.8.17", + "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.17.tgz", + "integrity": "sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/express": { "version": "4.17.20", "resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.20.tgz", @@ -72,6 +82,7 @@ "version": "1.17.13", "resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.13.tgz", "integrity": "sha512-GkhdWcMNiR5QSQRYnJ+/oXzu0+7JJEPC8vkWXK351BkhjraZF+1W13CUYARUvX9+NqIU2n6YHA4iwywsc/M6Sw==", + "dev": true, "dependencies": { "@types/node": "*" } @@ -86,6 +97,7 @@ "version": "20.8.10", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.8.10.tgz", "integrity": "sha512-TlgT8JntpcbmKUFzjhsyhGfP2fsiz1Mv56im6enJ905xG1DAYesxJaeSbGqQmAw8OWPdhyJGhGSQGKRNJ45u9w==", + "dev": true, "dependencies": { "undici-types": "~5.26.4" } @@ -847,7 +859,8 @@ "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true }, "node_modules/unpipe": { "version": "1.0.0", diff --git a/package.json b/package.json index d1ee0a8..1b558ac 100644 --- a/package.json +++ b/package.json @@ -9,13 +9,14 @@ "author": "", "license": "ISC", "dependencies": { - "@types/http-proxy": "^1.17.13", "cors": "^2.8.5", "express": "^4.18.2", "http-proxy": "^1.18.1" }, "devDependencies": { + "@types/cors": "^2.8.17", "@types/express": "^4.17.20", + "@types/http-proxy": "^1.17.13", "typescript": "^5.2.2" } -} +} \ No newline at end of file diff --git a/src/config_old.ts b/src/config_old.ts deleted file mode 100644 index f341f85..0000000 --- a/src/config_old.ts +++ /dev/null @@ -1,41 +0,0 @@ -import fs from 'fs'; - -export interface Config { - default?: ConfigProxyPaths - hosts: { - [domain: string]: ConfigProxyPaths - }, - certs: ConfigCerts -} -export interface ConfigCerts { - [folder: string]: { - domain: string - allowSubdomains: boolean - } -} -export interface ConfigProxyPaths { - [path: string]: ConfigProxy | ConfigProxyStatic -} - -type Cors = string[] | '*' - -export interface ConfigProxy { - domain: string - port?: number - headers?: { [key: string]: string } - https?: boolean - cors?: Cors - useNativeDomain?: boolean - relative?: boolean -} - -export interface ConfigProxyStatic { - static: string - cors?: Cors -} - -export default function getConfig(path: string): Config { - return JSON.parse( - fs.readFileSync(path, 'utf8') - ) -} \ No newline at end of file diff --git a/src/env.ts b/src/env.ts index d104812..3acf409 100644 --- a/src/env.ts +++ b/src/env.ts @@ -25,4 +25,4 @@ export const env: Env = new Proxy({}, { } } } -}); \ No newline at end of file +}) \ No newline at end of file diff --git a/src/https.ts b/src/https.ts index e17d108..3a567b5 100644 --- a/src/https.ts +++ b/src/https.ts @@ -1,9 +1,9 @@ -import { RequestListener } from "http"; -import https from "https"; -import tls from "tls"; -import fs from "fs"; -import { Config } from "./config"; -import { join } from "path"; +import { RequestListener } from "http" +import https from "https" +import tls from "tls" +import fs from "fs" +import { Config } from "./config" +import { join } from "path" export function getHttps(cfg: Config, letsencryptPath: string) { @@ -13,17 +13,17 @@ export function getHttps(cfg: Config, letsencryptPath: string) { // A function that will be called if the client supports SNI TLS extension. SNICallback: (domain, cb) => { - const ctx = secureContexts[domain]; + const ctx = secureContexts[domain] if (cb) { - ctx ? cb(null, ctx) : cb(Error("No ctx")); + ctx ? cb(null, ctx) : cb(Error("No ctx")) } else if (ctx) { - return ctx; + return ctx } }, - }; + } - return (app: RequestListener) => https.createServer(options, app); + return (app: RequestListener) => https.createServer(options, app) } function getSecureContexts(config: Config, letsencryptPath: string) { diff --git a/src/index.ts b/src/index.ts index 9121a50..773aec5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ -import { getHttps } from "./https"; +import { getHttps } from "./https" import { createServer as httpServer } from 'http' -import { Config } from "./config"; -import getProxy from "./proxy"; -import { env } from "./env"; +import { Config } from "./config" +import getProxy from "./proxy" +import { env } from "./env" const HTTP_PORT = env.HTTP_PORT(52080), @@ -23,9 +23,9 @@ async function main() { httpServer(app).on('upgrade', upgrade).listen(HTTP_PORT, function () { console.log("Listening http on port: " + HTTP_PORT) - }); + }) if (HTTPS_PORT != -1) { - const httpsServer = await getHttps(config, CERTS_PATH) + const httpsServer = getHttps(config, CERTS_PATH) httpsServer(app).on('upgrade', upgrade).listen(HTTPS_PORT, function () { console.log("Listening https on port: " + HTTPS_PORT) }) diff --git a/src/proxy.ts b/src/proxy.ts index 2c5b110..e0a471d 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -1,13 +1,13 @@ -import express, { Request, Response } from "express"; +import express, { Request, Response } from "express" import { Duplex } from "node:stream" -import { IncomingMessage } from 'http'; -import httpProxy from 'http-proxy'; -import cors from 'cors'; +import { IncomingMessage } from 'http' +import httpProxy from 'http-proxy' +import cors from 'cors' -import { resolve, join } from 'path'; -import { lstat } from 'fs'; -import { Config, ConfigAtPath } from "./config"; +import { resolve, join } from 'path' +import { lstat } from 'fs' +import { Config, ConfigAtPath } from "./config" export default function getProxy(cfg: Config) { @@ -34,8 +34,8 @@ export default function getProxy(cfg: Config) { if (req.res) req.res.locals.cors = answer - callback(null, { origin: answer }); - }; + callback(null, { origin: answer }) + } function prettyLog(data: any) { console.log(JSON.stringify(data, undefined, 2)) @@ -52,7 +52,7 @@ export default function getProxy(cfg: Config) { if (Config.isStatic(proxyData)) { log.PROXY_TYPE = 'STATIC' - const path = join(volumePath, proxyData.folder, '/' + req.path.substring(proxyData.exclude)); + const path = join(volumePath, proxyData.folder, '/' + req.path.substring(proxyData.exclude)) log.FILE_PATH = path lstat(path, (err, stat) => { if (err || !stat.isFile()) { @@ -60,14 +60,14 @@ export default function getProxy(cfg: Config) { prettyLog(log) res.status(404).send('

File not exist

') - return; + return } log.FILE_FOUND = false prettyLog(log) res.sendFile(path) }) - return; + return } const domain = req.headers.host = proxyData.rewriteDomain ? proxyData.domain : req.hostname @@ -96,7 +96,7 @@ export default function getProxy(cfg: Config) { app.use(cors(corsOptionsDelegate)) app.use((req, res) => { - const proxyData = getProxyData(req); + const proxyData = getProxyData(req) if (proxyData) return handleRequest(proxyData, req, res) @@ -114,7 +114,7 @@ export default function getProxy(cfg: Config) { app, upgrade: (req: IncomingMessage, socket: Duplex, head: Buffer) => { - const { hostname, pathname: path } = new URL(req.url, `http://${req.headers.host}`); + const { hostname, pathname: path } = new URL(req.url, `http://${req.headers.host}`) const proxyData = getProxyData({ hostname, path