1.1.0 • Published 9 months ago
fimiproxy v1.1.0
fimiproxy
simple HTTP | HTTPS | WS | WSS reverse proxy in node.js. currently supports:
- reverse proxy using incoming request
hostheader to pre-configured origin server. see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host - reverse proxy incoming
http | httpsrequest to originhttp | httpsserver - reverse proxy incoming
ws | wssrequest to originws | wssserver - supports graceful shutdown
- supports round-robin origin server selection
installation
- for global installation
npm i fimiproxy -g - for local installation
npm i fimiproxy - for local dev-dependency installation
npm i fimiproxy -D
replace npm with yarn or any other package manager of choice.
configuration
{
"exposeHttpProxy": false,
"exposeHttpsProxy": false,
"httpPort": "",
"httpsPort": "",
"httpsPublicKeyFilepath": "",
"httpsPrivateKeyFilepath": "",
"httpsPublicKey": "",
"httpsPrivateKey": "",
"routes": [
{
"originHost": "",
"originPort": "",
"originProtocol": "http:",
"incomingHostAndPort": ""
}
]
}exposeHttpProxy— set totrueto expose an HTTP server, requireshttpPortto be set iftrueexposeHttpsProxy— set totrueto expose an HTTPS server, requireshttpsPort,httpsPublicKeyORhttpsPublicKeyFilepath,httpsPrivateKeyORhttpsPrivateKeyFilepathto be set iftrueexposeWsProxyForHttp— set totrueto expose a WebSocket server for HTTP requests, requireshttpPortandexposeHttpProxyto be set iftrueexposeWsProxyForHttps— set totrueto expose a WebSocket server for HTTPS requests, requireshttpsPortandexposeHttpsProxyto be set iftruehttpPort— port HTTP server should listen on, whenexposeHttpProxyistruehttpsPort— port HTTPS server should listen on, whenexposeHttpsProxyistruehttpsPublicKeyFilepath— filepath to TLS certificate (public key) used with HTTPS serverhttpsPrivateKeyFilepath— filepath to TLS private key used with HTTPS serverhttpsPublicKey— TLS certificate (public key) string used with HTTPS server. takes precedence overhttpsPublicKeyFilepathhttpsPrivateKey— TLS private key string used with HTTPS server. takes precedence overhttpsPrivateKeyFilepathroutes— array of incoming host to origin protocol, host, and portoriginHost— origin host or IPoriginPort— origin portoriginProtocol— origin protocol. one ofhttp:orhttps:orws:orwss:. don't forget the:at the endincomingHostAndPort— incominghost:portto proxy to origin server. picked from HTTPhostheader field
How to run
- if installed globally, run
fimiproxy ./path/to/config.json - if installed locally, run
npm exec fimiproxy ./path/to/config.json - for one-time run, run
npx -y fimiproxy ./path/to/config.json
How to use as lib
import fimiproxy from "fimiproxy"
// start fimiproxy
await fimiproxy.startFimiproxyUsingConfig({
/** config */ {
exposeHttpProxy: false,
exposeHttpsProxy: false,
httpPort: "80",
httpsPort: "443",
routes: [{
originHost: "localhost",
originPort: "6001",
originProtocol: "https:",
incomingHostAndPort: "www.fimidara.com:80",
}],
httpsPublicKey: "",
httpsPrivateKey: "",
httpsPublicKeyFilepath: "",
httpsPrivateKeyFilepath: "",
},
/** shouldHandleGracefulShutdown */ true,
/** exitProcessOnShutdown */ true,
});
// end fimiproxy
await fimiproxy.endFimiproxy(/** exitProcessOnShutdown */ true);API
startFimiproxyUsingConfig— start fimiproxy using configconfig: FimiproxyRuntimeConfig— see configuration aboveshouldHandleGracefulShutdown— defaults totrue. iftrue, will listen forSIGINTandSIGTERM, and attempt to gracefully shut down the proxy serverexitProcessOnShutdown— defaults totrue. ifshouldHandleGracefulShutdownistrue, will callprocess.exit()after graceful shutdown. your process may not shut down afterSIGINTandSIGTERMif nottrue. currently untested behaviour (if process will shutdown or not) when set tofalseandshouldHandleGracefulShutdownistrue
startFimiproxyUsingConfigFile— start fimiproxy using config read from filepathfilepath: string— file at filepath should be a json file, see configuration section above
startFimiproxyUsingProcessArgs— start fimiproxy using filepath picked fromprocess.argv[2]see https://nodejs.org/docs/latest/api/process.html#processargv. example,node your-script.js ./path/to/config.jsonendFimiproxy— gracefully end fimiproxyexitProcess— defaults totrue. callsprocess.exit()iftrue
Limitations
- cannot sustain multiple start calls, because current state is managed using a module-global variable. we'll eventually transition to a class-based encapsulation system, so stick around (if you're versed in Typescript, you can contribute to this effort). multiple start calls will either lead to existing servers being garbage collected or memory leak, i haven't tested it. so call
endFimiproxybefore making another start call. start calls are calls tostartFimiproxyUsingConfig,startFimiproxyUsingConfigFile, orstartFimiproxyUsingProcessArgs