fix(secrets): 换号期间的旧账号代理与快照不得落到新账号
登出清理涉及两次异步桥调用(读/写 app.json),期间可能已经换号。 给锁定代理回填、代理补报和继承快照消费都加上当前 uid 复核: 旧账号的代理不写进新账号的本地 proxy_users,也不带新账号令牌补报; 继承在读到快照后再确认一次 uid,变了就放弃本次继承。
This commit is contained in:
@@ -132,6 +132,8 @@ export async function inheritSecretsFromDevice(
|
||||
console.warn('[api-secret] 读取本机继承快照失败(下次加载重试):', error)
|
||||
return null
|
||||
})
|
||||
// 等配置期间可能已经登出/换号:旧账号的快照不能写进新账号的服务端
|
||||
if (host.currentUid() !== uid) return { count: 0, notice: null }
|
||||
const stash = readInheritedSecretStash(config, host.moduleKeys())
|
||||
if (!stash || stash.fromUid === uid) return { count: 0, notice: null }
|
||||
const serverHas = new Set(serverItems.filter((item) => item?.exists).map((item) => item.moduleKey))
|
||||
|
||||
@@ -410,7 +410,10 @@ export function ensureApiSecretsLoaded(): Promise<ApiSecretLoadState> {
|
||||
return loadApiSecrets()
|
||||
}
|
||||
|
||||
async function applyServerBundle(bundle: UserApiSecretBundle | null | undefined): Promise<void> {
|
||||
async function applyServerBundle(
|
||||
bundle: UserApiSecretBundle | null | undefined,
|
||||
epoch: number,
|
||||
): Promise<void> {
|
||||
if (Array.isArray(bundle?.requiredModules) && bundle.requiredModules.length) {
|
||||
requiredModules = bundle.requiredModules
|
||||
}
|
||||
@@ -420,6 +423,8 @@ async function applyServerBundle(bundle: UserApiSecretBundle | null | undefined)
|
||||
}
|
||||
}
|
||||
serverProxy = toServerProxy(bundle?.proxy)
|
||||
// 登出/换号可能发生在读配置的等待里:旧账号的锁定代理绝不能写进新账号的本地配置
|
||||
if (epoch !== cacheEpoch) return
|
||||
if (serverProxy?.locked && serverProxy.full) {
|
||||
// 后台锁定代理:把后台值对齐到本地配置(客户端任务链读本地),先比对、幂等、失败静默
|
||||
await syncLockedProxyToLocal(serverProxy.full)
|
||||
@@ -434,7 +439,7 @@ async function doLoad(): Promise<ApiSecretLoadState> {
|
||||
try {
|
||||
let bundle = await fetchMyApiSecrets()
|
||||
if (epoch !== cacheEpoch) return 'unknown'
|
||||
await applyServerBundle(bundle)
|
||||
await applyServerBundle(bundle, epoch)
|
||||
if (epoch !== cacheEpoch) return 'unknown'
|
||||
bundleLoaded = true
|
||||
notify()
|
||||
@@ -443,7 +448,7 @@ async function doLoad(): Promise<ApiSecretLoadState> {
|
||||
if (migrated > 0) {
|
||||
bundle = await fetchMyApiSecrets()
|
||||
if (epoch !== cacheEpoch) return 'unknown'
|
||||
await applyServerBundle(bundle)
|
||||
await applyServerBundle(bundle, epoch)
|
||||
if (epoch !== cacheEpoch) return 'unknown'
|
||||
notify()
|
||||
}
|
||||
@@ -455,7 +460,7 @@ async function doLoad(): Promise<ApiSecretLoadState> {
|
||||
if (inherited > 0) {
|
||||
bundle = await fetchMyApiSecrets()
|
||||
if (epoch !== cacheEpoch) return 'unknown'
|
||||
await applyServerBundle(bundle)
|
||||
await applyServerBundle(bundle, epoch)
|
||||
if (epoch !== cacheEpoch) return 'unknown'
|
||||
notify()
|
||||
}
|
||||
@@ -524,9 +529,12 @@ async function tryMigrateLocalSecrets(serverItems: UserApiSecretItem[]): Promise
|
||||
*/
|
||||
async function tryBackfillProxyToServer(): Promise<boolean> {
|
||||
try {
|
||||
if (currentUid() === '0') return false
|
||||
const uid = currentUid()
|
||||
if (uid === '0') return false
|
||||
const config = await readConfigForBackfill()
|
||||
if (!config) return false
|
||||
// 等配置期间可能已经登出/换号:补报请求会带新账号令牌,必须放弃旧账号的本地代理
|
||||
if (currentUid() !== uid) return false
|
||||
const url = readLocalProxyUrl(config)
|
||||
if (!url) return false
|
||||
const result = await migrateMyApiSecrets([{ moduleKey: 'proxy', value: url }])
|
||||
@@ -612,9 +620,11 @@ async function syncLockedProxyToLocal(full: string): Promise<void> {
|
||||
if (typeof window === 'undefined') return
|
||||
const api = window.pywebview?.api
|
||||
if (!api?.read_config || !api?.save_config) return
|
||||
const uidAtStart = currentUid()
|
||||
const config = (await api.read_config()) as Record<string, unknown> | null
|
||||
if (currentUid() !== uidAtStart) return
|
||||
if (readLocalProxyUrl(config) === full) return
|
||||
const uid = currentUid()
|
||||
const uid = uidAtStart
|
||||
if (uid === '0') {
|
||||
const mode = Number(config?.proxy_mode) === 2 ? 2 : 1
|
||||
await api.save_config({ proxy_url: full, proxy_mode: mode })
|
||||
|
||||
Reference in New Issue
Block a user