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