Update README to reflect basic auth support.

Also refactor proxy exception handling code.
This commit is contained in:
Abhinav Singh 2018-12-13 22:31:56 +05:30
parent 3269998cd5
commit e23444b766
2 changed files with 24 additions and 17 deletions

View File

@ -1,14 +1,15 @@
proxy.py
========
Lightweight HTTP Proxy Server in Python.
Lightweight HTTP, HTTPS and WebSockets Proxy Server in Python.
Features
--------
- Distributed as a single file module
- No dependency other than the Python standard library
- Support for http, https, websockets request proxy
- No external dependency other than standard Python library
- Support for `http`, `https` and `websockets` request proxy
- Basic authentication support
Install
-------
@ -17,24 +18,27 @@ To install proxy.py, simply:
$ pip install proxy.py
This will add `proxy.py` inside your python environment bin folder.
Usage
-----
```
$ proxy.py -h
usage: proxy.py [-h] [--hostname HOSTNAME] [--port PORT]
[--log-level LOG_LEVEL]
usage: proxy.py [-h] [--hostname HOSTNAME] [--port PORT] [--backlog BACKLOG]
[--basic-auth BASIC_AUTH] [--log-level LOG_LEVEL]
proxy.py v0.1
proxy.py v0.3
optional arguments:
-h, --help show this help message and exit
--hostname HOSTNAME Default: 127.0.0.1
--port PORT Default: 8899
--backlog BACKLOG Default: 100. Maximum number of pending connections to
proxy server
--basic-auth BASIC_AUTH
Default: No authentication. Specify colon separated
user:password to enable basic authentication.
--log-level LOG_LEVEL
DEBUG, INFO, WARNING, ERROR, CRITICAL
DEBUG, INFO (default), WARNING, ERROR, CRITICAL
Having difficulty using proxy.py? Report at:
https://github.com/abhinavsingh/proxy.py/issues/new

View File

@ -294,13 +294,14 @@ class Connection(object):
self.what = what # server or client
def send(self, data):
# TODO: Gracefully handle BrokenPipeError exceptions
return self.conn.send(data)
def recv(self, b=8192):
try:
data = self.conn.recv(b)
if len(data) == 0:
logger.debug('recvd 0 bytes from %s' % self.what)
logger.debug('rcvd 0 bytes from %s' % self.what)
return None
logger.debug('rcvd %d bytes from %s' % (len(data), self.what))
return data
@ -505,14 +506,9 @@ class Proxy(threading.Thread):
try:
self._process_request(data)
except ProxyAuthenticationFailed as e:
except (ProxyAuthenticationFailed, ProxyConnectionFailed) as e:
logger.exception(e)
self.client.queue(PROXY_AUTHENTICATION_REQUIRED_RESPONSE_PKT)
self.client.flush()
return True
except ProxyConnectionFailed as e:
logger.exception(e)
self.client.queue(BAD_GATEWAY_RESPONSE_PKT)
self.client.queue(Proxy._get_response_pkt_by_exception(e))
self.client.flush()
return True
@ -547,6 +543,13 @@ class Proxy(threading.Thread):
logger.debug('client buffer is empty and maximum inactivity has reached, breaking')
break
@staticmethod
def _get_response_pkt_by_exception(e):
if e.__class__.__name__ == 'ProxyAuthenticationFailed':
return PROXY_AUTHENTICATION_REQUIRED_RESPONSE_PKT
if e.__class__.__name__ == 'ProxyConnectionFailed':
return BAD_GATEWAY_RESPONSE_PKT
def run(self):
logger.debug('Proxying connection %r' % self.client.conn)
try: