master
大蒟蒻 4 years ago
parent db0fef1cde
commit 921ae66e6d

@ -5,35 +5,36 @@ import requests
from pathlib import Path from pathlib import Path
from wgconfig import WireGuardConfig, wg_showconf, wg_syncconf from wgconfig import WireGuardConfig, wg_showconf, wg_syncconf
def local_hash(ifname: str):
return hashlib.sha256(wg_showconf(ifname).encode()).hexdigest()
cfg = yaml.safe_load(Path(__file__).with_suffix(".conf").read_text()) cfg = yaml.safe_load(Path(__file__).with_suffix(".conf").read_text())
last_hash = "" last_hash = ""
last_local_hash = "" last_local_hash = ""
url = f"{cfg['url']}/hash/{cfg['remote_wg']}" url = f"{cfg['url']}/hash/{cfg['remote_wg']}"
urlpat2 = f"{cfg['url']}/peers/{cfg['remote_wg']}" urlpat2 = f"{cfg['url']}/peers/{cfg['remote_wg']}"
while True: with requests.Session() as sess:
new_hash = requests.get( sess.verify = "ca.crt"
url, sess.cert = ("client1.crt", "client1.key")
cert=("client1.crt", "client1.key"), while True:
verify="ca.crt", try:
).text new_hash = sess.get(url).text
new_local_hash = hashlib.sha256(wg_showconf( new_local_hash = local_hash(cfg["local_wg"])
cfg["local_wg"]).encode()).hexdigest() if last_hash != new_hash or last_local_hash != new_local_hash:
if last_hash != new_hash or last_local_hash != new_local_hash: print(f"update hash to {new_hash}")
print(f"update hash to {new_hash}") local_cfg = WireGuardConfig.get_from_interface(cfg["local_wg"])
print(f"update local_hash to {new_local_hash}") for peer in cfg["peers"]:
local_cfg = WireGuardConfig.get_from_interface(cfg["local_wg"]) if p := local_cfg.get_peer(peer):
for peer in cfg["peers"]: url2 = f"{urlpat2}/{peer}"
if p := local_cfg.get_peer(peer): res = sess.get(url2).json()
url2 = f"{urlpat2}/{peer}" p["Endpoint"] = res["Endpoint"]
res = requests.get( wg_syncconf(cfg["local_wg"], str(local_cfg))
url2, new_local_hash = local_hash(cfg["local_wg"])
cert=("client1.crt", "client1.key"), print(f"update local_hash to {new_local_hash}")
verify="ca.crt", last_hash = new_hash
).json() last_local_hash = new_local_hash
p["Endpoint"] = res["Endpoint"] except Exception as ex:
wg_syncconf(cfg["local_wg"], str(local_cfg)) print(ex)
new_local_hash = hashlib.sha256(wg_showconf( sleep(5)
cfg["local_wg"]).encode()).hexdigest()
last_hash = new_hash
last_local_hash = new_local_hash
sleep(5)

Loading…
Cancel
Save