-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathwdaproxy-script.py
178 lines (141 loc) · 5.33 KB
/
wdaproxy-script.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
# coding: utf-8
import argparse
import asyncio
import io
import os
import socket
import sys
import urllib.request
import httpx
import requests
import tornado.ioloop
import tornado.web
from tornado.log import enable_pretty_logging
from tornado.websocket import WebSocketHandler
from tornado.iostream import IOStream
class MjpegReader():
"""
MJPEG format
Content-Type: multipart/x-mixed-replace; boundary=--BoundaryString
--BoundaryString
Content-type: image/jpg
Content-Length: 12390
... image-data here ...
--BoundaryString
Content-type: image/jpg
Content-Length: 12390
... image-data here ...
"""
def __init__(self, url: str):
self._url = url
async def aiter_content(self):
"""
Ref:
- https://stackoverflow.com/questions/32310951/how-to-get-the-underlying-socket-when-using-python-requests
- https://www.tornadoweb.org/en/stable/iostream.html
- https://realpython.com/async-io-python/#other-features-async-for-and-async-generators-comprehensions
"""
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
stream = IOStream(s)
try:
url = urllib.request.urlparse(self._url)
host, port = url.netloc.split(":")
port = int(port)
path = url.path or "/"
await stream.connect((host, port))
await stream.write(
"GET {path} HTTP/1.0\r\nHost: {netloc}\r\n\r\n".format(
path=path, netloc=url.netloc).encode('utf-8'))
header_data = await stream.read_until(b"\r\n\r\n")
while True:
line = await stream.read_until(b'\r\n')
if not line.startswith(b"Content-Length"):
continue
length = int(line.decode('utf-8').split(": ")[1])
await stream.read_until(b"\r\n")
yield await stream.read_bytes(length)
finally:
stream.close()
class CorsMixin:
def initialize(self):
self.set_header('Connection', 'close')
self.request.connection.no_keep_alive = True
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
def options(self):
# no body
self.set_status(204)
self.finish()
class ScreenWSHandler(CorsMixin, WebSocketHandler):
MJPEG_READER = None
def check_origin(self, origin):
return True
async def open(self):
# print("connection created")
assert self.MJPEG_READER
async for content in self.MJPEG_READER.aiter_content():
await self.write_message(content, binary=True)
def on_message(self, message):
# return super().on_message(message)
pass
def on_close(self):
return super().on_close()
# Ref: https://github.com/colevscode/quickproxy/blob/master/quickproxy/proxy.py
class ReverseProxyHandler(CorsMixin, tornado.web.RequestHandler):
# 超时时间手动设长,避免一些耗时操作(如获取元素树)直接超时失败
_default_http_client = httpx.AsyncClient(timeout=30.0)
TARGET_URL = None
async def handle_request(self, request):
assert self.TARGET_URL
url = self.TARGET_URL.lstrip("/") + request.uri
async with self._default_http_client.stream(request.method,
url,
headers=request.headers.get_all(),
data=request.body) as resp:
self.set_status(resp.status_code)
for k, v in resp.headers.items():
self.set_header(k, v)
async for chunk in resp.aiter_bytes():
self.write(chunk)
async def get(self):
await self.handle_request(self.request)
async def post(self):
await self.handle_request(self.request)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-p",
"--port",
type=int,
default=8200,
help="listen port")
parser.add_argument("--wda-url",
default="http://localhost:8100",
help="wda server url")
parser.add_argument("--mjpeg-url",
default="http://localhost:9100",
help="mjpeg server url")
args = parser.parse_args()
ScreenWSHandler.MJPEG_READER = MjpegReader(args.mjpeg_url)
ReverseProxyHandler.TARGET_URL = args.wda_url
app = tornado.web.Application([
(r"/screen", ScreenWSHandler),
(r"/.*", ReverseProxyHandler),
])
app.listen(args.port)
enable_pretty_logging()
tornado.ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()
# program
# .version("0.1.0")
# .option("-p, --port <port>", "listen port", parseInt)
# .option("--wda-url <wdaUrl>", "wda server url")
# .option("--mjpeg-url <mjpegUrl>", "mjpeg server url")
# .parse(process.argv)
# console.log(program.port)
# const app = makeApp(program.wdaUrl, program.mjpegUrl)
# app.listen(program.port, () => {
# console.log("Listen on port", program.port)
# })