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.
- norain/__init__.py +3 -0
- norain/__main__.py +5 -0
- norain/block.py +281 -0
- norain/boot.py +142 -0
- norain/cli.py +748 -0
- norain/control.py +703 -0
- norain/defaults.py +42 -0
- norain/generate.py +264 -0
- norain/install.py +312 -0
- norain/maintain.py +313 -0
- norain/model.py +409 -0
- norain/monitor.py +314 -0
- norain/paths.py +124 -0
- norain/probe.py +158 -0
- norain/proxy.py +136 -0
- norain/repair.py +278 -0
- norain/routing.py +332 -0
- norain/rules.py +214 -0
- norain/runner.py +59 -0
- norain-0.2.0.dev0.dist-info/METADATA +99 -0
- norain-0.2.0.dev0.dist-info/RECORD +53 -0
- norain-0.2.0.dev0.dist-info/WHEEL +5 -0
- norain-0.2.0.dev0.dist-info/entry_points.txt +3 -0
- norain-0.2.0.dev0.dist-info/licenses/LICENSE +21 -0
- norain-0.2.0.dev0.dist-info/top_level.txt +3 -0
- norain_core/__init__.py +4 -0
- norain_core/apply.py +70 -0
- norain_core/atomic.py +80 -0
- norain_core/cliutil.py +68 -0
- norain_core/daemonctl.py +192 -0
- norain_core/errors.py +60 -0
- norain_core/fetch.py +158 -0
- norain_core/log.py +104 -0
- norain_core/migrate.py +86 -0
- norain_core/osutil.py +332 -0
- norain_core/redact.py +60 -0
- norain_core/secrets.py +87 -0
- norain_core/singbox.py +93 -0
- norain_core/state.py +97 -0
- norain_core/supervisor.py +352 -0
- norain_core/sysproxy.py +112 -0
- norain_core/tomlio.py +86 -0
- norain_server/__init__.py +3 -0
- norain_server/__main__.py +5 -0
- norain_server/cert.py +276 -0
- norain_server/cli.py +477 -0
- norain_server/deploy.py +604 -0
- norain_server/firewall.py +226 -0
- norain_server/generate.py +112 -0
- norain_server/model.py +272 -0
- norain_server/paths.py +101 -0
- norain_server/runner.py +123 -0
- norain_server/service.py +83 -0
norain/routing.py
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
"""分流模块的运行时部分:按最初目标的四步流程判断一个域名该走哪条路,并把结论学进名单。
|
|
2
|
+
|
|
3
|
+
静态的分流(白名单、黑名单、规则集、默认动作)由 generate.py 写进 sing-box 配置,sing-box 在
|
|
4
|
+
进程内完成路由,Norain 无法逐个请求介入。所以「试连 → 改判 → 加入白名单」做成事后学习:
|
|
5
|
+
|
|
6
|
+
1. 从 sing-box 日志里发现访问失败的域名(Router.feed);
|
|
7
|
+
2. 对这个域名执行最初目标的流程(Classifier.classify);
|
|
8
|
+
3. 结论写入 learned.json,由内控制模块重新生成配置并重载 sing-box。
|
|
9
|
+
|
|
10
|
+
最初目标的流程与本实现的对应:
|
|
11
|
+
第 2 步 查白名单/黑名单 → 已被名单覆盖的域名不再判断
|
|
12
|
+
第 3 步 本地 DNS 解析并判断国内外 → 国内 DNS 解析 + geosite-cn(判不出时用 geoip-cn)
|
|
13
|
+
A 国内:直连试连 → 不行则代理试连(成功就改判国外、加入白名单)→ 再不行用公共 DNS 重新解析
|
|
14
|
+
B 国外:代理试连 → 不行则由服务器侧解析并更新本地解析结果
|
|
15
|
+
C 都不行 → 第 4 步
|
|
16
|
+
第 4 步 GeoIP 判断;查不到就交给服务器解析并走代理(默认动作)
|
|
17
|
+
特殊情况:本地没有联网时不判断;哪条路都不通时记为「目标不可达」(网站维护或 DNS 故障),不学习。
|
|
18
|
+
|
|
19
|
+
sing-box 的日志格式是按 1.x 的常见输出推定的,尚未用真实日志验证。
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from __future__ import annotations
|
|
23
|
+
|
|
24
|
+
import queue
|
|
25
|
+
import re
|
|
26
|
+
import threading
|
|
27
|
+
import time
|
|
28
|
+
from collections import OrderedDict
|
|
29
|
+
from dataclasses import dataclass, field
|
|
30
|
+
from typing import Callable
|
|
31
|
+
|
|
32
|
+
from norain_core.log import Logger
|
|
33
|
+
from norain_core.state import read_json, write_json
|
|
34
|
+
|
|
35
|
+
from . import probe
|
|
36
|
+
from .model import ClientConfig, entry_key
|
|
37
|
+
from .paths import Paths
|
|
38
|
+
from .rules import Geo
|
|
39
|
+
|
|
40
|
+
UNREACHABLE_TTL = 1800 # 「目标不可达」的记忆时间
|
|
41
|
+
RETRY_COOLDOWN = 600 # 同一域名两次判断的最小间隔
|
|
42
|
+
FAILURES_BEFORE_CLASSIFY = 2
|
|
43
|
+
|
|
44
|
+
_ANSI = re.compile(r"\x1b\[[0-9;]*m")
|
|
45
|
+
_CONN = re.compile(r"\[(\d+)[^\]]*\]\s+outbound/[\w-]+\[([^\]]+)\]: outbound (?:packet )?connection to "
|
|
46
|
+
r"\[?([^\s\]]+?)\]?:(\d+)\s*$")
|
|
47
|
+
_ERR = re.compile(r"\bERROR\b.*?\[(\d+)[^\]]*\]")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# ---- 学到的名单 ----------------------------------------------------------------
|
|
51
|
+
|
|
52
|
+
@dataclass
|
|
53
|
+
class Learned:
|
|
54
|
+
proxy: dict[str, dict] = field(default_factory=dict) # 域名 → {at, reason}
|
|
55
|
+
direct: dict[str, dict] = field(default_factory=dict)
|
|
56
|
+
hosts: dict[str, dict] = field(default_factory=dict) # 域名 → {ips, expires}
|
|
57
|
+
unreachable: dict[str, float] = field(default_factory=dict) # 域名 → 记忆到期时间
|
|
58
|
+
|
|
59
|
+
def for_generator(self) -> dict:
|
|
60
|
+
now = time.time()
|
|
61
|
+
return {"proxy": sorted(self.proxy), "direct": sorted(self.direct),
|
|
62
|
+
"hosts": {d: v["ips"] for d, v in self.hosts.items()
|
|
63
|
+
if v.get("expires", 0) > now and v.get("ips")}}
|
|
64
|
+
|
|
65
|
+
def prune(self) -> bool:
|
|
66
|
+
now = time.time()
|
|
67
|
+
before = (len(self.hosts), len(self.unreachable))
|
|
68
|
+
self.hosts = {d: v for d, v in self.hosts.items() if v.get("expires", 0) > now}
|
|
69
|
+
self.unreachable = {d: t for d, t in self.unreachable.items() if t > now}
|
|
70
|
+
return before[0] != len(self.hosts) # hosts 变化才需要重新生成配置
|
|
71
|
+
|
|
72
|
+
def as_dict(self) -> dict:
|
|
73
|
+
return {"proxy": self.proxy, "direct": self.direct, "hosts": self.hosts,
|
|
74
|
+
"unreachable": self.unreachable}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def load_learned(paths: Paths) -> Learned:
|
|
78
|
+
data = read_json(paths.learned) or {}
|
|
79
|
+
return Learned(**{k: data.get(k) or {} for k in ("proxy", "direct", "hosts", "unreachable")})
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def save_learned(paths: Paths, learned: Learned) -> None:
|
|
83
|
+
paths.routing_dir.mkdir(parents=True, exist_ok=True)
|
|
84
|
+
write_json(paths.learned, learned.as_dict())
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def covered(domain: str, entries) -> bool:
|
|
88
|
+
"""域名是否被名单里的某一条覆盖(后缀匹配,`full:` 精确匹配)。"""
|
|
89
|
+
domain = domain.lower().rstrip(".")
|
|
90
|
+
for raw in entries:
|
|
91
|
+
e = raw.strip().lower()
|
|
92
|
+
if e.startswith("full:"):
|
|
93
|
+
if domain == e[5:]:
|
|
94
|
+
return True
|
|
95
|
+
elif domain == entry_key(e) or domain.endswith("." + entry_key(e)):
|
|
96
|
+
return True
|
|
97
|
+
return False
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
# ---- 判断流程 ------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
@dataclass
|
|
103
|
+
class Verdict:
|
|
104
|
+
domain: str
|
|
105
|
+
decision: str # proxy | direct | unreachable | skip
|
|
106
|
+
steps: list[str]
|
|
107
|
+
changed: bool = False # 学到了新东西,需要重新生成配置
|
|
108
|
+
|
|
109
|
+
def as_dict(self) -> dict:
|
|
110
|
+
return {"domain": self.domain, "decision": self.decision, "changed": self.changed,
|
|
111
|
+
"steps": self.steps}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class Classifier:
|
|
115
|
+
def __init__(self, cfg: ClientConfig, paths: Paths, geo: Geo, *, direct: probe.Proxy,
|
|
116
|
+
proxy: probe.Proxy, internet_ok: Callable[[], bool] = lambda: True) -> None:
|
|
117
|
+
"""direct / proxy:强制直连和强制走代理的本地探测入口。"""
|
|
118
|
+
self.cfg, self.paths, self.geo = cfg, paths, geo
|
|
119
|
+
self.direct, self.proxy, self.internet_ok = direct, proxy, internet_ok
|
|
120
|
+
self.timeout = cfg.monitor.timeout_seconds
|
|
121
|
+
|
|
122
|
+
def _try(self, domain: str, via: str) -> bool:
|
|
123
|
+
return probe.reachable(domain, proxy=self.direct if via == "direct" else self.proxy,
|
|
124
|
+
timeout=self.timeout, via=via).ok
|
|
125
|
+
|
|
126
|
+
def _try_ips(self, ips: list[str], via: str) -> bool:
|
|
127
|
+
px = self.direct if via == "direct" else self.proxy
|
|
128
|
+
return any(probe.tcp(ip, 443, proxy=px, timeout=self.timeout) for ip in ips[:3])
|
|
129
|
+
|
|
130
|
+
def classify(self, domain: str, learned: Learned) -> Verdict:
|
|
131
|
+
domain = domain.lower().rstrip(".")
|
|
132
|
+
steps: list[str] = []
|
|
133
|
+
r, cfg = self.cfg.routing, self.cfg
|
|
134
|
+
|
|
135
|
+
def learn(bucket: str, reason: str, ips: list[str] | None = None) -> Verdict:
|
|
136
|
+
other = "direct" if bucket == "proxy" else "proxy"
|
|
137
|
+
getattr(learned, other).pop(domain, None)
|
|
138
|
+
getattr(learned, bucket)[domain] = {"at": time.strftime("%Y-%m-%dT%H:%M:%S%z"), "reason": reason}
|
|
139
|
+
if ips: # 更新解析结果
|
|
140
|
+
learned.hosts[domain] = {"ips": ips[:4], "expires": time.time() + max(cfg.dns.cache_ttl_seconds, 60)}
|
|
141
|
+
steps.append(f"更新解析结果:{', '.join(ips[:4])}")
|
|
142
|
+
learned.unreachable.pop(domain, None)
|
|
143
|
+
steps.append(f"结论:{'走代理(加入白名单)' if bucket == 'proxy' else '直连(加入黑名单)'}——{reason}")
|
|
144
|
+
return Verdict(domain, bucket, steps, changed=True)
|
|
145
|
+
|
|
146
|
+
# 特殊情况:本地没有联网
|
|
147
|
+
if not self.internet_ok():
|
|
148
|
+
return Verdict(domain, "skip", ["本地网络未连接,不做判断"])
|
|
149
|
+
# 第 2 步:名单
|
|
150
|
+
if covered(domain, r.proxy_domains):
|
|
151
|
+
return Verdict(domain, "proxy", ["在白名单中:走代理,由服务器侧解析"])
|
|
152
|
+
if covered(domain, r.direct_domains):
|
|
153
|
+
return Verdict(domain, "direct", ["在黑名单中:直连,用国内 DNS 解析"])
|
|
154
|
+
if learned.unreachable.get(domain, 0) > time.time():
|
|
155
|
+
return Verdict(domain, "unreachable", ["此前判定为目标不可达,暂不重试"])
|
|
156
|
+
|
|
157
|
+
# 第 3 步:本地(国内)DNS 解析,判断国内还是国外
|
|
158
|
+
local_ips = probe.doh(cfg.dns.domestic_doh, domain, proxy=self.direct, timeout=self.timeout)
|
|
159
|
+
steps.append(f"国内 DNS 解析:{', '.join(local_ips) or '无结果'}")
|
|
160
|
+
home = self.geo.domain_is_home(domain)
|
|
161
|
+
if home is None and local_ips:
|
|
162
|
+
home = self.geo.any_home(local_ips)
|
|
163
|
+
steps.append("判断:" + {True: "国内", False: "国外", None: "无法判断"}[home])
|
|
164
|
+
best_ips = local_ips
|
|
165
|
+
|
|
166
|
+
if home is True: # A
|
|
167
|
+
if self._try(domain, "direct"):
|
|
168
|
+
steps.append("国内分流(直连)访问正常")
|
|
169
|
+
if domain in learned.proxy or r.default_action == "proxy":
|
|
170
|
+
return learn("direct", "解析在国内且直连可用")
|
|
171
|
+
return Verdict(domain, "direct", steps)
|
|
172
|
+
steps.append("直连访问失败")
|
|
173
|
+
if self._try(domain, "proxy"):
|
|
174
|
+
return learn("proxy", "解析在国内但只有代理能访问,改判为国外")
|
|
175
|
+
steps.append("代理访问也失败,改用公共 DNS 解析")
|
|
176
|
+
pub_ips = probe.doh(cfg.dns.public_doh, domain, proxy=self.proxy, timeout=self.timeout)
|
|
177
|
+
steps.append(f"公共 DNS 解析:{', '.join(pub_ips) or '无结果'}")
|
|
178
|
+
if pub_ips and set(pub_ips) != set(local_ips):
|
|
179
|
+
best_ips = pub_ips
|
|
180
|
+
if self._try_ips(pub_ips, "direct"):
|
|
181
|
+
return learn("direct", "公共 DNS 的解析结果直连可用", pub_ips)
|
|
182
|
+
if self._try_ips(pub_ips, "proxy"):
|
|
183
|
+
return learn("proxy", "公共 DNS 的解析结果经代理可用", pub_ips)
|
|
184
|
+
elif home is False: # B
|
|
185
|
+
if self._try(domain, "proxy"):
|
|
186
|
+
steps.append("国外分流(代理)访问正常")
|
|
187
|
+
if domain in learned.direct or r.default_action == "direct":
|
|
188
|
+
return learn("proxy", "解析在国外且代理可用")
|
|
189
|
+
return Verdict(domain, "proxy", steps)
|
|
190
|
+
steps.append("代理访问失败,向服务器侧发起解析")
|
|
191
|
+
srv_ips = probe.doh(cfg.dns.public_doh, domain, proxy=self.proxy, timeout=self.timeout)
|
|
192
|
+
steps.append(f"服务器侧解析:{', '.join(srv_ips) or '无结果'}")
|
|
193
|
+
if srv_ips:
|
|
194
|
+
best_ips = srv_ips
|
|
195
|
+
if self._try_ips(srv_ips, "proxy"):
|
|
196
|
+
return learn("proxy", "服务器侧的解析结果经代理可用", srv_ips)
|
|
197
|
+
|
|
198
|
+
# 第 4 步:GeoIP
|
|
199
|
+
geo = self.geo.any_home(best_ips) if best_ips else None
|
|
200
|
+
steps.append("GeoIP:" + {True: "国内", False: "国外", None: "查不到"}[geo])
|
|
201
|
+
if geo is True and self._try(domain, "direct"):
|
|
202
|
+
return learn("direct", "GeoIP 判定为国内且直连可用")
|
|
203
|
+
if geo is not True and self._try(domain, "proxy"):
|
|
204
|
+
# 查不到时交给服务器解析并走代理;默认动作本来就是代理时不需要额外记一条
|
|
205
|
+
if geo is False or r.default_action == "direct":
|
|
206
|
+
return learn("proxy", "GeoIP 判定为国外且代理可用" if geo is False else "查不到归属,交给服务器解析")
|
|
207
|
+
steps.append("结论:按默认动作走代理")
|
|
208
|
+
return Verdict(domain, "proxy", steps)
|
|
209
|
+
# 特殊情况:服务器问题(DNS 连不上 / 网站本身在维护)
|
|
210
|
+
learned.unreachable[domain] = time.time() + UNREACHABLE_TTL
|
|
211
|
+
steps.append("直连和代理都无法访问:可能是网站自身维护或 DNS 故障,不修改名单")
|
|
212
|
+
return Verdict(domain, "unreachable", steps)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# ---- 模块 ----------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
class Router:
|
|
218
|
+
"""分流模块:消费 sing-box 日志里的失败记录,判断并学习。"""
|
|
219
|
+
|
|
220
|
+
name = "routing"
|
|
221
|
+
|
|
222
|
+
def __init__(self, cfg: ClientConfig, paths: Paths, classifier: Classifier, log: Logger,
|
|
223
|
+
on_change: Callable[[], None]) -> None:
|
|
224
|
+
self.cfg, self.paths, self.classifier, self.log, self.on_change = cfg, paths, classifier, log, on_change
|
|
225
|
+
self.learned = load_learned(paths)
|
|
226
|
+
self._learn = bool(cfg.routing.learn) # feed() 每行日志都读它,取成普通属性
|
|
227
|
+
self._q: queue.Queue[str] = queue.Queue(maxsize=256)
|
|
228
|
+
self._conns: OrderedDict[str, tuple[str, str]] = OrderedDict() # 连接 ID → (出站, 主机)
|
|
229
|
+
self._fails: dict[str, int] = {}
|
|
230
|
+
self._last: dict[str, float] = {}
|
|
231
|
+
self._stop = threading.Event()
|
|
232
|
+
self._thread: threading.Thread | None = None
|
|
233
|
+
self.last_result: dict | None = None
|
|
234
|
+
self.classified = 0
|
|
235
|
+
|
|
236
|
+
# sing-box 的每一行输出都会经过这里(在日志泵线程里),必须很快返回
|
|
237
|
+
def feed(self, line: str) -> None:
|
|
238
|
+
if not self._learn:
|
|
239
|
+
return
|
|
240
|
+
# 绝大多数日志行两种都不是:先用子串检查排除,再动用正则
|
|
241
|
+
is_conn = "connection to " in line
|
|
242
|
+
if not is_conn and "ERROR" not in line:
|
|
243
|
+
return
|
|
244
|
+
if "\x1b" in line:
|
|
245
|
+
line = _ANSI.sub("", line)
|
|
246
|
+
if is_conn:
|
|
247
|
+
m = _CONN.search(line)
|
|
248
|
+
if m:
|
|
249
|
+
cid, tag, host = m.group(1), m.group(2), m.group(3).lower()
|
|
250
|
+
self._conns[cid] = (tag, host)
|
|
251
|
+
if len(self._conns) > 2000:
|
|
252
|
+
self._conns.popitem(last=False)
|
|
253
|
+
return
|
|
254
|
+
m = _ERR.search(line)
|
|
255
|
+
if not m:
|
|
256
|
+
return
|
|
257
|
+
tag, host = self._conns.pop(m.group(1), (None, None))
|
|
258
|
+
if tag == "proxy-alt":
|
|
259
|
+
# Auto mode uses proxy-alt for the non-selected protocol. It is
|
|
260
|
+
# still a proxied path and must participate in post-failure learning.
|
|
261
|
+
tag = "proxy"
|
|
262
|
+
if not host or tag not in ("direct", "proxy") or _looks_like_ip(host):
|
|
263
|
+
return
|
|
264
|
+
self._fails[host] = self._fails.get(host, 0) + 1
|
|
265
|
+
if len(self._fails) > 2000:
|
|
266
|
+
self._fails.clear()
|
|
267
|
+
now = time.time()
|
|
268
|
+
if self._fails[host] >= FAILURES_BEFORE_CLASSIFY and now - self._last.get(host, 0) > RETRY_COOLDOWN:
|
|
269
|
+
if len(self._last) > 2000: # 只保留还在冷却期内的记录,长期运行时不会无限增长
|
|
270
|
+
self._last = {h: t for h, t in self._last.items() if now - t <= RETRY_COOLDOWN}
|
|
271
|
+
self._last[host] = now
|
|
272
|
+
self._fails.pop(host, None)
|
|
273
|
+
try:
|
|
274
|
+
self._q.put_nowait(host)
|
|
275
|
+
except queue.Full:
|
|
276
|
+
pass
|
|
277
|
+
|
|
278
|
+
def check(self, domain: str) -> Verdict:
|
|
279
|
+
"""立即判断一个域名(命令行和内控制都可以调用)。"""
|
|
280
|
+
v = self.classifier.classify(domain, self.learned)
|
|
281
|
+
self.classified += 1
|
|
282
|
+
self.last_result = v.as_dict()
|
|
283
|
+
save_learned(self.paths, self.learned)
|
|
284
|
+
self.log.info("routing.classified", **v.as_dict())
|
|
285
|
+
return v
|
|
286
|
+
|
|
287
|
+
def _run(self) -> None:
|
|
288
|
+
next_prune = time.monotonic() + 300
|
|
289
|
+
while not self._stop.is_set():
|
|
290
|
+
try:
|
|
291
|
+
domain = self._q.get(timeout=1)
|
|
292
|
+
except queue.Empty:
|
|
293
|
+
domain = None
|
|
294
|
+
changed = False
|
|
295
|
+
if domain:
|
|
296
|
+
try:
|
|
297
|
+
changed = self.check(domain).changed
|
|
298
|
+
except Exception as e: # noqa: BLE001 单个域名判断出错不能让模块退出
|
|
299
|
+
self.log.error("routing.classify_failed", domain=domain, error=str(e))
|
|
300
|
+
if time.monotonic() >= next_prune:
|
|
301
|
+
next_prune = time.monotonic() + 300
|
|
302
|
+
if self.learned.prune():
|
|
303
|
+
save_learned(self.paths, self.learned)
|
|
304
|
+
changed = True
|
|
305
|
+
if changed:
|
|
306
|
+
self.on_change()
|
|
307
|
+
|
|
308
|
+
def start(self) -> None:
|
|
309
|
+
if self.alive():
|
|
310
|
+
return
|
|
311
|
+
self._stop.clear()
|
|
312
|
+
self.learned = load_learned(self.paths)
|
|
313
|
+
self._thread = threading.Thread(target=self._run, name="norain-routing", daemon=True)
|
|
314
|
+
self._thread.start()
|
|
315
|
+
|
|
316
|
+
def stop(self) -> None:
|
|
317
|
+
self._stop.set()
|
|
318
|
+
if self._thread:
|
|
319
|
+
self._thread.join(self.cfg.monitor.timeout_seconds * 8 + 5)
|
|
320
|
+
|
|
321
|
+
def alive(self) -> bool:
|
|
322
|
+
return bool(self._thread and self._thread.is_alive())
|
|
323
|
+
|
|
324
|
+
def status(self) -> dict:
|
|
325
|
+
return {"state": "running" if self.alive() else "stopped", "learn": self.cfg.routing.learn,
|
|
326
|
+
"learned_proxy": len(self.learned.proxy), "learned_direct": len(self.learned.direct),
|
|
327
|
+
"hosts_overrides": len(self.learned.hosts), "queued": self._q.qsize(),
|
|
328
|
+
"classified": self.classified, "last_result": self.last_result}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def _looks_like_ip(host: str) -> bool:
|
|
332
|
+
return ":" in host or host.replace(".", "").isdigit()
|
norain/rules.py
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"""规则集供应(docs/decisions/0008):sing-box 只使用本地 .srs 文件,下载、校验、替换和回滚
|
|
2
|
+
都由 Norain 完成。GeoIP 判断也用规则集实现(sing-box 1.12 起移除了内置 GeoIP 数据库)。
|
|
3
|
+
|
|
4
|
+
来源是 SagerNet 官方的 sing-geosite / sing-geoip 仓库的 rule-set 分支。
|
|
5
|
+
`<配置目录>/rules.lock.toml` 里写了某个规则集的 sha256 时强制比对;没有锁定值时只记录本次的
|
|
6
|
+
哈希(首次信任),并在报告里标注「未锁定」。
|
|
7
|
+
|
|
8
|
+
下载地址、.srs 的文件头和 `sing-box rule-set match` 的输出格式均未用真实环境验证。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import hashlib
|
|
14
|
+
import os
|
|
15
|
+
import re
|
|
16
|
+
import shutil
|
|
17
|
+
import time
|
|
18
|
+
import tomllib
|
|
19
|
+
import urllib.request
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
from norain_core import osutil
|
|
23
|
+
from norain_core.state import read_json, write_json
|
|
24
|
+
|
|
25
|
+
from .model import KNOWN_RULE_SETS, ClientConfig
|
|
26
|
+
from .paths import Paths
|
|
27
|
+
|
|
28
|
+
SOURCES = {
|
|
29
|
+
"geosite-cn": "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs",
|
|
30
|
+
"geoip-cn": "https://raw.githubusercontent.com/SagerNet/sing-geoip/rule-set/geoip-cn.srs",
|
|
31
|
+
}
|
|
32
|
+
_MAGIC = b"SRS"
|
|
33
|
+
MAX_BYTES = 64 * 1024 * 1024
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def path_of(paths: Paths, name: str) -> Path:
|
|
37
|
+
return paths.rules_dir / f"{name}.srs"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _index(paths: Paths) -> dict:
|
|
41
|
+
return read_json(paths.rules_dir / "index.json") or {}
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _locked(paths: Paths) -> dict[str, str]:
|
|
45
|
+
try:
|
|
46
|
+
with (paths.home / "rules.lock.toml").open("rb") as f:
|
|
47
|
+
data = tomllib.load(f)
|
|
48
|
+
except (OSError, tomllib.TOMLDecodeError):
|
|
49
|
+
return {}
|
|
50
|
+
return {k: str(v.get("sha256", "")).lower() for k, v in data.items() if isinstance(v, dict)}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def status(cfg: ClientConfig, paths: Paths) -> list[dict]:
|
|
54
|
+
idx = _index(paths)
|
|
55
|
+
out = []
|
|
56
|
+
for name in cfg.routing.rule_set:
|
|
57
|
+
if name not in KNOWN_RULE_SETS:
|
|
58
|
+
out.append({"name": Path(name).stem, "source": "local-file", "available": Path(name).is_file()})
|
|
59
|
+
continue
|
|
60
|
+
f = path_of(paths, name)
|
|
61
|
+
out.append({"name": name, "available": f.is_file(), **(idx.get(name) or {})})
|
|
62
|
+
return out
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _download(url: str, dest: Path, proxy: tuple[str, int] | None, timeout: float) -> None:
|
|
66
|
+
handlers = []
|
|
67
|
+
if proxy:
|
|
68
|
+
addr = f"http://{proxy[0]}:{proxy[1]}"
|
|
69
|
+
handlers.append(urllib.request.ProxyHandler({"http": addr, "https": addr}))
|
|
70
|
+
opener = urllib.request.build_opener(*handlers)
|
|
71
|
+
total = 0
|
|
72
|
+
with opener.open(urllib.request.Request(url, headers={"User-Agent": "norain"}), timeout=timeout) as r, \
|
|
73
|
+
dest.open("wb") as f:
|
|
74
|
+
while chunk := r.read(1 << 16):
|
|
75
|
+
total += len(chunk)
|
|
76
|
+
if total > MAX_BYTES:
|
|
77
|
+
raise OSError("文件超过大小上限")
|
|
78
|
+
f.write(chunk)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def update(cfg: ClientConfig, paths: Paths, *, proxy: tuple[str, int] | None = None,
|
|
82
|
+
timeout: float = 60.0, downloader=_download) -> dict:
|
|
83
|
+
"""下载到临时文件 → 校验 → 保留上一版 → 原子替换。任何一步失败都保留当前版本。
|
|
84
|
+
|
|
85
|
+
proxy:经哪个本地入口下载(代理在运行时用 mixed-in,这样 GitHub 的地址走代理)。
|
|
86
|
+
"""
|
|
87
|
+
paths.rules_dir.mkdir(parents=True, exist_ok=True)
|
|
88
|
+
idx, locked = _index(paths), _locked(paths)
|
|
89
|
+
report: dict = {"updated": [], "unchanged": [], "failed": {}, "unpinned": []}
|
|
90
|
+
for name in cfg.routing.rule_set:
|
|
91
|
+
if name not in KNOWN_RULE_SETS:
|
|
92
|
+
continue
|
|
93
|
+
dest = path_of(paths, name)
|
|
94
|
+
tmp = dest.with_suffix(".srs.part")
|
|
95
|
+
try:
|
|
96
|
+
downloader(SOURCES[name], tmp, proxy, timeout)
|
|
97
|
+
data = tmp.read_bytes()
|
|
98
|
+
if len(data) < 16 or not data.startswith(_MAGIC):
|
|
99
|
+
raise OSError("不是 sing-box 二进制规则集(文件头不符)")
|
|
100
|
+
digest = hashlib.sha256(data).hexdigest()
|
|
101
|
+
want = locked.get(name)
|
|
102
|
+
if want and digest != want:
|
|
103
|
+
raise OSError(f"SHA-256 与 rules.lock.toml 不符(期望 {want},实际 {digest})")
|
|
104
|
+
if not want:
|
|
105
|
+
report["unpinned"].append(name)
|
|
106
|
+
if dest.is_file() and hashlib.sha256(dest.read_bytes()).hexdigest() == digest:
|
|
107
|
+
report["unchanged"].append(name)
|
|
108
|
+
tmp.unlink(missing_ok=True)
|
|
109
|
+
else:
|
|
110
|
+
if dest.is_file():
|
|
111
|
+
shutil.copy2(dest, dest.with_suffix(".srs.prev"))
|
|
112
|
+
os.replace(tmp, dest)
|
|
113
|
+
report["updated"].append(name)
|
|
114
|
+
idx[name] = {"sha256": digest, "url": SOURCES[name], "pinned": bool(want),
|
|
115
|
+
"updated_at": time.strftime("%Y-%m-%dT%H:%M:%S%z")}
|
|
116
|
+
except (OSError, ValueError) as e:
|
|
117
|
+
tmp.unlink(missing_ok=True)
|
|
118
|
+
report["failed"][name] = str(e)[:200]
|
|
119
|
+
idx["_checked_at"] = time.time()
|
|
120
|
+
write_json(paths.rules_dir / "index.json", idx)
|
|
121
|
+
return report
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def rollback(paths: Paths, name: str) -> bool:
|
|
125
|
+
cur, prev = path_of(paths, name), path_of(paths, name).with_suffix(".srs.prev")
|
|
126
|
+
if not prev.is_file():
|
|
127
|
+
return False
|
|
128
|
+
bad = cur.with_suffix(".srs.bad")
|
|
129
|
+
if cur.is_file():
|
|
130
|
+
os.replace(cur, bad)
|
|
131
|
+
os.replace(prev, cur)
|
|
132
|
+
idx = _index(paths)
|
|
133
|
+
idx[name] = {"sha256": hashlib.sha256(cur.read_bytes()).hexdigest(), "rolled_back": True,
|
|
134
|
+
"updated_at": time.strftime("%Y-%m-%dT%H:%M:%S%z")}
|
|
135
|
+
write_json(paths.rules_dir / "index.json", idx)
|
|
136
|
+
return True
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def due(cfg: ClientConfig, paths: Paths) -> bool:
|
|
140
|
+
"""是否到了自更新的时间;规则集文件缺失时立即更新。"""
|
|
141
|
+
names = [n for n in cfg.routing.rule_set if n in KNOWN_RULE_SETS]
|
|
142
|
+
if not names:
|
|
143
|
+
return False
|
|
144
|
+
missing = any(not path_of(paths, n).is_file() for n in names)
|
|
145
|
+
hours = cfg.routing.update_interval_hours
|
|
146
|
+
if not missing and not hours:
|
|
147
|
+
return False
|
|
148
|
+
checked = _index(paths).get("_checked_at", 0)
|
|
149
|
+
if not isinstance(checked, (int, float)):
|
|
150
|
+
checked = 0 # 索引文件被改坏时按「从未检查过」处理,而不是抛 TypeError
|
|
151
|
+
# 文件缺失(下载失败)时每 10 分钟重试一次
|
|
152
|
+
return time.time() - checked > (600 if missing else hours * 3600)
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
class Geo:
|
|
156
|
+
"""用规则集回答「这个 IP / 域名是不是国内的」。规则集不可用时返回 None(不知道)。"""
|
|
157
|
+
|
|
158
|
+
def __init__(self, binary: Path, paths: Paths) -> None:
|
|
159
|
+
self.binary, self.paths = binary, paths
|
|
160
|
+
self._cache: dict[tuple[str, str, int], bool | None] = {}
|
|
161
|
+
self._files: dict[str, Path] = {}
|
|
162
|
+
|
|
163
|
+
def _match(self, name: str, value: str) -> bool | None:
|
|
164
|
+
f = self._files.get(name) or self._files.setdefault(name, path_of(self.paths, name))
|
|
165
|
+
try:
|
|
166
|
+
mtime = os.stat(f).st_mtime_ns
|
|
167
|
+
except OSError:
|
|
168
|
+
return None
|
|
169
|
+
# 缓存键带规则集文件的修改时间:规则集更新或回滚之后,旧结论自动失效
|
|
170
|
+
key = (name, value, mtime)
|
|
171
|
+
if key in self._cache:
|
|
172
|
+
return self._cache[key]
|
|
173
|
+
# 规则集是二进制的 .srs:不加 -f binary 时真实 sing-box 会把它当 JSON 解析并 FATAL(exit 1),
|
|
174
|
+
# 「是否国内」就永远拿不到结论(2026-11-15 用 1.14.4 实测)。命中时输出
|
|
175
|
+
# "match rules.[0]: ...",未命中时输出为空且 exit 0——空输出必须判为不命中。
|
|
176
|
+
fmt = "binary" if f.suffix.lower() == ".srs" else "source"
|
|
177
|
+
proc = osutil.run([str(self.binary), "rule-set", "match", "-f", fmt, str(f), value], timeout=10)
|
|
178
|
+
if proc.returncode != 0:
|
|
179
|
+
result = None
|
|
180
|
+
else:
|
|
181
|
+
output = (proc.stdout + proc.stderr).lower()
|
|
182
|
+
# Do not treat "no match" as a positive result merely because it
|
|
183
|
+
# contains the word "match". Keep unknown formats conservative.
|
|
184
|
+
negative = re.search(r"\b(?:no\s+match|not\s+matched?|unmatched|false)\b|"
|
|
185
|
+
r"\bmatch\s*[:=]\s*(?:no|false|0)\b", output)
|
|
186
|
+
positive = re.search(r"\b(?:matched?|match|true)\b|"
|
|
187
|
+
r"\bmatch\s*[:=]\s*(?:yes|true|1)\b", output)
|
|
188
|
+
if negative:
|
|
189
|
+
result = False
|
|
190
|
+
elif positive:
|
|
191
|
+
result = True
|
|
192
|
+
elif not output.strip():
|
|
193
|
+
result = False # exit 0 且没有任何输出 = 不命中
|
|
194
|
+
else:
|
|
195
|
+
result = None # 输出格式不认识:保守处理
|
|
196
|
+
if len(self._cache) > 4096:
|
|
197
|
+
self._cache.clear()
|
|
198
|
+
self._cache[key] = result
|
|
199
|
+
return result
|
|
200
|
+
|
|
201
|
+
def ip_is_home(self, ip: str) -> bool | None:
|
|
202
|
+
return self._match("geoip-cn", ip)
|
|
203
|
+
|
|
204
|
+
def domain_is_home(self, domain: str) -> bool | None:
|
|
205
|
+
return self._match("geosite-cn", domain)
|
|
206
|
+
|
|
207
|
+
def any_home(self, ips: list[str]) -> bool | None:
|
|
208
|
+
"""有一个确定是国内就算国内;全部确定不是才算国外;否则不知道。"""
|
|
209
|
+
results = [self.ip_is_home(ip) for ip in ips]
|
|
210
|
+
if any(r is True for r in results):
|
|
211
|
+
return True
|
|
212
|
+
if results and all(r is False for r in results):
|
|
213
|
+
return False
|
|
214
|
+
return None
|
norain/runner.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""客户端守护的入口:`norain run` 的预检,以及在后台启动 / 停止它的后端。
|
|
2
|
+
|
|
3
|
+
`norain run` 运行的是内控制模块(control.Controller),代理、分流、监控、自维持都是它里面的组件。
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
|
|
10
|
+
from norain_core import singbox
|
|
11
|
+
from norain_core.apply import ApplyResult
|
|
12
|
+
from norain_core.daemonctl import DetachedBackend
|
|
13
|
+
from norain_core.secrets import Secret
|
|
14
|
+
from norain_core.supervisor import Listener
|
|
15
|
+
|
|
16
|
+
from . import generate
|
|
17
|
+
from .model import ClientConfig, load_config
|
|
18
|
+
from .paths import Paths
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class Prepared:
|
|
23
|
+
cfg: ClientConfig
|
|
24
|
+
paths: Paths
|
|
25
|
+
secret: Secret
|
|
26
|
+
binary: singbox.BinaryInfo
|
|
27
|
+
applied: ApplyResult | None
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def listeners(cfg: ClientConfig) -> list[Listener]:
|
|
31
|
+
return generate.listeners(cfg)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def locate_singbox(paths: Paths) -> singbox.BinaryInfo:
|
|
35
|
+
path = singbox.locate(paths.singbox, defaults=(str(paths.bundled_singbox),),
|
|
36
|
+
env_name="NORAIN_SINGBOX")
|
|
37
|
+
return singbox.inspect(path)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def prepare(paths: Paths, *, apply: bool = True) -> Prepared:
|
|
41
|
+
"""配置、秘密、二进制的全部预检。apply=True 时生成并替换运行配置(先过 sing-box check)。"""
|
|
42
|
+
cfg = load_config(paths.config)
|
|
43
|
+
paths = paths.with_log_dir(cfg.logging.directory)
|
|
44
|
+
secret = generate.resolve_secret(cfg)
|
|
45
|
+
info = locate_singbox(paths)
|
|
46
|
+
singbox.require(info, need_tags={"with_quic"} if "hysteria2" in cfg.protocols else set())
|
|
47
|
+
applied = generate.apply(cfg, secret, paths, info.path) if apply else None
|
|
48
|
+
return Prepared(cfg, paths, secret, info, applied)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def backend(paths: Paths) -> DetachedBackend:
|
|
52
|
+
return DetachedBackend(module="norain", runner_file=paths.runner,
|
|
53
|
+
last_error_file=paths.last_error, stop_file=paths.stop_file,
|
|
54
|
+
log_dir=paths.log_dir)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def run_foreground(paths: Paths, *, autostart: bool = False) -> int:
|
|
58
|
+
from .control import Controller
|
|
59
|
+
return Controller(paths, autostart=autostart).run()
|