Skip to content

Commit 3bfc885

Browse files
codeskyblueCopilot
andauthored
offline mode add --server-url (#58)
* support specify server url in offline mode * remove unsafe /proxy/http * Update uiautodev/cli.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 6276211 commit 3bfc885

File tree

4 files changed

+17
-49
lines changed

4 files changed

+17
-49
lines changed

DEVELOP.md

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,10 @@ make dev
3232
# try installing a stable version of the construct package to resolve it:
3333
# and restart: make dev
3434
pip install construct==2.9.45
35-
3635
```
3736

3837
运行测试
3938

4039
```sh
4140
make test
42-
```
43-
44-
## 代理转发
45-
46-
由于服务端是写在https外部网站上的,访问非localhost的http服务时会由于安全问题,禁止访问。
47-
比如A机器访问B机器的服务,就不行。
48-
所以需要本地转发,才能连接到其他的uiautodev客户端上。
49-
50-
测试WebSocket转发
51-
52-
```sh
53-
wscat -c "ws://localhost:20242/proxy/ws/wss://echo.websocket.events"
54-
```
55-
56-
> npm i -g wscat
57-
58-
测试HTTP转发
59-
60-
```sh
61-
curl http://localhost:20242/proxy/http/https://httpbin.org/get
62-
```
41+
```

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Start with
5555

5656
```sh
5757
uiautodev server --offline
58+
59+
# Specify server url (optional)
60+
uiautodev server --offline --server-url https://uiauto.dev
5861
```
5962

6063
Visit <http://localhost:20242> once, and then disconnecting from the internet will not affect usage.

uiautodev/cli.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ def pip_install(package: str):
150150
@click.option("--port", default=20242, help="port number", show_default=True)
151151
@click.option("--host", default="127.0.0.1", help="host", show_default=True)
152152
@click.option("--reload", is_flag=True, default=False, help="auto reload, dev only")
153-
@click.option("-f", "--force", is_flag=True, default=False, help="shutdown alrealy runningserver")
153+
@click.option("-f", "--force", is_flag=True, default=False, help="shutdown already running server")
154154
@click.option("-s", "--no-browser", is_flag=True, default=False, help="silent mode, do not open browser")
155155
@click.option("--offline", is_flag=True, default=False, help="offline mode, do not use internet")
156-
def server(port: int, host: str, reload: bool, force: bool, no_browser: bool, offline: bool):
156+
@click.option("--server-url", default="https://uiauto.dev", help="uiauto.dev server url", show_default=True)
157+
def server(port: int, host: str, reload: bool, force: bool, no_browser: bool, offline: bool, server_url: str):
157158
click.echo(f"uiautodev version: {__version__}")
158159
if force:
159160
try:
@@ -165,10 +166,13 @@ def server(port: int, host: str, reload: bool, force: bool, no_browser: bool, of
165166
if platform.system() == 'Windows':
166167
use_color = False
167168

169+
server_url = server_url.rstrip('/')
170+
from uiautodev.router import proxy
171+
proxy.base_url = server_url
172+
168173
if offline:
169-
from uiautodev.router.proxy import cache_dir
170-
cache_dir.mkdir(parents=True, exist_ok=True)
171-
logger.info("offline mode enabled, cache dir: %s", cache_dir)
174+
proxy.cache_dir.mkdir(parents=True, exist_ok=True)
175+
logger.info("offline mode enabled, cache dir: %s, server url: %s", proxy.cache_dir, proxy.base_url)
172176

173177
if not no_browser:
174178
th = threading.Thread(target=open_browser_when_server_start, args=(f"http://{host}:{port}", offline))

uiautodev/router/proxy.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,30 @@
77

88
import httpx
99
import websockets
10-
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request, WebSocket, WebSocketDisconnect
10+
from fastapi import APIRouter, Request, WebSocket, WebSocketDisconnect
1111
from fastapi.responses import Response, StreamingResponse
1212
from starlette.background import BackgroundTask
1313

1414
logger = logging.getLogger(__name__)
1515
router = APIRouter()
1616
cache_dir = Path("./cache")
17-
18-
# HTTP 转发
19-
@router.api_route("/proxy/http/{target_url:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"])
20-
async def proxy_http(request: Request, target_url: str):
21-
logger.info(f"HTTP target_url: {target_url}")
22-
23-
async with httpx.AsyncClient(timeout=httpx.Timeout(30.0)) as client:
24-
body = await request.body() if request.method in {"POST", "PUT", "PATCH", "DELETE"} else None
25-
headers = {k: v for k, v in request.headers.items() if k.lower() not in {"host", "x-target-url"}}
26-
headers['accept-encoding'] = '' # disable gzip
27-
resp = await client.request(
28-
request.method,
29-
target_url,
30-
content=body,
31-
headers=headers,
32-
)
33-
return Response(content=resp.content, status_code=resp.status_code, headers=dict(resp.headers))
34-
17+
base_url = 'https://uiauto.dev'
3518

3619
@router.get("/")
3720
@router.get("/android/{path:path}")
3821
@router.get("/ios/{path:path}")
3922
@router.get("/demo/{path:path}")
4023
@router.get("/harmony/{path:path}")
4124
async def proxy_html(request: Request):
42-
target_url = "https://uiauto.dev/"
43-
cache = HTTPCache(cache_dir, target_url, key='homepage')
25+
cache = HTTPCache(cache_dir, base_url, key='homepage')
4426
response = await cache.proxy_request(request, update_cache=True)
4527
return response
4628
# update
4729

4830
@router.get("/assets/{path:path}")
4931
@router.get('/favicon.ico')
5032
async def proxy_assets(request: Request, path: str = ""):
51-
target_url = f"https://uiauto.dev{request.url.path}"
33+
target_url = f"{base_url}{request.url.path}"
5234
cache = HTTPCache(cache_dir, target_url)
5335
return await cache.proxy_request(request)
5436

0 commit comments

Comments
 (0)