87 lines
2.9 KiB
Python
87 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import base64
|
|
import json
|
|
from datetime import datetime, timedelta, timezone
|
|
from pathlib import Path
|
|
|
|
from cryptography.hazmat.primitives import serialization
|
|
|
|
from douyin.license.license_manager import (
|
|
ACTIVATION_CODE_PREFIX,
|
|
canonical_payload_bytes,
|
|
get_default_license_path,
|
|
get_machine_fingerprint,
|
|
)
|
|
|
|
|
|
# 仅用于本地第一阶段测试。正式接服务器后,私钥应从服务器环境变量/密钥库读取。
|
|
DEV_PRIVATE_KEY_PEM = b"""-----BEGIN PRIVATE KEY-----
|
|
MC4CAQAwBQYDK2VwBCIEIOjSJg0Y4mQJcAnb/9o4dDANZSeTeLJYt04nUVZNvlEy
|
|
-----END PRIVATE KEY-----
|
|
"""
|
|
|
|
|
|
def _load_dev_private_key():
|
|
return serialization.load_pem_private_key(DEV_PRIVATE_KEY_PEM, password=None)
|
|
|
|
|
|
def create_dev_license(
|
|
output_path: str | Path,
|
|
machine_fingerprint: str,
|
|
expires_at: datetime,
|
|
customer: str = "local",
|
|
) -> Path:
|
|
if expires_at.tzinfo is None:
|
|
expires_at = expires_at.replace(tzinfo=timezone.utc)
|
|
expires_at = expires_at.astimezone(timezone.utc)
|
|
payload = {
|
|
"customer": customer,
|
|
"machine_fingerprint": machine_fingerprint,
|
|
"expires_at": expires_at.isoformat().replace("+00:00", "Z"),
|
|
"license_version": 1,
|
|
}
|
|
signature = _load_dev_private_key().sign(canonical_payload_bytes(payload))
|
|
doc = {
|
|
"payload": payload,
|
|
"signature": base64.b64encode(signature).decode("ascii"),
|
|
}
|
|
path = Path(output_path)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(json.dumps(doc, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
return path
|
|
|
|
|
|
def encode_activation_code(license_text: str) -> str:
|
|
raw = str(license_text or "").encode("utf-8")
|
|
encoded = base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
|
groups = "-".join(encoded[i:i + 5] for i in range(0, len(encoded), 5))
|
|
return f"{ACTIVATION_CODE_PREFIX}{groups}"
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description="生成本地开发 license.dat")
|
|
parser.add_argument("--output", default=str(get_default_license_path()))
|
|
parser.add_argument("--machine", default=get_machine_fingerprint())
|
|
parser.add_argument("--days", type=int, default=30)
|
|
parser.add_argument("--customer", default="local")
|
|
parser.add_argument("--print-code", action="store_true", help="同时输出可粘贴到 UI 的 DYGJ 卡密")
|
|
args = parser.parse_args(argv)
|
|
|
|
path = create_dev_license(
|
|
output_path=args.output,
|
|
machine_fingerprint=args.machine,
|
|
expires_at=datetime.now(timezone.utc) + timedelta(days=max(1, int(args.days))),
|
|
customer=str(args.customer or "local"),
|
|
)
|
|
print(f"[LICENSE] generated: {path}")
|
|
print(f"[LICENSE] machine_fingerprint: {args.machine}")
|
|
if args.print_code:
|
|
print(encode_activation_code(path.read_text(encoding="utf-8")))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|