You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
from fastapi import FastAPI, Response
|
|
from wgconfig import WireGuardConfig, wg_showconf, wg_syncconf
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
import hashlib
|
|
import yaml
|
|
|
|
app = FastAPI()
|
|
|
|
cfg = yaml.safe_load(Path(__file__).with_suffix(".conf").read_text())
|
|
|
|
|
|
@app.get("/peers/{ifname}")
|
|
def get_all_peer(ifname: str):
|
|
c = WireGuardConfig.get_from_interface(ifname)
|
|
return Response(c.get_peers_cfg())
|
|
|
|
|
|
@app.get("/peers/{ifname}/{pubkey:path}")
|
|
def get_peer(ifname: str, pubkey: str):
|
|
c = WireGuardConfig.get_from_interface(ifname)
|
|
return c.get_peer(pubkey)
|
|
|
|
|
|
@dataclass
|
|
class EndpointUpdateDTO:
|
|
pubkey: str
|
|
endpoint: str
|
|
|
|
|
|
@app.post("/endpoint/{ifname}")
|
|
def update_endpoint(ifname, u: EndpointUpdateDTO):
|
|
c = WireGuardConfig.get_from_interface(ifname)
|
|
if p := c.get_peer(u.pubkey):
|
|
p["Endpoint"] = u.endpoint
|
|
wg_syncconf(ifname, str(c))
|
|
|
|
|
|
@app.get("/hash/{ifname}")
|
|
def get_config_hash(ifname: str):
|
|
c = wg_showconf(ifname).encode()
|
|
return hashlib.sha256(c).hexdigest() |