Show WireGuard QR, full .conf and ZIP download after 3x-ui client create
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -43,6 +43,50 @@ def generate_keypair() -> KeyPair:
|
||||
)
|
||||
|
||||
|
||||
def public_key_from_private(private_key_b64: str) -> str:
|
||||
"""Derive WireGuard/X25519 public key from a base64 private key."""
|
||||
raw = base64.b64decode(private_key_b64.strip())
|
||||
if len(raw) != 32:
|
||||
raise ValueError("Invalid private key length")
|
||||
private = X25519PrivateKey.from_private_bytes(raw)
|
||||
public_bytes = private.public_key().public_bytes(Encoding.Raw, PublicFormat.Raw)
|
||||
return base64.b64encode(public_bytes).decode("ascii")
|
||||
|
||||
|
||||
def render_xui_wireguard_config(
|
||||
*,
|
||||
private_key: str,
|
||||
address: str,
|
||||
dns: str,
|
||||
mtu: int,
|
||||
server_public_key: str,
|
||||
endpoint: str,
|
||||
peer_allowed_ips: str = "0.0.0.0/0, ::/0",
|
||||
remark: str = "",
|
||||
) -> str:
|
||||
addr = address if "/" in address else f"{address}/32"
|
||||
lines = [
|
||||
"[Interface]",
|
||||
f"PrivateKey = {private_key}",
|
||||
f"Address = {addr}",
|
||||
f"DNS = {dns}",
|
||||
f"MTU = {mtu}",
|
||||
"",
|
||||
]
|
||||
if remark:
|
||||
lines.append(f"# {remark}")
|
||||
lines.extend(
|
||||
[
|
||||
"[Peer]",
|
||||
f"PublicKey = {server_public_key}",
|
||||
f"AllowedIPs = {peer_allowed_ips}",
|
||||
f"Endpoint = {endpoint}",
|
||||
"",
|
||||
]
|
||||
)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def generate_preshared_key() -> str:
|
||||
return base64.b64encode(secrets.token_bytes(32)).decode("ascii")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user