Proxy Server - SGApps.IO
A user-friendly library that creates a ProxyServer oriented to redirect traffic to other URL addresses
You may use it to redirect traffic, OR as a firewall to control traffic to a Virtual Machine on your server.
there are a lot of ways to use it.
by Sergiu Gordienco Vasile ( email: sergiu.gordienco@gmail.com )
Integration with expressjs / httpServer / httpsServer
in similar way you can integrate this server on an existing site / or other HTTP/HTTPS Server
npm install --save @sgapps.io/proxy-server
const proxyServerBuilder = require("@sgapps.io/proxy-server");
const express = require("express");
let app = express();
let configuration = require("./config.js");
// initialize proxy server only as a request handler
let proxyServer = proxyServerBuilder(configuration, false);
// connect proxy server to express server
proxyServer((server) => {
app.all(
(req, res) => {
server.handleRequest(req, res);
}
);
server.on("onError", function (err) {
// ...
});
server.on("onLog", function (message1, message2, messageN) {
// ...
});
});
app.listen(8080);
server.configuration- returns configuration objectserver.debugEnabled([boolean])- switch debug optionserver.debugEnabled()- debug return current debug optionserver.isRunning()- returns running statusserver.close(callback)- callback function recieves error if error ocurredserver.handleRequest(request, response)- handle HTTP Request / Responseserver.httpServer()- returns ahttp.createServer()
Using as a CLI Command
- Installing
npm install -g @sgapps.io/proxy-server
-
Running
sgapps-proxy-server --config /path/to/your-config-file.json
Using @sgapps.io/proxy-server as a server
const proxyServerBuilder = require("@sgapps.io/proxy-server");
let proxyServer = proxyServerBuilder({
httpServer : {
host : "0.0.0.0",
port : 80
},
defaultProtocol: "http", // optional, default is http
secureVerify : false, // disable SSL Certificate validation
proxyRules : [
{
client_port : 80,
client_host : "sgapps.io",
domains : [
"www.js.md",
"html5.md",
"www.html5.md",
"html.md",
"www.html.md",
"javascript.md",
"www.javascript.md",
"nodejs.md",
"www.nodejs.md"
]
},
{
client_port : 6080,
protocol : "https",
// host will be localhost
domains : [
'www.first-jump.com',
'first-jump.com',
'cloudwebcode.com',
'www.cloudwebcode.com'
]
}
],
debug: true
});
// accessing server to log errors
proxyServer(function (server) {
// function will be called when server will be ready
server.on("proxy-error", function (err, req, res) {
console.error(err);
});
server.on("onError", function (err) {
// ...
});
server.on("onLog", function (message1, message2, messageN) {
// ...
});
});
Set a custom Error Page for ProxyServer
// ... initialize server
proxyServer((server) => {
server.off("proxy-error", "response-error-render");
server.on("proxy-error", function (err, request, response) {
response.statusCode = 500;
response.write(
`<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Proxy Eror</title>
</head>
<body>
Opps, Proxy Error...
</body>
</html>`
);
response.end();
},
"response-error-render"
);
});
Contribution
if you find code interesting you may participate by updating documentation using pull request or mail messages to mailto:sergiu.gordienco@gmail.com
License of Application Prototype
Creative Commons License Creative Commons Attribution-NonCommercial 4.0 International License

JS Application Builder by JavaScript Application Builder is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at http://sgapps.io. Permissions beyond the scope of this license may be available at http://sgapps.io.
API
Table of Contents
Handler
Type: Function
proxyServerProxyServer
ProxyServer
Type: function ()
debugEnabled
check if debug mode is enabled
Type: function (): boolean
debugEnabled
enable disable debug mode
Type: function (status): boolean
statusboolean
error
log a error if debug mode is enabled
Type: function (err)
errError
log
logs if debug mode is enabled
Type: function (items)
items...any
handleRequest
handle for http server request / response
Type: function (request, response)
configuration
returns proxy current configuration
Type: function (): SGAppsProxy.Config
string2Rule
converts a string rule into regular expression ( case sensitive )
Type: function (rule): RegExp
rulestring example "*.sgapps.io"
close
close ProxyServer
Type: function (cb)
cbFunction callback receives an error if it occurs
isRunning
check if ProxyServer is running, it returns null if no http server is attached to ProxyServer
Type: function (): boolean
proxyServer
return proxy instance
Type: function (): ProxyServer
httpServer
returns http server user by ProxyServer return type Server
Type: function (): object
responseRenderPath
render a file in response, file piped to response
Type: function (response, path, status)
Cache
Type: object
questionobject appear after events proxy-upgrade or http-requestresultArray<SGAppsProxy.ProxyRule>
protocol
returns current used protocol
Type: function (defaultProtocol): string
defaultProtocolstring? if is empty returns configuration default protocol
SGAppsProxy
Type: function ()
ParsedUrl
Type: object
protocolstring ( ex: 'https:' )slashesboolean ( ex: true )authstring ( ex: null or 'username:password' )hoststring ( ex: 'sgapps.io:808' )portstring ( ex: '808' )hostnamestring ( ex: 'sgapps.io' )hashstring ( ex: '#23' )searchstring ( ex: '?query=234' )querystring ( ex: 'query=234' )pathnamestring ( ex: '/path' )pathstring ( ex: '/path?query=234' )hrefstring ( ex: 'https://sgapps.io:808/path?query=234#23' )
ProxyRule
Type: object
client_hoststring ( ex: '127.0.0.1' )client_portnumber ( ex: 80 )domainsArray<string> array or domain mappers ( ex: "*.sgapps.io" )client_pathstring? ( ex: '/some/path/' )protocol(string |"http"|"https")? ( ex: null or 'username:password' )proxyFilterSGAppsProxy.ProxyFilterproxyRefsobjectproxyOptionsSGAppsProxy.ProxyOptionsproxyOptionsHandlerSGAppsProxy.ProxyOptionsHandler
ProxyFilter
if returns null the proxy server will not continue to parse flow
if returns false the proxy server will ignore rule
if returns true the proxy server will accept rule
if returns Error the proxy will accept render error page
Type: Function
proxyRefsobjectproxyOptionsSGAppsProxy.ProxyOptionsrequestRequestresponseResponseruleSGAppsProxy.ProxyRulecacheSGAppsProxy.ProxyServer.Cache
ProxyOptionsHandler
if returns null the proxy server will not continue to parse flow
Type: Function
proxyOptionsSGAppsProxy.ProxyOptionsrequestRequestresponseResponsecacheSGAppsProxy.ProxyServer.CacheresultanyresponseRenderPathFunction
ProxyOptions
Type: object
targetstring? url string to be parsed with the url moduleforwardstring? url string to be parsed with the url moduleagentobject? object to be passed to http(s).request (see Node's https agent and http agent objects)sslobject? object to be passed to https.createServer()wsboolean? true/false, if you want to proxy websocketsxfwdboolean? true/false, adds x-forward headerssecureboolean? true/false, if you want to verify the SSL CertstoProxyboolean? true/false, passes the absolute URL as the path (useful for proxying to proxies)prependPathboolean? true/false, Default: true - specify whether you want to prepend the target's path to the proxy pathignorePathboolean? true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request (note: you will have to append / manually if required).localAddressstring? Local interface string to bind for outgoing connectionschangeOriginboolean? true/false, Default: false - changes the origin of the host header to the target URLpreserveHeaderKeyCaseboolean? true/false, Default: false - specify whether you want to keep letter case of response header keyauthstring? Basic authentication i.e. 'user:password' to compute an Authorization header.hostRewriteboolean? rewrites the location hostname on (201/301/302/307/308) redirects.autoRewriteboolean? rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.protocolRewriteboolean? rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.cookieDomainRewriteboolean? rewrites domain of set-cookie headers. Possible values: false (default): disable cookie rewriting String: new domain, for example cookieDomainRewrite: "new.domain". To remove the domain, use cookieDomainRewrite: "". Object: mapping of domains to new domains, use "*" to match all domains. For example keep one domain unchanged, rewrite one domain and remove other domains:cookieDomainRewrite: { "unchanged.domain": "unchanged.domain", "old.domain": "new.domain", "*": "" }cookiePathRewriteboolean? rewrites path of set-cookie headers. Possible values: false (default): disable cookie rewriting String: new path, for example cookiePathRewrite: "/newPath/". To remove the path, use cookiePathRewrite: "". To set path to root use cookiePathRewrite: "/". Object: mapping of paths to new paths, use "*" to match all paths. For example, to keep one path unchanged, rewrite one path and remove other paths:cookiePathRewrite: { "/unchanged.path/": "/unchanged.path/", "/old.path/": "/new.path/", "*": "" }headersobject? object with extra headers to be added to target requests.proxyTimeoutnumber? timeout (in milliseconds) for outgoing proxy requeststimeoutnumber? timeout (in milliseconds) for incoming requestsfollowRedirectsboolean? true/false, Default: false - specify whether you want to follow redirectsselfHandleResponseboolean? true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes eventbufferBuffer? stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:'use strict';const streamify = require('stream-array'); const HttpProxy = require('http-proxy'); const proxy = new HttpProxy();module.exports = (req, res, next) => {proxy.web(req, res, { target: 'http://localhost:4003/', buffer: streamify(req.rawBody) }, next);};responseobject?response.headersobject headers to be merger to response headers
Config
Type: object
defaultProtocolstring?secureVerifyboolean?proxyRulesArray<SGAppsProxy.ProxyRule>httpServerobject?debugboolean?
SGAppsProxy
SGApps.io / Proxy Server
Type: function (configuration, httpServerInstance): SGAppsProxy.Handler
configurationSGAppsProxy.ConfighttpServerInstancehttpServer
Meta
- author: Sergiu Gordienco Vasile
ParsedUrl
Type: object
protocolstring ( ex: 'https:' )slashesboolean ( ex: true )authstring ( ex: null or 'username:password' )hoststring ( ex: 'sgapps.io:808' )portstring ( ex: '808' )hostnamestring ( ex: 'sgapps.io' )hashstring ( ex: '#23' )searchstring ( ex: '?query=234' )querystring ( ex: 'query=234' )pathnamestring ( ex: '/path' )pathstring ( ex: '/path?query=234' )hrefstring ( ex: 'https://sgapps.io:808/path?query=234#23' )
ProxyRule
Type: object
client_hoststring ( ex: '127.0.0.1' )client_portnumber ( ex: 80 )domainsArray<string> array or domain mappers ( ex: "*.sgapps.io" )client_pathstring? ( ex: '/some/path/' )protocol(string |"http"|"https")? ( ex: null or 'username:password' )proxyFilterSGAppsProxy.ProxyFilterproxyRefsobjectproxyOptionsSGAppsProxy.ProxyOptionsproxyOptionsHandlerSGAppsProxy.ProxyOptionsHandler
ProxyFilter
if returns null the proxy server will not continue to parse flow
if returns false the proxy server will ignore rule
if returns true the proxy server will accept rule
if returns Error the proxy will accept render error page
Type: Function
proxyRefsobjectproxyOptionsSGAppsProxy.ProxyOptionsrequestRequestresponseResponseruleSGAppsProxy.ProxyRulecacheSGAppsProxy.ProxyServer.Cache
ProxyOptionsHandler
if returns null the proxy server will not continue to parse flow
Type: Function
proxyOptionsSGAppsProxy.ProxyOptionsrequestRequestresponseResponsecacheSGAppsProxy.ProxyServer.CacheresultanyresponseRenderPathFunction
ProxyOptions
Type: object
targetstring? url string to be parsed with the url moduleforwardstring? url string to be parsed with the url moduleagentobject? object to be passed to http(s).request (see Node's https agent and http agent objects)sslobject? object to be passed to https.createServer()wsboolean? true/false, if you want to proxy websocketsxfwdboolean? true/false, adds x-forward headerssecureboolean? true/false, if you want to verify the SSL CertstoProxyboolean? true/false, passes the absolute URL as the path (useful for proxying to proxies)prependPathboolean? true/false, Default: true - specify whether you want to prepend the target's path to the proxy pathignorePathboolean? true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request (note: you will have to append / manually if required).localAddressstring? Local interface string to bind for outgoing connectionschangeOriginboolean? true/false, Default: false - changes the origin of the host header to the target URLpreserveHeaderKeyCaseboolean? true/false, Default: false - specify whether you want to keep letter case of response header keyauthstring? Basic authentication i.e. 'user:password' to compute an Authorization header.hostRewriteboolean? rewrites the location hostname on (201/301/302/307/308) redirects.autoRewriteboolean? rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.protocolRewriteboolean? rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.cookieDomainRewriteboolean? rewrites domain of set-cookie headers. Possible values: false (default): disable cookie rewriting String: new domain, for example cookieDomainRewrite: "new.domain". To remove the domain, use cookieDomainRewrite: "". Object: mapping of domains to new domains, use "*" to match all domains. For example keep one domain unchanged, rewrite one domain and remove other domains:cookieDomainRewrite: { "unchanged.domain": "unchanged.domain", "old.domain": "new.domain", "*": "" }cookiePathRewriteboolean? rewrites path of set-cookie headers. Possible values: false (default): disable cookie rewriting String: new path, for example cookiePathRewrite: "/newPath/". To remove the path, use cookiePathRewrite: "". To set path to root use cookiePathRewrite: "/". Object: mapping of paths to new paths, use "*" to match all paths. For example, to keep one path unchanged, rewrite one path and remove other paths:cookiePathRewrite: { "/unchanged.path/": "/unchanged.path/", "/old.path/": "/new.path/", "*": "" }headersobject? object with extra headers to be added to target requests.proxyTimeoutnumber? timeout (in milliseconds) for outgoing proxy requeststimeoutnumber? timeout (in milliseconds) for incoming requestsfollowRedirectsboolean? true/false, Default: false - specify whether you want to follow redirectsselfHandleResponseboolean? true/false, if set to true, none of the webOutgoing passes are called and it's your responsibility to appropriately return the response by listening and acting on the proxyRes eventbufferBuffer? stream of data to send as the request body. Maybe you have some middleware that consumes the request stream before proxying it on e.g. If you read the body of a request into a field called 'req.rawbody' you could restream this field in the buffer option:'use strict';const streamify = require('stream-array'); const HttpProxy = require('http-proxy'); const proxy = new HttpProxy();module.exports = (req, res, next) => {proxy.web(req, res, { target: 'http://localhost:4003/', buffer: streamify(req.rawBody) }, next);};responseobject?response.headersobject headers to be merger to response headers
Config
Type: object
defaultProtocolstring?secureVerifyboolean?proxyRulesArray<SGAppsProxy.ProxyRule>httpServerobject?debugboolean?
ApplicationPrototype
- See: ApplicationPrototype
Type: function ()