4
4
import websockets
5
5
import json
6
6
7
+
7
8
async def send_ready_notification () -> None :
8
9
uri = "ws://localhost:8001"
9
- logging .info (f' sending mitmproxyReady JSON-RPC notification to { uri } ' )
10
+ logging .info (f" sending mitmproxyReady JSON-RPC notification to { uri } " )
10
11
async with websockets .connect (uri ) as websocket :
11
- notification_dto = { "jsonrpc" : "2.0" , "method" : "mitmproxyReady" }
12
+ notification_dto = {"jsonrpc" : "2.0" , "method" : "mitmproxyReady" }
12
13
data = json .dumps (notification_dto )
13
14
await websocket .send (data )
14
15
16
+
15
17
class MitmproxyAddon :
16
18
def running (self ) -> None :
17
19
# tell the control API that we’re ready to receive traffic
@@ -26,7 +28,9 @@ def request(self, flow: http.HTTPFlow) -> None:
26
28
27
29
# (b'Connection', b'Upgrade'), (b'Upgrade', b'websocket')
28
30
intercept = MitmproxyAddon .is_websocket_upgrade_request (flow .request )
29
- logging .info (f'MitmproxyAddon { "intercepting" if intercept else "not intercepting" } `request` { flow .request .url } , headers { flow .request .headers } ' )
31
+ logging .info (
32
+ f'MitmproxyAddon { "intercepting" if intercept else "not intercepting" } `request` { flow .request .url } , headers { flow .request .headers } '
33
+ )
30
34
# pretty_host takes the "Host" header of the request into account,
31
35
# which is useful in transparent mode where we usually only have the IP
32
36
# otherwise.
@@ -38,18 +42,26 @@ def request(self, flow: http.HTTPFlow) -> None:
38
42
39
43
flow .request .host = "localhost"
40
44
flow .request .port = 8002
41
- flow .request .scheme = ' http'
45
+ flow .request .scheme = " http"
42
46
# TODO understand how port fits into this
43
- flow .request .headers [' Ably-Test-Host' ] = original_host
47
+ flow .request .headers [" Ably-Test-Host" ] = original_host
44
48
match original_scheme :
45
- case ' http' :
46
- flow .request .headers [' Ably-Test-Proto' ] = 'ws'
47
- case ' https' :
48
- flow .request .headers [' Ably-Test-Proto' ] = ' wss'
49
+ case " http" :
50
+ flow .request .headers [" Ably-Test-Proto" ] = "ws"
51
+ case " https" :
52
+ flow .request .headers [" Ably-Test-Proto" ] = " wss"
49
53
50
54
@staticmethod
51
55
def is_websocket_upgrade_request (request : http .Request ) -> bool :
52
56
# TODO this request handling is a bit fragile, the special case for `split` is just to handle the fact that Firefox sends 'Connection: keep-alive, Upgrade'
53
- return True if 'Connection' in request .headers and ('Upgrade' in request .headers ['Connection' ].split (", " )) and 'Upgrade' in request .headers and request .headers ['Upgrade' ] == 'websocket' else False
57
+ return (
58
+ True
59
+ if "Connection" in request .headers
60
+ and ("Upgrade" in request .headers ["Connection" ].split (", " ))
61
+ and "Upgrade" in request .headers
62
+ and request .headers ["Upgrade" ] == "websocket"
63
+ else False
64
+ )
65
+
54
66
55
67
addons = [MitmproxyAddon ()]
0 commit comments