Skip to content

Commit

Permalink
Fix Token Issue (#136)
Browse files Browse the repository at this point in the history
* Fix Token Issue

* Fix log output
  • Loading branch information
PianCat authored Nov 13, 2023
1 parent 9ae162c commit b340811
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ SOFTWARE.
```
# **鸣谢**
## 项目
感谢由 [Xiaomi-Community-AutoTask](https://github.com/CMDQ8575/Xiaomi-Community-AutoTask) 启发的部分相关功能代码
## 社区
本项目所有贡献者感谢所有Star了本项目的人
Expand Down
57 changes: 55 additions & 2 deletions miuitask.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
import time
import json
import hashlib
import base64
import random
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5

from urllib import request
from http import cookiejar
Expand Down Expand Up @@ -280,10 +286,14 @@ def check_in(self):
'version': 'dev.231026',
'miui_vip_ph': str(self.miui_vip_ph)
}
data = {
'miui_vip_ph': str(self.miui_vip_ph),
'token': get_token()
}
try:
response = requests.post(
'https://api.vip.miui.com/mtop/planet/vip/user/checkinV2',
headers=headers, params=params)
headers=headers, params=params, data=data)
r_json = response.json()
if r_json['status'] == 401:
return w_log("每日签到失败:Cookie无效")
Expand Down Expand Up @@ -511,7 +521,7 @@ def start(miui_task: MIUITask, check_in: bool, browse_post: bool, browse_user_pa
sleep_ten_sec_more()
miui_task.browse_specialpage()
else:
w_log("自动跳过模拟请求「" + str(special_page_task) + "」")
w_log("自动跳过模拟请求「浏览指定专题页」")

if "加入小米社区圈子" in task_status and task_status["加入小米社区圈子"].get("showType",
0) == 1 and board_follow:
Expand Down Expand Up @@ -564,6 +574,49 @@ def main():
def main_handler(event, context):
main()

# 随机字符
def random_str(length):
s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=_+~`{}[]|:<>?/.'
return ''.join(random.choice(s) for _ in range(length))

# AES加密
def aes_encrypt(key, data):
iv = '0102030405060708'.encode('utf-8')
cipher = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv)
padded_data = pad(data.encode('utf-8'), AES.block_size, style='pkcs7')
ciphertext = cipher.encrypt(padded_data)
return base64.b64encode(ciphertext).decode('utf-8')

# RSA加密
def rsa_encrypt(key, data):
public_key = RSA.import_key(key)
cipher = PKCS1_v1_5.new(public_key)
ciphertext = cipher.encrypt(base64.b64encode(data.encode('utf-8')))
return base64.b64encode(ciphertext).decode('utf-8')

# 获取Token
def get_token():
key = random_str(16)
public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArxfNLkuAQ/BYHzkzVwtu
g+0abmYRBVCEScSzGxJIOsfxVzcuqaKO87H2o2wBcacD3bRHhMjTkhSEqxPjQ/FE
XuJ1cdbmr3+b3EQR6wf/cYcMx2468/QyVoQ7BADLSPecQhtgGOllkC+cLYN6Md34
Uii6U+VJf0p0q/saxUTZvhR2ka9fqJ4+6C6cOghIecjMYQNHIaNW+eSKunfFsXVU
+QfMD0q2EM9wo20aLnos24yDzRjh9HJc6xfr37jRlv1/boG/EABMG9FnTm35xWrV
R0nw3cpYF7GZg13QicS/ZwEsSd4HyboAruMxJBPvK3Jdr4ZS23bpN0cavWOJsBqZ
VwIDAQAB
-----END PUBLIC KEY-----"""
data = '{"type":0,"startTs":' + str(round(time.time() * 1000)) + ',"endTs":' + str(round(
time.time() * 1000)) + ',"env":{"p1":"","p2":"","p3":"","p4":"","p5":"","p6":"","p7":"","p8":"","p9":"","p10":"","p11":"","p12":"","p13":"","p14":"","p15":"","p16":"","p17":"","p18":"","p19":5,"p20":"","p21":"","p22":5,"p23":"","p24":"","p25":"","p26":"","p28":"","p29":"","p30":"","p31":"","p32":"","p33":"","p34":""},"action":{"a1":[],"a2":[],"a3":[],"a4":[],"a5":[],"a6":[],"a7":[],"a8":[],"a9":[],"a10":[],"a11":[],"a12":[],"a13":[],"a14":[]},"force":false,"talkBack":false,"uid":"' + random_str(
27) + '","nonce":{"t":' + str(round(time.time())) + ',"r":' + str(
round(time.time())) + '},"version":"2.0","scene":"GROW_UP_CHECKIN"}'
s = rsa_encrypt(public_key, key)
d = aes_encrypt(key, data)
url = 'https://verify.sec.xiaomi.com/captcha/v2/data?k=3dc42a135a8d45118034d1ab68213073&locale=zh_CN'
data = {'s': s, 'd': d, 'a': 'GROW_UP_CHECKIN'}
result = requests.post(url=url, data=data).json()
if result['msg'] != '参数错误':
return result['data']['token']

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
requests>=2.25.1
python-dotenv>=0.19.2
PyYAML>=6.0
crypto
onepush

0 comments on commit b340811

Please sign in to comment.