-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskyland.py
328 lines (272 loc) · 10.4 KB
/
skyland.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
import hashlib
import hmac
import json
import os.path
import time
from getpass import getpass
import logging
from urllib import parse
import requests
from datetime import date
token_save_name = 'TOKEN.txt'
app_code = '4ca99fa6b56cc2ba'
token_env = os.environ.get('TOKEN')
sign_token = ''
header = {
'cred': '',
'User-Agent': 'Skland/1.0.1 (com.hypergryph.skland; build:100001014; Android 31; ) Okhttp/4.11.0',
'Accept-Encoding': 'gzip',
'Connection': 'close'
}
header_login = {
'User-Agent': 'Skland/1.0.1 (com.hypergryph.skland; build:100001014; Android 31; ) Okhttp/4.11.0',
'Accept-Encoding': 'gzip',
'Connection': 'close'
}
# 签名请求头一定要这个顺序,否则失败
# timestamp是必填的,其它三个随便填,不要为none即可
header_for_sign = {
'platform': '',
'timestamp': '',
'dId': '',
'vName': ''
}
# 签到url
sign_url = "https://zonai.skland.com/api/v1/game/attendance"
# 绑定的角色url
binding_url = "https://zonai.skland.com/api/v1/game/player/binding"
# 验证码url
login_code_url = "https://as.hypergryph.com/general/v1/send_phone_code"
# 验证码登录
token_phone_code_url = "https://as.hypergryph.com/user/auth/v2/token_by_phone_code"
# 密码登录
token_password_url = "https://as.hypergryph.com/user/auth/v1/token_by_phone_password"
# 使用token获得认证代码
grant_code_url = "https://as.hypergryph.com/user/oauth2/v2/grant"
# 使用认证代码获得cred
cred_code_url = "https://zonai.skland.com/api/v1/user/auth/generate_cred_by_code"
def config_logger():
current_date = date.today().strftime('%Y-%m-%d')
if not os.path.exists('logs'):
os.mkdir('logs')
logger = logging.getLogger()
file_handler = logging.FileHandler(f'./logs/{current_date}.log', encoding='utf-8')
logger.addHandler(file_handler)
logging.getLogger().setLevel(logging.DEBUG)
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
def filter_code(text):
filter_key = ['code', 'cred', 'token']
try:
j = json.loads(text)
if not j.get('data'):
return text
data = j['data']
for i in filter_key:
if i in data:
data[i] = '*****'
return json.dumps(j, ensure_ascii=False)
except:
return text
_get = requests.get
_post = requests.post
def get(*args, **kwargs):
response = _get(*args, **kwargs)
logger.info(f'GET {args[0]} - {response.status_code} - {filter_code(response.text)}')
return response
def post(*args, **kwargs):
response = _post(*args, **kwargs)
logger.info(f'POST {args[0]} - {response.status_code} - {filter_code(response.text)}')
return response
# 替换 requests 中的方法
requests.get = get
requests.post = post
def generate_signature(token: str, path, body_or_query):
"""
获得签名头
接口地址+方法为Get请求?用query否则用body+时间戳+ 请求头的四个重要参数(dId,platform,timestamp,vName).toJSON()
将此字符串做HMAC加密,算法为SHA-256,密钥token为请求cred接口会返回的一个token值
再将加密后的字符串做MD5即得到sign
:param token: 拿cred时候的token
:param path: 请求路径(不包括网址)
:param body_or_query: 如果是GET,则是它的query。POST则为它的body
:return: 计算完毕的sign
"""
# 总是说请勿修改设备时间,怕不是yj你的服务器有问题吧,所以这里特地-2
t = str(int(time.time()) - 2)
token = token.encode('utf-8')
header_ca = json.loads(json.dumps(header_for_sign))
header_ca['timestamp'] = t
header_ca_str = json.dumps(header_ca, separators=(',', ':'))
s = path + body_or_query + t + header_ca_str
hex_s = hmac.new(token, s.encode('utf-8'), hashlib.sha256).hexdigest()
md5 = hashlib.md5(hex_s.encode('utf-8')).hexdigest().encode('utf-8').decode('utf-8')
logging.info(f'算出签名: {md5}')
return md5, header_ca
def get_sign_header(url: str, method, body, old_header):
h = json.loads(json.dumps(old_header))
p = parse.urlparse(url)
if method.lower() == 'get':
h['sign'], header_ca = generate_signature(sign_token, p.path, p.query)
else:
h['sign'], header_ca = generate_signature(sign_token, p.path, json.dumps(body))
for i in header_ca:
h[i] = header_ca[i]
return h
def login_by_code():
phone = input('请输入手机号码:')
resp = requests.post(login_code_url, json={'phone': phone, 'type': 2}, headers=header_login).json()
if resp.get("status") != 0:
raise Exception(f"发送手机验证码出现错误:{resp['msg']}")
code = input("请输入手机验证码:")
r = requests.post(token_phone_code_url, json={"phone": phone, "code": code}, headers=header_login).json()
return get_token(r)
def login_by_token():
token_code = input("请输入(登录森空岛电脑官网后请访问这个网址:https://web-api.skland.com/account/info/hg):")
return parse_user_token(token_code)
def parse_user_token(t):
try:
t = json.loads(t)
return t['data']['content']
except:
pass
return t
def login_by_password():
phone = input('请输入手机号码:')
password = getpass('请输入密码:')
r = requests.post(token_password_url, json={"phone": phone, "password": password}, headers=header_login).json()
return get_token(r)
def get_cred_by_token(token):
grant_code = get_grant_code(token)
return get_cred(grant_code)
def get_token(resp):
if resp.get('status') != 0:
raise Exception(f'获得token失败:{resp["msg"]}')
return resp['data']['token']
def get_grant_code(token):
response = requests.post(grant_code_url, json={
'appCode': app_code,
'token': token,
'type': 0
}, headers=header_login)
resp = response.json()
if response.status_code != 200:
raise Exception(f'获得认证代码失败:{resp}')
if resp.get('status') != 0:
raise Exception(f'获得认证代码失败:{resp["msg"]}')
return resp['data']['code']
def get_cred(grant):
resp = requests.post(cred_code_url, json={
'code': grant,
'kind': 1
}, headers=header_login).json()
if resp['code'] != 0:
raise Exception(f'获得cred失败:{resp["message"]}')
return resp['data']
def get_binding_list():
v = []
resp = requests.get(binding_url, headers=get_sign_header(binding_url, 'get', None, header)).json()
if resp['code'] != 0:
print(f"请求角色列表出现问题:{resp['message']}")
if resp.get('message') == '用户未登录':
print(f'用户登录可能失效了,请重新运行此程序!')
os.remove(token_save_name)
return []
for i in resp['data']['list']:
if i.get('appCode') != 'arknights':
continue
v.extend(i.get('bindingList'))
return v
def list_awards(game_id, uid):
resp = requests.get(sign_url, headers=header, params={'gameId': game_id, 'uid': uid}).json()
print(resp)
def do_sign(cred_resp):
# 加到全局里去吧,反正没有多线程
global sign_token
sign_token = cred_resp['token']
header['cred'] = cred_resp['cred']
characters = get_binding_list()
for i in characters:
body = {
'gameId': 1,
'uid': i.get('uid')
}
# list_awards(1, i.get('uid'))
resp = requests.post(sign_url, headers=get_sign_header(sign_url, 'post', body, header), json=body).json()
if resp['code'] != 0:
print(f'角色{i.get("nickName")}({i.get("channelName")})签到失败了!原因:{resp.get("message")}')
continue
awards = resp['data']['awards']
for j in awards:
res = j['resource']
print(
f'角色{i.get("nickName")}({i.get("channelName")})签到成功,获得了{res["name"]}×{j.get("count") or 1}'
)
def save(token):
with open(token_save_name, 'w') as f:
f.write(token)
print(
f'您的鹰角网络通行证已经保存在{token_save_name}, 打开这个可以把它复制到云函数服务器上执行!\n如果需要再次运行,删除创建的这个文件即可')
def read(path):
v = []
with open(path, 'r', encoding='utf-8') as f:
for i in f.readlines():
i = i.strip()
i and i not in v and v.append(i)
return v
def read_from_env():
v = []
token_list = token_env.split(',')
for i in token_list:
i = i.strip()
if i and i not in v:
v.append(parse_user_token(i))
print(f'从环境变量中读取到{len(v)}个token...')
return v
def do_init():
if token_env:
print('使用环境变量里面的token')
# 对于github action,不需要存储token,因为token在环境变量里
return read_from_env()
# 检测文件里是否有token
if os.path.exists(token_save_name):
v = read(token_save_name)
if v:
return v
# 没有的话
token = ''
print("请输入你需要做什么:")
print("1.使用用户名密码登录(非常推荐,但可能因为人机验证失败)")
print("2.使用手机验证码登录(非常推荐,但可能因为人机验证失败)")
print("3.手动输入鹰角网络通行证账号登录(推荐)")
mode = input('请输入(1,2,3):')
if mode == '' or mode == '1':
token = login_by_password()
elif mode == '2':
token = login_by_code()
elif mode == '3':
token = login_by_token()
else:
exit(-1)
save(token)
return [token]
def start():
token = do_init()
for i in token:
try:
do_sign(get_cred_by_token(i))
except Exception as ex:
print(f'签到失败,原因:{str(ex)}')
logging.error('', exc_info=ex)
print("签到完成!")
if __name__ == '__main__':
print('本项目源代码仓库:https://github.com/xxyz30/skyland-auto-sign(已被github官方封禁)')
print('https://gitee.com/FancyCabbage/skyland-auto-sign')
config_logger()
logging.info('=========starting==========')
start_time = time.time()
start()
end_time = time.time()
logging.info(f'complete with {(end_time - start_time) * 1000} ms')
logging.info('===========ending============')