Files
MoviePilot-Plugins/plugins/dynamicwechat/update_help.py
ramen a7b0c422bc 在原来“自动设置动态IP到企业微信应用可信IP”的基础上添加了两个第三方应用的api用于扫码登陆更新cookie.默认使用cookieCC登陆
配置两个第三方api主要缓解以下问题:
1. 需要经常在异地登陆使用企业微信的用户cookie经常失效,要异地电脑安装、配置cookieCC重新上传cookie的问题
2. 在仅有手机微信的情况下更新cookie相对麻烦
2024-10-16 00:01:32 +08:00

110 lines
3.6 KiB
Python

import base64
import hashlib
from typing import Dict, Any
import json
import requests
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from os import urandom
BLOCK_SIZE = 16
def pad(data: bytes) -> bytes:
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(data) + padder.finalize()
return padded_data
def unpad(data: bytes) -> bytes:
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
unpadded_data = unpadder.update(data) + unpadder.finalize()
return unpadded_data
def bytes_to_key(data: bytes, salt: bytes, output: int = 48) -> bytes:
assert len(salt) == 8, len(salt)
data += salt
key = hashlib.md5(data).digest()
final_key = key
while len(final_key) < output:
key = hashlib.md5(key + data).digest()
final_key += key
return final_key[:output]
def encrypt(message: bytes, passphrase: bytes) -> bytes:
salt = urandom(8)
key_iv = bytes_to_key(passphrase, salt, 32 + 16)
key = key_iv[:32]
iv = key_iv[32:]
# Create AES cipher object
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
encrypted_message = encryptor.update(pad(message)) + encryptor.finalize()
return base64.b64encode(b"Salted__" + salt + encrypted_message)
def decrypt(encrypted: bytes, passphrase: bytes) -> bytes:
encrypted = base64.b64decode(encrypted)
assert encrypted[0:8] == b"Salted__"
salt = encrypted[8:16]
key_iv = bytes_to_key(passphrase, salt, 32 + 16)
key = key_iv[:32]
iv = key_iv[32:]
# Create AES cipher object
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
decryptor = cipher.decryptor()
decrypted_message = decryptor.update(encrypted[16:]) + decryptor.finalize()
return unpad(decrypted_message)
class PyCookieCloud:
def __init__(self, url: str, uuid: str, password: str):
self.url: str = url
self.uuid: str = uuid
self.password: str = password
def check_connection(self) -> bool:
"""
Test the connection to the CookieCloud server.
:return: True if the connection is successful, False otherwise.
"""
try:
resp = requests.get(self.url)
if resp.status_code == 200:
return True
else:
return False
except Exception as e:
return False
def update_cookie(self, cookie: Dict[str, Any]) -> bool:
"""
Update cookie data to CookieCloud.
:param cookie: cookie value to update, if this cookie does not contain 'cookie_data' key, it will be added into 'cookie_data'.
:return: if update success, return True, else return False.
"""
if 'cookie_data' not in cookie:
cookie = {'cookie_data': cookie}
raw_data = json.dumps(cookie)
encrypted_data = encrypt(raw_data.encode('utf-8'), self.get_the_key().encode('utf-8')).decode('utf-8')
cookie_cloud_request = requests.post(self.url + '/update', json={'uuid': self.uuid, 'encrypted': encrypted_data})
if cookie_cloud_request.status_code == 200:
if cookie_cloud_request.json()['action'] == 'done':
return True
return False
def get_the_key(self) -> str:
"""
Get the key used to encrypt and decrypt data.
:return: the key.
"""
md5 = hashlib.md5()
md5.update((self.uuid + '-' + self.password).encode('utf-8'))
return md5.hexdigest()[:16]