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.

39 lines
954 B
Python

from fastapi import FastAPI, Response
from wgconfig import WireGuardConfig, wg_showconf, wg_syncconf
from dataclasses import dataclass
import hashlib
app = FastAPI()
@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):
print(pubkey)
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()