Skip to content

Instantly share code, notes, and snippets.

@uubulb
Created September 25, 2024 10:41
Show Gist options
  • Save uubulb/e00233ed077e7955f1fa521e63c793b8 to your computer and use it in GitHub Desktop.
Save uubulb/e00233ed077e7955f1fa521e63c793b8 to your computer and use it in GitHub Desktop.
ugreenforum.py
from argparse import ArgumentParser
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import base64
import hashlib
import requests
from requests.exceptions import RequestException, ConnectionError, Timeout
ENDPOINT = "https://cloud.ugreengroup.com/api"
session = requests.Session()
class UGreenData:
def __init__(self, username: str, password: str):
self._username = username
self._password = password
self.key = None
self.key_uuid = None
def _hash_password(self) -> str:
first_hash = hashlib.md5(self._password.encode('utf-8')).hexdigest()
second_hash = hashlib.md5(first_hash.encode('utf-8')).hexdigest()
return second_hash
def encrypt_from_key(self) -> dict:
encrypted_data = {}
cipher = AES.new(self.key.encode('utf-8'), AES.MODE_ECB)
hashed_password = self._hash_password()
for field, text in {'username': self._username, 'password': hashed_password}.items():
padded_text = pad(text.encode('utf-8'), AES.block_size)
ciphertext = cipher.encrypt(padded_text)
encrypted_data[field] = base64.b64encode(ciphertext).decode('utf-8')
return encrypted_data
def login_ugos(username: str, password: str) -> str:
ugreen = UGreenData(username=username, password=password)
request_key_url = f'{ENDPOINT}/user/v2/sa/encrypt/key'
request_token_url = f'{ENDPOINT}/oauth/token'
try:
response = session.get(request_key_url, timeout=10)
if response.status_code == 200:
data = response.json()
if data['code'] == 200:
ugreen.key = data['data']['encryptKey']
ugreen.key_uuid = data['data']['uuid']
else:
print(f"Error: {data['msg']}")
return ""
else:
print(f"Failed to retrieve data. Status code: {response.status_code}")
return ""
except (RequestException, ConnectionError, Timeout) as e:
print(f"Error during request to fetch encryption key: {e}")
return ""
try:
encrypted_data = ugreen.encrypt_from_key()
form_data = {
'username': encrypted_data['username'],
'password': encrypted_data['password'],
'uuid': ugreen.key_uuid,
'grant_type': 'password',
'bid': '912f0c74297c9543d367ecf8f27c3c29', # generated from browser fingerprint
'platform': '浏览器'
}
response = session.post(request_token_url, data=form_data, timeout=10)
if response.status_code == 200:
data = response.json()
if data['code'] == 200:
return data['data']['accessToken']['access_token']
else:
print(f"Error: {data['msg']}")
return ""
else:
print(f"Failed to retrieve token. Status code: {response.status_code}")
return ""
except (RequestException, ConnectionError, Timeout) as e:
print(f"Error during request to login: {e}")
return ""
return ""
def get_token(access_token: str) -> None:
forum_token_url = f'{ENDPOINT}/user/v2/auth/third'
headers = {
'Authorization': access_token
}
try:
response = session.get(forum_token_url, headers=headers)
if response.status_code == 200:
data = response.json()
if data['code'] == 200:
token = data['data']['token']
print(f'Forum login url: https://bbs.ugreengroup.com/?token={token}')
else:
print(f"Error: {data['msg']}")
else:
print(f"Failed to retrieve token. Status code: {response.status_code}")
except (RequestException, ConnectionError, Timeout) as e:
print(f"Error during request to login: {e}")
def main() -> None:
parser = ArgumentParser()
parser.add_argument('-u', '--username', type=str, required=True, help='绿联帐号用户名')
parser.add_argument('-p', '--password', type=str, required=True, help='绿联帐号密码')
args = parser.parse_args()
access_token = login_ugos(args.username, args.password)
if access_token:
get_token(access_token)
else:
print("Failed to retrieve access token.")
session.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment