forked from Jasonliu-0/Newapi-checkin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckin.py
More file actions
616 lines (515 loc) · 21.8 KB
/
Copy pathcheckin.py
File metadata and controls
616 lines (515 loc) · 21.8 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
NewAPI 自动签到脚本
支持多账号签到,通过 GitHub Actions 定时执行
"""
import os
import sys
import json
import base64
import requests
from datetime import datetime
from typing import Optional
try:
from cf_bypass import detect_cloudflare_block, CloudflareBypasser
CF_BYPASS_AVAILABLE = True
except ImportError:
CF_BYPASS_AVAILABLE = False
detect_cloudflare_block = None
CloudflareBypasser = None
try:
from dingtalk_notifier import send_checkin_notification
except ImportError:
send_checkin_notification = None
class NewAPICheckin:
"""NewAPI 签到类"""
@staticmethod
def _mask_url(url: str) -> str:
"""
脱敏 URL,隐藏域名细节
例如: https://api.example.com -> https://api.***.**
"""
try:
from urllib.parse import urlparse
parsed = urlparse(url)
domain_parts = parsed.netloc.split('.')
if len(domain_parts) >= 2:
# 保留第一部分和最后一部分,中间用 *** 代替
masked_domain = f"{domain_parts[0]}.***." + '.'.join(domain_parts[-1:])
else:
masked_domain = '***'
return f"{parsed.scheme}://{masked_domain}"
except Exception:
return 'https://***'
@staticmethod
def _mask_user_id(user_id: str) -> str:
"""
脱敏用户ID
例如: 1429 -> ****
"""
return '****'
def __init__(self, base_url: str, session_cookie: str, user_id: str = None, cf_clearance: str = None):
self.base_url = base_url.rstrip('/')
self.session_cookie = session_cookie
self.original_cf_clearance = cf_clearance
self.cf_bypassed = False
self.session = requests.Session()
self.session.cookies.set('session', session_cookie)
if cf_clearance:
self.session.cookies.set('cf_clearance', cf_clearance)
self.session.headers.update({
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Cache-Control': 'no-store',
'Pragma': 'no-cache',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
})
if user_id:
self.user_id = user_id
self.session.headers.update({'new-api-user': str(user_id)})
else:
self.user_id = self._extract_user_id_from_session(session_cookie)
if self.user_id:
self.session.headers.update({'new-api-user': str(self.user_id)})
def _extract_user_id_from_session(self, session_cookie: str) -> Optional[str]:
"""
从 Session Cookie 中提取用户ID
Session Cookie 格式通常是 Base64 编码的数据
"""
try:
# 尝试解码 Session Cookie
# Session 格式类似:MTc2NzQxMzYzM3xE...
# 解码后可能包含用户信息
decoded = base64.b64decode(session_cookie + '==') # 添加 padding
decoded_str = decoded.decode('utf-8', errors='ignore')
# 查找可能的用户ID模式
# 例如:linuxdo_988 中的 988
import re
# 查找 "linuxdo_数字" 或 "id"=数字 等模式
patterns = [
r'linuxdo[_-](\d+)', # linuxdo_988
r'"id"[:\s]+(\d+)', # "id": 988
r'user[_-](\d+)', # user_988
r'userid[:\s]+(\d+)', # userid: 988
]
for pattern in patterns:
match = re.search(pattern, decoded_str, re.IGNORECASE)
if match:
return match.group(1)
except Exception:
pass
return None
def get_user_info(self, verbose: bool = False) -> Optional[dict]:
"""
获取用户信息
自动设置 new-api-user 请求头
Args:
verbose: 是否显示详细调试信息
"""
try:
resp = self.session.get(f'{self.base_url}/api/user/self', timeout=30)
if verbose:
print(f' [调试] HTTP 状态码: {resp.status_code}')
print(f' [调试] 响应内容预览: {resp.text[:200]}...')
# 检查认证失败
if resp.status_code == 401:
print(f'[错误] 认证失败 (401): Session 可能已过期')
if verbose:
print(f' [调试] 完整响应: {resp.text[:500]}')
return None
# 尝试解析 JSON
try:
data = resp.json()
except json.JSONDecodeError as e:
# 检测是否是 Cloudflare 拦截
if detect_cloudflare_block:
is_blocked, reason = detect_cloudflare_block(resp.status_code, resp.text)
if is_blocked:
print(f'[CF] 获取用户信息时检测到 Cloudflare 拦截: {reason}')
print(f'[CF] 该站点需要 CF 绕过才能访问')
return None
print(f'[错误] 响应格式错误 (HTTP {resp.status_code}): 无法解析 JSON')
if verbose:
print(f' [调试] 原始响应: {resp.text[:500]}')
return None
if verbose:
print(f' [调试] success 字段: {data.get("success")}')
print(f' [调试] message 字段: {data.get("message")}')
if resp.status_code == 200:
if data.get('success'):
user_data = data.get('data')
# 保存用户ID并设置到请求头
if user_data and 'id' in user_data:
self.user_id = user_data['id']
self.session.headers.update({
'new-api-user': str(self.user_id)
})
return user_data
else:
if verbose:
print(f' [调试] API 返回失败: {data.get("message", "未知错误")}')
else:
print(f'[错误] HTTP {resp.status_code}: {data.get("message", "未知错误")}')
return None
except requests.exceptions.Timeout:
print(f'[错误] 请求超时')
return None
except requests.exceptions.RequestException as e:
print(f'[错误] 网络请求失败: {e}')
return None
except Exception as e:
print(f'[错误] 未知错误: {e}')
if verbose:
import traceback
traceback.print_exc()
return None
def checkin(self) -> dict:
"""
执行签到
流程(借鉴 Chrome 扩展 background.js:115-248):
1. requests 直连签到(快速模式)
2. CF 拦截检测 → Playwright 获取 cookie 后重新签到
3. 仍然失败 → Playwright 浏览器内直接签到(终极回退)
Returns:
签到结果字典
"""
result = {
'success': False,
'message': '',
'checkin_date': None,
'quota_awarded': None
}
try:
resp = self.session.post(f'{self.base_url}/api/user/checkin', timeout=30)
if resp.status_code == 401:
result['message'] = '认证失败: Session 可能已过期,请重新获取'
return result
try:
data = resp.json()
except json.JSONDecodeError:
if detect_cloudflare_block:
is_blocked, reason = detect_cloudflare_block(resp.status_code, resp.text)
if is_blocked:
print(f'[CF] 检测到 Cloudflare 拦截: {reason}')
return self._cf_bypass_checkin()
content_preview = resp.text[:200] if resp.text else '(空响应)'
result['message'] = f'响应格式错误 (HTTP {resp.status_code}): {content_preview}'
return result
if detect_cloudflare_block and resp.status_code in (403, 503):
is_blocked, reason = detect_cloudflare_block(resp.status_code, json.dumps(data))
if is_blocked:
print(f'[CF] 检测到 Cloudflare 拦截: {reason}')
return self._cf_bypass_checkin()
if resp.status_code == 200:
if data.get('success'):
result['success'] = True
result['message'] = data.get('message', '签到成功')
checkin_data = data.get('data', {})
result['checkin_date'] = checkin_data.get('checkin_date')
result['quota_awarded'] = checkin_data.get('quota_awarded')
else:
result['message'] = data.get('message', '签到失败')
else:
result['message'] = f'HTTP {resp.status_code}: {data.get("message", "未知错误")}'
except requests.exceptions.Timeout:
result['message'] = '请求超时'
except requests.exceptions.RequestException as e:
result['message'] = f'网络请求失败: {e}'
except Exception as e:
result['message'] = f'未知错误: {e}'
return result
def _cf_bypass_checkin(self) -> dict:
"""
CF 绕过签到流程
在同一个 Playwright 会话中完成 CF 绕过 + 签到,
不拆分 cookie 提取和 requests 重试(因为 cf_clearance 绑定浏览器指纹)
"""
result = {
'success': False,
'message': '',
'checkin_date': None,
'quota_awarded': None
}
if not CF_BYPASS_AVAILABLE or not CloudflareBypasser:
result['message'] = 'Cloudflare 拦截: 需安装 Playwright 才能自动绕过 (pip install playwright && playwright install chromium)'
return result
bypasser = CloudflareBypasser(self.base_url, self.session_cookie, self.user_id)
if not bypasser.is_available():
result['message'] = 'Cloudflare 拦截: Playwright 未正确安装'
return result
print('[CF] 开始 Playwright 绕过流程...')
browser_result = bypasser.bypass_and_checkin()
if not browser_result:
result['message'] = 'Cloudflare 绕过失败: 无法通过 CF 验证'
return result
self.cf_bypassed = True
if browser_result.get('error'):
result['message'] = f'CF 绕过后签到失败: {browser_result["error"]}'
return result
if browser_result.get('alreadyCheckedIn'):
result['success'] = True
result['message'] = browser_result.get('message', '今日已签到 (CF绕过)')
elif browser_result.get('success'):
result['success'] = True
result['message'] = browser_result.get('message', '签到成功 (CF绕过)')
data = browser_result.get('data', {})
if isinstance(data, dict):
checkin_data = data.get('data', data)
result['checkin_date'] = checkin_data.get('checkin_date')
result['quota_awarded'] = checkin_data.get('quota_awarded')
else:
result['message'] = browser_result.get('message', 'CF 绕过后签到失败')
return result
def get_checkin_history(self, month: str = None) -> Optional[dict]:
"""
获取签到历史
Args:
month: 月份,格式 YYYY-MM,默认当前月
"""
if month is None:
month = datetime.now().strftime('%Y-%m')
try:
resp = self.session.get(
f'{self.base_url}/api/user/checkin',
params={'month': month},
timeout=30
)
if resp.status_code == 200:
data = resp.json()
if data.get('success'):
return data.get('data')
return None
except Exception as e:
print(f'[错误] 获取签到历史失败: {e}')
return None
def parse_accounts(accounts_str: str) -> list:
"""
解析账号配置
支持格式:
1. 单账号: BASE_URL#SESSION_COOKIE
2. 多账号: BASE_URL1#SESSION1,BASE_URL2#SESSION2
3. JSON格式: [{"url": "...", "session": "..."}]
"""
accounts = []
if not accounts_str:
return accounts
# 尝试 JSON 格式
try:
data = json.loads(accounts_str)
if isinstance(data, list):
for item in data:
if isinstance(item, dict) and 'url' in item and 'session' in item:
account = {
'url': item['url'],
'session': item['session'],
'name': item.get('name', '')
}
# 如果提供了 user_id,添加到账号信息中
if 'user_id' in item:
account['user_id'] = item['user_id']
# 如果提供了 cf_clearance,添加到账号信息中
if 'cf_clearance' in item:
account['cf_clearance'] = item['cf_clearance']
accounts.append(account)
return accounts
except json.JSONDecodeError:
pass
# 简单格式: URL#SESSION,URL#SESSION
for part in accounts_str.split(','):
part = part.strip()
if '#' in part:
url, session = part.split('#', 1)
accounts.append({
'url': url.strip(),
'session': session.strip(),
'name': ''
})
return accounts
def load_config_from_cloud(config_url: str, config_auth: str = None) -> Optional[str]:
"""
从云端(WebDAV)加载配置
支持:
- 坚果云 WebDAV
- 群晖 NAS WebDAV
- NextCloud WebDAV
- 任何支持 WebDAV/直接链接的云存储
Args:
config_url: 配置文件 URL (WebDAV 或直接下载链接)
config_auth: 认证信息,格式:
- Basic Auth: "username:password"
- Token Auth: "token:your_token"
"""
try:
headers = {}
if config_auth:
if config_auth.startswith('token:'):
headers['Authorization'] = 'Bearer ' + config_auth[6:]
elif ':' in config_auth:
import base64 as b64mod
credentials = b64mod.b64encode(config_auth.encode('utf-8')).decode('utf-8')
headers['Authorization'] = 'Basic ' + credentials
print(f'[云端] 正在从云端加载配置: {NewAPICheckin._mask_url(config_url)}')
resp = requests.get(config_url, headers=headers, timeout=30)
if resp.status_code == 401:
print('[云端] 认证失败: 请检查 CONFIG_AUTH 配置')
return None
elif resp.status_code == 404:
print('[云端] 配置文件不存在: 请先通过配置生成器保存到云端')
return None
elif resp.status_code != 200:
print(f'[云端] 加载失败: HTTP {resp.status_code}')
return None
data = resp.json()
if isinstance(data, list):
accounts_str = json.dumps(data)
print(f'[云端] 成功加载 {len(data)} 个账号配置')
return accounts_str
elif isinstance(data, dict) and 'accounts' in data:
accounts = data['accounts']
accounts_str = json.dumps(accounts)
print(f'[云端] 成功加载 {len(accounts)} 个账号配置')
if data.get('dingtalk'):
dt = data['dingtalk']
if dt.get('webhook') and not os.environ.get('DINGTALK_WEBHOOK'):
os.environ['DINGTALK_WEBHOOK'] = dt['webhook']
if dt.get('secret') and not os.environ.get('DINGTALK_SECRET'):
os.environ['DINGTALK_SECRET'] = dt['secret']
if dt.get('webhook'):
print('[云端] 已从云端加载钉钉通知配置')
return accounts_str
else:
print('[云端] 配置格式错误: 无法解析账号列表')
return None
except json.JSONDecodeError:
print('[云端] 配置文件不是有效的 JSON 格式')
return None
except requests.exceptions.Timeout:
print('[云端] 请求超时')
return None
except requests.exceptions.RequestException as e:
print(f'[云端] 网络请求失败: {e}')
return None
except Exception as e:
print(f'[云端] 加载失败: {e}')
return None
def main():
"""主函数"""
import pytz
beijing_tz = pytz.timezone('Asia/Shanghai')
execution_time = datetime.now(beijing_tz).strftime("%Y-%m-%d %H:%M:%S")
print('=' * 50)
print('NewAPI 自动签到')
print(f'执行时间: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}')
print('=' * 50)
config_url = os.environ.get('CONFIG_URL', '')
config_auth = os.environ.get('CONFIG_AUTH', '')
accounts_str = ''
if config_url:
accounts_str = load_config_from_cloud(config_url, config_auth) or ''
if not accounts_str:
accounts_str = os.environ.get('NEWAPI_ACCOUNTS', '')
if not accounts_str:
print('[错误] 未配置账号信息')
print('请设置 CONFIG_URL(云端配置)或 NEWAPI_ACCOUNTS(本地配置)环境变量')
sys.exit(1)
accounts = parse_accounts(accounts_str)
if not accounts:
print('[错误] 账号配置解析失败')
sys.exit(1)
print(f'共 {len(accounts)} 个账号待签到\n')
success_count = 0
fail_count = 0
checkin_results = []
for i, account in enumerate(accounts, 1):
url = account['url']
session_cookie = account['session']
user_id = account.get('user_id') # 获取用户ID(如果提供)
cf_clearance = account.get('cf_clearance') # 获取 CF clearance(如果提供)
name = account.get('name') or f'账号{i}'
print(f'[{i}/{len(accounts)}] {name}')
print(f' 站点: {NewAPICheckin._mask_url(url)}')
if user_id:
print(f' 用户ID: {NewAPICheckin._mask_user_id(user_id)}')
client = NewAPICheckin(url, session_cookie, user_id, cf_clearance)
# 获取用户信息
user_info = client.get_user_info()
if user_info:
username = user_info.get('username', '未知')
# 用户名也脱敏,只显示前3个字符
masked_username = username[:3] + '***' if len(username) > 3 else '***'
print(f' 用户: {masked_username}')
else:
print(' 用户: 获取失败(可能 session 已过期)')
# 执行签到
result = client.checkin()
checkin_count = 0 # 默认值,避免历史接口失败时未定义
if result['success']:
success_count += 1
print(f' 结果: ✅ {result["message"]}')
# 显示签到日期
if result['checkin_date']:
print(f' 日期: {result["checkin_date"]}')
# 显示获得的额度(格式化显示)
if result['quota_awarded']:
quota = result['quota_awarded']
# 格式化额度显示
if quota >= 1000000:
quota_str = f'{quota / 1000000:.2f}M'
elif quota >= 1000:
quota_str = f'{quota / 1000:.2f}K'
else:
quota_str = str(quota)
print(f' 奖励: +{quota_str} 额度 ({quota:,} tokens)')
# 获取本月签到统计
history = client.get_checkin_history()
if history and history.get('stats'):
stats = history['stats']
checkin_count = stats.get('checkin_count', 0)
total_quota = stats.get('total_quota', 0)
if total_quota >= 1000000:
total_str = f'{total_quota / 1000000:.2f}M'
elif total_quota >= 1000:
total_str = f'{total_quota / 1000:.2f}K'
else:
total_str = str(total_quota)
print(f' 统计: 本月已签 {checkin_count} 天,累计 {total_str} 额度')
# 收集结果用于钉钉通知
account_result = {
'name': name,
'success': True,
'message': result['message'],
'quota_awarded': result.get('quota_awarded'),
'checkin_count': checkin_count
}
checkin_results.append(account_result)
else:
fail_count += 1
print(f' 结果: ❌ {result["message"]}')
# 收集结果用于钉钉通知
message = result.get('message', '')
account_result = {
'name': name,
'success': False,
'message': message,
'session_expired': 'session' in message.lower() or '认证' in message
}
checkin_results.append(account_result)
print()
# 汇总
print('=' * 50)
print(f'签到完成: 成功 {success_count}, 失败 {fail_count}')
print('=' * 50)
# 发送钉钉通知
if send_checkin_notification:
print('正在发送钉钉通知...')
send_checkin_notification(checkin_results, execution_time)
elif os.environ.get('DINGTALK_WEBHOOK'):
print('[警告] 已配置 DINGTALK_WEBHOOK 但无法导入通知模块')
# 如果全部失败则返回错误码
if fail_count == len(accounts):
sys.exit(1)
if __name__ == '__main__':
main()
# === DINGTALK NOTIFICATION PATCH ===
# This section was added to send DingTalk notifications