forked from gaoyucindy/KR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh5st.ts
72 lines (65 loc) · 1.93 KB
/
h5st.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import axios from "axios"
import {format} from "date-fns"
import * as CryptoJS from 'crypto-js'
class H5ST {
tk: string;
timestamp: string;
rd: string;
appId: string;
fp: string;
time: number;
ua: string
enc: string;
constructor(appId: string, ua: string, fp: string) {
this.appId = appId
this.ua = ua
this.fp = fp || this.__genFp()
}
__genFp() {
let e = "0123456789";
let a = 13;
let i = '';
for (; a--;)
i += e[Math.random() * e.length | 0];
return (i + Date.now()).slice(0, 16)
}
async __genAlgo() {
this.time = Date.now()
this.timestamp = format(this.time, "yyyyMMddHHmmssSSS")
let {data} = await axios.post(`https://cactus.jd.com/request_algo?g_ty=ajax`, {
'version': '3.0',
'fp': this.fp,
'appId': this.appId.toString(),
'timestamp': this.time,
'platform': 'web',
'expandParams': ''
}, {
headers: {
'Host': 'cactus.jd.com',
'accept': 'application/json',
'content-type': 'application/json',
'user-agent': this.ua,
}
})
this.tk = data.data.result.tk
this.rd = data.data.result.algo.match(/rd='(.*)'/)[1]
this.enc = data.data.result.algo.match(/algo\.(.*)\(/)[1]
}
__genKey(tk: string, fp: string, ts: string, ai: string, algo: object) {
let str = `${tk}${fp}${ts}${ai}${this.rd}`;
return algo[this.enc](str, tk)
}
__genH5st(body: object) {
let y = this.__genKey(this.tk, this.fp, this.timestamp, this.appId, CryptoJS).toString(CryptoJS.enc.Hex)
let s = ''
for (let key of Object.keys(body)) {
key === 'body' ? s += `${key}:${CryptoJS.SHA256(body[key]).toString(CryptoJS.enc.Hex)}&` : s += `${key}:${body[key]}&`
}
s = s.slice(0, -1)
s = CryptoJS.HmacSHA256(s, y).toString(CryptoJS.enc.Hex)
return encodeURIComponent(`${this.timestamp};${this.fp};${this.appId.toString()};${this.tk};${s};3.0;${this.time.toString()}`)
}
}
export {
H5ST
}