81 lines
3.0 KiB
Python
81 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
|
|
|
|
# This MUST match the key in your generator exactly.
|
|
# Because this file is compiled via Cython to a .so/.pyd file,
|
|
# This MUST match the key in your generator exactly.
|
|
_SECRET_KEY = b"mxPIC_Super_Secret_Master_Key_2026!"
|
|
_ENV_VAR_NAME = "LIC_MXPIC_OPTIHK_DIR"
|
|
|
|
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 the user pointed to a directory, append the default filename
|
|
if path_obj.is_dir():
|
|
return path_obj / "mxpic.lic"
|
|
# Otherwise, assume they pointed directly to the file itself
|
|
return path_obj
|
|
else:
|
|
# Fallback: Look in the current working directory where the script is run
|
|
return Path("mxpic.lic")
|
|
|
|
def verify_license() -> None:
|
|
"""Verifies the license file. Halts execution if invalid."""
|
|
|
|
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)
|
|
|
|
local_mac = _get_local_mac()
|
|
|
|
# 1. 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)
|
|
|
|
# 2. 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)
|
|
|
|
# 3. 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) |