cleanup string interpolations

This commit is contained in:
Michele Guerini Rocco 2019-05-14 19:47:54 +02:00
parent 4d7b0bd27f
commit abfb140dc8
Signed by: rnhmjoj
GPG Key ID: 91BE884FBA4B591A

118
main.py
View File

@ -148,7 +148,7 @@ class FrontRequestHandler(ProxyRequestHandler):
Sit between the client and Privoxy Sit between the client and Privoxy
Convert https request to http Convert https request to http
""" """
server_version = "%s front/%s" % (_name, __version__) server_version = f'{_name} front/{__version__}'
def do_CONNECT(self): def do_CONNECT(self):
"Descrypt https request and dispatch to http handler" "Descrypt https request and dispatch to http handler"
@ -159,13 +159,13 @@ class FrontRequestHandler(ProxyRequestHandler):
things = (fnmatch.fnmatch(self.host, pattern) things = (fnmatch.fnmatch(self.host, pattern)
for pattern in pools.blacklist) for pattern in pools.blacklist)
if any(things): if any(things):
# BLACK LIST # blacklist
self.deny_request() self.deny_request()
logger.info("%03d " % self.reqNum + 'Denied by blacklist: s' logger.info('{:03d} denied by blacklist: {}'.format(
% self.host) self.reqNum, self.host))
elif any((fnmatch.fnmatch(self.host, pattern) elif any((fnmatch.fnmatch(self.host, pattern)
for pattern in pools.sslpasslist)): for pattern in pools.sslpasslist)):
# SSL Pass-Thru # TLS passthru
if self.proxy and self.proxy.startswith('https'): if self.proxy and self.proxy.startswith('https'):
self.forward_to_https_proxy() self.forward_to_https_proxy()
elif self.proxy and self.proxy.startswith('socks5'): elif self.proxy and self.proxy.startswith('socks5'):
@ -176,10 +176,10 @@ class FrontRequestHandler(ProxyRequestHandler):
# closed explictly, so we close the local connection too # closed explictly, so we close the local connection too
self.close_connection = 1 self.close_connection = 1
else: else:
# SSL MITM # TLS MITM
self.wfile.write(("HTTP/1.1 200 Connection established\r\n" + self.wfile.write(('HTTP/1.1 200 Connection established\r\n'
"Proxy-agent: %s\r\n" % self.version_string() + f'Proxy-agent: {self.version_string()}\r\n'
"\r\n").encode('ascii')) '\r\n').encode('ascii'))
if self.host.count('.') >= 2: if self.host.count('.') >= 2:
commonname = '.' + self.host.partition('.')[-1] commonname = '.' + self.host.partition('.')[-1]
else: else:
@ -205,24 +205,24 @@ class FrontRequestHandler(ProxyRequestHandler):
if self.ssltunnel: if self.ssltunnel:
# https request # https request
host = (self.host if self.port == '443' host = (self.host if self.port == '443'
else "%s:%s" % (self.host, self.port)) else ':'.join((self.host, self.port)))
url = "https://%s%s" % (host, self.path) url = 'https://' + ''.join((host, self.path))
self.bypass = any((fnmatch.fnmatch(url, pattern) self.bypass = any((fnmatch.fnmatch(url, pattern)
for pattern in pools.bypasslist)) for pattern in pools.bypasslist))
if not self.bypass: if not self.bypass:
url = "http://%s%s" % (host, self.path) url = 'http://' + ''.join((host, self.path))
# Tag the request so Privoxy can recognize it # Tag the request so Privoxy can recognize it
self.headers["Tagged"] = (self.version_string() self.headers["Tagged"] = (self.version_string()
+ ":%d" % self.reqNum) + f':{self.reqNum:d}')
else: else:
# http request # http request
self.host = urlparse(self.path).hostname self.host = urlparse(self.path).hostname
if any((fnmatch.fnmatch(self.host, pattern) if any((fnmatch.fnmatch(self.host, pattern)
for pattern in pools.blacklist)): for pattern in pools.blacklist)):
# BLACK LIST # blacklist
self.deny_request() self.deny_request()
logger.info("%03d " % self.reqNum logger.info('{:03d} denied by blacklist: {}'.format(
+ 'Denied by blacklist: %s' % self.host) self.reqNum, self.host))
return return
host = urlparse(self.path).netloc host = urlparse(self.path).netloc
self.proxy, self.pool, self.noverify = pools.getpool( self.proxy, self.pool, self.noverify = pools.getpool(
@ -240,11 +240,13 @@ class FrontRequestHandler(ProxyRequestHandler):
buffer = self.rfile.read() buffer = self.rfile.read()
if buffer: if buffer:
logger.warning( logger.warning(
"%03d " % self.reqNum + '{:03d} '
'POST w/o "Content-Length" header (Bytes: %d |' 'POST w/o "Content-Length" header (Bytes: {} | '
' Transfer-Encoding: %s | HTTPS: %s', 'Transfer-Encoding: {} | '
len(buffer), "Transfer-Encoding" in self.headers, 'HTTPS: {}'.format(
self.ssltunnel) self.reqNum, len(buffer),
"Transfer-Encoding" in self.headers,
self.ssltunnel))
# Remove hop-by-hop headers # Remove hop-by-hop headers
self.purge_headers(self.headers) self.purge_headers(self.headers)
r = None r = None
@ -273,13 +275,14 @@ class FrontRequestHandler(ProxyRequestHandler):
else: else:
prefix = '[D]' prefix = '[D]'
if self.command in ("GET", "HEAD"): if self.command in ("GET", "HEAD"):
logger.info("%03d " % self.reqNum + '%s "%s %s" %s %s' % logger.info('{:03d} {} "{} {}" {} {}'.format(
(prefix, self.command, url, r.status, self.reqNum, prefix, self.command, url,
r.getheader('Content-Length', '-')))
else:
logger.info("%03d " % self.reqNum + '%s "%s %s %s" %s %s' %
(prefix, self.command, url, data_length,
r.status, r.getheader('Content-Length', '-'))) r.status, r.getheader('Content-Length', '-')))
else:
logger.info('{:03d} {} "{} {} {}" {} {}'.format(
self.reqNum, prefix, self.command, url,
data_length, r.status,
r.getheader('Content-Length', '-')))
self.send_response_only(r.status, r.reason) self.send_response_only(r.status, r.reason)
# HTTPResponse.msg is easier to handle # HTTPResponse.msg is easier to handle
@ -301,12 +304,12 @@ class FrontRequestHandler(ProxyRequestHandler):
# Regular https request exceptions should be handled by rear server # Regular https request exceptions should be handled by rear server
except urllib3.exceptions.TimeoutError as e: except urllib3.exceptions.TimeoutError as e:
self.sendout_error(url, 504, message="Timeout", explain=e) self.sendout_error(url, 504, message="Timeout", explain=e)
logger.warning("%03d " % self.reqNum + '[F] %s on "%s %s"', logger.warning(f'{self.reqNum:03d} [F] {e} on '
e, self.command, url) f'"{self.command} {url}"')
except (urllib3.exceptions.HTTPError,) as e: except (urllib3.exceptions.HTTPError,) as e:
self.sendout_error(url, 502, message="HTTPError", explain=e) self.sendout_error(url, 502, message="HTTPError", explain=e)
logger.warning("%03d " % self.reqNum + '[F] %s on "%s %s"', logger.warning(f'{self.reqNum:03d} [F] {e} on '
e, self.command, url) f'"{self.command} {url}"')
finally: finally:
if r: if r:
# Release the connection back into the pool # Release the connection back into the pool
@ -320,7 +323,7 @@ class RearRequestHandler(ProxyRequestHandler):
Supposed to be the parent proxy for Privoxy for tagged requests Supposed to be the parent proxy for Privoxy for tagged requests
Convert http request to https Convert http request to https
""" """
server_version = "%s rear/%s" % (_name, __version__) server_version = f'{_name} front/{__version__}'
def do_METHOD(self): def do_METHOD(self):
"Convert http request to https" "Convert http request to https"
@ -333,10 +336,10 @@ class RearRequestHandler(ProxyRequestHandler):
else: else:
self.sendout_error( self.sendout_error(
self.path, 400, self.path, 400,
explain="The proxy setting of the client" explain='The proxy setting of the client'
" is misconfigured.\n\n" + ' is misconfigured.\n\n'
"Please set the HTTPS proxy port to %s " % config.FRONTPORT + f'Please set the HTTPS proxy port to {config.FRONTPORT} '
"and check the Docs for other settings.") 'and check the Docs for other settings.')
logger.error("[Misconfigured HTTPS proxy port] " + self.path) logger.error("[Misconfigured HTTPS proxy port] " + self.path)
return return
@ -370,16 +373,16 @@ class RearRequestHandler(ProxyRequestHandler):
retries=1, redirect=False, preload_content=False, retries=1, redirect=False, preload_content=False,
decode_content=False) decode_content=False)
if proxy: if proxy:
logger.debug('Using Proxy - %s' % proxy) logger.debug('Using Proxy - ' + proxy)
if self.command in ("GET", "HEAD"): if self.command in ("GET", "HEAD"):
logger.info( logger.info('{:03d} {} "{} {}" {} {}'.format(
"%03d " % self.reqNum + '%s "%s %s" %s %s' % self.reqNum, prefix,
(prefix, self.command, url, r.status, self.command, url, r.status,
r.getheader('Content-Length', '-'))) r.getheader('Content-Length', '-')))
else: else:
logger.info( logger.info('{:03d} {} "{} {} {}" {} {}'.format(
"%03d " % self.reqNum + '%s "%s %s %s" %s %s' % self.reqNum, prefix,
(prefix, self.command, url, data_length, r.status, self.command, url, data_length, r.status,
r.getheader('Content-Length', '-'))) r.getheader('Content-Length', '-')))
self.send_response_only(r.status, r.reason) self.send_response_only(r.status, r.reason)
@ -398,18 +401,17 @@ class RearRequestHandler(ProxyRequestHandler):
self.close_connection = 1 self.close_connection = 1
except urllib3.exceptions.SSLError as e: except urllib3.exceptions.SSLError as e:
self.sendout_error(url, 417, message="SSL Certificate Failed", self.sendout_error(url, 417, message='TLS Certificate Failed',
explain=e) explain=e)
logger.error("%03d " % self.reqNum + "[SSL Certificate Error] " logger.error(f'{self.reqNum:03d} [TLS Certificate Error] {url}')
+ url)
except urllib3.exceptions.TimeoutError as e: except urllib3.exceptions.TimeoutError as e:
self.sendout_error(url, 504, message="Timeout", explain=e) self.sendout_error(url, 504, message='Timeout', explain=e)
logger.warning("%03d " % self.reqNum + '[R]%s "%s %s" %s', logger.warning(f'{self.reqNum:03d} [R]{prefix} '
prefix, self.command, url, e) f'"{self.command} {url}" {e}')
except (urllib3.exceptions.HTTPError,) as e: except (urllib3.exceptions.HTTPError,) as e:
self.sendout_error(url, 502, message="HTTPError", explain=e) self.sendout_error(url, 502, message="HTTPError", explain=e)
logger.warning("%03d " % self.reqNum + '[R]%s "%s %s" %s', logger.warning(f'{self.reqNum:03d} [R]{prefix} '
prefix, self.command, url, e) f'"{self.command} {url}" {e}')
finally: finally:
if r: if r:
@ -436,13 +438,13 @@ def main():
thread.daemon = True thread.daemon = True
thread.start() thread.start()
print("=" * 40) print('=' * 40)
print('%s %s (urllib3/%s)' % (_name, __version__, urllib3.__version__)) print(f'{_name} {__version__} (urllib3/{urllib3.__version__})')
print('Front : localhost:%s' % config.FRONTPORT) print(f'Front : localhost:{config.FRONTPORT}')
print('Privoxy :', config.PROXADDR) print(f'Privoxy : {config.PROXADDR}')
print('Rear : localhost:%s' % config.REARPORT) print(f'Rear : localhost:{config.REARPORT}')
print('Parent : %s' % config.GeneralPROXY) print(f'Parent : {config.GeneralPROXY}')
print("=" * 40) print('=' * 40)
while True: while True:
time.sleep(1) time.sleep(1)