Files
ykxmz/_build/remember_relay.py
T
xmz7891 0a4ec4f0c4 1.6.2:常规发布
- 版本号 1.6.2 -> 1.6.2
- 重建三端并打包
- 关键测试全过
2026-10-06 20:47:30 +08:00

113 lines
4.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 -*-
r"""让被控端记住上次连通的中转地址。
问题:
设备 IP 会变(DHCP),内置的默认地址写死了就过期。
start() 每次都要把候选地址挨个探一遍,每个等到超时 ——
实测 2 个候选要 10 秒,这 10 秒里局域网发现都还没起来。
改法:
1. 挑中的地址存进 store(relay_last_good)
2. 下次启动把它排到候选列表**最前面**,通常一次就通,启动瞬间完成
3. 地址失效时会自动往下试,试通了再存新的 —— 自愈
4. 内置默认地址更新成当前设备地址(192.168.2.156)
"""
from __future__ import annotations
import ast
import sys
from pathlib import Path
ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
RELAY_PY = ROOT / "windows" / "lanremote" / "relay.py"
SERVER_PY = ROOT / "windows" / "lanremote" / "server.py"
# ---------- 1. 记住并优先使用上次通的地址 ----------
OLD_PICK = ''' self.candidates = candidates
# 先探一遍,挑第一个通的
chosen = self._pick_candidate(candidates)
self.url = chosen
if len(candidates) > 1:
self._log(f"中转地址候选 {len(candidates)} 个,选用 {chosen}")'''
NEW_PICK = ''' # 上次连通的地址排到最前面。
# 设备 IP 会变(DHCP),但大概率还是上次那个;
# 排前面能省掉"挨个探到超时"的那几秒(实测 2 个候选要 10 秒,
# 而这段时间局域网发现都还没起来,手表搜不到电脑)。
try:
last_good = str(self.store.get("relay_last_good") or "").strip()
except Exception: # noqa: BLE001
last_good = ""
if last_good:
normalized = last_good.rstrip("/")
if normalized in candidates:
candidates.remove(normalized)
candidates.insert(0, normalized)
self.candidates = candidates
# 先探一遍,挑第一个通的
chosen = self._pick_candidate(candidates)
self.url = chosen
if len(candidates) > 1:
self._log(f"中转地址候选 {len(candidates)} 个,选用 {chosen}")
# 记下来,下次优先试它
if chosen:
try:
if str(self.store.get("relay_last_good") or "") != chosen:
self.store.set("relay_last_good", chosen)
except Exception: # noqa: BLE001
pass'''
# ---------- 2. 更新内置默认地址 ----------
OLD_DEFAULT = 'DEFAULT_RELAY_URL = "192.168.10.105:8080;https://relay.xxmxxs.top"'
NEW_DEFAULT = ('# 内置的中转地址:局域网优先,连不上自动切公网隧道。\n'
'# 第一个是随身 WiFi 的固定地址(写死是因为它常在同一网段、'
'直连最快);\n'
'# 它变了也没关系 —— 连上过的地址会被记住并排到最前面。\n'
'DEFAULT_RELAY_URL = "192.168.2.156:8080;'
'https://relay.xxmxxs.top"')
def main() -> int:
ok = True
text = RELAY_PY.read_text(encoding="utf-8")
if "relay_last_good" in text:
print(" relay.py: 已经改过")
elif OLD_PICK in text:
text = text.replace(OLD_PICK, NEW_PICK, 1)
RELAY_PY.write_text(text, encoding="utf-8", newline="\n")
print(" relay.py: 加上「记住上次通的地址」")
else:
print(" [失败] relay.py 锚点没找到")
ok = False
stext = SERVER_PY.read_text(encoding="utf-8")
if "192.168.2.156" in stext:
print(" server.py: 默认地址已经是新的")
elif OLD_DEFAULT in stext:
stext = stext.replace(OLD_DEFAULT, NEW_DEFAULT, 1)
SERVER_PY.write_text(stext, encoding="utf-8", newline="\n")
print(" server.py: 默认地址改成 192.168.2.156")
else:
print(" [提示] server.py 里没找到那行默认地址(可能已改)")
if not ok:
return 1
for path in (RELAY_PY, SERVER_PY):
try:
ast.parse(path.read_text(encoding="utf-8"))
print(f" {path.name} 语法 OK")
except SyntaxError as exc:
print(f" {path.name} 语法错误: 行 {exc.lineno}: {exc.msg}")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())