norain 0.2.0.dev0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. norain/__init__.py +3 -0
  2. norain/__main__.py +5 -0
  3. norain/block.py +281 -0
  4. norain/boot.py +142 -0
  5. norain/cli.py +748 -0
  6. norain/control.py +703 -0
  7. norain/defaults.py +42 -0
  8. norain/generate.py +264 -0
  9. norain/install.py +312 -0
  10. norain/maintain.py +313 -0
  11. norain/model.py +409 -0
  12. norain/monitor.py +314 -0
  13. norain/paths.py +124 -0
  14. norain/probe.py +158 -0
  15. norain/proxy.py +136 -0
  16. norain/repair.py +278 -0
  17. norain/routing.py +332 -0
  18. norain/rules.py +214 -0
  19. norain/runner.py +59 -0
  20. norain-0.2.0.dev0.dist-info/METADATA +99 -0
  21. norain-0.2.0.dev0.dist-info/RECORD +53 -0
  22. norain-0.2.0.dev0.dist-info/WHEEL +5 -0
  23. norain-0.2.0.dev0.dist-info/entry_points.txt +3 -0
  24. norain-0.2.0.dev0.dist-info/licenses/LICENSE +21 -0
  25. norain-0.2.0.dev0.dist-info/top_level.txt +3 -0
  26. norain_core/__init__.py +4 -0
  27. norain_core/apply.py +70 -0
  28. norain_core/atomic.py +80 -0
  29. norain_core/cliutil.py +68 -0
  30. norain_core/daemonctl.py +192 -0
  31. norain_core/errors.py +60 -0
  32. norain_core/fetch.py +158 -0
  33. norain_core/log.py +104 -0
  34. norain_core/migrate.py +86 -0
  35. norain_core/osutil.py +332 -0
  36. norain_core/redact.py +60 -0
  37. norain_core/secrets.py +87 -0
  38. norain_core/singbox.py +93 -0
  39. norain_core/state.py +97 -0
  40. norain_core/supervisor.py +352 -0
  41. norain_core/sysproxy.py +112 -0
  42. norain_core/tomlio.py +86 -0
  43. norain_server/__init__.py +3 -0
  44. norain_server/__main__.py +5 -0
  45. norain_server/cert.py +276 -0
  46. norain_server/cli.py +477 -0
  47. norain_server/deploy.py +604 -0
  48. norain_server/firewall.py +226 -0
  49. norain_server/generate.py +112 -0
  50. norain_server/model.py +272 -0
  51. norain_server/paths.py +101 -0
  52. norain_server/runner.py +123 -0
  53. norain_server/service.py +83 -0
norain/control.py ADDED
@@ -0,0 +1,703 @@
1
+ """内控制模块:协调各个模块的运行,控制它们的运行参数。
2
+
3
+ `norain run` 启动的就是它(Controller)。职责:
4
+ - 审批:所有多模块交互操作、启动和结束模块都要经过 approve()。
5
+ - 启动测试检测模式:按「分流 → 代理 → 监控」的顺序拉起,监控通过后激活互拉保活、启动自维持;
6
+ 这期间抑制自维持模块的拉起请求。
7
+ - 代理模块的协议选择:protocol = "auto" 时按测速和连通性自适应切换(Selector)。
8
+ - 分流模块的自更新:定期更新规则集;分流模块学到新名单后重新生成配置并重载。
9
+ - 接受命令行(外控制模块)的请求,并把各模块的运行情况写到 status.json。
10
+
11
+ 进程模型:本进程里代理(sing-box 监督器)、分流、监控、自维持各是一个线程;
12
+ 另有一个独立的看门狗进程与本进程互拉(maintain.py)。
13
+
14
+ 命令行与本进程之间用状态目录里的请求文件通信(`run/ctl/<id>.req.json` → `<id>.resp.json`):
15
+ 不占端口,不需要鉴权,权限由目录权限(0700)保证。
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import collections
21
+ import os
22
+ import signal
23
+ import statistics
24
+ import threading
25
+ import time
26
+
27
+ from norain_core import osutil
28
+ from norain_core.daemonctl import alive as pid_alive
29
+ from norain_core.errors import (BIZ_PROCESS_EXITED, BIZ_RESTART_LIMIT, EXIT_CONFLICT, EXIT_OK,
30
+ EXIT_PERMISSION, EXIT_RESTART_LIMIT, EXIT_TIMEOUT, NorainError)
31
+ from norain_core.log import Logger, RotatingFile, new_op_id
32
+ from norain_core.state import RUNNING, get_desired, read_json, write_json
33
+ from norain_core.supervisor import Spec, check_ports_free, record_error
34
+
35
+ from . import block, boot, generate, maintain, probe, rules
36
+ from .model import PROTOCOLS, ClientConfig
37
+ from .monitor import MEANING, OK, Monitor, Ports, report_text
38
+ from .paths import Paths
39
+ from .proxy import ProxyModule
40
+ from .routing import Classifier, Learned, Router, save_learned
41
+
42
+ BOOT, RUNNING_PHASE, REPAIRING, BLOCKED, STOPPING = "boot-test", "running", "repairing", "blocked", "stopping"
43
+ MODULES = ("proxy", "routing", "monitor", "maintain", "block", "repair", "boot", "control", "cli")
44
+ ACTIVE_MODULES = ("routing", "proxy", "monitor", "maintain") # 有自己线程、可以启动和结束的模块
45
+ RELOAD_DEBOUNCE = 30.0 # 分流学习触发的重载之间的最小间隔(重载会中断已有连接)
46
+ STATE_HEARTBEAT = 30.0 # 状态没有变化时,状态文件最多隔这么久刷新一次
47
+ RULES_CHECK_INTERVAL = 60.0 # 多久看一次规则集是否到了更新时间(更新间隔以小时计,不需要每半秒看)
48
+ REQUEST_MAX_AGE = 120.0 # 命令行请求和回应文件的最长保留时间
49
+
50
+
51
+ # ---- 命令行 → 内控制 的请求通道 ------------------------------------------------
52
+
53
+ def send(paths: Paths, request: dict, *, timeout: float = 60.0) -> dict:
54
+ """命令行一侧:发一个请求并等待回应。内控制进程没有运行时抛出错误。"""
55
+ info = read_json(paths.runner) or {}
56
+ if not pid_alive(info.get("pid")):
57
+ raise NorainError("代理没有运行", exit_code=EXIT_CONFLICT, module="control", hint="先运行 norain start")
58
+ rid = new_op_id()
59
+ paths.ctl_dir.mkdir(parents=True, exist_ok=True)
60
+ req, resp = paths.ctl_dir / f"{rid}.req.json", paths.ctl_dir / f"{rid}.resp.json"
61
+ write_json(req, {**request, "id": rid, "at": time.time()})
62
+ deadline = time.monotonic() + timeout
63
+ try:
64
+ while time.monotonic() < deadline:
65
+ data = read_json(resp)
66
+ if data is not None:
67
+ return data
68
+ if not pid_alive(info.get("pid")):
69
+ raise NorainError("代理进程在处理请求时退出了", exit_code=EXIT_CONFLICT, module="control")
70
+ time.sleep(0.1)
71
+ raise NorainError("内控制模块没有在限定时间内回应", exit_code=EXIT_TIMEOUT, module="control",
72
+ retryable=True)
73
+ finally:
74
+ req.unlink(missing_ok=True)
75
+ resp.unlink(missing_ok=True)
76
+
77
+
78
+ # ---- 自适应选协议 --------------------------------------------------------------
79
+
80
+ class Selector:
81
+ """按连通性和延迟在 Hysteria2 与 Trojan 之间选择(docs/decisions/0009)。
82
+
83
+ 每个协议各有一个本地探测入口,测量不受当前选择的影响。切换条件:
84
+ - 当前协议连续 failure_threshold 次失败,而另一个协议可用 → 立即切换(不受冷却限制);
85
+ - 冷却期过后,当前协议的延迟中位数比另一个差到 latency_ratio 倍且差值超过 latency_margin_ms;
86
+ - 指标不相上下时回到 prefer 指定的协议。
87
+ """
88
+
89
+ def __init__(self, cfg: ClientConfig, paths: Paths, log: Logger) -> None:
90
+ self.cfg, self.paths, self.log = cfg, paths, log
91
+ self.sel = cfg.proxy.selection
92
+ self.ports = generate.probe_ports(cfg)
93
+ self.samples = {p: collections.deque(maxlen=5) for p in PROTOCOLS}
94
+ self.fail_streak = {p: 0 for p in PROTOCOLS}
95
+ self.switched_at = float((read_json(paths.selection) or {}).get("switched_ts") or 0)
96
+ self.next_at = 0.0
97
+ self.last: dict | None = None
98
+
99
+ def measure(self) -> dict:
100
+ out = {}
101
+ for p in PROTOCOLS:
102
+ r = probe.http(self.sel.test_url, proxy=(generate.LOOPBACK, self.ports[p]),
103
+ timeout=self.cfg.monitor.timeout_seconds, via=p)
104
+ ms = r.elapsed_ms if r.ok else None
105
+ self.samples[p].append(ms)
106
+ self.fail_streak[p] = 0 if r.ok else self.fail_streak[p] + 1
107
+ out[p] = round(ms, 1) if ms is not None else None
108
+ return out
109
+
110
+ def _median(self, p: str) -> float | None:
111
+ good = [s for s in self.samples[p] if s is not None]
112
+ return statistics.median(good) if len(good) >= 3 else None
113
+
114
+ def decide(self, active: str) -> tuple[str | None, str]:
115
+ other = next(p for p in PROTOCOLS if p != active)
116
+ other_up = bool(self.samples[other]) and self.samples[other][-1] is not None
117
+ if self.fail_streak[active] >= self.sel.failure_threshold and other_up:
118
+ return other, f"{active} 连续 {self.fail_streak[active]} 次探测失败"
119
+ if time.time() - self.switched_at < self.sel.cooldown_seconds:
120
+ return None, "冷却中"
121
+ ma, mo = self._median(active), self._median(other)
122
+ if ma is None or mo is None:
123
+ return None, "样本不足"
124
+ if ma > mo * self.sel.latency_ratio and ma - mo > self.sel.latency_margin_ms:
125
+ return other, f"{active} 延迟中位数 {ma:.0f} ms,{other} 为 {mo:.0f} ms"
126
+ if active != self.sel.prefer and mo <= ma:
127
+ return other, f"指标相当,回到优先协议 {other}"
128
+ return None, "保持"
129
+
130
+ def tick(self, active: str) -> tuple[str | None, str]:
131
+ self.next_at = time.monotonic() + self.sel.interval_seconds
132
+ sample = self.measure()
133
+ new, why = self.decide(active)
134
+ self.last = {"at": time.strftime("%Y-%m-%dT%H:%M:%S%z"), "sample_ms": sample, "active": active,
135
+ "decision": new or "keep", "reason": why}
136
+ return new, why
137
+
138
+ def record(self, new: str, reason: str) -> None:
139
+ self.switched_at = time.time()
140
+ data = read_json(self.paths.selection) or {}
141
+ data.update({"active": new, "reason": reason, "switched_ts": self.switched_at,
142
+ "switched_at": time.strftime("%Y-%m-%dT%H:%M:%S%z")})
143
+ write_json(self.paths.selection, data)
144
+
145
+ def status(self) -> dict:
146
+ return {"last_result": self.last, "fail_streak": dict(self.fail_streak),
147
+ "median_ms": {p: self._median(p) for p in PROTOCOLS}}
148
+
149
+
150
+ # ---- 内控制 --------------------------------------------------------------------
151
+
152
+ class Controller:
153
+ def __init__(self, paths: Paths, *, autostart: bool = False) -> None:
154
+ self.paths0 = paths
155
+ self.paths = paths
156
+ self.autostart = autostart
157
+ self.phase = BOOT
158
+ self.started_at = time.strftime("%Y-%m-%dT%H:%M:%S%z")
159
+ self.user_stopped: set[str] = set()
160
+ self.keepalive = False
161
+ self.decisions: collections.deque[dict] = collections.deque(maxlen=20)
162
+ self.last_request: dict | None = None
163
+ self.modules: dict = {}
164
+ self.selector: Selector | None = None
165
+ self._op = threading.RLock() # 串行化模块的启动 / 结束 / 重启
166
+ self._stop = threading.Event()
167
+ self._exit_code = EXIT_OK
168
+ self._reload_wanted: float | None = None
169
+ self._last_reload = 0.0
170
+ self._rules_thread: threading.Thread | None = None
171
+ self._rules_report: dict | None = None
172
+ self._boot_info: dict = {}
173
+ self._autostart_registered: bool | None = None
174
+ self._last_runner: dict | None = None
175
+ self._last_status: dict | None = None
176
+ self._state_written_at = 0.0
177
+ self._next_rules_check = 0.0
178
+ self.log: Logger = Logger("control", stderr=True)
179
+
180
+ # ---- 构建 ----
181
+
182
+ def _logger(self, module: str) -> Logger:
183
+ return Logger(module, op_id=self.log.op_id, sink=self.log.sink, stderr=False,
184
+ level=self.cfg.logging.level)
185
+
186
+ def _prepare(self) -> None:
187
+ from . import runner
188
+ self.prep = runner.prepare(self.paths0, apply=True)
189
+ self.cfg: ClientConfig = self.prep.cfg
190
+ self.paths = self.prep.paths
191
+ sink = RotatingFile(self.paths.log_file, max_bytes=self.cfg.logging.max_size_mb << 20,
192
+ keep=self.cfg.logging.retain_files)
193
+ self.log = Logger("control", op_id=self.log.op_id, sink=sink, stderr=False, level=self.cfg.logging.level)
194
+
195
+ def _spec(self) -> Spec:
196
+ cfg, p = self.cfg, self.paths
197
+ return Spec(
198
+ binary=self.prep.binary.path, config_json=p.current_json,
199
+ config_version=(read_json(p.meta_json) or {}).get("config_version", ""),
200
+ listeners=generate.listeners(cfg), check_interval=cfg.supervisor.check_interval_seconds,
201
+ restart_limit=cfg.supervisor.restart_limit, backoff=cfg.supervisor.backoff_seconds,
202
+ log_file=p.log_file, log_max_bytes=cfg.logging.max_size_mb << 20,
203
+ log_keep=cfg.logging.retain_files, log_level=cfg.logging.level,
204
+ runner_file=p.proxy_state, last_error_file=p.last_error, stop_file=p.proxy_stop,
205
+ on_line=self.router.feed)
206
+
207
+ def _local_proxy(self) -> tuple[str, int]:
208
+ listen = self.cfg.local.listen
209
+ host = {"0.0.0.0": "127.0.0.1", "::": "::1"}.get(listen, listen)
210
+ return host, self.cfg.local.port
211
+
212
+ def _probe(self, name: str) -> probe.Proxy:
213
+ if not generate.needs_probes(self.cfg):
214
+ return None
215
+ port = generate.probe_ports(self.cfg).get(name)
216
+ return (generate.LOOPBACK, port) if port else None
217
+
218
+ def _monitor_ports(self) -> Ports:
219
+ if not self.proxy.ready:
220
+ return Ports()
221
+ return Ports(routed=self._local_proxy(), direct=self._probe("direct"))
222
+
223
+ @property
224
+ def active(self) -> str:
225
+ return generate.active_protocol(self.cfg, self.paths)
226
+
227
+ def _build_modules(self) -> None:
228
+ cfg, paths = self.cfg, self.paths
229
+ self.geo = rules.Geo(self.prep.binary.path, paths)
230
+ self.monitor = Monitor(cfg, self._monitor_ports, self.geo, self._logger("monitor"),
231
+ on_change=lambda code, rnd: self.maintainer.notify(code, rnd.reason))
232
+ classifier = Classifier(cfg, paths, self.geo, direct=self._probe("direct"),
233
+ proxy=self._probe(self.active), internet_ok=self.monitor.internet_ok)
234
+ self.router = Router(cfg, paths, classifier, self._logger("routing"), on_change=self._want_reload)
235
+ self.proxy = ProxyModule(self._spec,
236
+ sysproxy_state=(paths.sysproxy_state
237
+ if cfg.proxy.traffic_mode == "system-proxy" else None))
238
+ self.maintainer = maintain.Maintainer(self, self._logger("maintain"))
239
+ self.selector = Selector(cfg, paths, self._logger("control")) if cfg.proxy.protocol == "auto" else None
240
+ self.modules = {"routing": self.router, "proxy": self.proxy, "monitor": self.monitor,
241
+ "maintain": self.maintainer}
242
+
243
+ # ---- 审批 ----
244
+
245
+ def approve(self, action: str, module: str, requester: str) -> tuple[bool, str]:
246
+ """所有启动 / 结束 / 重启模块的请求都从这里过。返回 (是否批准, 原因)。"""
247
+ ok, why = True, "批准"
248
+ starting = action in ("start", "restart")
249
+ if module not in ACTIVE_MODULES:
250
+ ok, why = False, f"{module} 不是可以单独启停的模块"
251
+ elif self.phase == STOPPING:
252
+ ok, why = False, "正在停止"
253
+ elif self.phase == BLOCKED and starting:
254
+ ok, why = False, "已进入阻断模式"
255
+ elif starting and requester != "boot" and get_desired(self.paths.desired) != RUNNING:
256
+ ok, why = False, "用户已要求停止(期望状态为 stopped)"
257
+ elif self.phase == BOOT and requester not in ("boot", "control"):
258
+ ok, why = False, "正在启动测试,暂不受理" # 抑制自维持模块的拉起
259
+ elif self.phase == REPAIRING and requester not in ("maintain", "control"):
260
+ ok, why = False, "自维持模块正在修复,稍后再试" # 修复期间不让别人插手
261
+ elif module == "monitor" and starting and not self.cfg.monitor.enabled:
262
+ ok, why = False, "配置里关闭了监控(monitor.enabled = false)"
263
+ self.decisions.append({"at": time.strftime("%H:%M:%S"), "action": action, "module": module,
264
+ "by": requester, "approved": ok, "reason": why})
265
+ (self.log.debug if ok else self.log.info)("control.approval", action=action, target=module,
266
+ requester=requester, approved=ok, reason=why)
267
+ return ok, why
268
+
269
+ def start_module(self, name: str, requester: str) -> tuple[bool, str]:
270
+ ok, why = self.approve("start", name, requester)
271
+ if not ok:
272
+ return False, why
273
+ with self._op:
274
+ try:
275
+ self.modules[name].start()
276
+ except Exception as e: # noqa: BLE001
277
+ return False, f"启动失败:{e}"
278
+ if requester == "cli":
279
+ self.user_stopped.discard(name)
280
+ if name == "maintain" and self.cfg.maintain.watchdog and self.keepalive:
281
+ self.activate_keepalive()
282
+ return True, "已启动"
283
+
284
+ def stop_module(self, name: str, requester: str) -> tuple[bool, str]:
285
+ ok, why = self.approve("stop", name, requester)
286
+ if not ok:
287
+ return False, why
288
+ with self._op:
289
+ if requester == "cli":
290
+ self.user_stopped.add(name) # 用户手动停的模块,自维持不再拉起
291
+ self.modules[name].stop()
292
+ if name == "maintain" and requester == "cli":
293
+ maintain.kill_watchdog(self.paths)
294
+ return True, "已停止"
295
+
296
+ def restart_proxy(self, requester: str, reason: str) -> tuple[bool, str]:
297
+ """重新生成配置并重启 sing-box。生成或校验失败时保留旧配置继续运行。"""
298
+ ok, why = self.approve("restart", "proxy", requester)
299
+ if not ok:
300
+ return False, why
301
+ with self._op:
302
+ try:
303
+ res = generate.apply(self.cfg, self.prep.secret, self.paths, self.prep.binary.path)
304
+ except NorainError as e:
305
+ self.log.error("control.regenerate_failed", error=e.message, reason=reason)
306
+ if self.proxy.alive():
307
+ return False, f"新配置没有通过校验,继续使用旧配置:{e.message}"
308
+ res = None
309
+ self.proxy.stop()
310
+ self.router.classifier.proxy = self._probe(self.active)
311
+ self.proxy.start()
312
+ self._last_reload = time.monotonic()
313
+ self.log.info("control.proxy_restarted", requester=requester, reason=reason,
314
+ config_version=res.version if res else None)
315
+ return True, "已重启"
316
+
317
+ # ---- 提供给自维持模块的接口 ----
318
+
319
+ def monitor_code(self) -> int:
320
+ return self.monitor.code if self.cfg.monitor.enabled else OK
321
+
322
+ def verify(self, rounds: int) -> int:
323
+ if not self.cfg.monitor.enabled:
324
+ return OK if self.proxy.ready else maintain.PROXY_DOWN
325
+ return self.monitor.verify(rounds)
326
+
327
+ def enter_repair(self, code: int) -> None:
328
+ self.phase = REPAIRING
329
+ self.monitor.pause()
330
+ self.log.warn("control.phase", phase=REPAIRING, code=code)
331
+
332
+ def leave_repair(self, outcome: str) -> None:
333
+ if self.phase == REPAIRING:
334
+ self.phase = RUNNING_PHASE
335
+ self.monitor.resume(reset=True)
336
+ self.log.info("control.phase", phase=self.phase, repair_outcome=outcome)
337
+
338
+ def activate_keepalive(self) -> None:
339
+ """激活互拉保活:拉起看门狗进程。"""
340
+ self.keepalive = True
341
+ if not maintain.watchdog_alive(self.paths):
342
+ maintain.spawn_watchdog(self.paths)
343
+ self.log.info("control.keepalive_activated")
344
+
345
+ def set_override(self, *, dns_strategy: str | None) -> None:
346
+ data = read_json(self.paths.selection) or {}
347
+ if dns_strategy:
348
+ data["dns_strategy"] = dns_strategy
349
+ else:
350
+ data.pop("dns_strategy", None)
351
+ write_json(self.paths.selection, data)
352
+
353
+ def clear_learned(self) -> None:
354
+ self.router.learned = Learned()
355
+ save_learned(self.paths, self.router.learned)
356
+
357
+ def switch_protocol(self, requester: str, reason: str) -> str | None:
358
+ """切换到另一个协议(只写选择结果,由调用方负责重启代理)。"""
359
+ if self.selector is None:
360
+ return None
361
+ new = next(p for p in PROTOCOLS if p != self.active)
362
+ self.selector.record(new, f"{requester}: {reason}")
363
+ self.log.warn("control.protocol_switched", new=new, requester=requester, reason=reason)
364
+ return new
365
+
366
+ def request_shutdown(self, *, blocked: bool = False, code: int = 0) -> None:
367
+ if blocked:
368
+ self.phase = BLOCKED
369
+ self._exit_code = EXIT_RESTART_LIMIT if (blocked or code) else EXIT_OK
370
+ if code:
371
+ record_error(self.paths.last_error, BIZ_RESTART_LIMIT if code == maintain.PROXY_DOWN else code,
372
+ f"代理无法恢复(错误码 {code}:{MEANING.get(code, '代理进程无法维持')})"
373
+ + (",已进入阻断模式" if blocked else ",阻断没有生效"))
374
+ self._stop.set()
375
+
376
+ # ---- 运行参数的调整 ----
377
+
378
+ def _want_reload(self) -> None:
379
+ self._reload_wanted = time.monotonic()
380
+
381
+ def _tick_reload(self) -> None:
382
+ if self._reload_wanted is None or self.phase != RUNNING_PHASE:
383
+ return
384
+ if time.monotonic() - self._last_reload < RELOAD_DEBOUNCE:
385
+ return
386
+ self._reload_wanted = None
387
+ self.restart_proxy("control", "分流名单或规则集有更新")
388
+
389
+ def _tick_selection(self) -> None:
390
+ sel = self.selector
391
+ if sel is None or self.phase != RUNNING_PHASE or time.monotonic() < sel.next_at or not self.proxy.ready:
392
+ return
393
+ new, why = sel.tick(self.active)
394
+ if new:
395
+ sel.record(new, why)
396
+ self.log.warn("control.protocol_switched", new=new, requester="control", reason=why)
397
+ self.restart_proxy("control", f"自适应切换协议:{why}")
398
+
399
+ def _update_rules(self) -> None:
400
+ try:
401
+ px = self._local_proxy() if self.proxy.ready else None
402
+ rep = rules.update(self.cfg, self.paths, proxy=px)
403
+ self._rules_report = {**rep, "at": time.strftime("%Y-%m-%dT%H:%M:%S%z")}
404
+ self.log.info("control.rules_updated", **rep)
405
+ if rep["updated"]:
406
+ self._want_reload()
407
+ except Exception as e: # noqa: BLE001
408
+ self.log.error("control.rules_update_failed", error=repr(e))
409
+
410
+ def _tick_rules(self) -> None:
411
+ """分流模块的自更新:到期时在后台线程里下载,不阻塞主循环。"""
412
+ if self.phase != RUNNING_PHASE or time.monotonic() < self._next_rules_check:
413
+ return
414
+ if self._rules_thread and self._rules_thread.is_alive():
415
+ return
416
+ self._next_rules_check = time.monotonic() + RULES_CHECK_INTERVAL
417
+ if rules.due(self.cfg, self.paths):
418
+ self._rules_thread = threading.Thread(target=self._update_rules, name="norain-rules", daemon=True)
419
+ self._rules_thread.start()
420
+
421
+ # ---- 状态 ----
422
+
423
+ def status(self) -> dict:
424
+ mods = {name: self.modules[name].status() for name in ACTIVE_MODULES if name in self.modules}
425
+ for name in self.user_stopped:
426
+ if name in mods:
427
+ mods[name]["stopped_by_user"] = True
428
+ blk = block.status(self.paths)
429
+ mods["block"] = {"state": "blocking" if blk["active"] else "idle", "last_result": blk if blk["active"] else None}
430
+ mods["repair"] = {"state": "repairing" if self.phase == REPAIRING else "idle",
431
+ "last_result": self.maintainer.last_result}
432
+ mods["boot"] = {"state": "testing" if self.phase == BOOT else "done", "mode": self.cfg.app.mode,
433
+ "autostart_registered": self._autostart_registered, "last_result": self._boot_info}
434
+ mods["control"] = {"state": self.phase, "last_result": list(self.decisions)[-5:]}
435
+ mods["cli"] = {"state": "listening", "last_result": self.last_request}
436
+ return {
437
+ "updated_at": time.strftime("%Y-%m-%dT%H:%M:%S%z"), "started_at": self.started_at,
438
+ "pid": os.getpid(), "phase": self.phase, "traffic_mode": self.cfg.proxy.traffic_mode,
439
+ "protocol": {"configured": self.cfg.proxy.protocol, "active": self.active,
440
+ "selection": self.selector.status() if self.selector else None},
441
+ "monitor": {"code": self.monitor_code(), "meaning": MEANING.get(self.monitor_code(), "")},
442
+ "rules": {"sets": rules.status(self.cfg, self.paths), "last_update": self._rules_report},
443
+ "modules": mods,
444
+ }
445
+
446
+ def _write_state(self) -> None:
447
+ """把运行状态写给命令行看。内容没有变化时不写盘(最多每 STATE_HEARTBEAT 秒刷新一次);
448
+ 这两个文件丢了可以重建,所以不 fsync——否则常驻进程每秒都在强制刷盘。"""
449
+ runner = {"pid": os.getpid(), "child_pid": self.proxy.child_pid, "ready": self.proxy.ready,
450
+ "restarts": self.proxy.restarts, "phase": self.phase, "started_at": self.started_at,
451
+ "log_file": str(self.paths.log_file)}
452
+ status = self.status()
453
+ stamp = status.pop("updated_at")
454
+ now = time.monotonic()
455
+ stale = now - self._state_written_at >= STATE_HEARTBEAT
456
+ try:
457
+ if stale or runner != self._last_runner or not self.paths.runner.exists():
458
+ write_json(self.paths.runner, runner, durable=False)
459
+ self._last_runner = runner
460
+ if stale or status != self._last_status:
461
+ write_json(self.paths.status_file, {**status, "updated_at": stamp}, durable=False)
462
+ self._last_status = status
463
+ if stale:
464
+ self._state_written_at = now
465
+ except OSError:
466
+ pass
467
+
468
+ # ---- 命令行请求 ----
469
+
470
+ def _handle(self, req: dict) -> dict:
471
+ op = req.get("op")
472
+ if op == "module":
473
+ name, action = req.get("name"), req.get("action")
474
+ if name not in ACTIVE_MODULES:
475
+ return {"ok": False, "message": f"{name} 不能单独启停;可以启停的模块:{'、'.join(ACTIVE_MODULES)}"}
476
+ if action == "start":
477
+ ok, why = self.start_module(name, "cli")
478
+ elif action == "stop":
479
+ ok, why = self.stop_module(name, "cli")
480
+ elif name == "proxy":
481
+ ok, why = self.restart_proxy("cli", "命令行请求")
482
+ else:
483
+ ok, why = self.stop_module(name, "cli")
484
+ if ok:
485
+ ok, why = self.start_module(name, "cli")
486
+ return {"ok": ok, "message": why, "module": self.modules[name].status()}
487
+ if op == "reload":
488
+ return self._reconfigure()
489
+ if op == "monitor":
490
+ rnd = self.monitor.check_once(feed=False)
491
+ return {"ok": True, "result": rnd.as_dict(), "text": report_text(rnd)}
492
+ if op == "select":
493
+ if self.selector is None:
494
+ return {"ok": False, "message": "protocol 不是 auto,没有自适应选择"}
495
+ new, why = self.selector.tick(self.active)
496
+ if new:
497
+ self.selector.record(new, why)
498
+ self.restart_proxy("cli", f"自适应切换协议:{why}")
499
+ return {"ok": True, "active": self.active, "switched": bool(new), "reason": why,
500
+ "result": self.selector.status()}
501
+ if op == "route":
502
+ v = self.router.check(str(req.get("domain", "")))
503
+ if v.changed:
504
+ self._want_reload()
505
+ return {"ok": True, "result": v.as_dict()}
506
+ if op == "rules-update":
507
+ self._update_rules()
508
+ return {"ok": True, "result": self._rules_report}
509
+ if op == "status":
510
+ return {"ok": True, "result": self.status()}
511
+ return {"ok": False, "message": f"未知请求:{op}"}
512
+
513
+ def _serve(self) -> None:
514
+ d = self.paths.ctl_dir
515
+ try:
516
+ names = sorted(os.listdir(d)) # 一次系统调用;目录不存在或为空是常态
517
+ except OSError:
518
+ return
519
+ now = time.time()
520
+ for name in names:
521
+ if name.endswith(".resp.json"):
522
+ # 命令行等不到回应就走了(超时、被中断):没人会来取的回应不能一直堆在目录里
523
+ try:
524
+ if now - (d / name).stat().st_mtime > REQUEST_MAX_AGE:
525
+ (d / name).unlink(missing_ok=True)
526
+ except OSError:
527
+ pass
528
+ continue
529
+ if not name.endswith(".req.json"):
530
+ continue
531
+ req_file = d / name
532
+ req = read_json(req_file)
533
+ try:
534
+ req_file.unlink(missing_ok=True)
535
+ except OSError:
536
+ continue # 删不掉就不处理,否则同一个请求会被反复执行
537
+ if not isinstance(req, dict):
538
+ continue
539
+ at = req.get("at", 0)
540
+ if not isinstance(at, (int, float)) or now - at > REQUEST_MAX_AGE:
541
+ continue
542
+ # 回应文件名取自请求文件名,不信任请求内容里的 id(缺失时会 KeyError,拖垮整个内控制)
543
+ rid = name[:-len(".req.json")]
544
+ try:
545
+ resp = self._handle(req)
546
+ except NorainError as e:
547
+ resp = {"ok": False, "message": e.message, "hint": e.hint}
548
+ except Exception as e: # noqa: BLE001 单个请求出错不能拖垮内控制
549
+ resp = {"ok": False, "message": f"处理请求时出错:{e!r}"}
550
+ self.last_request = {"at": time.strftime("%H:%M:%S"), "op": req.get("op"),
551
+ "ok": resp.get("ok"), "message": resp.get("message")}
552
+ try:
553
+ write_json(d / f"{rid}.resp.json", resp, durable=False)
554
+ except (OSError, TypeError, ValueError) as e: # 回应写不出去只影响这一个请求
555
+ self.log.error("control.response_failed", op=str(req.get("op")), error=repr(e))
556
+
557
+ def _reconfigure(self) -> dict:
558
+ """cfg.toml 改过之后重新加载:新配置通过校验才替换,否则继续用旧配置运行。"""
559
+ from . import runner
560
+ if self.phase != RUNNING_PHASE:
561
+ return {"ok": False, "message": f"当前阶段是 {self.phase},稍后再试"}
562
+ try:
563
+ new = runner.prepare(self.paths0, apply=False)
564
+ except NorainError as e:
565
+ detail = ";".join(f"{f.path} {f.message}" for f in e.field_errors) or e.message
566
+ return {"ok": False, "message": f"新配置无效,继续使用旧配置:{detail}"}
567
+ ready = False
568
+ with self._op:
569
+ self.phase = BOOT # 重建期间抑制自维持
570
+ try:
571
+ # 先生成并运行 sing-box check。候选配置无效时保持旧模块完整运行,
572
+ # 不要先停掉代理再把错误留给 finally 处理。
573
+ generate.apply(new.cfg, new.secret, new.paths, new.binary.path)
574
+ for name in ("maintain", "monitor", "routing", "proxy"):
575
+ self.modules[name].stop()
576
+ self.prep, self.cfg, self.paths = new, new.cfg, new.paths
577
+ self._build_modules()
578
+ for name in ("routing", "proxy"):
579
+ self.modules[name].start()
580
+ ready = self.proxy.wait_ready(30)
581
+ if self.cfg.monitor.enabled and "monitor" not in self.user_stopped:
582
+ self.monitor.start()
583
+ finally:
584
+ self.phase = RUNNING_PHASE
585
+ # 无论重建是否成功,自维持都要回到岗位上(除非用户手动停了它)
586
+ if "maintain" not in self.user_stopped:
587
+ self.modules["maintain"].start()
588
+ try:
589
+ self._autostart_registered = boot.registered()
590
+ except Exception: # noqa: BLE001
591
+ pass
592
+ return {"ok": ready, "message": "已按新配置重新加载" if ready else "已重新加载,但代理还没有就绪"}
593
+
594
+ # ---- 主流程 ----
595
+
596
+ def _boot_sequence(self) -> None:
597
+ """启动测试检测模式:分流 → 代理 → 监控;通过后激活互拉保活并启动自维持。"""
598
+ cfg = self.cfg
599
+ self.start_module("routing", "boot")
600
+ self.start_module("proxy", "boot")
601
+ code, reason = OK, ""
602
+ if not self.proxy.wait_ready(max(30.0, cfg.supervisor.backoff_seconds * 4)):
603
+ code, reason = maintain.PROXY_DOWN, "sing-box 没有在限定时间内就绪"
604
+ else:
605
+ self._write_state() # 让等待中的 `norain start` 尽快返回
606
+ if cfg.monitor.enabled:
607
+ code = self.monitor.verify(cfg.monitor.recovery_threshold)
608
+ reason = self.monitor.last.reason if self.monitor.last else ""
609
+ self.start_module("monitor", "boot")
610
+ self._boot_info.update({"test_code": code, "test_meaning": MEANING.get(code, "代理进程无法维持"),
611
+ "passed": code == OK})
612
+ self.phase = RUNNING_PHASE
613
+ if code == OK:
614
+ self.log.info("boot.test_passed")
615
+ if cfg.maintain.watchdog:
616
+ self.activate_keepalive()
617
+ else:
618
+ self.log.error("boot.test_failed", code=code, reason=reason)
619
+ if code != 100:
620
+ self.maintainer.notify(code, reason) # 交给自维持模块:修复,修不好就阻断
621
+ self.start_module("maintain", "boot")
622
+
623
+ def _shutdown(self) -> None:
624
+ self.phase = BLOCKED if self.phase == BLOCKED else STOPPING
625
+ for name in ("maintain", "monitor", "routing", "proxy"):
626
+ mod = self.modules.get(name)
627
+ if mod is not None:
628
+ try:
629
+ mod.stop()
630
+ except Exception: # noqa: BLE001
631
+ pass
632
+ # 用户要求停止或已进入阻断时,看门狗不应再活着;其他情况(例如本进程被系统结束)留着它
633
+ if get_desired(self.paths.desired) != RUNNING or block.is_active(self.paths):
634
+ maintain.kill_watchdog(self.paths)
635
+ for f in (self.paths.runner, self.paths.status_file, self.paths.stop_file):
636
+ f.unlink(missing_ok=True)
637
+ self.log.info("control.stopped", exit_code=self._exit_code, phase=self.phase)
638
+
639
+ def run(self) -> int:
640
+ paths = self.paths0
641
+ if self.autostart and get_desired(paths.desired) != RUNNING:
642
+ return EXIT_OK # 用户手动 stop 过:开机自启动时不拉起
643
+ other = read_json(paths.runner) or {}
644
+ if other.get("pid") != os.getpid() and pid_alive(other.get("pid")):
645
+ raise NorainError(f"已经有一个 Norain 在运行(PID {other['pid']})", exit_code=EXIT_CONFLICT,
646
+ module="control")
647
+ paths.run_dir.mkdir(parents=True, exist_ok=True)
648
+ paths.stop_file.unlink(missing_ok=True)
649
+ write_json(paths.runner, {"pid": os.getpid(), "child_pid": None, "ready": False, "phase": BOOT})
650
+ try:
651
+ self._boot_info = boot.pre_start(paths) # 处于阻断模式时先恢复为非阻断模式
652
+ self._prepare()
653
+ if self.cfg.proxy.traffic_mode == "tun" and not osutil.is_admin():
654
+ raise NorainError("TUN 模式需要管理员权限", exit_code=EXIT_PERMISSION, module="control",
655
+ hint="用管理员 / root 运行,或把 proxy.traffic_mode 改为 mixed")
656
+ check_ports_free(generate.listeners(self.cfg))
657
+ except NorainError as e:
658
+ record_error(paths.last_error, e.business_code or BIZ_PROCESS_EXITED, e.message)
659
+ paths.runner.unlink(missing_ok=True)
660
+ raise
661
+ if self._boot_info.get("was_blocked"):
662
+ self.log.info("boot.block_released")
663
+ try:
664
+ signal.signal(signal.SIGTERM, lambda *_: self._stop.set())
665
+ signal.signal(signal.SIGINT, lambda *_: self._stop.set())
666
+ except ValueError:
667
+ pass
668
+ self._build_modules()
669
+ try:
670
+ self._autostart_registered = boot.registered()
671
+ except Exception: # noqa: BLE001
672
+ self._autostart_registered = None
673
+ self.log.info("control.started", traffic_mode=self.cfg.proxy.traffic_mode,
674
+ protocol=self.cfg.proxy.protocol, active=self.active, mode=self.cfg.app.mode)
675
+ try:
676
+ self._boot_sequence()
677
+ next_state = 0.0
678
+ while not self._stop.wait(0.5):
679
+ if paths.stop_file.exists():
680
+ break
681
+ self._serve()
682
+ self._tick_reload()
683
+ self._tick_selection()
684
+ self._tick_rules()
685
+ if time.monotonic() >= next_state:
686
+ next_state = time.monotonic() + 2
687
+ self._write_state()
688
+ finally:
689
+ self._shutdown()
690
+ return self._exit_code
691
+
692
+
693
+ def read_status(paths: Paths) -> dict | None:
694
+ """命令行读取各模块的运行情况。内控制进程不在时返回 None。"""
695
+ info = read_json(paths.runner) or {}
696
+ if not pid_alive(info.get("pid")):
697
+ return None
698
+ return read_json(paths.status_file)
699
+
700
+
701
+ def module_names() -> tuple[str, ...]:
702
+ return MODULES
703
+