全量细审修复:部署漏配防清空、scrypt 并发闸、发布冲突可解、价格文件只在改了才写、排位扩容

Browse Source
- cms.py:没设 YS_PORTAL_DIR / YS_ORDER_DIR 或缺 content/site.json 时拒绝工作(不建空的第一版,免得一发布清空网站);
  同时最多 2 个 scrypt(内存上限 256M);登录冷却记录定期清理;价格 / 付款只在这次发布真改了才写回
  (不覆盖 admin.py price 的改动)
- 后台页面:发布冲突二选一(丢掉我的草稿 / 确认后覆盖发布);草稿没存上、还有图在上传时不让发布
- layout.js:中圈排满后分档到外圈空槽之间找位置,40 条世界线也不重叠;去掉慢的 Math.hypot
- config.js:后台把世界线删光也照实显示;门户删掉旧启动仪式留下的死数据;版本号 v=3
- 测试:内容后台 78 / 回归 27 / 数据库 38 全过;端到端浏览器 36 全过
- 文档同步(交接文档、内容后台设计「实现对照」、后端交接说明、交付清单)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
yws
2026-10-05 21:55:13 +00:00
co-authored by Claude Opus 5.5
parent 12e66badc6
commit 74bf490baa
12 changed files with 146 additions and 62 deletions
+1
View File
@@ -508,6 +508,7 @@ def cmd_price(argv):
raise SystemExit("用法:price / price per N / price set N 价格 [链接] / price rm N") raise SystemExit("用法:price / price per N / price set N 价格 [链接] / price rm N")
d["tiers"] = tiers d["tiers"] = tiers
S.jsave(S.PRICES_FILE, d) S.jsave(S.PRICES_FILE, d)
print("⚠️ 2026-10-05 起价格建议在内容后台改(「价格与付款」页):后台下次改价格并发布时,会以后台里的为准覆盖这里的改动。")
try: try:
os.chmod(S.PRICES_FILE, 0o644) os.chmod(S.PRICES_FILE, 0o644)
except OSError: except OSError:
+45 -18
View File
@@ -608,6 +608,8 @@ class CMS(object):
self.fails = {} # 登录失败:key -> [时间戳] self.fails = {} # 登录失败:key -> [时间戳]
self.fail_lock = threading.Lock() self.fail_lock = threading.Lock()
self.uploads = {} # 上传限频:uid -> [时间戳] self.uploads = {} # 上传限频:uid -> [时间戳]
# 算一次 scrypt 要 16MB 内存:这台机器服务进程上限 256M,同时最多算 2 个,免得一波并发登录把进程撑爆
self.hash_sem = threading.BoundedSemaphore(2)
c = store.conn() c = store.conn()
c.executescript(_SCHEMA) c.executescript(_SCHEMA)
with store.tx() as c: # 一个账号都没有 → 建初始账号(BEGIN IMMEDIATE:两个进程同时首启也只建一个) with store.tx() as c: # 一个账号都没有 → 建初始账号(BEGIN IMMEDIATE:两个进程同时首启也只建一个)
@@ -708,13 +710,27 @@ class CMS(object):
return int(worst + 0.999) if worst > 0 else 0 return int(worst + 0.999) if worst > 0 else 0
def _fail(self, keys): def _fail(self, keys):
now = time.time()
with self.fail_lock: with self.fail_lock:
for k in keys: for k in keys:
self.fails.setdefault(k, []).append(time.time()) self.fails.setdefault(k, []).append(now)
if len(self.fails) > 2000: # 被大量 IP 扫的时候:把过了窗口期的记录清掉,内存不跟着涨
for k in [k for k, v in self.fails.items() if not v or now - v[-1] > FAIL_WIN]:
self.fails.pop(k, None)
# ---------------------------------------------------------------- 内容 # ---------------------------------------------------------------- 内容
def dirs_ok(self):
"""门户 / 下单页目录配好了没有。没配好就**什么都不写**:不然会在当前目录建个叫 <PORTAL_DIR> 的怪目录,
第一版内容还会是空骨架 —— 店主一发布,网站内容就被清空。"""
for name, d in (("YS_PORTAL_DIR", PORTAL_DIR), ("YS_ORDER_DIR", ORDER_DIR)):
if not d or d.startswith("<") or not os.path.isdir(d):
raise Bad("服务器没配好 %s(现在是「%s」)—— 内容后台先不能用,请照交接文档 7.1b 配好再重启" % (name, d))
def _seed(self): def _seed(self):
"""第一次用:把网站现在的内容文件当成第 1 版(文件没有就用空骨架)。""" """第一次用:把网站现在的内容文件当成第 1 版。门户的 content/site.json 必须在(防止从空骨架开始、一发布就把网站清空)。"""
self.dirs_ok()
if not os.path.isfile(os.path.join(PORTAL_DIR, "content", "site.json")):
raise Bad("门户目录里没有 content/site.json —— 先把仓库里的 portal/content/ 放上去再用内容后台")
def jl(p): def jl(p):
try: try:
with open(p, encoding="utf-8") as f: with open(p, encoding="utf-8") as f:
@@ -734,8 +750,7 @@ class CMS(object):
try: try:
doc = normalize({"site": site, "order": order}, self.tpl_keys) doc = normalize({"site": site, "order": order}, self.tpl_keys)
except Bad as e: except Bad as e:
self.log("内容后台:现有内容文件不合格(%s),从空骨架开始" % e) raise Bad("网站现有的内容文件不合格(%s)—— 修好再用内容后台" % e)
doc = normalize({}, self.tpl_keys)
with self.store.tx() as c: with self.store.tx() as c:
if not c.execute("SELECT 1 FROM cms_versions LIMIT 1").fetchone(): if not c.execute("SELECT 1 FROM cms_versions LIMIT 1").fetchone():
c.execute("INSERT INTO cms_versions (no, data, note, by, at, chg) VALUES (1, ?, '从网站现有内容导入', 'system', ?, '全部内容')", c.execute("INSERT INTO cms_versions (no, data, note, by, at, chg) VALUES (1, ?, '从网站现有内容导入', 'system', ?, '全部内容')",
@@ -752,8 +767,14 @@ class CMS(object):
r = self.store.conn().execute("SELECT data, base, updated, by FROM cms_draft WHERE k='doc'").fetchone() r = self.store.conn().execute("SELECT data, base, updated, by FROM cms_draft WHERE k='doc'").fetchone()
return (json.loads(r[0]), r[1], r[2], r[3]) if r else (None, None, None, None) return (json.loads(r[0]), r[1], r[2], r[3]) if r else (None, None, None, None)
def write_files(self, doc): def write_files(self, doc, prev=None):
"""prev = 网站上现在那一版。价格档位 / 付款方式**只有这次真的改了**才写回 prices.json / payinfo.json ——
不然有人用 admin.py price 在命令行改过价格,店主随便发布一张画就会把它悄悄改回去。"""
self.dirs_ok()
site, order = doc["site"], doc["order"] site, order = doc["site"], doc["order"]
po = (prev or {}).get("order") or {}
write_prices = prev is None or po.get("prices") != order["prices"]
write_pay = prev is None or po.get("pay") != order["pay"]
stamp = datetime.now(CST).strftime("%Y-%m-%d %H:%M") stamp = datetime.now(CST).strftime("%Y-%m-%d %H:%M")
head = "/* 由内容后台发布生成(%s)。别手改:下次发布会被覆盖。改内容请用内容后台。 */\n" % stamp head = "/* 由内容后台发布生成(%s)。别手改:下次发布会被覆盖。改内容请用内容后台。 */\n" % stamp
_atomic_write(os.path.join(PORTAL_DIR, "content", "site.json"), json.dumps(site, ensure_ascii=False, indent=1)) _atomic_write(os.path.join(PORTAL_DIR, "content", "site.json"), json.dumps(site, ensure_ascii=False, indent=1))
@@ -768,13 +789,15 @@ class CMS(object):
return v if isinstance(v, dict) else {} return v if isinstance(v, dict) else {}
except (OSError, ValueError): except (OSError, ValueError):
return {} return {}
pr = jl(self.prices_file) if write_prices:
pr["per"] = order["prices"]["per"] pr = jl(self.prices_file)
pr["tiers"] = {str(t["n"]): {"price": t["price"], "payplatform": t["url"]} for t in order["prices"]["tiers"]} pr["per"] = order["prices"]["per"]
_atomic_write(self.prices_file, json.dumps(pr, ensure_ascii=False, indent=1), 0o640) pr["tiers"] = {str(t["n"]): {"price": t["price"], "payplatform": t["url"]} for t in order["prices"]["tiers"]}
pay = jl(self.payinfo_file) _atomic_write(self.prices_file, json.dumps(pr, ensure_ascii=False, indent=1), 0o640)
pay.update(order["pay"]) if write_pay:
_atomic_write(self.payinfo_file, json.dumps(pay, ensure_ascii=False, indent=1), 0o640) pay = jl(self.payinfo_file)
pay.update(order["pay"])
_atomic_write(self.payinfo_file, json.dumps(pay, ensure_ascii=False, indent=1), 0o640)
def publish(self, who, ip, doc, note, base): def publish(self, who, ip, doc, note, base):
with self.lock: with self.lock:
@@ -784,7 +807,7 @@ class CMS(object):
rows = diff_summary(cur, doc) rows = diff_summary(cur, doc)
if not rows: if not rows:
raise Bad("草稿和网站上的一模一样,没有要发布的") raise Bad("草稿和网站上的一模一样,没有要发布的")
self.write_files(doc) self.write_files(doc, cur)
no = cur_no + 1 no = cur_no + 1
with self.store.tx() as c: with self.store.tx() as c:
c.execute("INSERT INTO cms_versions (no, data, note, by, at, chg) VALUES (?,?,?,?,?,?)", c.execute("INSERT INTO cms_versions (no, data, note, by, at, chg) VALUES (?,?,?,?,?,?)",
@@ -796,6 +819,7 @@ class CMS(object):
# ---------------------------------------------------------------- 上传 # ---------------------------------------------------------------- 上传
def save_upload(self, uid, name, data_url): def save_upload(self, uid, name, data_url):
self.dirs_ok()
now = time.time() now = time.time()
arr = [t for t in self.uploads.get(uid, []) if now - t < 3600] arr = [t for t in self.uploads.get(uid, []) if now - t < 3600]
if len(arr) >= 120: if len(arr) >= 120:
@@ -966,9 +990,10 @@ class CMS(object):
if wait: if wait:
return self._reply(h, 429, {"ok": False, "error": "错的次数太多了,%d 秒后再试" % wait, "wait": wait}) return self._reply(h, 429, {"ok": False, "error": "错的次数太多了,%d 秒后再试" % wait, "wait": wait})
r = self.user_row(who) if who else None r = self.user_row(who) if who else None
ok = bool(r) and not r[5] and check_pw(pw, r[3]) with self.hash_sem:
if not r: ok = bool(r) and not r[5] and check_pw(pw, r[3])
check_pw(pw, "scrypt$16384$8$1$AAAAAAAAAAAAAAAAAAAAAA==$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=") # 账号不存在也算一遍,别让时间差暴露账号在不在 if not r:
check_pw(pw, "scrypt$16384$8$1$AAAAAAAAAAAAAAAAAAAAAA==$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=") # 账号不存在也算一遍,别让时间差暴露账号在不在
if not ok: if not ok:
self._fail(keys) self._fail(keys)
self.audit(who or "?", ip, "登录失败") self.audit(who or "?", ip, "登录失败")
@@ -1000,8 +1025,10 @@ class CMS(object):
def _ep_post_password(self, h, s, q, b): def _ep_post_password(self, h, s, q, b):
r = self.store.conn().execute("SELECT pw FROM cms_users WHERE id=?", (s["uid"],)).fetchone() r = self.store.conn().execute("SELECT pw FROM cms_users WHERE id=?", (s["uid"],)).fetchone()
old, new = str(b.get("old") or ""), str(b.get("new") or "") old, new = str(b.get("old") or "")[:200], str(b.get("new") or "")[:200]
if not check_pw(old, r[0]): with self.hash_sem:
right = check_pw(old, r[0])
if not right:
return self._reply(h, 400, {"ok": False, "error": "现在的密码不对"}) return self._reply(h, 400, {"ok": False, "error": "现在的密码不对"})
p = pw_problem(new, old) p = pw_problem(new, old)
if p: if p:
@@ -223,6 +223,9 @@ check("付款方式写回 payinfo.json", pay.get("note") == "付款时留言写
check("下单页的价格接口立刻反映新档位", S.price_for(3) == 15) check("下单页的价格接口立刻反映新档位", S.price_for(3) == 15)
lit = [x for x in json.loads(site_js.split("window.JZ_CONTENT = ", 1)[1].rsplit(";", 1)[0])["universe"]["UNIVERSES"] if not x.get("empty")] lit = [x for x in json.loads(site_js.split("window.JZ_CONTENT = ", 1)[1].rsplit(";", 1)[0])["universe"]["UNIVERSES"] if not x.get("empty")]
check("星系图数据:新世界线没写位置(交给 layout.js 自动排)", any(x["no"] == "0005" for x in lit)) check("星系图数据:新世界线没写位置(交给 layout.js 自动排)", any(x["no"] == "0005" for x in lit))
# 命令行改了价格之后,后台发布别的内容不能把它改回去
pr = json.load(open(S.PRICES_FILE, encoding="utf-8")); pr["tiers"]["5"] = {"price": 22, "payplatform": ""}
S.jsave(S.PRICES_FILE, pr)
b = C(ip="203.0.113.40"); b.login("admin", NEWPW) b = C(ip="203.0.113.40"); b.login("admin", NEWPW)
st, j = b.req("content") st, j = b.req("content")
d2 = j["draft"]; d2["site"]["hero"]["cta1"] = "看看画" d2 = j["draft"]; d2["site"]["hero"]["cta1"] = "看看画"
@@ -231,6 +234,7 @@ st, j = b.req("publish", {"base": 1})
check("别人已经发布过(底稿是旧版)→ 409 冲突,不静默覆盖", st == 409 and j.get("conflict"), j) check("别人已经发布过(底稿是旧版)→ 409 冲突,不静默覆盖", st == 409 and j.get("conflict"), j)
st, j = b.req("publish", {"base": 2}) st, j = b.req("publish", {"base": 2})
check("刷新到新底稿再发 → v3", st == 200 and j.get("version") == 3, j) check("刷新到新底稿再发 → v3", st == 200 and j.get("version") == 3, j)
check("后台发布没动价格时,不覆盖命令行改过的 prices.json", json.load(open(S.PRICES_FILE, encoding="utf-8")).get("tiers", {}).get("5", {}).get("price") == 22)
st, j = a.req("rollback", {"no": 1}) st, j = a.req("rollback", {"no": 1})
check("回到 v1 → 记为 v4", st == 200 and j.get("version") == 4, j) check("回到 v1 → 记为 v4", st == 200 and j.get("version") == 4, j)
v1 = json.loads(db().execute("SELECT data FROM cms_versions WHERE no=1").fetchone()[0]) v1 = json.loads(db().execute("SELECT data FROM cms_versions WHERE no=1").fetchone()[0])
@@ -321,6 +325,24 @@ ps = [subprocess.Popen([sys.executable, "-c", probe], env=dict(os.environ, YS_RO
n = sqlite3.connect(os.path.join(R2, "ys.db")).execute("SELECT COUNT(*) FROM cms_users").fetchone()[0] n = sqlite3.connect(os.path.join(R2, "ys.db")).execute("SELECT COUNT(*) FROM cms_users").fetchone()[0]
check("三个进程同时首次启动:只建了一个初始账号", n == 1, n) check("三个进程同时首次启动:只建了一个初始账号", n == 1, n)
# ================================================================== 11b. 部署漏配:目录不对 / 缺内容文件 → 不建第一版、不写文件
import store as ST
R3 = tempfile.mkdtemp(prefix="ys-cms-dirs-")
c3 = S.cms.CMS(ST.Store(R3), R3, log=lambda *a: None, prices_file=os.path.join(R3, "prices.json"), payinfo_file=os.path.join(R3, "payinfo.json"), tpl_keys=S.TPL_KEYS, pngcheck=S.pngcheck)
old_p = S.cms.PORTAL_DIR
for bad_dir, why in (("<PORTAL_DIR>", "没配环境变量"), (os.path.join(R3, "empty-www"), "目录里没有 content/site.json")):
if not bad_dir.startswith("<"):
os.makedirs(bad_dir, exist_ok=True)
S.cms.PORTAL_DIR = bad_dir
try:
c3.current(); refused = False
except S.cms.Bad as e:
refused = True; msg = str(e)
n = sqlite3.connect(os.path.join(R3, "ys.db")).execute("SELECT COUNT(*) FROM cms_versions").fetchone()[0]
check("部署漏配(%s):内容后台拒绝工作、不建空的第一版" % why, refused and n == 0 and not os.path.exists("<PORTAL_DIR>"), msg if refused else "没拒")
S.cms.PORTAL_DIR = old_p
shutil.rmtree(R3, ignore_errors=True)
# ================================================================== 12. 原有接口不受影响 # ================================================================== 12. 原有接口不受影响
try: try:
with urllib.request.urlopen("http://127.0.0.1:%d/api/status" % srv.server_address[1], timeout=10) as r: with urllib.request.urlopen("http://127.0.0.1:%d/api/status" % srv.server_address[1], timeout=10) as r:
+1 -1
View File
@@ -54,7 +54,7 @@
| `cms.py` | ~900 | **内容后台**(2026-10-05 晚):账号 / 会话 / 草稿 / 发布 / 回滚 / 传图;`server.py` 把 `/api/cms/*` 交给它。见仓库根目录 `内容后台设计.md` | | `cms.py` | ~900 | **内容后台**(2026-10-05 晚):账号 / 会话 / 草稿 / 发布 / 回滚 / 传图;`server.py` 把 `/api/cms/*` 交给它。见仓库根目录 `内容后台设计.md` |
| `store.py` | ~400 | **数据层**(2026-10-05 新增):SQLite 建库 / 第一次启动自动导入旧 JSON / 存取 / 导出回旧格式 / 体检 / 热备份 | | `store.py` | ~400 | **数据层**(2026-10-05 新增):SQLite 建库 / 第一次启动自动导入旧 JSON / 存取 / 导出回旧格式 / 体检 / 热备份 |
| `tests/回执复现测试.py` | ~280 | 回归 + 漏洞复现(27 条,HTTP 层打接口) | | `tests/回执复现测试.py` | ~280 | 回归 + 漏洞复现(27 条,HTTP 层打接口) |
| `tests/内容后台测试.py` | ~330 | 内容后台专项(75 条:强制改密、无明文、Cookie、https / CSRF / 来源 / 冷却、校验、发布、回滚、传图、会话、命令行) | | `tests/内容后台测试.py` | ~360 | 内容后台专项(78 条:强制改密、无明文、Cookie、https / CSRF / 来源 / 冷却、校验、发布、回滚、传图、会话、命令行) |
| `tests/数据库测试.py` | ~290 | 数据库专项(38 条:全新安装、迁移、回退、事务、并发、规模),数据全是现编的 | | `tests/数据库测试.py` | ~290 | 数据库专项(38 条:全新安装、迁移、回退、事务、并发、规模),数据全是现编的 |
> 这几个文件是**自足**的:没有 import 本项目之外的模块。**部署时 `store.py`、`cms.py` 必须和 `server.py` 放在同一个目录**(`server.py` 直接 `import store` / `import cms`)。 > 这几个文件是**自足**的:没有 import 本项目之外的模块。**部署时 `store.py`、`cms.py` 必须和 `server.py` 放在同一个目录**(`server.py` 直接 `import store` / `import cms`)。