Project Status
Use case
Easy and dynamically configurable reverse proxy. This tool can act as a middleware to check incoming requests via external bot filtering services such as ReCaptcha or friendlycaptcha. Adding state via session token with external authentication apis is possible as applying any external api given data to the incoming and conditionally forwarded requests.
Content
Installation
You can install via package manager, simply download the compiled version as zip file here and inject or request via CDN in HTML:
npm install reverse-proxy-middleware
Example
Simple forwarding
Simple reverse proxy request from http://localhost:8080 to https://www.google.com without modifying the entire request.
{
"forwarders": {
"google": {
"host": "www.google.com"
}
}
}
Since the proxy starts at localhost on port 8080 as a default configuration you can check the configuration via a simple curl command:
curl --verbose http://localhost:8080
Behind there are a some commonly use defaults configured under key "configuration" in package.json. Please have a look.
Distributing requests to different backends
Here is how to distribute incoming requests randomly between google and bing:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"useExpression": "Math.random() < 0.5"
},
"google": {
"host": "www.google.com"
}
}
}
Forwarders are iterated in alphabetical order of their name and useExpression are getting evaluated. Since "google" is always "used" it depends on the random outcome of bing's expression if it is beeing used or not.
Since this proxy just streams the whole request through it could be used as basic load balancer with this configuration.
Rewriting headers for underlying backend
Headers can be replaced in both directions. Client-Request to forward or retrieved responses given from configured backend:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"headerTransformations": {
"send": {
"source": "/X-Special-Client-Header-Name: (.+)/gi",
"target": "'New-Header-Name: $1'"
},
"retrieve": {
"source": "/set-cookie: (.+)/gi",
"target": "'X-Prevented-Cookie: $1'"
}
}
}
}
}
Test via:
curl \
--header 'X-Special-Client-Header-Name: value' \
--verbose \
http://localhost:8080 \
1>/dev/null
You should see a lot of cookie header ("set-cookie: ..." replaced by "X-Prevented-Cookie: ...". Note that muting the standart output ("1>/dev/null") enables you to focus on retrieved headers printed via secondary error output.
State-APIs
State-APIs enables you to conditionally trigger requests to third party endpoints and use responses for further decisions how to proceed. When a state api is used for a specified request that response can be used via expressions to transform subsequent api requests, decide which backend to use or transform the final backend request.
In the following example you can see how a state api is configured to catch specific request to deal with. The last running pre expressions which results in a boolean value will trigger if the referenced state api should be used for given request.
Here is an example:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"stateAPIs": {
"name": "sense",
"url": "https://www.google.com/search?q=${data.question}",
"data": {
"question": "What is the meaning of life?"
},
"expressions": {
"pre": "request.headers['ask-for-sense-of-live'] ? true : null",
"post": "response.statusCode >= 200 && response.statusCode < 300 ? null : response.statusCode"
}
}
}
}
}
What is going on here? We generally forward requests to "www.bing.com" but if there is a header called "ask-for-sense-of-live" present in client request we will first ask google for the meaning of life. If google answers with a "negative" response code not between 200 and 300 we will just do the final forwarding to bing. If google responses in a positive manner the resulting status code we will be used for answering client and no forwarding to "www.bing.com" happens.
Here es a test curl command:
curl \
--header 'ask-for-sense-of-live: value' \
--verbose \
http://localhost:8080 \
1>/dev/null
This should result in a simple response but:
curl --verbose http://localhost:8080 1>/dev/null
will finally request "www.bing.com".
Pre and post evaluations can have various results. The meanings of them are described here:
Pre-Evaluation Results
| Result | Meaning |
|---|---|
| break (string) | Do not evaluate subsequent pre evaluations. |
| null or undefined | Just jump to the next evaluation to run. |
| true (boolean) | Use this state api configuration. Run the configured request. |
| false (boolean) | Do not use this state api and to not run subsequent pre evaluations. |
| code (number) | Answer client request with provided http status code and do not run any subsequent pre-evaluations, state-api request or request forwarding to the underlying backend. |
Post-Evaluation Results
| Result | Meaning |
|---|---|
| break (string) | Do not evaluate subsequent post evaluations. |
| null or undefined | Just jump to the next evaluation to run. |
| code (number) | Answer client request with provided http status code and do not run any subsequent post-evaluations, state-api request or request forwarding to the underlying backend. |
Smart configurations
Whenever you can configure list of items you can either use just one or a list of them. Consider this configuration example:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"headerTransformations": [{...}, {...}, ...],
"stateAPIs": {
"expressions": {
"pre": ["...", "...", ...],
"post": ["...", "...", ...]
}
}
}
}
}
If only one item is needed please consider that:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"headerTransformations": [{...}],
"stateAPIs": {
"expressions": {
"pre": ["..."],
"post": ["..."]
}
}
}
}
}
is equivalent to:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"headerTransformations": {...},
"stateAPIs": {
"expressions": {
"pre": "...",
"post": "..."
}
}
}
}
}
Use environment variables
While some configuration values are interpret as expression to be evalued at runtime e.g. to decide which endpoint to use:
{
"forwarders": {
"endpoint": {
...
"useExpression": "..."
}
}
}
Every item can utilize expression to dynamically derive intial configurations:
{
"port": {
"__evaluate__": "environment.PORT ?? 8080"
},
...
}
If an environment variabel "PORT" is set it will be used or "8080" as a fallback.
Use Base Forwarder
Base forwarder are inherited by every specific forwarder. This configuration:
{
"forwarders": {
"base: {
"headerTransformations": {
"send": {
"source": "/(GET|POST) \\/.* (.*)/",
"target": "'$1 /sub/path/to/forward/to $2'"
}
}
},
"bing": {
"host": "www.bing.com",
"useExpression": "Math.random() < 0.5"
},
"google": {
"host": "www.google.com"
}
}
}
is equivalent to:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"useExpression": "Math.random() < 0.5",
"headerTransformations": {
"send": {
"source": "/(GET|POST) \\/.* (.*)/",
"target": "'$1 /sub/path/to/forward/to $2'"
}
}
},
"google": {
"host": "www.google.com",
"headerTransformations": {
"send": {
"source": "/(GET|POST) \\/.* (.*)/",
"target": "'$1 /sub/path/to/forward/to $2'"
}
}
}
}
}
Use base State-APIs
As we support generic base forwarder confgurations there are also base state api configuraton sections. Consider the follwing configuration example:
{
"forwarders": {
"bing": {
"host": "www.bing.com",
"stateAPIs": [
{
"name": "base",
"url": "https://www.google.com/search?q=${data.query}",
"expressions": {
"pre": "request.headers['ask-google'] ? true : null",
"post": "response.statusCode >= 200 && response.statusCode < 300 ? null : response.statusCode"
}
}
{
"name": "sense",
"data": {
"query": "What is the meaning of life?"
}
},
{
"name": "nonesense",
"data": {
"query": "baby cats"
}
}
]
}
}
}
Note that url and expression are inherited to the state apis "sense" and "nonsense". The Data field can save various configuration items to be used in runtime expressions. Please also note that it is possible to access configuration, request or response informations from other state apis in every runtime expression.
Every expression can access the following environment:
| Name | Meaning |
|---|---|
| data | Generic configuration items. |
| error | Error object if some occured. |
| request | Client request informations. |
| response | Response informations (available in post evaluations). |
| stateAPIs | Access oher state api informations. |
| Tools | see |
About this website
Provider ofhttps://tsickert.com/reverse-proxy-middleware:
Torben Sickert
Waldstraße 29
791194 Gundelfingen i.Br.
Phone: +49 (0) 176 / 10 248 185
Email: [email protected]
Website: https://tsickert.com
Disclaimer
Liability for content
The content of our pages has been prepared with the utmost care. However, we cannot guarantee the accuracy, completeness or timeliness of the content. As a service provider, we are responsible for our own content on these pages under general law. However, we are not obliged to monitor transmitted or stored third-party information or investigate circumstances that indicate illegal activity. Obligations to remove or block the use of information under general law remain unaffected. Liability in this respect is only possible from the time we become aware of a specific infringement. Upon becoming aware of any such legal violations, we will remove this content immediately.
Liability for links
Our website contains links to external third-party websites whose content is beyond our control. We therefore cannot accept any responsibility for such third-party content. The respective provider or operator of each linked site is always responsible for its content. The linked sites were checked for possible legal violations when the links were created. No illegal content was apparent at that time. However, continuous monitoring of linked sites is not reasonable without specific evidence of a legal violation. If we become aware of any legal violations, we will remove the relevant links immediately.
Copyright
Content and works on these pages created by the site operators are subject to German copyright law. Reproduction, adaptation, distribution and any use beyond the scope of copyright law require the written consent of the respective author or creator. Downloads and copies of this site are permitted for private, non-commercial use only. Where content on this site was not created by the operator, third-party copyrights are respected. In particular, third-party content is identified as such. If you become aware of a copyright infringement, please notify us. Upon becoming aware of any legal violations, we will remove the relevant content immediately.
Privacy Policy
You can generally use our website without providing personal data. Where personal data (such as names, addresses or email addresses) is collected on our pages, this is, wherever possible, always done on a voluntary basis. This data will not be passed on to third parties without your express consent. Please note that data transmission over the internet (for example, when communicating by email) may have security vulnerabilities. It is not possible to completely protect data against access by third parties. We expressly object to the use by third parties of contact details published to meet legal notice requirements for sending unsolicited advertising or information. The site operators expressly reserve the right to take legal action if unsolicited advertising, such as spam emails, is sent.
Content Delivery Network (Cloudflare)
We use Cloudflare (Cloudflare, Inc., 101 Townsend St., San Francisco, CA 94107, USA) to protect and speed up our website. This constitutes a legitimate interest within the meaning of Art. 6(1)(f) GDPR. Cloudflare processes log data (including IP addresses) and sets technically necessary cookies to defend against malicious bot traffic. We have concluded a Data Processing Addendum (DPA) with Cloudflare based on the EU Standard Contractual Clauses. Cloudflare is certified under the EU-U.S. Data Privacy Framework.
Privacy Statement for using Google Analytics
This website uses Google Analytics 4, a web analytics service provided by Google Ireland Limited ("Google", Gordon House, Barrow Street, Dublin 4, Ireland). Data is processed solely on the basis of your express consent (Art. 6(1)(a) GDPR). Google Analytics uses cookies that enable analysis of how you use the website. Information generated by these cookies about your use of this website is transmitted to and stored on a Google server; transfer of data to third countries (such as the United States) cannot be ruled out. IP anonymization is enabled by default on this website, so Google truncates your IP address within a member state of the European Union or another state party to the Agreement on the European Economic Area before transmission. You may withdraw your consent at any time with effect for the future through the cookie settings (our consent tool), or prevent data collection by using the official Google browser add-on .
Copyright Statement
Unless noted otherwise, all artwork on this website is protected property of the website author. Any use for commercial purpose, reproduction and publication requires the author's written permission. Commercial work created in cooperation or bound by contract with third parties are specifically marked. Respective copyrights apply.