|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + proxy.py |
| 4 | + ~~~~~~~~ |
| 5 | + ⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on |
| 6 | + Network monitoring, controls & Application development, testing, debugging. |
| 7 | +
|
| 8 | + :copyright: (c) 2013-present by Abhinav Singh and contributors. |
| 9 | + :license: BSD, see LICENSE for more details. |
| 10 | +""" |
| 11 | +import socket |
| 12 | + |
| 13 | +from typing import Optional, Tuple |
| 14 | + |
| 15 | +from ..http.parser import HttpParser |
| 16 | +from ..http.proxy import HttpProxyBasePlugin |
| 17 | + |
| 18 | + |
| 19 | +class CustomDnsResolverPlugin(HttpProxyBasePlugin): |
| 20 | + """This plugin demonstrate how to use your own custom DNS resolver.""" |
| 21 | + |
| 22 | + def resolve_dns(self, host: str, port: int) -> Tuple[Optional[str], Optional[Tuple[str, int]]]: |
| 23 | + """Here we are using in-built python resolver for demonstration. |
| 24 | +
|
| 25 | + Ideally you would like to query your custom DNS server or even use DoH to make |
| 26 | + real sense out of this plugin. |
| 27 | +
|
| 28 | + 2nd parameter returned is None. Return a 2-tuple to configure underlying interface |
| 29 | + to use for connection to the upstream server. |
| 30 | + """ |
| 31 | + try: |
| 32 | + return socket.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)[0][4][0], None |
| 33 | + except socket.gaierror: |
| 34 | + # Ideally we can also thrown HttpRequestRejected or HttpProtocolException here |
| 35 | + # Returning None simply fallback to core generated exceptions. |
| 36 | + return None, None |
| 37 | + |
| 38 | + def before_upstream_connection( |
| 39 | + self, request: HttpParser, |
| 40 | + ) -> Optional[HttpParser]: |
| 41 | + return request |
| 42 | + |
| 43 | + def handle_client_request( |
| 44 | + self, request: HttpParser, |
| 45 | + ) -> Optional[HttpParser]: |
| 46 | + return request |
| 47 | + |
| 48 | + def handle_upstream_chunk(self, chunk: memoryview) -> memoryview: |
| 49 | + return chunk |
| 50 | + |
| 51 | + def on_upstream_connection_close(self) -> None: |
| 52 | + pass |
0 commit comments