1. Cython compile system build. 2. License system build. 3. Auto md file generation for Sphinx build.

This commit is contained in:
2026-05-07 13:51:42 +08:00
parent 15610b623c
commit dda69d5b84
129 changed files with 4458 additions and 516 deletions
+39
View File
@@ -0,0 +1,39 @@
import hmac
import hashlib
import json
from datetime import datetime, timedelta
# This is your master key. NEVER share this or put it in public code.
SECRET_KEY = b"mxPIC_Super_Secret_Master_Key_2026!"
def generate_license(mac_address: str, days_valid: int, output_file: str = "mxpic.lic"):
"""Generates a cryptographically signed license file."""
# Calculate the expiration date
expiration = (datetime.now() + timedelta(days=days_valid)).strftime("%Y-%m-%d")
# Create the payload message we want to sign
message = f"{mac_address}|{expiration}".encode('utf-8')
# Generate the unforgeable signature using HMAC + SHA256
signature = hmac.new(SECRET_KEY, message, hashlib.sha256).hexdigest()
# Create the license dictionary
license_data = {
"mac_address": mac_address,
"expiration": expiration,
"signature": signature
}
# Save to file
with open(output_file, "w") as f:
json.dump(license_data, f, indent=4)
#
print(f"✅ License generated for MAC {mac_address}")
print(f"✅ Expires on {expiration}")
print(f"✅ Saved to {output_file}")
if __name__ == "__main__":
# Example: Customer sends MAC "A1:B2:C3:D4:E5:F6", they bought a 1-year license
generate_license("D4:54:8B:F1:46:49", days_valid=365,output_file="mxpic.lic")
+81
View File
@@ -0,0 +1,81 @@
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)