Metadata-Version: 2.1
Name: proxy3
Version: 0.1.1
Summary: Proxy3 - Man-in-the-middle http/https proxy in a single python script
Author-email: Yifei Kong <kong@yifei.me>
License: Copyright (c) 2015, inaz2
        Copyright (c) 2023, yifeikong
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        * Redistributions of source code must retain the above copyright notice, this
          list of conditions and the following disclaimer.
        
        * Redistributions in binary form must reproduce the above copyright notice,
          this list of conditions and the following disclaimer in the documentation
          and/or other materials provided with the distribution.
        
        * Neither the name of proxy2 nor the names of its
          contributors may be used to endorse or promote products derived from
          this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: repository, https://github.com/yifeikong/proxy3
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE

# proxy3

Man-in-the-middle http/https proxy in a single python script

## Features

* easy to customize
* require no external modules
* support both of IPv4 and IPv6
* support HTTP/1.1 Persistent Connection
* support dynamic certificate generation for HTTPS intercept

This script works on Python 3.10+.
You need to install `openssl` to intercept HTTPS connections.


## Usage

Just clone and run as a script:

    $ python proxy3.py

Or, install using pip:

    $ pip install proxy3
    $ proxy3

Above command runs the proxy on localhost:6666. Verify it works by typing the below
command in another terminal of the same host.

    # test http proxy
    $ http_proxy=localhost:6666 curl http://www.example.com/

To bind to another host or port:

    $ python proxy3.py --host 0.0.0.0 --port 3128


## Enable HTTPS intercept

To intercept HTTPS connections, generate private keys and a private CA certificate:

    $ python proxy3.py --make-certs
    $ https_proxy=localhost:8887 curl https://www.example.com/

Through the proxy, you can access http://proxy3.test/ and install the CA certificate in the browsers.


## Detailed Usage

    $ python proxy3.py --help

    usage: proxy3.py [-h] [-H HOST] [-p PORT] [--timeout TIMEOUT] [--ca-key CA_KEY]
                     [--ca-cert CA_CERT] [--ca-signing-key CA_SIGNING_KEY]
                     [--cert-dir CERT_DIR] [--request-handler REQUEST_HANDLER]
                     [--response-handler RESPONSE_HANDLER] [--save-handler SAVE_HANDLER]
                     [--make-certs]

    options:
      -h, --help            show this help message and exit
      -H HOST, --host HOST  Host to bind, default localhost
      -p PORT, --port PORT  Port to bind, default 6666
      --timeout TIMEOUT     Timeout, default 5
      --ca-key CA_KEY       CA key file
      --ca-cert CA_CERT     CA cert file
      --ca-signing-key CA_SIGNING_KEY
                            CA cert key file
      --cert-dir CERT_DIR   Site certs files
      --request-handler REQUEST_HANDLER
                            Request handler function
      --response-handler RESPONSE_HANDLER
                            Response handler function
      --save-handler SAVE_HANDLER
                            Save handler function
      --make-certs          Create https intercept certs


## Customization

You can easily customize the proxy and modify the requests/responses or save something to the files.
The ProxyRequestHandler class has 3 methods to customize:

* request_handler: called before accessing the upstream server
* response_handler: called before responding to the client
* save_handler: called after responding to the client with the exclusive lock, so you can safely write out to the terminal or the file system

By default, only save_handler is implemented which outputs HTTP(S) headers and some useful data to the standard output.
