Files
ykxmz/_build/run_java_relay_test.py
T
xmz7891 b62ac865ea ykxmz 1.5.0:修复手表端 TLS 证书校验 + MTU 修复网络稳定性
被控端
- 修复 MTU 问题:夹 MSS + 路径 MTU 探测(公网可用率 48% -> 100%)
- 复用 HTTP 连接(免掉每次新建 TLS 的 30% 握手失败)
- 每分钟回头找局域网,自动切回快 400 倍的路径
- 画面心跳帧:静止时省流量,客户端仍知道流是活的
- 锁屏时给出可操作的提示

手表端
- 内置 GTS Root R4 / GlobalSign Root CA,修 SSLHandshakeException
- 连接错误翻译成人话(证书/时间/解析/超时分别提示)
- 经中转连上后用服务器报的地址刷新已配对记录
- 新增局域网连接页面、服务器延迟检测

服务器端
- 支持 chunked 请求体(修每 16 秒一次的连接错乱与大面积 502)
- 网络调优脚本 net-tune.sh(开机自动应用)
- 管理页:批量删除、网络体检、下载区
- 隧道改 HTTP/2 + 双连接

文档
- 许可协议 / 安装前必读 / 安装后须知(作者:小麦子)
2026-10-06 19:01:05 +08:00

167 lines
6.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""跑 Java 手表端经中转连被控端的联调测试。
流程:
1. 起本地中转服务器
2. 起 Python 被控端(配置成连这个中转)
3. 编译并运行 JavaRelayTest(走同一套 RelayTransport)
4. 清理
这条链路就是最终要交付的形态:
Java 手表 --HTTPS--> 中转 <--HTTPS-- Python 被控端
"""
from __future__ import annotations
import os
import shutil
import subprocess
import sys
import tempfile
import time
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
WINDOWS = ROOT / "windows"
TOOLCHAIN = Path(r"C:\Users\shu\Desktop\deepseekagent\starklink\toolchain")
JAVAC = TOOLCHAIN / "jdk17" / "bin" / "javac.exe"
JAVA = TOOLCHAIN / "jdk17" / "bin" / "java.exe"
RELAY_PORT = 8091
AGENT_PORT = 8898
RELAY_URL = f"http://127.0.0.1:{RELAY_PORT}"
PAIR_CODE = "246802"
def main() -> int:
real_data = WINDOWS / "data"
backup = WINDOWS / "data_bk_jrelay"
if backup.exists():
shutil.rmtree(backup, ignore_errors=True)
had_data = real_data.exists()
if had_data:
shutil.move(str(real_data), str(backup))
work = Path(tempfile.mkdtemp(prefix="javarelay_"))
relay_proc = agent_proc = None
try:
# ---- 配置被控端 ----
real_data.mkdir(parents=True, exist_ok=True)
sys.path.insert(0, str(WINDOWS))
from lanremote.config import Store
store = Store(real_data)
store.set_pairing_code(PAIR_CODE)
store.set("setup_done", True)
store.set("allow_new_pairing", True)
store.set("close_pair_after_first", False)
store.set("server_name", "Java测试电脑")
store.set("relay_url", RELAY_URL)
store.set("port", AGENT_PORT)
# ---- 起中转 ----
print("起本地中转服务器…")
env = dict(os.environ)
env.update({
"RELAY_PORT": str(RELAY_PORT), "RELAY_BIND": "127.0.0.1",
"RELAY_DATA": str(work / "relaydata"), "RELAY_POLL": "8",
"PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1",
})
(work / "relaydata").mkdir(parents=True, exist_ok=True)
relay_log = open(work / "relay.log", "wb")
relay_proc = subprocess.Popen(
[sys.executable, "-u", str(ROOT / "relay" / "relay.py")],
env=env, stdout=relay_log, stderr=subprocess.STDOUT)
for _ in range(40):
time.sleep(0.5)
try:
with urllib.request.urlopen(RELAY_URL + "/v1/health",
timeout=2) as r:
if r.status == 200:
break
except Exception: # noqa: BLE001
continue
print(" 中转已就绪")
# ---- 起被控端 ----
print("起被控端(它会主动连中转)…")
agent_log = open(work / "agent.log", "wb")
agent_proc = subprocess.Popen(
[sys.executable, "-u", str(WINDOWS / "run_agent.py"),
"--silent", "--port", str(AGENT_PORT)],
cwd=str(WINDOWS), stdout=agent_log, stderr=subprocess.STDOUT)
online = False
for _ in range(60):
time.sleep(1.0)
try:
with urllib.request.urlopen(RELAY_URL + "/v1/health",
timeout=3) as r:
import json
snap = json.loads(r.read())
if any(d.get("online") for d in snap.get("devices", [])):
online = True
break
except Exception: # noqa: BLE001
continue
if not online:
print(" 被控端没登记上,日志:")
print((work / "agent.log").read_text(encoding="utf-8",
errors="replace")[-1200:])
return 1
print(" 被控端已登记并在线")
print()
# ---- 编译并跑 Java 测试 ----
classes = work / "classes"
classes.mkdir(parents=True, exist_ok=True)
sources = [
ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "Json.java",
ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "B64.java",
ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "Transport.java",
ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "RelayTransport.java",
ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "RemoteClient.java",
ROOT / "android" / "src" / "com" / "lanremote" / "watch" / "DiscoveryCore.java",
ROOT / "_tests" / "java" / "JavaRelayTest.java",
]
compile_result = subprocess.run(
[str(JAVAC), "--release", "8", "-encoding", "UTF-8",
"-d", str(classes)] + [str(s) for s in sources],
capture_output=True, text=True, encoding="utf-8", errors="replace")
if compile_result.returncode != 0:
print("Java 编译失败:")
print(compile_result.stderr[:2000])
return 1
print("Java 编译通过")
print()
result = subprocess.run(
[str(JAVA), "-Dfile.encoding=UTF-8", "-cp", str(classes),
"JavaRelayTest", RELAY_URL, PAIR_CODE],
capture_output=True, text=True, encoding="utf-8", errors="replace")
print(result.stdout)
if result.returncode != 0 and result.stderr.strip():
print("[stderr] " + result.stderr[-800:])
return result.returncode
finally:
for process in (agent_proc, relay_proc):
if process is not None:
try:
process.terminate()
process.wait(timeout=8)
except Exception: # noqa: BLE001
try:
process.kill()
except Exception: # noqa: BLE001
pass
time.sleep(1)
if real_data.exists():
shutil.rmtree(real_data, ignore_errors=True)
if had_data and backup.exists():
shutil.move(str(backup), str(real_data))
shutil.rmtree(work, ignore_errors=True)
if __name__ == "__main__":
sys.exit(main())