/
mode_specs.py
310 lines (235 loc) · 9.02 KB
/
mode_specs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
This module is responsible for parsing proxy mode specifications such as
`"regular"`, `"reverse:https://example.com"`, or `"socks5@1234"`. The general syntax is
mode [: mode_configuration] [@ [listen_addr:]listen_port]
For a full example, consider `reverse:https://example.com@127.0.0.1:443`.
This would spawn a reverse proxy on port 443 bound to localhost.
The mode is `reverse`, and the mode data is `https://example.com`.
Examples:
mode = ProxyMode.parse("regular@1234")
assert mode.listen_port == 1234
assert isinstance(mode, RegularMode)
ProxyMode.parse("reverse:example.com@invalid-port") # ValueError
RegularMode.parse("regular") # ok
RegularMode.parse("socks5") # ValueError
"""
from __future__ import annotations
import dataclasses
import sys
from abc import ABCMeta
from abc import abstractmethod
from dataclasses import dataclass
from functools import cache
from typing import ClassVar
from typing import Literal
import mitmproxy_rs
from mitmproxy.coretypes.serializable import Serializable
from mitmproxy.net import server_spec
if sys.version_info < (3, 11):
from typing_extensions import Self # pragma: no cover
else:
from typing import Self
@dataclass(frozen=True) # type: ignore
class ProxyMode(Serializable, metaclass=ABCMeta):
"""
Parsed representation of a proxy mode spec. Subclassed for each specific mode,
which then does its own data validation.
"""
full_spec: str
"""The full proxy mode spec as entered by the user."""
data: str
"""The (raw) mode data, i.e. the part after the mode name."""
custom_listen_host: str | None
"""A custom listen host, if specified in the spec."""
custom_listen_port: int | None
"""A custom listen port, if specified in the spec."""
type_name: ClassVar[
str
] # automatically derived from the class name in __init_subclass__
"""The unique name for this proxy mode, e.g. "regular" or "reverse"."""
__types: ClassVar[dict[str, type[ProxyMode]]] = {}
def __init_subclass__(cls, **kwargs):
cls.type_name = cls.__name__.removesuffix("Mode").lower()
assert cls.type_name not in ProxyMode.__types
ProxyMode.__types[cls.type_name] = cls
def __repr__(self):
return f"ProxyMode.parse({self.full_spec!r})"
@abstractmethod
def __post_init__(self) -> None:
"""Validation of data happens here."""
@property
@abstractmethod
def description(self) -> str:
"""The mode description that will be used in server logs and UI."""
@property
def default_port(self) -> int:
"""
Default listen port of servers for this mode, see `ProxyMode.listen_port()`.
"""
return 8080
@property
@abstractmethod
def transport_protocol(self) -> Literal["tcp", "udp"] | None:
"""The transport protocol used by this mode's server."""
@classmethod
@cache
def parse(cls, spec: str) -> Self:
"""
Parse a proxy mode specification and return the corresponding `ProxyMode` instance.
"""
head, _, listen_at = spec.rpartition("@")
if not head:
head = listen_at
listen_at = ""
mode, _, data = head.partition(":")
if listen_at:
if ":" in listen_at:
host, _, port_str = listen_at.rpartition(":")
else:
host = None
port_str = listen_at
try:
port = int(port_str)
if port < 0 or 65535 < port:
raise ValueError
except ValueError:
raise ValueError(f"invalid port: {port_str}")
else:
host = None
port = None
try:
mode_cls = ProxyMode.__types[mode.lower()]
except KeyError:
raise ValueError(f"unknown mode")
if not issubclass(mode_cls, cls):
raise ValueError(f"{mode!r} is not a spec for a {cls.type_name} mode")
return mode_cls(
full_spec=spec, data=data, custom_listen_host=host, custom_listen_port=port
)
def listen_host(self, default: str | None = None) -> str:
"""
Return the address a server for this mode should listen on. This can be either directly
specified in the spec or taken from a user-configured global default (`options.listen_host`).
By default, return an empty string to listen on all hosts.
"""
if self.custom_listen_host is not None:
return self.custom_listen_host
elif default is not None:
return default
else:
return ""
def listen_port(self, default: int | None = None) -> int:
"""
Return the port a server for this mode should listen on. This can be either directly
specified in the spec, taken from a user-configured global default (`options.listen_port`),
or from `ProxyMode.default_port`.
"""
if self.custom_listen_port is not None:
return self.custom_listen_port
elif default is not None:
return default
else:
return self.default_port
@classmethod
def from_state(cls, state):
return ProxyMode.parse(state)
def get_state(self):
return self.full_spec
def set_state(self, state):
if state != self.full_spec:
raise dataclasses.FrozenInstanceError("Proxy modes are immutable.")
TCP: Literal["tcp", "udp"] = "tcp"
UDP: Literal["tcp", "udp"] = "udp"
def _check_empty(data):
if data:
raise ValueError("mode takes no arguments")
class RegularMode(ProxyMode):
"""A regular HTTP(S) proxy that is interfaced with `HTTP CONNECT` calls (or absolute-form HTTP requests)."""
description = "HTTP(S) proxy"
transport_protocol = TCP
def __post_init__(self) -> None:
_check_empty(self.data)
class TransparentMode(ProxyMode):
"""A transparent proxy, see https://docs.mitmproxy.org/dev/howto-transparent/"""
description = "Transparent Proxy"
transport_protocol = TCP
def __post_init__(self) -> None:
_check_empty(self.data)
class UpstreamMode(ProxyMode):
"""A regular HTTP(S) proxy, but all connections are forwarded to a second upstream HTTP(S) proxy."""
description = "HTTP(S) proxy (upstream mode)"
transport_protocol = TCP
scheme: Literal["http", "https"]
address: tuple[str, int]
# noinspection PyDataclass
def __post_init__(self) -> None:
scheme, self.address = server_spec.parse(self.data, default_scheme="http")
if scheme != "http" and scheme != "https":
raise ValueError("invalid upstream proxy scheme")
self.scheme = scheme
class ReverseMode(ProxyMode):
"""A reverse proxy. This acts like a normal server, but redirects all requests to a fixed target."""
description = "reverse proxy"
transport_protocol = TCP
scheme: Literal[
"http", "https", "http3", "tls", "dtls", "tcp", "udp", "dns", "quic"
]
address: tuple[str, int]
# noinspection PyDataclass
def __post_init__(self) -> None:
self.scheme, self.address = server_spec.parse(self.data, default_scheme="https")
if self.scheme in ("http3", "dtls", "udp", "dns", "quic"):
self.transport_protocol = UDP
self.description = f"{self.description} to {self.data}"
@property
def default_port(self) -> int:
if self.scheme == "dns":
return 53
return super().default_port
class Socks5Mode(ProxyMode):
"""A SOCKSv5 proxy."""
description = "SOCKS v5 proxy"
default_port = 1080
transport_protocol = TCP
def __post_init__(self) -> None:
_check_empty(self.data)
class DnsMode(ProxyMode):
"""A DNS server."""
description = "DNS server"
default_port = 53
transport_protocol = UDP
def __post_init__(self) -> None:
_check_empty(self.data)
# class Http3Mode(ProxyMode):
# """
# A regular HTTP3 proxy that is interfaced with absolute-form HTTP requests.
# (This class will be merged into `RegularMode` once the UDP implementation is deemed stable enough.)
# """
#
# description = "HTTP3 proxy"
# transport_protocol = UDP
#
# def __post_init__(self) -> None:
# _check_empty(self.data)
class WireGuardMode(ProxyMode):
"""Proxy Server based on WireGuard"""
description = "WireGuard server"
default_port = 51820
transport_protocol = UDP
def __post_init__(self) -> None:
pass
class LocalMode(ProxyMode):
"""OS-level transparent proxy."""
description = "Local redirector"
transport_protocol = None
def __post_init__(self) -> None:
# should not raise
mitmproxy_rs.LocalRedirector.describe_spec(self.data)
class OsProxyMode(ProxyMode): # pragma: no cover
"""Deprecated alias for LocalMode"""
description = "Deprecated alias for LocalMode"
transport_protocol = None
def __post_init__(self) -> None:
raise ValueError(
"osproxy mode has been renamed to local mode. Thanks for trying our experimental features!"
)