-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunicornsdk.py
358 lines (304 loc) · 11.3 KB
/
unicornsdk.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from __future__ import annotations
import asyncio
from typing import Optional, TYPE_CHECKING
from enum import Enum
import urllib3
urllib3.disable_warnings()
import gzip
import base64
from datetime import datetime
from requests_futures.sessions import FuturesSession
import requests
from dateutil import parser
import configparser
from loguru import logger
def parse_from_iso8601_to_local(dt_str):
if not dt_str:
return None
return parser.isoparse(dt_str).astimezone()
def now_time_s():
return int(datetime.now().timestamp())
class PlatForm(str, Enum):
WINDOWS = "WINDOWS"
ANDROID = "ANDROID"
IOS = "IOS"
OSX = "OSX"
class KpsdkVersion(str, Enum):
v20210513 = "v20210513"
v202107 = "v202107"
class UnicornSDK:
AUTH = {
"access_token": None
}
CFG = configparser.ConfigParser(interpolation=None)
def __init__(self, token=None, api_url="https://us.unicorn-bot.com"):
self._api_url = api_url
# self._api_url = "http://127.0.0.1:9000"
self._CLIENT = FuturesSession()
self._XSESSIONDATA = None
self._token = token
self.device_info = None
def get_proxys_for_sdk(self):
cur_proxyuri = "http://127.0.0.1:8888"
proxies = {
"http": cur_proxyuri,
"https": cur_proxyuri,
}
return proxies
# return None
def _get_client(self):
return self._CLIENT
def get_authorization(self):
return {
"Authorization": "Bearer " + self.access_token
}
@property
def XSESSIONDATA(self):
"""
get back the XSESSIONDATA for save
:return:
"""
return self._XSESSIONDATA
@XSESSIONDATA.setter
def XSESSIONDATA(self, v):
self._XSESSIONDATA = v
@property
def access_token(self):
return self._token or self.AUTH.get("access_token")
@property
def api_url(self):
return self._api_url or self.CFG.get("api_url")
@classmethod
def load_settings(cls, CFG_PATH):
cls.CFG.read(CFG_PATH)
@classmethod
def save_settings(cls, CFG_PATH):
with open(CFG_PATH, "w", encoding="utf8") as f:
cls.CFG.write(f)
@classmethod
def check_auth(cls, CFG_PATH):
cls.load_settings(CFG_PATH)
settings = cls.CFG
do_login = False
if not settings["auth"].get("access_token"):
do_login = True
if not settings["auth"].get("refresh_token_expiration") or parse_from_iso8601_to_local(
settings["auth"]["refresh_token_expiration"]).timestamp() - now_time_s() < 3600 * 24:
do_login = True
# 剩余时长小于一小时直接刷新一个
if not settings["auth"].get("access_token_expiration") or parse_from_iso8601_to_local(
settings["auth"]["access_token_expiration"]).timestamp() - now_time_s() < 3600:
do_login = True
if do_login:
if not settings["auth"].get("username") or not settings["auth"].get("password"):
raise Exception("missing auth info!")
cls.login(settings)
cls.set_auth(access_token=settings["auth"]["access_token"])
cls.save_settings(CFG_PATH)
@classmethod
def login(cls, settings=None):
assert settings
assert settings["auth"]["username"]
assert settings["auth"]["password"]
param = {
"username": settings["auth"]["username"],
# "email": settings["auth"].get("email"),
"password": settings["auth"]["password"],
}
api_url = settings["auth"]["api_url"]
resp = requests.post(api_url + "/auth/login/", json=param, timeout=30)
if resp.status_code == 200:
j = resp.json()
settings["auth"]["access_token"] = j["access_token"]
settings["auth"]["refresh_token"] = j["refresh_token"]
settings["auth"]["access_token_expiration"] = j["access_token_expiration"]
settings["auth"]["refresh_token_expiration"] = j["refresh_token_expiration"]
else:
raise Exception(f"Login Faild! {resp.text}")
@classmethod
def set_auth(cls, *, access_token):
cls.AUTH["access_token"] = access_token
async def init_session(self, sessionid: str, platform: PlatForm, locale="zh_CN"):
url = self.api_url + "/api/session/init/"
params = {
"sessionid": sessionid,
"platform": platform,
"locale": locale,
}
headers = self.get_authorization()
resp = await asyncio.wrap_future(self._get_client().post(url, json=params, headers=headers, verify=False, proxies=self.get_proxys_for_sdk()))
if resp.status_code != 200:
raise Exception(resp.text)
self._XSESSIONDATA = resp.cookies["XSESSIONDATA"]
self.device_info = resp.json()
return self.device_info
def load_state(self, bundle):
self._XSESSIONDATA = bundle.get("XSESSIONDATA")
self.device_info = bundle.get("device_info")
def save_state(self, bundle):
bundle["XSESSIONDATA"] = self._XSESSIONDATA
bundle["device_info"] = self.device_info
async def kpsdk_parse_ips(self, ips_content, *, ver=KpsdkVersion.v202107, host, site=None, compress_method="GZIP",
proxy_uri=None, cookie=None, cookiename=None):
try:
gzipjps = gzip.compress(ips_content)
client = self._get_client()
param = {
"kpver": ver.value,
"host": host,
"site": site,
"proxy_uri": proxy_uri,
"compress_method": compress_method,
"cookie": cookie,
"cookiename": cookiename,
}
resp = await asyncio.wrap_future(client.post(self.api_url + "/api/kpsdk/ips/", params=param, headers={
"Authorization": "Bearer " + self.access_token
}, cookies={
"XSESSIONDATA": self.XSESSIONDATA
}, files={"ips_js": gzipjps}, verify=False, proxies=self.get_proxys_for_sdk()))
if resp.status_code == 200:
kpparam = resp.json()
tl_body_b64 = kpparam.get("tl_body_b64")
if tl_body_b64:
body = gzip.decompress(base64.b64decode(tl_body_b64))
kpparam["body"] = body
return kpparam
elif resp.status_code == 403:
raise Exception("Not Authenticated")
else:
logger.error(resp.text)
raise Exception(resp.text)
except Exception as e:
logger.error(repr(e))
raise e
async def kpsdk_answer(self, x_kpsdk_ct, x_kpsdk_st, st_diff, x_kpsdk_cr=True):
try:
param = {
"x_kpsdk_ct": x_kpsdk_ct,
"x_kpsdk_cr": x_kpsdk_cr,
"x_kpsdk_st": x_kpsdk_st,
"st_diff": st_diff,
}
client = self._get_client()
resp = await asyncio.wrap_future(client.post(self.api_url + "/api/kpsdk/answer/", headers={
"Authorization": "Bearer " + self.access_token
}, cookies={
"XSESSIONDATA": self.XSESSIONDATA
}, json=param, verify=False, proxies=self.get_proxys_for_sdk()))
if resp.status_code == 200:
kpparam = resp.json()
return kpparam
elif resp.status_code == 403:
raise Exception("Not Authenticated")
else:
logger.error(resp.text)
raise Exception(resp.text)
except Exception as e:
logger.error(repr(e))
raise e
def get_bmak(self, bmak_url, timezoneoffset=None):
return Bmak(
bmak_url=bmak_url,
timezoneoffset=timezoneoffset,
sdk=self
)
async def bmak_sensordata(
self, *,
forminfo="", forminfo_cns="", url=None, bmak_url=None, _abck=None, newpage=False,
timezoneoffset=None, input=None, element=None, element_type=None, click=False, move=None):
try:
param = {
"_abck": _abck,
"newpage": newpage,
"input": input,
"element": element,
"element_type": element_type,
"click": click,
"move": move,
}
if url is not None:
param["url"] = url
if bmak_url:
param["bmak_url"] = bmak_url
if forminfo is not None:
param["forminfo"] = forminfo
if forminfo_cns is not None:
param["forminfo_cns"] = forminfo_cns
if timezoneoffset:
param["timezoneoffset"] = timezoneoffset
client = self._get_client()
resp = await asyncio.wrap_future(client.post(self.api_url + "/api/bmak/sensordata/", json=param, headers={
"Authorization": "Bearer " + self.access_token
}, cookies={
"XSESSIONDATA": self.XSESSIONDATA
}, verify=False, proxies=self.get_proxys_for_sdk()))
if resp.status_code == 200:
return resp.json()
elif resp.status_code == 403:
raise Exception("Not Authenticated")
else:
logger.error(resp.text)
raise Exception(resp.text)
except Exception as e:
logger.error(repr(e))
raise e
class Bmak:
def __init__(self,
bmak_url,
timezoneoffset=None,
sdk:UnicornSDK=None):
self.sdk = sdk
self.bmak_url=bmak_url
self.timezoneoffset = timezoneoffset
self.newpage = True
def ab(self, str):
"""
hash element name or id
"""
if not str:
return -1
try:
t = 0
for r in str:
n = ord(r)
if n < 128:
t += n
return t
except Exception as e:
logger.error("ab() failed! " + str)
return -2
async def bmak_sensordata(
self, *,
url,
_abck=None,
input:str=None, element=None, element_type=None,
click=False,
move=None,
forminfo="", forminfo_cns="",
):
"""
gen sensor from api
:param url: current page url
:param _abck: current _abck cookie
:param input: generate keyboard event for the str, when use input, you should also set element / element_type
:param element: the name or id or the input element
:param element_type: only need when you want to generate input password event, set it to ”password“
:param click: generate click event
:param move: generate mouse move event, set to "short" / "long“
:param forminfo:
:param forminfo_cns:
:return:
"""
ret = await self.sdk.bmak_sensordata(
url=url,
bmak_url=self.bmak_url,
timezoneoffset=self.timezoneoffset,
newpage=self.newpage,
input=input, element=element, element_type=element_type,
click=click,
move=move,
forminfo=forminfo, forminfo_cns=forminfo_cns
)
self.newpage=False
return ret