Fix #Final
This commit is contained in:
17
package-lock.json
generated
17
package-lock.json
generated
@@ -9,13 +9,14 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/http-proxy": "^1.17.13",
|
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"http-proxy": "^1.18.1"
|
"http-proxy": "^1.18.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/cors": "^2.8.17",
|
||||||
"@types/express": "^4.17.20",
|
"@types/express": "^4.17.20",
|
||||||
|
"@types/http-proxy": "^1.17.13",
|
||||||
"typescript": "^5.2.2"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -38,6 +39,15 @@
|
|||||||
"@types/node": "*"
|
"@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": {
|
"node_modules/@types/express": {
|
||||||
"version": "4.17.20",
|
"version": "4.17.20",
|
||||||
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.20.tgz",
|
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.20.tgz",
|
||||||
@@ -72,6 +82,7 @@
|
|||||||
"version": "1.17.13",
|
"version": "1.17.13",
|
||||||
"resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.13.tgz",
|
"resolved": "https://registry.npmjs.org/@types/http-proxy/-/http-proxy-1.17.13.tgz",
|
||||||
"integrity": "sha512-GkhdWcMNiR5QSQRYnJ+/oXzu0+7JJEPC8vkWXK351BkhjraZF+1W13CUYARUvX9+NqIU2n6YHA4iwywsc/M6Sw==",
|
"integrity": "sha512-GkhdWcMNiR5QSQRYnJ+/oXzu0+7JJEPC8vkWXK351BkhjraZF+1W13CUYARUvX9+NqIU2n6YHA4iwywsc/M6Sw==",
|
||||||
|
"dev": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/node": "*"
|
"@types/node": "*"
|
||||||
}
|
}
|
||||||
@@ -86,6 +97,7 @@
|
|||||||
"version": "20.8.10",
|
"version": "20.8.10",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.8.10.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.8.10.tgz",
|
||||||
"integrity": "sha512-TlgT8JntpcbmKUFzjhsyhGfP2fsiz1Mv56im6enJ905xG1DAYesxJaeSbGqQmAw8OWPdhyJGhGSQGKRNJ45u9w==",
|
"integrity": "sha512-TlgT8JntpcbmKUFzjhsyhGfP2fsiz1Mv56im6enJ905xG1DAYesxJaeSbGqQmAw8OWPdhyJGhGSQGKRNJ45u9w==",
|
||||||
|
"dev": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"undici-types": "~5.26.4"
|
"undici-types": "~5.26.4"
|
||||||
}
|
}
|
||||||
@@ -847,7 +859,8 @@
|
|||||||
"node_modules/undici-types": {
|
"node_modules/undici-types": {
|
||||||
"version": "5.26.5",
|
"version": "5.26.5",
|
||||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
|
"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": {
|
"node_modules/unpipe": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
|
|||||||
@@ -9,13 +9,14 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/http-proxy": "^1.17.13",
|
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"http-proxy": "^1.18.1"
|
"http-proxy": "^1.18.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/cors": "^2.8.17",
|
||||||
"@types/express": "^4.17.20",
|
"@types/express": "^4.17.20",
|
||||||
|
"@types/http-proxy": "^1.17.13",
|
||||||
"typescript": "^5.2.2"
|
"typescript": "^5.2.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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')
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -25,4 +25,4 @@ export const env: Env = new Proxy<Env>({}, {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
22
src/https.ts
22
src/https.ts
@@ -1,9 +1,9 @@
|
|||||||
import { RequestListener } from "http";
|
import { RequestListener } from "http"
|
||||||
import https from "https";
|
import https from "https"
|
||||||
import tls from "tls";
|
import tls from "tls"
|
||||||
import fs from "fs";
|
import fs from "fs"
|
||||||
import { Config } from "./config";
|
import { Config } from "./config"
|
||||||
import { join } from "path";
|
import { join } from "path"
|
||||||
|
|
||||||
|
|
||||||
export function getHttps(cfg: Config, letsencryptPath: string) {
|
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.
|
// A function that will be called if the client supports SNI TLS extension.
|
||||||
SNICallback: (domain, cb) => {
|
SNICallback: (domain, cb) => {
|
||||||
|
|
||||||
const ctx = secureContexts[domain];
|
const ctx = secureContexts[domain]
|
||||||
|
|
||||||
if (cb) {
|
if (cb) {
|
||||||
ctx ? cb(null, ctx) : cb(Error("No ctx"));
|
ctx ? cb(null, ctx) : cb(Error("No ctx"))
|
||||||
} else if (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) {
|
function getSecureContexts(config: Config, letsencryptPath: string) {
|
||||||
|
|||||||
12
src/index.ts
12
src/index.ts
@@ -1,8 +1,8 @@
|
|||||||
import { getHttps } from "./https";
|
import { getHttps } from "./https"
|
||||||
import { createServer as httpServer } from 'http'
|
import { createServer as httpServer } from 'http'
|
||||||
import { Config } from "./config";
|
import { Config } from "./config"
|
||||||
import getProxy from "./proxy";
|
import getProxy from "./proxy"
|
||||||
import { env } from "./env";
|
import { env } from "./env"
|
||||||
|
|
||||||
|
|
||||||
const HTTP_PORT = env.HTTP_PORT(52080),
|
const HTTP_PORT = env.HTTP_PORT(52080),
|
||||||
@@ -23,9 +23,9 @@ async function main() {
|
|||||||
|
|
||||||
httpServer(app).on('upgrade', upgrade).listen(HTTP_PORT, function () {
|
httpServer(app).on('upgrade', upgrade).listen(HTTP_PORT, function () {
|
||||||
console.log("Listening http on port: " + HTTP_PORT)
|
console.log("Listening http on port: " + HTTP_PORT)
|
||||||
});
|
})
|
||||||
if (HTTPS_PORT != -1) {
|
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 () {
|
httpsServer(app).on('upgrade', upgrade).listen(HTTPS_PORT, function () {
|
||||||
console.log("Listening https on port: " + HTTPS_PORT)
|
console.log("Listening https on port: " + HTTPS_PORT)
|
||||||
})
|
})
|
||||||
|
|||||||
28
src/proxy.ts
28
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 { Duplex } from "node:stream"
|
||||||
import { IncomingMessage } from 'http';
|
import { IncomingMessage } from 'http'
|
||||||
import httpProxy from 'http-proxy';
|
import httpProxy from 'http-proxy'
|
||||||
import cors from 'cors';
|
import cors from 'cors'
|
||||||
|
|
||||||
import { resolve, join } from 'path';
|
import { resolve, join } from 'path'
|
||||||
import { lstat } from 'fs';
|
import { lstat } from 'fs'
|
||||||
import { Config, ConfigAtPath } from "./config";
|
import { Config, ConfigAtPath } from "./config"
|
||||||
|
|
||||||
|
|
||||||
export default function getProxy(cfg: Config) {
|
export default function getProxy(cfg: Config) {
|
||||||
@@ -34,8 +34,8 @@ export default function getProxy(cfg: Config) {
|
|||||||
if (req.res)
|
if (req.res)
|
||||||
req.res.locals.cors = answer
|
req.res.locals.cors = answer
|
||||||
|
|
||||||
callback(null, { origin: answer });
|
callback(null, { origin: answer })
|
||||||
};
|
}
|
||||||
|
|
||||||
function prettyLog(data: any) {
|
function prettyLog(data: any) {
|
||||||
console.log(JSON.stringify(data, undefined, 2))
|
console.log(JSON.stringify(data, undefined, 2))
|
||||||
@@ -52,7 +52,7 @@ export default function getProxy(cfg: Config) {
|
|||||||
|
|
||||||
if (Config.isStatic(proxyData)) {
|
if (Config.isStatic(proxyData)) {
|
||||||
log.PROXY_TYPE = 'STATIC'
|
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
|
log.FILE_PATH = path
|
||||||
lstat(path, (err, stat) => {
|
lstat(path, (err, stat) => {
|
||||||
if (err || !stat.isFile()) {
|
if (err || !stat.isFile()) {
|
||||||
@@ -60,14 +60,14 @@ export default function getProxy(cfg: Config) {
|
|||||||
prettyLog(log)
|
prettyLog(log)
|
||||||
|
|
||||||
res.status(404).send('<h1>File not exist</h1>')
|
res.status(404).send('<h1>File not exist</h1>')
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
log.FILE_FOUND = false
|
log.FILE_FOUND = false
|
||||||
prettyLog(log)
|
prettyLog(log)
|
||||||
|
|
||||||
res.sendFile(path)
|
res.sendFile(path)
|
||||||
})
|
})
|
||||||
return;
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const domain = req.headers.host = proxyData.rewriteDomain ? proxyData.domain : req.hostname
|
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(cors(corsOptionsDelegate))
|
||||||
|
|
||||||
app.use((req, res) => {
|
app.use((req, res) => {
|
||||||
const proxyData = getProxyData(req);
|
const proxyData = getProxyData(req)
|
||||||
if (proxyData)
|
if (proxyData)
|
||||||
return handleRequest(proxyData, req, res)
|
return handleRequest(proxyData, req, res)
|
||||||
|
|
||||||
@@ -114,7 +114,7 @@ export default function getProxy(cfg: Config) {
|
|||||||
app,
|
app,
|
||||||
upgrade: (req: IncomingMessage, socket: Duplex, head: Buffer) => {
|
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({
|
const proxyData = getProxyData({
|
||||||
hostname,
|
hostname,
|
||||||
path
|
path
|
||||||
|
|||||||
Reference in New Issue
Block a user