@mutable/proxy
Advanced tools
Comparing version 2.0.1 to 2.0.2
{ | ||
"name": "@mutable/proxy", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Proxy", | ||
@@ -5,0 +5,0 @@ "author": "pellepelle3", |
@@ -57,6 +57,2 @@ const fs = require('fs'); | ||
function RoutePage404(res, content) { | ||
SendWeb(res, content || '404', 404); | ||
} | ||
function HasEncryptedConnection(req) { | ||
@@ -81,35 +77,6 @@ return req.connection.encrypted || req.connection.pair; | ||
function OnError(err, req, res) { | ||
TimeTrack(req, '_onError'); | ||
if (!err || !err.code) return RoutePage500(res); | ||
if (err.code === 'ECONNREFUSED') return RoutePage500(res, 'Error with ECONNREFUSED'); | ||
return RoutePage404(res); | ||
} | ||
function CheckRoutes(req, res) { | ||
const theUrlis = Url.parse(`http://${req.headers.host}${req.url}`); | ||
switch (theUrlis.pathname) { | ||
case '/health': | ||
res.end(`${tooBusy.lag()}`); | ||
break; | ||
default: | ||
RoutePage404(res); | ||
} | ||
} | ||
function IsLocal(req, res) { | ||
if ( | ||
req.headers.host === `${process.env.MYIP}:${process.env.PORT}` | ||
|| (!IsIP.test(req.headers.host) | ||
&& req.headers.host !== `${process.env.MYHOST || 'localhost'}:${process.env.PORT}` | ||
&& req.headers.host !== `${process.env.HOSTNAME || ''}:${process.env.PORT}`) | ||
) return false; | ||
CheckRoutes(req, res); | ||
return true; | ||
} | ||
class Proxy { | ||
constructor(config) { | ||
this._proxy = httpProxy.createProxyServer({ ws: true, xfwd: false }); | ||
this._proxy.on('error', (err, req, res) => OnError(err, req, res)); | ||
this._proxy.on('error', (err, req, res) => this._onError(err, req, res)); | ||
this._proxy.on('proxyRes', (proxyRes, req) => TimeTrack(req, '_onProxyRes')); | ||
@@ -136,3 +103,3 @@ this._routes = new Routes(config); | ||
_proxyingWeb(req, res) { | ||
if (!IsLocal(req, res)) { | ||
if (!this._isLocal(req, res)) { | ||
DebugPrint('_proxyingWeb', req.headers.host + req.url); | ||
@@ -148,3 +115,3 @@ TimeTrack(req, '_proxyingWeb'); | ||
_proxyingWebSockets(req, res, socket, head) { | ||
if (!IsLocal(req, res)) { | ||
if (!this._isLocal(req, res)) { | ||
this._routes | ||
@@ -204,7 +171,40 @@ .getTarget(`http://${req.headers.host}${req.url}`) | ||
} else { | ||
CheckRoutes(req, res); | ||
this._checkRoutes(req, res); | ||
} | ||
} | ||
_routePage404(res) { | ||
SendWeb(res, this._page404 || '404', 404); | ||
} | ||
_onError(err, req, res) { | ||
TimeTrack(req, '_onError'); | ||
if (!err || !err.code) return RoutePage500(res); | ||
if (err.code === 'ECONNREFUSED') return RoutePage500(res, 'Error with ECONNREFUSED'); | ||
return this._routePage404(res); | ||
} | ||
_checkRoutes(req, res) { | ||
const theUrlis = Url.parse(`http://${req.headers.host}${req.url}`); | ||
switch (theUrlis.pathname) { | ||
case '/health': | ||
res.end(`${tooBusy.lag()}`); | ||
break; | ||
default: | ||
this._routePage404(res); | ||
} | ||
} | ||
_isLocal(req, res) { | ||
if ( | ||
req.headers.host === `${process.env.MYIP}:${process.env.PORT}` | ||
|| (!IsIP.test(req.headers.host) | ||
&& req.headers.host !== `${process.env.MYHOST || 'localhost'}:${process.env.PORT}` | ||
&& req.headers.host !== `${process.env.HOSTNAME || ''}:${process.env.PORT}`) | ||
) return false; | ||
this._checkRoutes(req, res); | ||
return true; | ||
} | ||
} | ||
module.exports = Proxy; |
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
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
21350