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