Files
2026-05-17 11:58:41 +08:00

88 lines
3.0 KiB
Python

import uuid
import hmac
import hashlib
import json
import sys
import os
from datetime import datetime
from pathlib import Path
# --- SECURITY CONSTANTS ---
_SECRET_KEY = b"mxPIC_Super_Secret_Master_Key_2026!"
_ENV_VAR_NAME = "LIC_MXPIC_OPTIHK_DIR"
# --- THE DEVELOPER WHITELIST ---
# Add your team's MAC addresses here. Use uppercase.
_DEVELOPER_MACS = {
"D4:54:8B:F1:46:49", # Qin Yue's Workstation
"28:92:00:C9:13:E5", # Qin Yue's in HKSTP
}
def _get_local_mac() -> str:
"""Retrieves the physical MAC address of the current machine."""
mac = uuid.getnode()
return ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))
def _locate_license_file() -> Path:
"""Intelligently resolves the path to the license file."""
env_path_str = os.getenv(_ENV_VAR_NAME)
if env_path_str:
path_obj = Path(env_path_str)
if path_obj.is_dir():
return path_obj / "mxpic.lic"
return path_obj
else:
return Path("mxpic.lic")
def verify_license() -> None:
"""Verifies the license file. Halts execution if invalid."""
# 1. IMMEDIATE BYPASS: Check the Developer Whitelist first
local_mac = _get_local_mac()
if local_mac in _DEVELOPER_MACS:
# Silently approve and exit the verification function
return
# 2. STANDARD VERIFICATION: For normal customers
lic_file = _locate_license_file()
if not lic_file.exists():
print(f"\n❌ mxPIC FATAL ERROR: License file not found.")
print(f"Searched at: {lic_file.resolve()}")
print(f"Please set the '{_ENV_VAR_NAME}' environment variable to point to your license file.")
sys.exit(1)
try:
with open(lic_file, "r", encoding="utf-8") as f:
lic = json.load(f)
# Check MAC match
if lic["mac_address"] != local_mac:
print(f"\n❌ mxPIC FATAL ERROR: License registered to different machine.")
print(f"Registered: {lic['mac_address']} | Local: {local_mac}")
sys.exit(1)
# Check Expiration
exp_date = datetime.strptime(lic["expiration"], "%Y-%m-%d")
if datetime.now() > exp_date:
print(f"\n❌ mxPIC FATAL ERROR: License expired on {lic['expiration']}.")
sys.exit(1)
# Verify Signature
message = f"{lic['mac_address']}|{lic['expiration']}".encode('utf-8')
expected_sig = hmac.new(_SECRET_KEY, message, hashlib.sha256).hexdigest()
if not hmac.compare_digest(expected_sig, lic["signature"]):
print("\n❌ mxPIC FATAL ERROR: License signature is corrupted or forged.")
sys.exit(1)
except json.JSONDecodeError:
print(f"\n❌ mxPIC FATAL ERROR: License file is corrupted (invalid JSON).")
sys.exit(1)
except KeyError as e:
print(f"\n❌ mxPIC FATAL ERROR: License file is missing required data: {e}")
sys.exit(1)
except Exception as e:
print(f"\n❌ mxPIC FATAL ERROR: Failed to read license. {e}")
sys.exit(1)