2019-08-12 16:45:27 +00:00
|
|
|
from urllib import parse as urlparse
|
2019-08-23 00:09:35 +00:00
|
|
|
from proxy import HttpProtocolBasePlugin, HttpRequestRejected
|
2019-08-12 16:45:27 +00:00
|
|
|
|
|
|
|
|
2019-08-20 23:10:05 +00:00
|
|
|
class RedirectToCustomServerPlugin(HttpProtocolBasePlugin):
|
2019-08-12 16:45:27 +00:00
|
|
|
"""Modifies client request to redirect all incoming requests to a fixed server address."""
|
|
|
|
|
2019-08-23 00:09:35 +00:00
|
|
|
def __init__(self, config, client, request):
|
|
|
|
super(RedirectToCustomServerPlugin, self).__init__(config, client, request)
|
2019-08-12 16:45:27 +00:00
|
|
|
|
2019-08-20 23:10:05 +00:00
|
|
|
def on_request_complete(self):
|
|
|
|
if self.request.method != 'CONNECT':
|
|
|
|
self.request.url = urlparse.urlsplit(b'http://localhost:9999')
|
2019-08-12 16:45:27 +00:00
|
|
|
|
|
|
|
|
2019-08-20 23:10:05 +00:00
|
|
|
class FilterByTargetDomainPlugin(HttpProtocolBasePlugin):
|
2019-08-12 16:45:27 +00:00
|
|
|
"""Only accepts specific requests dropping all other requests."""
|
|
|
|
|
2019-08-23 00:09:35 +00:00
|
|
|
def __init__(self, config, client, request):
|
|
|
|
super(FilterByTargetDomainPlugin, self).__init__(config, client, request)
|
2019-08-12 16:45:27 +00:00
|
|
|
self.allowed_domains = [b'google.com', b'www.google.com', b'google.com:443', b'www.google.com:443']
|
|
|
|
|
2019-08-20 23:10:05 +00:00
|
|
|
def on_request_complete(self):
|
2019-08-12 16:45:27 +00:00
|
|
|
# TODO: Refactor internals to cleanup mess below, due to how urlparse works, hostname/path attributes
|
|
|
|
# are not consistent between CONNECT and non-CONNECT requests.
|
2019-08-20 23:10:05 +00:00
|
|
|
if (self.request.method != b'CONNECT' and self.request.url.hostname not in self.allowed_domains) or \
|
|
|
|
(self.request.method == b'CONNECT' and self.request.url.path not in self.allowed_domains):
|
2019-08-23 00:09:35 +00:00
|
|
|
raise HttpRequestRejected(status_code=418, body='I\'m a tea pot')
|
2019-08-20 23:10:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SaveHttpResponses(HttpProtocolBasePlugin):
|
|
|
|
"""Saves Http Responses locally on disk."""
|
|
|
|
|
2019-08-23 00:09:35 +00:00
|
|
|
def __init__(self, config, client, request):
|
|
|
|
super(SaveHttpResponses, self).__init__(config, client, request)
|
2019-08-20 23:10:05 +00:00
|
|
|
|
|
|
|
def handle_response_chunk(self, chunk):
|
|
|
|
return chunk
|