feat: add simple license activation server
This commit is contained in:
1 parent
b2db5ed499
commit
05be83d289
17 files changed
+1053
-6
No files matched your search
@@ -0,0 +1,97 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseActionResult, LicenseService
|
||||
|
||||
|
||||
class ActivateRequest(BaseModel):
|
||||
card_key: str
|
||||
machine_fingerprint: str
|
||||
|
||||
|
||||
class VerifyRequest(BaseModel):
|
||||
card_key: str
|
||||
machine_fingerprint: str
|
||||
|
||||
|
||||
def result_response(result: LicenseActionResult) -> JSONResponse:
|
||||
body: dict[str, Any] = {
|
||||
"ok": result.ok,
|
||||
"reason": result.reason,
|
||||
"message": result.message,
|
||||
}
|
||||
if result.license_doc is not None:
|
||||
body["license"] = result.license_doc
|
||||
if result.expires_at is not None:
|
||||
body["expires_at"] = result.expires_at.isoformat().replace("+00:00", "Z")
|
||||
status = 200 if result.ok else _status_for_reason(result.reason)
|
||||
return JSONResponse(body, status_code=status)
|
||||
|
||||
|
||||
def _status_for_reason(reason: str) -> int:
|
||||
if reason == "not_found":
|
||||
return 404
|
||||
if reason in {"revoked", "inactive", "device_limit", "machine_mismatch"}:
|
||||
return 403
|
||||
if reason in {"missing_machine", "not_activated"}:
|
||||
return 400
|
||||
return 400
|
||||
|
||||
|
||||
def load_private_key_pem() -> bytes:
|
||||
raw_path = str(os.environ.get("DYGJ_LICENSE_PRIVATE_KEY_PATH") or "").strip()
|
||||
if raw_path:
|
||||
return Path(raw_path).read_bytes()
|
||||
raw_pem = str(os.environ.get("DYGJ_LICENSE_PRIVATE_KEY_PEM") or "").strip()
|
||||
if raw_pem:
|
||||
return raw_pem.encode("utf-8")
|
||||
return DEV_PRIVATE_KEY_PEM
|
||||
|
||||
|
||||
def create_service_from_env() -> LicenseService:
|
||||
db_path = os.environ.get("DYGJ_LICENSE_DB_PATH") or "/opt/douyin-license-server/data/app.db"
|
||||
return LicenseService(db_path=db_path, private_key_pem=load_private_key_pem())
|
||||
|
||||
|
||||
def create_app(*, service: LicenseService | None = None) -> FastAPI:
|
||||
app = FastAPI(title="Douyin Guaji License Server")
|
||||
app.state.license_service = service
|
||||
|
||||
def get_service() -> LicenseService:
|
||||
if app.state.license_service is None:
|
||||
app.state.license_service = create_service_from_env()
|
||||
return app.state.license_service
|
||||
|
||||
@app.get("/api/v1/health")
|
||||
def health() -> dict[str, Any]:
|
||||
return {"ok": True, "service": "douyin-license-server"}
|
||||
|
||||
@app.post("/api/v1/activate")
|
||||
def activate(req: ActivateRequest) -> JSONResponse:
|
||||
return result_response(
|
||||
get_service().activate(
|
||||
req.card_key,
|
||||
machine_fingerprint=req.machine_fingerprint,
|
||||
)
|
||||
)
|
||||
|
||||
@app.post("/api/v1/verify")
|
||||
def verify(req: VerifyRequest) -> JSONResponse:
|
||||
return result_response(
|
||||
get_service().verify(
|
||||
req.card_key,
|
||||
machine_fingerprint=req.machine_fingerprint,
|
||||
)
|
||||
)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
app = create_app()
|
||||
Reference in new issue
Block a user