Skip to content

Commit

Permalink
feat: header sign
Browse files Browse the repository at this point in the history
  • Loading branch information
vv314 committed Oct 5, 2024
1 parent c8607a3 commit acd05a8
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 96 deletions.
23 changes: 9 additions & 14 deletions src/coupons/gundam.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ import { ECODE } from './const.js'
function resolveRedMod(text, renderList) {
try {
for (const instanceId of renderList) {
const data =
matchMoudleData(
text,
`gdc-fx-v2-netunion-red-envelope-${instanceId}`,
'isStopTJCoupon'
) ??
matchMoudleData(
text,
`gdc-fx-new-netunion-red-envelope-${instanceId}`,
'isStopTJCoupon'
)
const data = matchMoudleData(
text,
`gdc-fx-v2-netunion-red-envelope-${instanceId}`,
',directives'
)

if (data) {
data.instanceID = instanceId
data.isStopTJCoupon = true

return data
}
Expand All @@ -43,15 +36,16 @@ async function getPayload({ gundamId, gdId, appJs, renderList }) {
return {
actualLatitude: 0,
actualLongitude: 0,
ctype: 'h5',
app: -1,
platform: 3,
couponAllConfigIdOrderString: data.expandCouponIds.keys.join(','),
couponConfigIdOrderCommaString: data.priorityCouponIds.keys.join(','),
// 这里取 number 类型的 gdId
gundamId: gdId,
instanceId: data.instanceID,
h5Fingerprint: '',
rubikCouponKey: data.cubeToken || '',
h5Fingerprint:
'eJztVlev49gN/ivGBbzIQjdXvc1iEKhbVrGs5hIsBuqSVa0uBfnvkWdmJ9nkJXnLw1oyyI+H5yOPjUPyb29j1L59eoM/tuft/a2Vww1BMwRtoO/ePsEkQkE0gtE4RtHvb8HvbTiNv7/5rcu/fforgSDvJIL++jKYG/4rjMPIO00hv75/UymC/PUdhbb35SNvLm9p3zfdJxAsvTaP+o/Jy0ov+yijrB+86iOoSzAJwS6rkiL6SPuy+EtUfPGz9fM3x5821HhJ9DkZqtArP4raC6P2p2/oSxZ+RhTmUvE/BXUYfYYRGMKwn8ou+bwXiD0l7CnqpdD8nkL3ArlnoD0F7wV8z5J7FtkL1J4V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N0atkYbl3O2JzqrU04a3DINcStRkzChFutcVPPPCD+0RgmDCx5FKSXBrxMgVb3ISUOgIDh2OHRhmF/OQmc1SBnnWoToVpGwCPLmV+Y4MIwzQXHQ2tkj9ek0qTMa+wH5oLmHLLPYq7nc4+VA3uP+oL0h+SUw2IeBJMbh2KPMe1TdVgqp4F6wfOpJMDMxiMV8QN7vNmoEtjx84E+V9By80Or27Z8NuYm1zvxyHdGZjA33CSwnDYeOBc4z8WKYC0c2Xgy4Ff15wbQL+2v91woRDu3hnPJcVsFmV4V5NsM8ELmD7TbhKRutoHZbFq9ZkXhgfgHtPuT5gVZ1ddd+stOrvqo2G2G3cnaXXcw9AXGv5A/75imKaJvRCCOkh8osfuTcrA19X1XZHm0k6Igr3/efRthQBihP6DXs7O82Guz71u24F23FTz4/a34Tdbct1Gp+zEn2FaHG5bvwDovQ9pW7BlXKu+9lcLiccUabXkiqyqs+rp92VFWBVq0fVpXqEIzg/G+UvPRduSHI6srts6hLyOrQK2Q0/frI6SLIp7JE74G1FXnyXAcKhEc71UAeCBKG+ttRJFignSb7tHRncQ7HwFX9QHj4SyeDelK+qjRlDCmtxW6bNPjKWfFdb0hRoja9ME+8A1cofdTROIYqSQn27F6RbVmXbXwmc1dLCifyu3y9FK/LDK3VDKvnIO7cr/dlTT3pdlzt3X/aHaZqAXXacm9p8qB6vZnGey96e+M5Gi2qdmqM+m2tVgre7JzWNWhZbbU+0lXmVWDCjV5ZpIkwMrAMvwkQH359vd/ACCzCfU=',
needTj: data.isStopTJCoupon
}
}
Expand Down Expand Up @@ -98,6 +92,7 @@ async function grabCoupon(cookie, gundamId, guard) {
Origin: actUrl.origin,
Referer: actUrl.origin + '/'
},
signType: 'header',
guard
}
)
Expand Down
8 changes: 4 additions & 4 deletions src/coupons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ async function runTask(cookie, guard) {
const asyncResults = await Promise.all([
...gundamActConfs.map((conf) =>
gundam.grabCoupon(cookie, conf.gid, guard).catch(() => [])
),
// 微信服务号活动
...wxfwhActConfs.map((conf) =>
wxfwh.grabCoupon(cookie, conf.gid, guard).catch(() => [])
)
// 微信服务号活动
// ...wxfwhActConfs.map((conf) =>
// wxfwh.grabCoupon(cookie, conf.gid, guard).catch(() => [])
// )
])

results.push(...asyncResults.flat())
Expand Down
39 changes: 20 additions & 19 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import timeoutSignal from 'timeout-signal'
import HttpsProxyAgent from 'https-proxy-agent'

const cookieJarMap = new Map()
const UA =
'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Mobile/15E148 Safari/604.1'

const ECODE = {
FETCH: 'FETCH_ERROR',
Expand All @@ -13,13 +15,11 @@ const ECODE = {

async function fetch(url, opts = {}) {
const cookieJar = opts.cookie
const guard = opts.guard
const existCookie = cookieJar?.getCookieStringSync?.(url)
const optCookie = opts.headers?.cookie || ''
const defHeader = {
// 重要:需设置 UA
'User-Agent':
'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
'User-Agent': UA,
Connection: 'keep-alive'
}
let urlObj = new URL(url)
Expand All @@ -39,7 +39,7 @@ async function fetch(url, opts = {}) {
opts.agent = new HttpsProxyAgent(opts.proxy)
}

opts.headers = Object.assign({}, defHeader, opts.headers)
opts.headers = { ...defHeader, ...opts.headers }

if (opts.params) {
Object.keys(opts.params).forEach((key) => {
Expand All @@ -51,12 +51,18 @@ async function fetch(url, opts = {}) {
delete opts.timeout
delete opts.params

if (guard) {
urlObj = await guard.sign({
url: urlObj,
method: opts.method,
body: opts.body
})
if (opts.guard) {
const { url, headers } = await opts.guard.sign(
{
url: urlObj,
method: opts.method,
body: opts.body
},
opts.signType
)

urlObj = url
opts.headers = { ...opts.headers, ...headers }
}

try {
Expand All @@ -82,11 +88,8 @@ async function fetch(url, opts = {}) {

async function doGet(url, opts = {}) {
const res = await fetch(url, {
headers: opts.headers,
cookie: opts.cookie,
params: opts.params,
timeout: opts.timeout ?? 10000,
guard: opts.guard
...opts,
timeout: opts.timeout ?? 10000
})

if (res.ok) return res.json()
Expand All @@ -109,15 +112,13 @@ async function doPost(url, data, opts = {}) {
}

const res = await fetch(url, {
...opts,
method: 'POST',
body: body,
cookie: opts.cookie,
params: opts.params,
headers: Object.assign({}, opts.headers, {
'Content-Type': cType
}),
timeout: opts.timeout ?? 10000,
guard: opts.guard
timeout: opts.timeout ?? 10000
})

if (res.ok) return res.json()
Expand Down
Binary file modified src/shadow/guard.wasm
Binary file not shown.
23 changes: 15 additions & 8 deletions src/shadow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ShadowGuard {
async getReqSig(reqOpt) {
const guardURL = new URL(formatUrl(reqOpt.url || ''))

guardURL.searchParams.append('gdBs', '')
guardURL.searchParams.append('yodaReady', yodaReady)
guardURL.searchParams.append('csecplatform', csecPlatform)
guardURL.searchParams.append('csecversion', this.version)
Expand All @@ -60,27 +61,33 @@ class ShadowGuard {
return { guardURL, reqSig }
}

async getMtgSig(reqSig, isShort) {
async getMtgSig(reqSig, signType = 'url') {
return getMtgSig(reqSig, {
...this.context,
isShort
signType
})
}

/**
* @param {FetchOptions} reqOpt
* @param {boolean} isShort
* @param {'url' | 'header'} signType
* @returns
*/
async sign(reqOpt, isShort) {
async sign(reqOpt, signType) {
if (!reqOpt) return reqOpt

const { guardURL, reqSig } = await this.getReqSig(reqOpt)
const mtgSig = await this.getMtgSig(reqSig, isShort)

guardURL.searchParams.append('mtgsig', JSON.stringify(mtgSig.data))
const res = await this.getMtgSig(reqSig, signType)
const mtgSig = JSON.stringify(res.data)
const headers = {}

if (signType === 'header') {
headers.mtgsig = mtgSig
} else {
guardURL.searchParams.append('mtgsig', mtgSig)
}

return guardURL.toString()
return { url: guardURL.toString(), headers }
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function matchMoudleData(text, start, end) {

if (!res) return null

const data = eval(`({moduleId:"${res[0]}})`)
const data = eval(`({moduleId:"${res[0]})`)

return data
}
Expand Down
22 changes: 11 additions & 11 deletions test/coupons.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ const cookie = createMTCookie(tokens[0].token)

beforeAll(() => guard.init(gundam.getActUrl(mainActConf.gid)))

test('Test Main Grab', async () => {
test('Main Grab', async () => {
const res = await gundam.grabCoupon(cookie, mainActConf.gid, guard)

return expect(res.length).toBeGreaterThan(0)
})

test('Test Token Error', async () => {
test('Token Error', async () => {
const res = await grabCoupons('invalid token', {
// proxy: 'http://127.0.0.1:8887'
})

return expect(res.code).toBe(ECODE.AUTH)
})

test('Test Wxfwh grab', async () => {
const res = await wxfwh.grabCoupon(cookie, wxfwhActConfs[0].gid, guard)
// test('Wxfwh grab', async () => {
// const res = await wxfwh.grabCoupon(cookie, wxfwhActConfs[0].gid, guard)

return expect(res).toBeTruthy()
})
// return expect(res).toBeTruthy()
// })

test('Test wxfwh Result', async () => {
const res = await wxfwh.getCouponList(cookie, 'I5r2SYd5kTN1l1AkMhwCNA')
// test('Wxfwh Result', async () => {
// const res = await wxfwh.getCouponList(cookie, 'I5r2SYd5kTN1l1AkMhwCNA')

return expect(res.length).toBeGreaterThan(0)
})
// return expect(res.length).toBeGreaterThan(0)
// })

// test('Test lottery Result', async () => {
// test('Lottery Result', async () => {
// const tmplData = await lottery.getTemplateData(cookie, '1VlhFT', guard)
// const ticketConfig = await lottery.getTicketConfig(
// tmplData.gdId,
Expand Down
2 changes: 1 addition & 1 deletion test/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const content = [
'- ¥15 (满59可用 - 便利店满减红包)'
].join('\n')

test('Test Notifier', async () => {
test('Notifier', async () => {
const res = await Promise.all(
notifier.notify(title, `账号 X:\n${content}\n- Time ${time}`)
)
Expand Down
37 changes: 18 additions & 19 deletions test/payload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const cookie = createMTCookie(tokens[0].token)

beforeAll(() => guard.init(gundam.getActUrl(mainActConf.gid)))

test('Test Main Payload', async () => {
test('Main Payload', async () => {
const tmplData = await getTemplateData(cookie, mainActConf.gid, guard)
const payload = await gundam.getPayload(tmplData)

Expand All @@ -25,25 +25,24 @@ test('Test Main Payload', async () => {
gundamId: tmplData.gdId,
needTj: expect.any(Boolean),
instanceId: expect.any(String),
h5Fingerprint: '',
rubikCouponKey: expect.any(String)
h5Fingerprint: expect.any(String)
})
})

test('Test Wxfwh Payload', async () => {
const tmplData = await getTemplateData(cookie, wxfwhActConfs[0].gid, guard)
const payload = await wxfwh.getPayload(cookie, tmplData, guard)
// test('Wxfwh Payload', async () => {
// const tmplData = await getTemplateData(cookie, wxfwhActConfs[0].gid, guard)
// const payload = await wxfwh.getPayload(cookie, tmplData, guard)

return expect(payload).toMatchObject({
ctype: 'wm_wxapp',
fpPlatform: 13,
wxOpenId: '',
appVersion: '',
gdId: tmplData.gdId,
pageId: tmplData.pageId,
tabs: expect.any(Array),
activityViewId: expect.any(String),
instanceId: expect.any(String),
mtFingerprint: expect.any(String)
})
})
// return expect(payload).toMatchObject({
// ctype: 'wm_wxapp',
// fpPlatform: 13,
// wxOpenId: '',
// appVersion: '',
// gdId: tmplData.gdId,
// pageId: tmplData.pageId,
// tabs: expect.any(Array),
// activityViewId: expect.any(String),
// instanceId: expect.any(String),
// mtFingerprint: expect.any(String)
// })
// })
11 changes: 5 additions & 6 deletions test/shadow.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ const guard = new ShadowGuard({

beforeAll(() => guard.init(gundam.getActUrl(mainActConf.gid)))

test('Test Generate Session', () =>
expect(guard.meta.sessionId).toHaveLength(32))
test('Generate Session', () => expect(guard.meta.sessionId).toHaveLength(32))

test('Test Generate Meta', () => expect(guard.meta).toBeTruthy())
test('Generate Meta', () => expect(guard.meta).toBeTruthy())

test('Test Web FpId', async () => {
test('Web FpId', async () => {
const dfpId = await guard.getWebDfpId(guard.fingerprint)

return expect(dfpId).toHaveLength(56)
})

test('Test MtgSig', async () => {
test('MtgSig', async () => {
guard.context.timestamp = 1702734030440
guard.context.runtimeKey = 'r0ejVfUUFC1DvZh3L/0z'
guard.context.siua =
Expand All @@ -34,7 +33,7 @@ test('Test MtgSig', async () => {
expect(mtgSig).toBeTruthy()
})

test('Test Base Signature', async () => {
test('Base Signature', async () => {
const sig = await guard.getReqSig({
url: 'https://mediacps.meituan.com/gundam/gundamLogin',
method: 'POST'
Expand Down
4 changes: 2 additions & 2 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const cookie = createMTCookie(tokens[0].token)

beforeAll(() => guard.init(gundam.getActUrl(wxfwhActConfs[0].gid)))

test('Test getTemplateData', async () => {
test('GetTemplateData', async () => {
const res = await getTemplateData(cookie, wxfwhActConfs[0].gid, guard)

return expect(res).toMatchObject({
Expand All @@ -21,7 +21,7 @@ test('Test getTemplateData', async () => {
})
})

test('Test getRenderList', async () => {
test('GetRenderList', async () => {
const renderList = await getRenderList(
cookie,
{ gundamViewId: '1I9uL6', pageId: 544003, gdId: 466632 },
Expand Down
4 changes: 2 additions & 2 deletions test/update.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import updateNotifier from '../src/update-notifier.js'

test('Test Update', () => {
test('Update', () => {
const timeout = 5000

return expect(updateNotifier(timeout)).resolves.toBeDefined()
})

test('Test Update Timeout', () => {
test('Update Timeout', () => {
const timeout = 1

return expect(updateNotifier(timeout)).rejects.toMatch('请求超时')
Expand Down
Loading

0 comments on commit acd05a8

Please sign in to comment.