Skip to content

A simple content-altering HTTP web proxy developed in Python.

Notifications You must be signed in to change notification settings

alexandengstrom/proxy-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP web proxy

A content-altering HTTP web proxy developed in python.

1. Installation

To run the proxy server the following files are required

  • main.py
  • proxy.py
  • request.py
  • response.py
  • timeparser.py

You also need Python installed on your machine.

2. Configuration

First we create an instance of the proxy, this can be done in the following way:

proxy = Proxy("127.0.0.1", 9999, 10)

When a new proxy-server is created, three values are required. The first value is the IP-address where the proxy will be hosted. The second value is which port the proxy is going to use. The last value is the maximum numbers of connections in the queue waiting to be accepted.

When an instance of a proxy-server is created, it can be configured to match your requirements:

Use caching:

proxy.use_cache = True

If use_cache is set to True the proxy will use its own cache to retrieve websites you have already visited without sending a new requests to the server.

Close all connections directly:

proxy.keep_alive = False

If keep_alive is set to False the header "Connection:" will be set to "close" in all requests. Otherwise it is set to "keep-alive".

Manipulate requests:

proxy.add_request_replacement("match", "replacement")

The first parameter "match" should be a regular expression and everything that matches that pattern will be replaced with the parameter "replacement". The proxy will only replace content in the first line of the request when using this method.

Manipulate responses:

proxy.add_request_replacement("match", "replacement")

The first parameter "match" should be a regular expression and everything that matches that pattern will be replaced with the parameter "replacement". The proxy will replace everything in the response-body.

3. Run

When the proxy is configured to match your requirements, call the method run to start the server.

proxy.run()

4. Unit Tests

Run the file "unit_test.py" to run all existing unit tests.

Examples

This is how you could configure the proxy server to do the following thing:

  • Replace all jpg and png-images to a trollface-picture.
from proxy import Proxy

if __name__ == "__main__":
    proxy = Proxy("127.0.0.1", 9999, 10)
    proxy.keep_alive = False
    proxy.use_cache = True
    proxy.add_request_replacement("(?<= ).*.[jpg|png]", "https://yourhost.com/trollface.jpg")
    proxy.run()

How the website would look without the proxy:

Screenshot from 2023-02-06 14-16-12

How the website would look with the proxy:

Screenshot from 2023-02-06 14-15-26

In this example we want to replace all occurences of the word "Alice" to "Trolly".

from proxy import Proxy

if __name__ == "__main__":
    proxy = Proxy("127.0.0.1", 9999, 10)
    proxy.keep_alive = False
    proxy.use_cache = True
    proxy.add_response_replacement('(?<=[\s|"])Alice', 'Trolly')
    proxy.run()

How the website would look without the proxy:

Screenshot from 2023-02-06 14-21-20

How the website would look with the proxy:

Screenshot from 2023-02-06 14-25-51

About

A simple content-altering HTTP web proxy developed in Python.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages