|
| 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 gzip |
| 12 | +from typing import Any, Dict, Optional |
| 13 | + |
| 14 | +from ..common.flag import flags |
| 15 | +from ..common.utils import build_http_response |
| 16 | +from ..common.constants import PROXY_AGENT_HEADER_VALUE, PROXY_AGENT_HEADER_KEY |
| 17 | + |
| 18 | +from .codes import httpStatusCodes |
| 19 | + |
| 20 | + |
| 21 | +PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT = memoryview( |
| 22 | + build_http_response( |
| 23 | + httpStatusCodes.OK, |
| 24 | + reason=b'Connection established', |
| 25 | + ), |
| 26 | +) |
| 27 | + |
| 28 | +PROXY_TUNNEL_UNSUPPORTED_SCHEME = memoryview( |
| 29 | + build_http_response( |
| 30 | + httpStatusCodes.BAD_REQUEST, |
| 31 | + reason=b'Unsupported protocol scheme', |
| 32 | + conn_close=True, |
| 33 | + ), |
| 34 | +) |
| 35 | + |
| 36 | +PROXY_AUTH_FAILED_RESPONSE_PKT = memoryview( |
| 37 | + build_http_response( |
| 38 | + httpStatusCodes.PROXY_AUTH_REQUIRED, |
| 39 | + reason=b'Proxy Authentication Required', |
| 40 | + headers={ |
| 41 | + PROXY_AGENT_HEADER_KEY: PROXY_AGENT_HEADER_VALUE, |
| 42 | + b'Proxy-Authenticate': b'Basic', |
| 43 | + }, |
| 44 | + body=b'Proxy Authentication Required', |
| 45 | + conn_close=True, |
| 46 | + ), |
| 47 | +) |
| 48 | + |
| 49 | +NOT_FOUND_RESPONSE_PKT = memoryview( |
| 50 | + build_http_response( |
| 51 | + httpStatusCodes.NOT_FOUND, |
| 52 | + reason=b'NOT FOUND', |
| 53 | + headers={ |
| 54 | + b'Server': PROXY_AGENT_HEADER_VALUE, |
| 55 | + b'Content-Length': b'0', |
| 56 | + }, |
| 57 | + conn_close=True, |
| 58 | + ), |
| 59 | +) |
| 60 | + |
| 61 | +NOT_IMPLEMENTED_RESPONSE_PKT = memoryview( |
| 62 | + build_http_response( |
| 63 | + httpStatusCodes.NOT_IMPLEMENTED, |
| 64 | + reason=b'NOT IMPLEMENTED', |
| 65 | + headers={ |
| 66 | + b'Server': PROXY_AGENT_HEADER_VALUE, |
| 67 | + b'Content-Length': b'0', |
| 68 | + }, |
| 69 | + conn_close=True, |
| 70 | + ), |
| 71 | +) |
| 72 | + |
| 73 | +BAD_GATEWAY_RESPONSE_PKT = memoryview( |
| 74 | + build_http_response( |
| 75 | + httpStatusCodes.BAD_GATEWAY, |
| 76 | + reason=b'Bad Gateway', |
| 77 | + headers={ |
| 78 | + PROXY_AGENT_HEADER_KEY: PROXY_AGENT_HEADER_VALUE, |
| 79 | + }, |
| 80 | + body=b'Bad Gateway', |
| 81 | + conn_close=True, |
| 82 | + ), |
| 83 | +) |
| 84 | + |
| 85 | + |
| 86 | +def okResponse( |
| 87 | + content: Optional[bytes] = None, |
| 88 | + headers: Optional[Dict[bytes, bytes]] = None, |
| 89 | + compress: bool = True, |
| 90 | + **kwargs: Any, |
| 91 | +) -> memoryview: |
| 92 | + do_compress: bool = False |
| 93 | + if compress and flags.args and content and len(content) > flags.args.min_compression_limit: |
| 94 | + do_compress = True |
| 95 | + if not headers: |
| 96 | + headers = {} |
| 97 | + headers.update({ |
| 98 | + b'Content-Encoding': b'gzip', |
| 99 | + }) |
| 100 | + return memoryview( |
| 101 | + build_http_response( |
| 102 | + 200, |
| 103 | + reason=b'OK', |
| 104 | + headers=headers, |
| 105 | + body=gzip.compress(content) |
| 106 | + if do_compress and content |
| 107 | + else content, |
| 108 | + **kwargs, |
| 109 | + ), |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def permanentRedirectResponse(location: bytes) -> memoryview: |
| 114 | + return memoryview( |
| 115 | + build_http_response( |
| 116 | + httpStatusCodes.PERMANENT_REDIRECT, |
| 117 | + reason=b'Permanent Redirect', |
| 118 | + headers={ |
| 119 | + b'Location': location, |
| 120 | + b'Content-Length': b'0', |
| 121 | + }, |
| 122 | + conn_close=True, |
| 123 | + ), |
| 124 | + ) |
| 125 | + |
| 126 | + |
| 127 | +def seeOthersResponse(location: bytes) -> memoryview: |
| 128 | + return memoryview( |
| 129 | + build_http_response( |
| 130 | + httpStatusCodes.SEE_OTHER, |
| 131 | + reason=b'See Other', |
| 132 | + headers={ |
| 133 | + b'Location': location, |
| 134 | + b'Content-Length': b'0', |
| 135 | + }, |
| 136 | + conn_close=True, |
| 137 | + ), |
| 138 | + ) |
0 commit comments