-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjx_init.py
230 lines (195 loc) · 5.79 KB
/
jx_init.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
# @Time : 2021/7/21 4:50 下午
# @File : jx_init.py
# @Project : jd_scripts
# @Desc : 加密算法
import hashlib
import aiohttp
import hmac
import json
import random
import time
from furl import furl
import re
from utils.console import println
from urllib.parse import unquote
def hmacSha256(key, value):
"""
sha256加密
"""
obj = hmac.new(value.encode(), key.encode(), hashlib.sha256)
return obj.hexdigest()
def hmacSha512(key, value):
"""
hmacsha512加密
:param key:
:param value:
:return:
"""
obj = hmac.new(value.encode(), key.encode(), hashlib.sha512)
return obj.hexdigest()
def md5(value):
"""
md5加密
:param value:
:return:
"""
obj = hashlib.md5()
obj.update(value.encode())
return obj.hexdigest()
def sha512(value):
"""
sha512加密
:param value:
:return:
"""
obj = hashlib.sha512()
obj.update(value.encode())
return obj.hexdigest()
def sha256(value):
"""
sha256加密
:param value:
:return:
"""
obj = hashlib.sha256()
obj.update(value.encode())
return obj.hexdigest()
def hmacMD5(key, value):
"""
hamcMd5加密
:param key:
:param value:
:return:
"""
obj = hmac.new(value.encode(), key.encode('utf-8'), hashlib.md5)
return obj.hexdigest()
async def encrypt(self, timestamp=None, url='', stk=''):
"""
获取签名
"""
timestamp = timestamp.strftime('%Y%m%d%H%M%S%f')[0:17]
if not stk:
url = furl(url)
stk = url.args.get('_stk', '')
s = '{}{}{}{}{}'.format(self.token, self.fp, timestamp, self.appid, self.random)
try:
hash1 = self.algo(s, self.token)
except Exception as e:
hash1 = self.algo(s)
tmp = []
tmp_url = furl(url)
for key in stk.split(','):
if key == '':
continue
tmp_s = '{}:{}'.format(key, tmp_url.args.get(key, ''))
tmp.append(tmp_s)
st = '&'.join(tmp)
hash2 = hmacSha256(st, hash1)
return ';'.join([str(timestamp), str(self.fp), self.appid, self.token, hash2])
async def get_encrypt(self):
"""
获取签名算法
"""
if not hasattr(self, '_fp'):
self.fp = self.generate_fp()
if not hasattr(self, '_appid'):
self.appid = '10001'
url = 'https://cactus.jd.com/request_algo?g_ty=ajax'
headers = {
'Authority': 'cactus.jd.com',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache',
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, '
'like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
'Content-Type': 'application/json',
'Origin': 'https://st.jingxi.com',
'Sec-Fetch-Site': 'cross-site',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Referer': 'https://st.jingxi.com/',
'Accept-Language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7'
}
body = {
"version": "1.0",
"fp": self.fp,
"appId": self.appid,
"timestamp": int(time.time() * 1000),
"platform": "web",
"expandParams": ""
}
try:
async with aiohttp.ClientSession(cookies=self.cookies, headers=headers) as session:
response = await session.post(url=url, data=json.dumps(body))
text = await response.text()
data = json.loads(text)
if data['status'] == 200:
self.token = data['data']['result']['tk']
self.random = re.search("rd='(.*)';", data['data']['result']['algo']).group(1)
algo = re.search(r'algo\.(.*)\(', data['data']['result']['algo']).group(1)
if algo.lower() in self.algo_map:
self.algo = self.algo_map[algo.lower()]
else:
algo = 'hmacsha512'
self.algo = self.algo_map[algo]
self.random = '5gkjB6SpmC9s'
self.token = 'tk01wcdf61cb3a8nYUtHcmhSUFFCfddDPRvKvYaMjHkxo6Aj7dhzO+GXGFa9nPXfcgT' \
'+mULoF1b1YIS1ghvSlbwhE0Xc'
println('{}, Token:{}!\n签名算法:{}!'.format(self.account, self.token, algo))
else:
println('{}, 获取签名算法失败!'.format(self.account))
except Exception as e:
println('{}, 获取签名算法失败, {}!'.format(self.account, e.args))
def generate_fp(self):
"""
生成获取签名算法参数的请求参数
"""
e = "0123456789"
a = 13
i = ''
while a > 0:
i += e[int(random.random() * len(e)) | 0]
a -= 1
i += str(int(time.time() * 100))
return i[0:16]
def jx_init(cls):
"""
京喜APP签名算法混入类
"""
def init(self, **kwargs):
"""
初始化方法
:param self:
:param kwargs:
:return:
"""
pt_pin = kwargs.get('pt_pin')
pt_key = kwargs.get('pt_key')
self.pin = pt_pin
self.algo_map = {
'md5': md5,
'hmacmd5': hmacMD5,
'sha256': sha256,
'hmacsha256': hmacSha256,
'sha512': sha512,
'hmacsha512': hmacSha512,
}
# 参数错误
if not pt_pin or not pt_key:
raise ValueError('Invalid Parameter!')
self.account = kwargs.get('account', None)
if not self.account:
self.account = unquote(pt_pin)
self.cookies = { # cookies
'pt_pin': pt_pin,
'pt_key': pt_key,
}
self.sort = kwargs.get('sort', 1)
self.message = ''
cls.__init__ = init
cls.get_encrypt = get_encrypt
cls.encrypt = encrypt
cls.generate_fp = generate_fp
return cls