Skip to content

A simple reverse http proxy server for testing purposes. It forwards requests to another server, port and path prefix and returns the response.

License

Notifications You must be signed in to change notification settings

ankraft/simple-reverse-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-reverse-proxy

A simple reverse proxy server for testing purposes. It forwards requests to another server, port and path prefix and returns the response.

The following features are supported:

  • Forwarding of HTTP requests to another server
  • Configurable target server, port, and path prefix
  • Mapping of incoming request paths to target paths
  • Delay simulation for testing purposes
  • Simulate unreliable network conditions with random failures

Please note that this is a simple implementation intended for testing and development purposes only. It is not suitable for production use.

Installation

Download the code and run the following command to install the required dependencies:

pip install -r requirements.txt

Requirements

  • Python 3.10 or higher

Running the Proxy

The proxy server can be run with various options to customize its behavior.

  • --port, -p PORT
    Port to listen on. Default is 9090.
  • --paths, -pa PATHS [PATHS ...]
    List of path prefixes to map. Default is '/'.
  • --target-port, -tp TARGET_PORT
    Target server port. Default is 8080.
  • --target-host, -th TARGET_HOST
    Target server host. Default is 'localhost'.
  • --target-paths, -tpa TARGET_PATHS [TARGET_PATHS ...]
    Target server path prefix list to map to. Default is '/'.
  • --target-scheme, -ts {http,https}
    Target server scheme (http or https). Default is 'http'.
  • --delay, -d DELAY
    Artificial delay in ms before forwarding each request. Default is 0 (no delay).
  • --fail-rate, -fr FAIL_RATE
    Simulated failure rate (0.0 to 1.0). Default is 0.0 (no failures).
  • --fail-type, -ft {unavailable,timeout,unreachable}
    Type of simulated failure. Default is 'unavailable'.
  • --verbose, -v
    Enable verbose output. Default is false.

Both the --paths and --target-paths options accept multiple values to map different path prefixes. The number of entries in both lists must match.

Basic Requests Forwarding

To run the proxy server to forward requests, use the following command:

python proxy.py --target-host <TARGET_HOST> --target-port <TARGET_PORT>

Example

python proxy.py --target-host example.com --target-port 80

Forwarding with Path Mapping

To forward requests with path mapping, use the --path-mapping option:

python proxy.py --target-host <TARGET_HOST> --target-port <TARGET_PORT> --paths <Path at proxy> --target-paths <PATH_MAPPING>

This will map requests from <Path at proxy> to <PATH_MAPPING> on the target server.

Example

python proxy.py --target-host example.com --target-port 80 --paths /api --target-paths /v1/api

Simulating Delays and Failures

To simulate network delays and failures, use the --delay, --fail-rate, and --fail-type options:

python proxy.py --target-host <TARGET_HOST> --target-port <TARGET_PORT> --delay <DELAY_IN_MS> --fail-rate <FAILURE_RATE> --fail-type <FAILURE_TYPE>

Example

To introduce a 1000 ms delay before forwarding each request:

python proxy.py --target-host example.com --target-port 80 --delay 1000

To simulate a 10% failure rate with timeout failures:

python proxy.py --target-host example.com --target-port 80 --fail-rate 0.1 --fail-type timeout

Handling Location Headers in Responses

The proxy server automatically adjusts Location headers in HTTP responses to ensure that redirects point back to the proxy server rather than the target server. This is particularly useful when the target server issues redirects, and you want clients to continue interacting with the proxy.

About

A simple reverse http proxy server for testing purposes. It forwards requests to another server, port and path prefix and returns the response.

Resources

License

Stars

Watchers

Forks

Languages