blob: 79dae1cd03befec66620fcfff2eb8180b92b0bdd (
plain)
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
|
#!/usr/bin/env python
from __future__ import annotations
import sys
import typing
import tornado.httpserver
import tornado.ioloop
import tornado.web
from dummyserver.proxy import ProxyHandler
from dummyserver.server import DEFAULT_CERTS, ssl_options_to_context
def run_proxy(port: int, certs: dict[str, typing.Any] = DEFAULT_CERTS) -> None:
"""
Run proxy on the specified port using the provided certs.
Example usage:
python -m dummyserver.https_proxy
You'll need to ensure you have access to certain packages such as trustme,
tornado, urllib3.
"""
upstream_ca_certs = certs.get("ca_certs")
app = tornado.web.Application(
[(r".*", ProxyHandler)], upstream_ca_certs=upstream_ca_certs
)
ssl_opts = ssl_options_to_context(**certs)
http_server = tornado.httpserver.HTTPServer(app, ssl_options=ssl_opts)
http_server.listen(port)
ioloop = tornado.ioloop.IOLoop.instance()
try:
ioloop.start()
except KeyboardInterrupt:
ioloop.stop()
if __name__ == "__main__":
port = 8443
if len(sys.argv) > 1:
port = int(sys.argv[1])
print(f"Starting HTTPS proxy on port {port}")
run_proxy(port)
|