authcaptureproxy.auth_capture_proxy

Python Package for auth capture proxy.

Module Contents

class authcaptureproxy.auth_capture_proxy.AuthCaptureProxy(proxy_url: yarl.URL, host_url: yarl.URL, session: Optional[httpx.AsyncClient] = None)

Class to handle proxy login connections.

This class relies on tests to be provided to indicate the proxy has completed. At proxy completion all data can be found in self.session, self.data, and self.query.

Initialize proxy object.

Parameters
  • proxy_url (URL) – url for proxy location. e.g., http://192.168.1.1/. If there is any path, the path is considered part of the base url. If no explicit port is specified, a random port will be generated. If https is passed in, ssl_context must be provided at start_proxy() or the url will be downgraded to http.

  • host_url (URL) – original url for login, e.g., http://amazon.com

  • session (httpx.AsyncClient) – httpx client to make queries. Optional

property active(self)bool

Return whether proxy is started.

property all_handler_active(self)bool

Return whether all handler is active.

property port(self)int

Return port setting.

property tests(self)Dict[Text, Callable]

Return tests setting.

Setter

value (Dict[Text, Any]): A dictionary of tests. The key should be the name of the test and the value should be a function or coroutine that takes a httpx.Response, a dictionary of post variables, and a dictioary of query variables and returns a URL or string. See authcaptureproxy.examples.testers for examples.

property modifiers(self)Dict[Text, Union[Callable, Dict[Text, Callable]]]

Return modifiers setting.

Setter

value (Dict[Text, Dict[Text, Callable]): A nested dictionary of modifiers. The key shoud be a MIME type and the value should be a dictionary of modifiers for that MIME type where the key should be the name of the modifier and the value should be a function or couroutine that takes a string and returns a modified string. If parameters are necessary, functools.partial should be used. See authcaptureproxy.examples.modifiers for examples.

access_url(self)yarl.URL

Return access url for proxy with port.

async change_host_url(self, new_url: yarl.URL)None

Change the host url of the proxy.

This will also reset all stored data.

Parameters

new_url (URL) – original url for login, e.g., http://amazon.com

async reset_data(self)None

Reset all stored data.

A proxy may need to service multiple login requests if the route is not torn down. This function will reset all data between logins.

refresh_tests(self)None

Refresh tests.

Because tests may use partials, they will freeze their parameters which is a problem with self.access() if the port hasn’t been assigned.

refresh_modifiers(self, site: Optional[yarl.URL] = None)None

Refresh modifiers.

Because modifiers may use partials, they will freeze their parameters which is a problem with self.access() if the port hasn’t been assigned.

Parameters

site (Optional[URL], optional) – The current site. Defaults to None.

async all_handler(self, request: aiohttp.web.Request, **kwargs)aiohttp.web.Response

Handle all requests.

This handler will exit on succesful test found in self.tests or if a /stop url is seen. This handler can be used with any aiohttp webserver and disabled after registered using self.all_handler_active.

Args

request (web.Request): The request to process

Returns

web.Response: The webresponse to the browser

Raises

web.HTTPFound: Redirect URL upon success web.HTTPNotFound: Return 404 when all_handler is disabled

async start_proxy(self, host: Optional[Text] = None, ssl_context: Optional[ssl.SSLContext] = None)None

Start proxy.

Parameters
  • host (Optional[Text], optional) – The host interface to bind to. Defaults to None which is “0.0.0.0” all interfaces.

  • ssl_context (Optional[SSLContext], optional) – SSL Context for the server. Defaults to None.

async stop_proxy(self, delay: int = 0)None

Stop proxy server.

Parameters

delay (int, optional) – How many seconds to delay. Defaults to 0.

async modify_headers(self, site: yarl.URL, request: aiohttp.web.Request)dict

Modify headers.

Return modified headers based on site and request. To disable auto header generation, pass in to the header a key const.SKIP_AUTO_HEADERS with a list of keys to not generate.

For example, to prevent User-Agent generation: {SKIP_AUTO_HEADERS : [“User-Agent”]}

Parameters
  • site (URL) – URL of the next host request.

  • request (web.Request) – Proxy directed request. This will need to be changed for the actual host request.

Returns

Headers after modifications

Return type

dict

check_redirects(self)None

Change host if redirect detected and regex does not match self.redirect_filters.

Self.redirect_filters is a dict with key as attr in resp and value as list of regex expressions to filter against.