init
This commit is contained in:
16
utils/aes.py
Normal file
16
utils/aes.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import base64
|
||||
from Crypto.Cipher import AES
|
||||
from Crypto.Util.Padding import pad, unpad
|
||||
|
||||
|
||||
def encrypt_ecb(key: str, data: str):
|
||||
cipher = AES.new(bytes.fromhex(key.replace(" ", "")), AES.MODE_ECB)
|
||||
encrypted = cipher.encrypt(pad(data.encode(), AES.block_size))
|
||||
return base64.b64encode(encrypted).decode()
|
||||
|
||||
|
||||
def decrypt_ecb(key: str, data: str):
|
||||
data = base64.b64decode(data)
|
||||
cipher = AES.new(bytes.fromhex(key.replace(" ", "")), AES.MODE_ECB)
|
||||
decrypted = cipher.decrypt(data)
|
||||
return unpad(decrypted, AES.block_size).decode()
|
||||
42
utils/config.py
Normal file
42
utils/config.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from dataclasses import asdict, dataclass
|
||||
import os
|
||||
import json
|
||||
from dacite import from_dict
|
||||
|
||||
|
||||
@dataclass
|
||||
class ServerConfig:
|
||||
IP: str
|
||||
Port: int
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConfigData:
|
||||
LogLevel: str
|
||||
GameServer: ServerConfig
|
||||
SDKServer: ServerConfig
|
||||
RegionName: str
|
||||
def write_default_config():
|
||||
config = ConfigData(
|
||||
LogLevel="INFO",
|
||||
GameServer=ServerConfig(IP="127.0.0.1", Port=23301),
|
||||
SDKServer=ServerConfig(IP="127.0.0.1", Port=21000),
|
||||
RegionName="NeonSR",
|
||||
)
|
||||
with open("Config.json", "w") as f:
|
||||
f.write(json.dumps(asdict(config), indent=2))
|
||||
|
||||
return config
|
||||
|
||||
def load():
|
||||
if not os.path.exists("Config.json"):
|
||||
return ConfigData.write_default_config()
|
||||
|
||||
with open("Config.json", "r", encoding="utf-8") as f:
|
||||
try:
|
||||
return from_dict(ConfigData, json.load(f))
|
||||
except Exception:
|
||||
return ConfigData.write_default_config()
|
||||
|
||||
|
||||
Config : ConfigData = ConfigData.load()
|
||||
25
utils/crypto.py
Normal file
25
utils/crypto.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os
|
||||
import hashlib
|
||||
import base64
|
||||
from datetime import datetime
|
||||
|
||||
class Crypto:
|
||||
@staticmethod
|
||||
def create_session_key(account_uid: str) -> str:
|
||||
random_bytes = os.urandom(64)
|
||||
temp = f"{account_uid}.{datetime.now().timestamp()}.{random_bytes.hex()}"
|
||||
try:
|
||||
hash_bytes = hashlib.sha512(temp.encode('utf-8')).digest()
|
||||
return base64.b64encode(hash_bytes).decode('utf-8')
|
||||
except Exception as e:
|
||||
hash_bytes = hashlib.sha512(temp.encode('utf-8')).digest()
|
||||
return base64.b64encode(hash_bytes).decode('utf-8')
|
||||
|
||||
|
||||
def generate_dispatch_token(uid) -> str:
|
||||
dispatch_token = Crypto.create_session_key(uid)
|
||||
return dispatch_token
|
||||
|
||||
def generate_combo_token(uid) -> str:
|
||||
dispatch_token = Crypto.create_session_key(uid)
|
||||
return dispatch_token
|
||||
50
utils/logger.py
Normal file
50
utils/logger.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import sys
|
||||
import os
|
||||
from datetime import datetime
|
||||
from loguru import logger
|
||||
from utils.config import Config
|
||||
|
||||
logger.remove()
|
||||
|
||||
LevelList = ["ERROR", "WARNING", "INFO", "DEBUG"]
|
||||
CodeColorDict = {"ERROR": "red", "WARNING": "yellow", "INFO": "green", "DEBUG": "blue"}
|
||||
|
||||
|
||||
def custom_format(record):
|
||||
color = CodeColorDict[record["level"].name]
|
||||
return f"<{color}>{record['level'].name}</{color}> : {record['message']}\n"
|
||||
|
||||
logger.add(sys.stdout, format=custom_format, colorize=True, level=Config.LogLevel)
|
||||
|
||||
def get_new_log_path():
|
||||
today = datetime.now().strftime("%Y-%m-%d")
|
||||
log_dir = "logs"
|
||||
os.makedirs(log_dir, exist_ok=True)
|
||||
|
||||
count = 1
|
||||
while True:
|
||||
filename = f"{today}-{count}.log"
|
||||
filepath = os.path.join(log_dir, filename)
|
||||
if not os.path.exists(filepath):
|
||||
return filepath
|
||||
count += 1
|
||||
|
||||
|
||||
log_file_path = get_new_log_path()
|
||||
logger.add(log_file_path, level=Config.LogLevel, format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}")
|
||||
|
||||
def Log(msg, types):
|
||||
if types in CodeColorDict and LevelList.index(types) <= LevelList.index(Config.LogLevel):
|
||||
getattr(logger, types.lower())(msg)
|
||||
|
||||
def Error(msg):
|
||||
Log(msg, "ERROR")
|
||||
|
||||
def Warn(msg):
|
||||
Log(msg, "WARNING")
|
||||
|
||||
def Info(msg):
|
||||
Log(msg, "INFO")
|
||||
|
||||
def Debug(msg):
|
||||
Log(msg, "DEBUG")
|
||||
4
utils/time.py
Normal file
4
utils/time.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from time import time
|
||||
|
||||
def cur_timestamp_ms():
|
||||
return int((time() * 1000))
|
||||
Reference in New Issue
Block a user