-
-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathhttps_connect_tunnel.py
70 lines (54 loc) · 1.96 KB
/
https_connect_tunnel.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
# -*- coding: utf-8 -*-
"""
proxy.py
~~~~~~~~
⚡⚡⚡ Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on
Network monitoring, controls & Application development, testing, debugging.
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
import time
from typing import Any, Optional
from proxy import Proxy
from proxy.core.base import BaseTcpTunnelHandler
from proxy.http.responses import (
PROXY_TUNNEL_UNSUPPORTED_SCHEME, PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT,
)
class HttpsConnectTunnelHandler(BaseTcpTunnelHandler):
"""A https CONNECT tunnel."""
def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
def handle_data(self, data: memoryview) -> Optional[bool]:
# Queue for upstream if connection has been established
if self.upstream and self.upstream._conn is not None:
self.upstream.queue(data)
return None
# Parse client request
self.request.parse(data)
# Drop the request if not a CONNECT request
if not self.request.is_https_tunnel:
self.work.queue(PROXY_TUNNEL_UNSUPPORTED_SCHEME)
return True
# CONNECT requests are short and we need not worry about
# receiving partial request bodies here.
assert self.request.is_complete
# Establish connection with upstream
self.connect_upstream()
# Queue tunnel established response to client
self.work.queue(PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT)
return None
def main() -> None:
# This example requires `threadless=True`
with Proxy(
work_klass=HttpsConnectTunnelHandler,
threadless=True,
num_workers=1,
port=12345,
):
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
if __name__ == '__main__':
main()