fix: simplify kyapp qx notifications
This commit is contained in:
+10
-22
@@ -37,24 +37,9 @@ function getHeader(headers, name) {
|
||||
return ''
|
||||
}
|
||||
function parseJson(s, def) { try { return JSON.parse(s) } catch (e) { return def } }
|
||||
function b64urlDecode(seg) {
|
||||
let s = String(seg || '').replace(/-/g, '+').replace(/_/g, '/')
|
||||
while (s.length % 4) s += '='
|
||||
try { return decodeURIComponent(escape(atob(s))) } catch (e) { try { return atob(s) } catch (_) { return '' } }
|
||||
}
|
||||
function decodeJwt(token) {
|
||||
const parts = String(token || '').split('.')
|
||||
if (parts.length < 2) return null
|
||||
return parseJson(b64urlDecode(parts[1]), null)
|
||||
}
|
||||
function tokenLooksValid(token) {
|
||||
return /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/.test(String(token || '').trim())
|
||||
}
|
||||
function mask(s) {
|
||||
s = String(s || '')
|
||||
if (s.length <= 12) return s ? '***' : ''
|
||||
return s.slice(0, 6) + '...' + s.slice(-6)
|
||||
}
|
||||
function normalizeHost(host) {
|
||||
host = trimSlash(host)
|
||||
if (!host) return ''
|
||||
@@ -184,10 +169,14 @@ async function syncToQingLong(records) {
|
||||
let merged = Array.isArray(base) ? base : []
|
||||
records.forEach(r => { merged = mergeRecord(merged, r) })
|
||||
const value = JSON.stringify(merged)
|
||||
if (env) await updateQlEnv(host, qlToken, env, value)
|
||||
else await createQlEnv(host, qlToken, value)
|
||||
if (env) {
|
||||
await updateQlEnv(host, qlToken, env, value)
|
||||
saveLocalRecords(merged)
|
||||
return { synced: true, count: merged.length }
|
||||
return { synced: true, action: 'updated' }
|
||||
}
|
||||
await createQlEnv(host, qlToken, value)
|
||||
saveLocalRecords(merged)
|
||||
return { synced: true, action: 'created' }
|
||||
}
|
||||
async function main() {
|
||||
const url = ($request && $request.url) || ''
|
||||
@@ -201,8 +190,7 @@ async function main() {
|
||||
if (token) pending.token_body = token
|
||||
if (device) pending.device_id = device
|
||||
setPending(pending)
|
||||
const jwt = decodeJwt(pending.token_body)
|
||||
notify('已抓到 token', `uid=${jwt && jwt.uid ? jwt.uid : '未知'} device=${mask(pending.device_id)}`)
|
||||
notify('CK 获取成功', '')
|
||||
}
|
||||
|
||||
if (/\/task\/get_task_page_info/.test(url)) {
|
||||
@@ -218,7 +206,7 @@ async function main() {
|
||||
return done()
|
||||
}
|
||||
if (!tokenLooksValid(pending.token_body)) {
|
||||
notify('token 格式异常', mask(pending.token_body))
|
||||
notify('token 格式异常', '')
|
||||
return done()
|
||||
}
|
||||
|
||||
@@ -227,7 +215,7 @@ async function main() {
|
||||
saveLocalRecords(records)
|
||||
try {
|
||||
const ret = await syncToQingLong([record])
|
||||
if (ret.synced) notify('已更新青龙 KYAPP', `本地/青龙合并后 ${ret.count} 个账号,当前 token=${mask(record.token_body)}`)
|
||||
if (ret.synced) notify(ret.action === 'created' ? '增加成功' : '更新成功', '')
|
||||
} catch (e) {
|
||||
notify('同步青龙失败,已本地保存', String(e && e.message || e).slice(0, 220))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user