feat: 落实决策216/219/227取价锚点与公式口径,增加商业版权益和源码完整性校验

This commit is contained in:
lianjianwei2024
2026-10-27 12:16:47 +08:00
parent 5d9eaf774b
commit 380a39f8db
36 changed files with 18821 additions and 879 deletions

View File

@@ -12,6 +12,7 @@ pinned_keys.json + 已存授权状态;配置了凭据且信任根就绪则起
本地开发豁免:data/license/config.json 写 "enforce": false(交付/发布环境
不带该文件或 enforce=true,代码默认 true)。
"""
import hashlib
import json
import os
import threading
@@ -32,6 +33,34 @@ _cfg = {"enforce": True, "api_base": DEFAULT_API_BASE, "selfheal_seconds": 300}
_hb_thread = None
_hb_stop = None
_last_selfheal = 0.0 # monotonic:门禁自愈触发节流
_INTEGRITY_FAIL = "" # N1 源码保护(2026-10-27):非空 = 交付包文件被改,全线拦截
def _check_build_integrity():
"""N1 源码保护:交付包启动时校验 .py 哈希清单(build 期烧入 BUILD.INTEGRITY,
覆盖 server/core 全部 .py)。被改 = fail closed 全线拦截(含授权口——防的是
「改掉本文件/门禁调用后再激活」的绕过路径)。开发版(BUILD=None)不校验。
INTEGRITY 键相对 app 根(server/...、core/...、shell.py)。"""
global _INTEGRITY_FAIL
if BUILD is None:
return
manifest = getattr(BUILD, "INTEGRITY", None)
if not isinstance(manifest, dict) or not manifest:
return
app_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
for rel, want in sorted(manifest.items()):
p = os.path.join(app_root, rel.replace("/", os.sep))
try:
with open(p, "rb") as f:
got = hashlib.sha256(f.read()).hexdigest()
except OSError:
_INTEGRITY_FAIL = rel + "(缺失)"
print("⚠ 完整性校验失败:%s" % _INTEGRITY_FAIL)
return
if got != str(want).lower():
_INTEGRITY_FAIL = rel
print("⚠ 完整性校验失败:%s 哈希不符(文件被改动?)" % rel)
return
# ---------------------------------------------------------------- 初始化
@@ -73,6 +102,7 @@ def init(data_dir):
expected_product = None
selfheal = 300
if BUILD is not None:
_check_build_integrity() # N1:烧入清单校验,被改 → 全线拦截
_cfg = {"enforce": bool(BUILD.ENFORCE),
"api_base": str(BUILD.API_BASE).rstrip("/"),
"selfheal_seconds": getattr(BUILD, "SELFHEAL_SECONDS", 300)}
@@ -167,6 +197,12 @@ def heartbeat_running():
def intercept(method, path, body=None):
"""None = 放行;否则返回 {"kind":"json"|"html","code":int,...} 由 Handler 发送。"""
if _INTEGRITY_FAIL: # N1:文件被改 = 无任何放行(含授权口)
if method == "GET" and not path.startswith("/api/"):
return {"kind": "html", "code": 200, "text": _integrity_page()}
return {"kind": "json", "code": 403,
"obj": {"error": {"code": "INTEGRITY_FAILED",
"message": "软件文件完整性校验失败,请重新安装或联系软件提供方"}}}
if path.startswith("/api/license/"):
return _license_api(method, path, body)
if not _cfg.get("enforce", True):
@@ -194,6 +230,23 @@ def can_run_now():
return c.can_run_safe()
def _integrity_page():
"""N1 完整性拦截页(自包含):文件被改时的唯一可见页面。"""
return """<!DOCTYPE html>
<html lang="zh"><head><meta charset="utf-8"><title>软件文件校验失败</title>
<style>body{font-family:"Microsoft YaHei",sans-serif;background:#f0f2f5;display:flex;
align-items:center;justify-content:center;min-height:100vh;margin:0}
.c{background:#fff;border-radius:10px;box-shadow:0 2px 12px rgba(0,0,0,.08);
max-width:520px;padding:32px 36px}h1{font-size:19px;color:#b02a2a;margin:0 0 12px}
p{color:#33475b;line-height:1.8;font-size:14px;margin:8px 0}
.k{color:#8a97a5;font-size:12px;margin-top:16px}</style></head><body><div class="c">
<h1>⚠ 软件文件完整性校验失败</h1>
<p>检测到程序文件被改动,为保障授权与数据安全,软件已停止服务。</p>
<p>请重新安装官方软件包,或联系软件提供方处理。您的数据不受影响。</p>
<div class="k">涉及文件见服务日志「完整性校验失败」行。</div>
</div></body></html>"""
def _blocked(reason):
return {"kind": "json", "code": 403,
"obj": {"error": {"code": "LICENSE_REQUIRED", "message": reason}}}
@@ -406,7 +459,8 @@ button:disabled{opacity:.5;cursor:default}
<div class="st" id="impPv" style="display:none;margin-top:12px;margin-bottom:0"></div>
<div class="msg" id="msg"></div>
<div class="tip">本页面由本机服务(127.0.0.1)提供;激活凭据只保存在本机,不会上传到别处。
激活成功后页面会自动进入系统。若长时间显示网络错误,请检查本机能否访问授权服务器。</div>
激活成功后页面会自动进入系统。若长时间显示网络错误,请检查本机能否访问授权服务器。
授权到期或被冻结时会停在本页(到期即锁):联系软件提供方续费/解冻并重新激活即可恢复使用,本机数据不受任何影响。</div>
</div></div>
<script>
var TIMER=null;