Files
licensing/client_example.py
2026-10-19 17:08:07 -05:00

46 lines
2.0 KiB
Python

"""Example licensing consumer; does not modify or inject the Minecraft client."""
import argparse
import getpass
import hashlib
import json
import platform
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import Request, urlopen
from urllib.parse import urlsplit
def machine_fingerprint():
if platform.system()=="Windows":
import winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,r"SOFTWARE\Microsoft\Cryptography",0,winreg.KEY_READ|winreg.KEY_WOW64_64KEY) as key:
identifier=winreg.QueryValueEx(key,"MachineGuid")[0]
else:
identifier=Path("/etc/machine-id").read_text().strip()
return hashlib.sha256(("amnesia/device/v1:"+identifier.strip().lower()).encode()).hexdigest()
def request(base,path,body,token=None):
headers={"Content-Type":"application/json"}
if token:headers["Authorization"]="Bearer "+token
req=Request(base.rstrip("/")+path,data=json.dumps(body).encode(),headers=headers,method="POST")
try:
with urlopen(req,timeout=20) as response:return json.load(response)
except HTTPError as error:
result=json.load(error)
raise RuntimeError(result.get("error","licensing request failed")) from None
if __name__=="__main__":
parser=argparse.ArgumentParser()
parser.add_argument("--url",required=True,help="https://your-domain/api")
parser.add_argument("--username",required=True)
args=parser.parse_args()
url=urlsplit(args.url)
if url.scheme!="https" and not (url.scheme=="http" and url.hostname in ("127.0.0.1","localhost")):
parser.error("HTTPS is required except for loopback testing")
hardware=machine_fingerprint()
result=request(args.url,"/client/activate",{"username":args.username,"password":getpass.getpass("Password: "),"hwid":hardware,"device_name":platform.node()[:48] or "my device"})
verified=request(args.url,"/client/verify",{"hwid":hardware},result["token"])
print(f"License verified for {verified['account']['username']} / UID {verified['account']['uid']}.")