40 lines
926 B
Python
40 lines
926 B
Python
#!/usr/bin/env python3
|
|
import pydantic
|
|
from typing import Optional
|
|
import socket
|
|
|
|
|
|
class Config(pydantic.BaseModel):
|
|
public_key: str
|
|
host: str
|
|
port: int
|
|
private_key: str
|
|
|
|
|
|
def write_wg(config: Config):
|
|
wg_conf = (
|
|
"[Interface]\n"
|
|
f"privatekey = {config.private_key}\n\n"
|
|
"[Peer]\n"
|
|
f"publickey = {config.public_key}\n"
|
|
f"endpoint = {config.host}:{config.port}\n"
|
|
"persistentkeepalive = 20\n"
|
|
"allowedips = 0.0.0.0/0, ::/0\n"
|
|
)
|
|
|
|
try:
|
|
with open("/run/vpnclient/wg.conf", mode="w", encoding="utf-8") as f:
|
|
f.write(wg_conf)
|
|
except IOError:
|
|
pass
|
|
|
|
|
|
def get_config() -> Optional[Config]:
|
|
with open("/snacks/wireguard/wg.json", "r", encoding="utf-8") as f:
|
|
config = Config.model_validate_json(f.read())
|
|
config.host = socket.gethostbyname(config.host)
|
|
return config
|
|
|
|
|
|
write_wg(get_config())
|