Enable WebServer plugin when --pac_file serving is requested.
1. Also binary read pac file instead of str 2. Return 404 for unhandled WebServer requests
This commit is contained in:
parent
2c239a1d25
commit
e463d428b9
|
@ -0,0 +1,4 @@
|
||||||
|
function FindProxyForURL(url, host)
|
||||||
|
{
|
||||||
|
return "PROXY localhost:8899; DIRECT";
|
||||||
|
}
|
22
proxy.py
22
proxy.py
|
@ -55,7 +55,7 @@ DEFAULT_ENABLE_WEB_SERVER = False
|
||||||
DEFAULT_LOG_LEVEL = 'INFO'
|
DEFAULT_LOG_LEVEL = 'INFO'
|
||||||
DEFAULT_OPEN_FILE_LIMIT = 1024
|
DEFAULT_OPEN_FILE_LIMIT = 1024
|
||||||
DEFAULT_PAC_FILE = None
|
DEFAULT_PAC_FILE = None
|
||||||
DEFAULT_PAC_FILE_URL_PATH = '/'
|
DEFAULT_PAC_FILE_URL_PATH = b'/'
|
||||||
DEFAULT_NUM_WORKERS = 0
|
DEFAULT_NUM_WORKERS = 0
|
||||||
DEFAULT_PLUGINS = {}
|
DEFAULT_PLUGINS = {}
|
||||||
DEFAULT_LOG_FORMAT = '%(asctime)s - %(levelname)s - pid:%(process)d - %(funcName)s:%(lineno)d - %(message)s'
|
DEFAULT_LOG_FORMAT = '%(asctime)s - %(levelname)s - pid:%(process)d - %(funcName)s:%(lineno)d - %(message)s'
|
||||||
|
@ -122,8 +122,9 @@ class TcpConnection(object):
|
||||||
'Exception while receiving from connection %s %r with reason %r' % (self.what, self.conn, e))
|
'Exception while receiving from connection %s %r with reason %r' % (self.what, self.conn, e))
|
||||||
|
|
||||||
def close(self) -> bool:
|
def close(self) -> bool:
|
||||||
self.conn.close()
|
if not self.closed:
|
||||||
self.closed = True
|
self.conn.close()
|
||||||
|
self.closed = True
|
||||||
return self.closed
|
return self.closed
|
||||||
|
|
||||||
def buffer_size(self) -> int:
|
def buffer_size(self) -> int:
|
||||||
|
@ -854,14 +855,14 @@ class HttpWebServerPlugin(HttpProtocolBasePlugin):
|
||||||
self.request.url.path == self.config.pac_file_url_path:
|
self.request.url.path == self.config.pac_file_url_path:
|
||||||
self.serve_pac_file()
|
self.serve_pac_file()
|
||||||
else:
|
else:
|
||||||
|
# Catch all unhandled web server requests, return 404
|
||||||
self.client.queue(CRLF.join([
|
self.client.queue(CRLF.join([
|
||||||
b'HTTP/1.1 200 OK',
|
b'HTTP/1.1 404 NOT FOUND',
|
||||||
b'Server: proxy.py v%s' % version,
|
b'Server: proxy.py v%s' % version,
|
||||||
b'Content-Length: 42',
|
|
||||||
b'Connection: Close',
|
b'Connection: Close',
|
||||||
CRLF,
|
CRLF
|
||||||
b'https://github.com/abhinavsingh/proxy.py'
|
|
||||||
]))
|
]))
|
||||||
|
# But is client ready for flush?
|
||||||
self.client.flush()
|
self.client.flush()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -875,7 +876,7 @@ class HttpWebServerPlugin(HttpProtocolBasePlugin):
|
||||||
def serve_pac_file(self):
|
def serve_pac_file(self):
|
||||||
self.client.queue(self.PAC_FILE_RESPONSE_PREFIX)
|
self.client.queue(self.PAC_FILE_RESPONSE_PREFIX)
|
||||||
try:
|
try:
|
||||||
with open(self.config.pac_file, 'r') as f:
|
with open(self.config.pac_file, 'rb') as f:
|
||||||
logger.debug('Serving pac file from disk')
|
logger.debug('Serving pac file from disk')
|
||||||
self.client.queue(f.read())
|
self.client.queue(f.read())
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -966,7 +967,8 @@ class HttpProtocolHandler(threading.Thread):
|
||||||
plugin_response = plugin.on_request_complete()
|
plugin_response = plugin.on_request_complete()
|
||||||
if type(plugin_response) is bool:
|
if type(plugin_response) is bool:
|
||||||
return True
|
return True
|
||||||
except HttpProtocolException as e: # ProxyAuthenticationFailed, ProxyConnectionFailed, HttpRequestRejected
|
# ProxyAuthenticationFailed, ProxyConnectionFailed, HttpRequestRejected
|
||||||
|
except HttpProtocolException as e:
|
||||||
# logger.exception(e)
|
# logger.exception(e)
|
||||||
response = e.response(self.request)
|
response = e.response(self.request)
|
||||||
if response:
|
if response:
|
||||||
|
@ -1125,6 +1127,8 @@ def main():
|
||||||
client_recvbuf_size=args.client_recvbuf_size,
|
client_recvbuf_size=args.client_recvbuf_size,
|
||||||
pac_file=args.pac_file,
|
pac_file=args.pac_file,
|
||||||
pac_file_url_path=args.pac_file_url_path)
|
pac_file_url_path=args.pac_file_url_path)
|
||||||
|
if config.pac_file is not None:
|
||||||
|
args.enable_web_server = True
|
||||||
|
|
||||||
default_plugins = ''
|
default_plugins = ''
|
||||||
if args.enable_http_proxy:
|
if args.enable_http_proxy:
|
||||||
|
|
Loading…
Reference in New Issue