@mutable/proxy
Advanced tools
Comparing version 2.0.4 to 2.1.0
{ | ||
"name": "@mutable/proxy", | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"description": "Proxy", | ||
@@ -8,2 +8,6 @@ "author": "pellepelle3", | ||
"main": "src/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:mutable/node-proxy.git" | ||
}, | ||
"dependencies": { | ||
@@ -10,0 +14,0 @@ "@mutable/meta": "^1.3.0", |
@@ -14,2 +14,3 @@ const fs = require('fs'); | ||
const defaultPage404 = fs.readFileSync(path.join(__dirname, '../static/404.html')).toString(); | ||
const defaultPage401 = fs.readFileSync(path.join(__dirname, '../static/401.html')).toString(); | ||
@@ -78,3 +79,5 @@ function TimeTrack(req, label) { | ||
class Proxy { | ||
constructor(config) { | ||
constructor(config, checkAuth) { | ||
if (checkAuth && typeof checkAuth === 'function') this._checkAuth = checkAuth; | ||
if (config) this.config = config; | ||
this._proxy = httpProxy.createProxyServer({ ws: true, xfwd: false }); | ||
@@ -90,2 +93,3 @@ this._proxy.on('error', (err, req, res) => this._onError(err, req, res)); | ||
this._page404 = config.page404 || defaultPage404; | ||
this._page401 = config.page401 || defaultPage401; | ||
this._routes.updateConfig(config); | ||
@@ -123,7 +127,16 @@ } | ||
_proxyWebSockets(req, socket, head, opt, cb) { | ||
async _proxyWebSockets(req, socket, head, opt, cb) { | ||
const url = Url.parse(opt.target); | ||
req.url = url.path; | ||
opt.target = Url.format({ protocol: url.protocol, host: url.host }); | ||
DebugPrint('_proxyWeb', opt.target + req.url); | ||
DebugPrint('_proxyWebSockets', opt.target + req.url); | ||
if (this._checkAuth) { | ||
try { | ||
await this._checkAuth(req, opt, this.config); | ||
} catch (e) { | ||
DebugPrint('_proxyWebSockets', e); | ||
} | ||
} | ||
try { | ||
@@ -134,3 +147,3 @@ SetXHeaders(req); | ||
this._proxy.ws(req, socket, head, opt, (err) => { | ||
return this._proxy.ws(req, socket, head, opt, (err) => { | ||
if (err && err.code && err.code === 'ECONNREFUSED' && typeof cb === 'function') cb(); | ||
@@ -140,36 +153,45 @@ }); | ||
console.error('error:: _proxyWebSockets: ', e); | ||
return e; | ||
} | ||
} | ||
_proxyWeb(req, res, opt) { | ||
if (IsObject(opt)) { | ||
const url = Url.parse(opt.target); | ||
req.headers.host = req.headers.host || ''; | ||
req.url = url.path; | ||
url.pathname = ''; | ||
opt.target = Url.format({ protocol: url.protocol, host: url.host }); | ||
DebugPrint('_proxyWeb', opt.target + req.url); | ||
async _proxyWeb(req, res, opt) { | ||
if (!IsObject(opt)) return this._checkRoutes(req, res); | ||
if (opt.redirect) { | ||
res.writeHead(opt.statusCode || 302, { | ||
location: Url.resolve(opt.target, req.url), | ||
}); | ||
res.end(); | ||
} else if (opt.content) { | ||
SendWeb(res, opt.content, opt.statusCode || 200); | ||
} else { | ||
const url = Url.parse(opt.target); | ||
req.headers.host = req.headers.host || ''; | ||
req.url = url.path; | ||
url.pathname = ''; | ||
opt.target = Url.format({ protocol: url.protocol, host: url.host }); | ||
DebugPrint('_proxyWeb', opt.target + req.url); | ||
if (opt.redirect) { | ||
res.writeHead(opt.statusCode || 302, { | ||
location: Url.resolve(opt.target, req.url), | ||
}); | ||
res.end(); | ||
} else if (opt.content) { | ||
SendWeb(res, opt.content, opt.statusCode || 200); | ||
} else { | ||
if (this._checkAuth) { | ||
try { | ||
SetXHeaders(req); | ||
if (opt.changeHost) req.headers.host = url.host; | ||
DebugPrint('headers', req.headers); | ||
TimeTrack(req, '_proxyWeb'); | ||
this._proxy.web(req, res, opt); | ||
await this._checkAuth(req, opt, this.config); | ||
} catch (e) { | ||
TimeTrack(req, '_proxyWeb catch'); | ||
this._routePage404(res); | ||
DebugPrint('_proxyWeb', e); | ||
this._routePage401(req, res); | ||
} | ||
} | ||
} else { | ||
this._checkRoutes(req, res); | ||
try { | ||
SetXHeaders(req); | ||
if (opt.changeHost) req.headers.host = url.host; | ||
DebugPrint('headers', req.headers); | ||
TimeTrack(req, '_proxyWeb'); | ||
this._proxy.web(req, res, opt); | ||
} catch (e) { | ||
TimeTrack(req, '_proxyWeb catch'); | ||
this._routePage404(res); | ||
} | ||
} | ||
return res; | ||
} | ||
@@ -181,2 +203,6 @@ | ||
_routePage401(res) { | ||
SendWeb(res, this._page401 || '401', 401); | ||
} | ||
_onError(err, req, res) { | ||
@@ -183,0 +209,0 @@ TimeTrack(req, '_onError'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
28120
13
471
8
3