mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-12-03 03:12:51 +08:00
fix(qq): import the running shared cookie into the account pool
`import-shared` read the cookie from qq.json, which is only the on-disk copy. After #188 renews credentials through the managed session that file goes stale, so this route would seed the pool with a dead cookie. Prefer the running QQ provider's in-memory cookie and fall back to the cookie store when no QQ provider is mounted (tests that only register netease keep the old behaviour). Two regressions: the rotated cookie wins over the file copy, and a provider without a login 409s instead of creating a dead row.
This commit is contained in:
1 parent
aceccc93e1
commit
91b2533176
2 files changed
+51
-4
No files matched your search
@@ -217,4 +217,35 @@ describe("admin music account pool", () => {
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error).toBe("no_shared_login");
|
||||
});
|
||||
|
||||
it("imports the running QQ cookie instead of the stale file copy", async () => {
|
||||
const cookieStore = { save: vi.fn(), load: vi.fn(() => "uin=999; qm_keyst=stale") };
|
||||
const qq = {
|
||||
platform: "qq",
|
||||
withCookie: vi.fn(),
|
||||
// 托管会话轮换后,内存里已经是新 cookie,qq.json 还是旧的
|
||||
getCookie: () => "uin=1; qm_keyst=rotated",
|
||||
};
|
||||
const { app, db } = mount({ cookieStore, providers: { qq } });
|
||||
const res = await request(app)
|
||||
.post("/api/music-accounts/import-shared")
|
||||
.send({ platform: "qq", label: "QQ 主号" });
|
||||
expect(res.status).toBe(201);
|
||||
expect(db.listMusicAccounts()[0]?.cookie).toBe("uin=1; qm_keyst=rotated");
|
||||
expect(cookieStore.load).not.toHaveBeenCalled();
|
||||
// cookie 不出服务器:响应里不能出现完整值
|
||||
expect(JSON.stringify(res.body)).not.toContain("rotated");
|
||||
});
|
||||
|
||||
it("409s a QQ import when the running provider has no login", async () => {
|
||||
const cookieStore = { save: vi.fn(), load: vi.fn(() => "uin=999; qm_keyst=stale") };
|
||||
const qq = { platform: "qq", withCookie: vi.fn(), getCookie: () => "" };
|
||||
const { app, db } = mount({ cookieStore, providers: { qq } });
|
||||
const res = await request(app)
|
||||
.post("/api/music-accounts/import-shared")
|
||||
.send({ platform: "qq" });
|
||||
expect(res.status).toBe(409);
|
||||
expect(res.body.error).toBe("no_shared_login");
|
||||
expect(db.listMusicAccounts()).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
@@ -84,7 +84,7 @@ export function createMusicAccountsRouter(
|
||||
};
|
||||
};
|
||||
|
||||
const isPlatform = (v: unknown): v is string =>
|
||||
const isPlatform = (v: unknown): v is (typeof PERSONAL_MUSIC_PLATFORMS)[number] =>
|
||||
typeof v === "string" && knownPlatforms.has(v);
|
||||
|
||||
const nonEmpty = (v: unknown, max: number): v is string =>
|
||||
@@ -302,11 +302,15 @@ export function createMusicAccountsRouter(
|
||||
* the shared account can hand it to selected members without re-scanning.
|
||||
*/
|
||||
router.post("/import-shared", (req, res) => {
|
||||
if (!cookieStore) {
|
||||
const { platform, label, capacity } = req.body ?? {};
|
||||
// QQ 的共享登录以"运行中的 provider"为准:登录写的是它内存里的 cookie,
|
||||
// qq.json 只是落盘副本。托管会话轮换凭据之后文件就旧了(#188),
|
||||
// 从这里导入会把一个过期 cookie 塞进账号池。
|
||||
const sharedQq = platform === "qq" ? providers.qq : undefined;
|
||||
if (!sharedQq && !cookieStore) {
|
||||
res.status(501).json({ error: "cookie store unavailable" });
|
||||
return;
|
||||
}
|
||||
const { platform, label, capacity } = req.body ?? {};
|
||||
if (!isPlatform(platform)) {
|
||||
res.status(400).json({ error: "unsupported platform" });
|
||||
return;
|
||||
@@ -315,7 +319,19 @@ export function createMusicAccountsRouter(
|
||||
res.status(400).json({ error: "capacity must be a non-negative number" });
|
||||
return;
|
||||
}
|
||||
const cookie = cookieStore.load(platform as Parameters<CookieStore["load"]>[0]);
|
||||
// 没有 qq provider(例如单测只挂了 netease)时仍然回落到 cookie store,
|
||||
// 所以这个改动对已有平台零影响。
|
||||
let cookie: string | undefined;
|
||||
if (sharedQq) {
|
||||
try {
|
||||
cookie = sharedQq.getCookie();
|
||||
} catch {
|
||||
// Provider 还没建立登录态时抛错等价于「没有可导入的登录」
|
||||
cookie = "";
|
||||
}
|
||||
} else {
|
||||
cookie = cookieStore?.load(platform);
|
||||
}
|
||||
if (!cookie || !cookie.trim()) {
|
||||
res.status(409).json({ error: "no_shared_login", platform });
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user