Skip to content

Commit

Permalink
fix: remove flags (maybe?)
Browse files Browse the repository at this point in the history
  • Loading branch information
daidr committed Nov 5, 2022
1 parent 7a025a9 commit b991396
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
36 changes: 20 additions & 16 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,22 @@ const writeDataToStorage = async function <T>(key: string, data: T) {
let selectedUid = ''

const targetPages = [
'https://api-os-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_global&asource=paimon',
'https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn&asource=paimon',
'https://bbs-api-os.hoyolab.com/game_record/app/genshin/api/dailyNote?asource=paimon*',
'https://api-takumi-record.mihoyo.com/game_record/app/genshin/api/dailyNote?asource=paimon*',
'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/createVerification?asource=paimon*',
'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/verifyVerification?asource=paimon*',
'https://bbs-api-os.hoyolab.com/game_record/app/card/wapi/createVerification?asource=paimon*',
'https://bbs-api-os.hoyolab.com/game_record/app/card/wapi/verifyVerification?asource=paimon*',
'https://api-os-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_global',
'https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn',
'https://bbs-api-os.hoyolab.com/game_record/app/genshin/api/dailyNote*',
'https://api-takumi-record.mihoyo.com/game_record/app/genshin/api/dailyNote*',
'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/createVerification*',
'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/verifyVerification*',
'https://bbs-api-os.hoyolab.com/game_record/app/card/wapi/createVerification*',
'https://bbs-api-os.hoyolab.com/game_record/app/card/wapi/verifyVerification*',
]

let currentCookie = ''
const ruleID = 114514

const updateRules = async () => {
const rules = []
for (const i of [0, 1, 2, 3]) {
for (const i of [0, 1, 2, 3, 4, 5, 6, 7]) {
rules.push({
id: ruleID + i,
priority: 1,
Expand All @@ -196,7 +196,11 @@ const updateRules = async () => {
})
}

await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: [ruleID, ruleID + 1, ruleID + 2, ruleID + 3], addRules: rules })
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: [ruleID, ruleID + 1, ruleID + 2, ruleID + 3, ruleID + 4, ruleID + 5, ruleID + 6, ruleID + 7], addRules: rules })
}

const resetRules = async () => {
await chrome.declarativeNetRequest.updateDynamicRules({ removeRuleIds: [ruleID, ruleID + 1, ruleID + 2, ruleID + 3, ruleID + 4, ruleID + 5, ruleID + 6, ruleID + 7] })
}

const responseRuleID = 19198
Expand Down Expand Up @@ -410,7 +414,7 @@ const refreshData = async function (uiOnly = false) {
let hasUpdatedBadge = false
// 遍历启用的 role
for (const role of enabledRoleList) {
const data = uiOnly ? role.data : await getRoleDataByCookie(role.serverType === 'os', role.cookie, role.uid, role.serverRegion, setCookie)
const data = uiOnly ? role.data : await getRoleDataByCookie(role.serverType === 'os', role.cookie, role.uid, role.serverRegion, setCookie, resetRules)

if (Number.isInteger(data)) {
// error code
Expand Down Expand Up @@ -575,7 +579,7 @@ onMessage<{ oversea: boolean }, 'request_cookie_read'>('request_cookie_read', as
await updateRules()
}

const result = await getRoleInfoByCookie(oversea, cookie, setCookie)
const result = await getRoleInfoByCookie(oversea, cookie, setCookie, resetRules)

if (result) {
for (const item of result)
Expand Down Expand Up @@ -611,7 +615,7 @@ onMessage<{ uid: string }, 'create_verification'>('create_verification', async (
await updateRules()
}

return await createVerification(oversea, cookie, setCookie)
return await createVerification(oversea, cookie, setCookie, resetRules)
})

onMessage<{ uid: string }, 'request_captcha_bg'>('request_captcha_bg', async ({ data: { uid } }) => {
Expand Down Expand Up @@ -652,7 +656,7 @@ onMessage<{ uid: string }, 'request_captcha_bg'>('request_captcha_bg', async ({
await updateRules()
}

const verification = await createVerification(oversea, cookie, setCookie)
const verification = await createVerification(oversea, cookie, setCookie, resetRules)
if (verification && curtab.id)
await sendMessage('request_captcha', { verification, uid, tabId: curtab.id }, { tabId: curtab.id, context: 'content-script' })
})
Expand All @@ -669,10 +673,10 @@ onMessage('finish_captcha', async ({ data: { tabId, uid, geetest } }) => {
currentCookie = cookie
await updateRules()
}
const result = await verifyVerification(oversea, cookie, geetest, setCookie)
const result = await verifyVerification(oversea, cookie, geetest, setCookie, resetRules)

tabs.remove(tabId)
getRoleInfoByCookie(oversea, cookie, setCookie)
getRoleInfoByCookie(oversea, cookie, setCookie, resetRules)
refreshData()
return result
})
41 changes: 20 additions & 21 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,11 +354,11 @@ function getHeader(oversea: boolean, params: Record<string, string>, body: objec
return header
}

async function getRoleInfoByCookie(oversea: boolean, cookie: string, setCookie?: Function): Promise<IRoleDataItem[] | false> {
async function getRoleInfoByCookie(oversea: boolean, cookie: string, setCookie?: Function, resetCookie?: Function): Promise<IRoleDataItem[] | false> {
// 根据 oversea 参数选择对应 api 地址
const url = oversea
? 'https://api-os-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_global&asource=paimon'
: 'https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn&asource=paimon'
? 'https://api-os-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_global'
: 'https://api-takumi.mihoyo.com/binding/api/getUserGameRolesByCookie?game_biz=hk4e_cn'

// 生成 header
const headers = getHeader(oversea, {}, {}, false)
Expand All @@ -377,7 +377,7 @@ async function getRoleInfoByCookie(oversea: boolean, cookie: string, setCookie?:
)

// 发送请求
return await fetch(req)
const _ret = await fetch(req)
.then(response => {
// get Set-Cookie header
const setCookieHeader = response.headers.get('Set-Cookie')
Expand All @@ -393,15 +393,16 @@ async function getRoleInfoByCookie(oversea: boolean, cookie: string, setCookie?:
.catch(() => {
return false
})
resetCookie && await resetCookie()
return _ret
}

async function getRoleDataByCookie(oversea: boolean, cookie: string, role_id: string, serverRegion: serverRegions, setCookie?: Function): Promise<IUserData | false | number> {
async function getRoleDataByCookie(oversea: boolean, cookie: string, role_id: string, serverRegion: serverRegions, setCookie?: Function, resetCookie?: Function): Promise<IUserData | false | number> {
// 根据 oversea 参数选择对应 api 地址
const url = new URL(oversea ? 'https://bbs-api-os.hoyolab.com/game_record/app/genshin/api/dailyNote' : 'https://api-takumi-record.mihoyo.com/game_record/app/genshin/api/dailyNote')

// 补全 url query
const params = {
asource: 'paimon',
server: serverRegion,
role_id,
}
Expand All @@ -426,7 +427,7 @@ async function getRoleDataByCookie(oversea: boolean, cookie: string, role_id: st
)

// 发送请求
return await fetch(req)
const _ret = await fetch(req)
.then(response => response.json())
.then((data) => {
if (data.retcode === 0)
Expand All @@ -440,15 +441,16 @@ async function getRoleDataByCookie(oversea: boolean, cookie: string, role_id: st
.catch(() => {
return false
})
resetCookie && await resetCookie()
return _ret
}

async function createVerification(oversea: boolean, cookie: string, setCookie?: Function): Promise<ICaptchaResponse | false> {
async function createVerification(oversea: boolean, cookie: string, setCookie?: Function, resetCookie?: Function): Promise<ICaptchaResponse | false> {
// 根据 oversea 参数选择对应 api 地址
const url = new URL(oversea ? 'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/createVerification' : 'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/createVerification')

// 补全 url query
const params = {
asource: 'paimon',
is_high: 'true',
}

Expand All @@ -472,7 +474,7 @@ async function createVerification(oversea: boolean, cookie: string, setCookie?:
)

// 发送请求
return await fetch(req)
const _ret = await fetch(req)
.then(response => response.json())
.then((data) => {
if (data.retcode === 0)
Expand All @@ -483,22 +485,16 @@ async function createVerification(oversea: boolean, cookie: string, setCookie?:
.catch(() => {
return false
})
resetCookie && await resetCookie()
return _ret
}

async function verifyVerification(oversea: boolean, cookie: string, geetest: ICaptchaRequest, setCookie?: Function): Promise<boolean> {
async function verifyVerification(oversea: boolean, cookie: string, geetest: ICaptchaRequest, setCookie?: Function, resetCookie?: Function): Promise<boolean> {
// 根据 oversea 参数选择对应 api 地址
const url = new URL(oversea ? 'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/verifyVerification' : 'https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/verifyVerification')

// 补全 url query
const params = {
asource: 'paimon',
}

for (const [key, value] of Object.entries(params))
url.searchParams.append(key, value)

// 生成 header
const headers = getHeader(oversea, params, geetest, true)
const headers = getHeader(oversea, {}, geetest, true)

// 为 header 追加 cookie
headers.set('Cookie', cookie)
Expand All @@ -515,7 +511,7 @@ async function verifyVerification(oversea: boolean, cookie: string, geetest: ICa
)

// 发送请求
return await fetch(req)
const _ret = await fetch(req)
.then(response => response.json())
.then((data) => {
if (data.retcode === 0)
Expand All @@ -526,6 +522,9 @@ async function verifyVerification(oversea: boolean, cookie: string, geetest: ICa
.catch(() => {
return false
})

resetCookie && await resetCookie()
return _ret
}

export { md5, randomIntFromInterval, getTime, getClock, getDS, getHeader, getRoleInfoByCookie, getRoleDataByCookie, createVerification, verifyVerification }

0 comments on commit b991396

Please sign in to comment.