Compare commits

..

2 commits

3 changed files with 56 additions and 35 deletions

View file

@ -144,7 +144,7 @@ class PeeringManager:
print(wg_enable) print(wg_enable)
wg_up = subprocess.run(self.__config["wg-commands"]["up"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) wg_up = subprocess.run(self.__config["wg-commands"]["up"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))
print(wg_up) print(wg_up)
time.sleep(5) time.sleep(3)
with open(f"{self.__config['bird-peers']}/dn42_{peering['MNT'][:-4].lower()}_{peering['ASN'][-4:]}.conf", "w") as bgp_file: with open(f"{self.__config['bird-peers']}/dn42_{peering['MNT'][:-4].lower()}_{peering['ASN'][-4:]}.conf", "w") as bgp_file:
bgp_file.write(bgp_conf) bgp_file.write(bgp_conf)
bgp_reload = subprocess.run(self.__config["bird-reload"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) bgp_reload = subprocess.run(self.__config["bird-reload"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))
@ -161,7 +161,7 @@ class PeeringManager:
print(wg_down) print(wg_down)
wg_up = subprocess.run(self.__config["wg-commands"]["up"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) wg_up = subprocess.run(self.__config["wg-commands"]["up"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))
print(wg_up) print(wg_up)
time.sleep(5) time.sleep(3)
with open(f"{self.__config['bird-peers']}/dn42_{peering['MNT'][:-4].lower()}_{peering['ASN'][-4:]}.conf", "w") as bgp_file: with open(f"{self.__config['bird-peers']}/dn42_{peering['MNT'][:-4].lower()}_{peering['ASN'][-4:]}.conf", "w") as bgp_file:
bgp_file.write(bgp_conf) bgp_file.write(bgp_conf)
bgp_reload = subprocess.run(self.__config["bird-reload"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) bgp_reload = subprocess.run(self.__config["bird-reload"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))
@ -172,7 +172,7 @@ class PeeringManager:
os.remove(f"{self.__config['bird-peers']}/dn42_{peering['MNT'][:-4].lower()}_{peering['ASN'][-4:]}.conf") os.remove(f"{self.__config['bird-peers']}/dn42_{peering['MNT'][:-4].lower()}_{peering['ASN'][-4:]}.conf")
bgp_reload = subprocess.run(self.__config["bird-reload"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) bgp_reload = subprocess.run(self.__config["bird-reload"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))
print(bgp_reload) print(bgp_reload)
time.sleep(5) time.sleep(3)
wg_down = subprocess.run(self.__config["wg-commands"]["down"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) wg_down = subprocess.run(self.__config["wg-commands"]["down"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))
print(wg_down) print(wg_down)
wg_disable = subprocess.run(self.__config["wg-commands"]["disable"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" ")) wg_disable = subprocess.run(self.__config["wg-commands"]["disable"].replace("{PEERING}",peering['ASN'][-6:] if len(peering["ASN"]) >=6 else peering["ASN"]).split(" "))

View file

@ -391,8 +391,9 @@ def peerings_new():
if not peering_valid: if not peering_valid:
return render_template("peerings-new.html", session=session, config=config, peerings=peerings, msg=peering_or_msg), 400 return render_template("peerings-new.html", session=session, config=config, peerings=peerings, msg=peering_or_msg), 400
if not peerings.add_peering(session["user-data"]["asn"], request.args["node"], session["user-data"]["mnt"], peering_or_msg["peer-wgkey"], peering_or_msg["peer-endpoint"], peering_or_msg["peer-v6ll"], peering_or_msg["peer-v4"], peering_or_msg["peer-v6"], peering_or_msg["bgp-mp"], peering_or_msg["bgp-enh"]): success, code = peerings.add_peering(session["user-data"]["asn"], request.args["node"], session["user-data"]["mnt"], peering_or_msg["peer-wgkey"], peering_or_msg["peer-endpoint"], peering_or_msg["peer-v6ll"], peering_or_msg["peer-v4"], peering_or_msg["peer-v6"], peering_or_msg["bgp-mp"], peering_or_msg["bgp-enh"])
return render_template("peerings-new.html", session=session, config=config, peerings=peerings, msg="this ASN already has a peering with the requested node"), 400 if not success:
return render_template("peerings-new.html", session=session, config=config, peerings=peerings, msg="this ASN already has a peering with the requested node or something failed in the backend, please retry later"), code
return redirect(f"{config['base-dir']}peerings") return redirect(f"{config['base-dir']}peerings")
@ -411,6 +412,7 @@ def peerings_view():
elif request.method == "DELETE": elif request.method == "DELETE":
return peerings_delete() return peerings_delete()
else: else:
# shouldn't get here
abort(405) abort(405)

View file

@ -15,15 +15,22 @@ class NodeCommunicator:
self.__api_addr = config["api-con"] self.__api_addr = config["api-con"]
def update(self, action: str, peering: dict): def update(self, action: str, peering: dict):
try:
if action == "add": if action == "add":
print(requests.post(f"{self.__api_addr}peerings", json=peering)) req = requests.post(f"{self.__api_addr}peerings", json=peering, timeout=6)
elif action == "update": elif action == "update":
print(requests.put(f"{self.__api_addr}peerings", json=peering)) req = requests.put(f"{self.__api_addr}peerings", json=peering, timeout=6)
elif action == "delete": elif action == "delete":
print(requests.delete(f"{self.__api_addr}peerings", json=peering)) req = requests.delete(f"{self.__api_addr}peerings", json=peering, timeout=6)
else: else:
return 400 return 400
# should only get here if a request was "successful" (not necessarily http code 200)
return req.status_code
except requests.exceptions.ReadTimeout:
# if it took more than timeout for the client to respond
return 504
class PeeringManager: class PeeringManager:
@ -78,15 +85,13 @@ class PeeringManager:
new_peering: if mode=="update" the new peering to update to new_peering: if mode=="update" the new peering to update to
""" """
if peering["node"] in self._nodes: if peering["node"] in self._nodes:
#thread = threading.Thread(target= return_code = self._nodes[peering["node"]].update(action=action, peering = peering if not new_peering else new_peering)
self._nodes[peering["node"]].update(#), kwargs={ if return_code in [200, 201]:
action=action, peering = peering if not new_peering else new_peering#, }) return True, return_code
)
#thread.start()
#self._threads.append(thread)
else: else:
return False return False, return_code
else:
return False, 404
def _update_amounts(self): def _update_amounts(self):
__new = {} __new = {}
@ -158,10 +163,14 @@ class PeeringManager:
peering = {"MNT": mnt, "ASN": asn, "node": node, "wg_key": wg_key, "endpoint": endpoint, peering = {"MNT": mnt, "ASN": asn, "node": node, "wg_key": wg_key, "endpoint": endpoint,
"ipv6ll": ipv6ll, "ipv4": ipv4, "ipv6": ipv6, "bgp_mp": bgp_mp, "bgp_enh": bgp_enh} "ipv6ll": ipv6ll, "ipv4": ipv4, "ipv6": ipv6, "bgp_mp": bgp_mp, "bgp_enh": bgp_enh}
success, code = self._update_nodes("add", peering=peering)
if not success:
# if ssomething failed notify user and don't add it to the list
return False, code
#
self.peerings["asn"][asn].append(peering) self.peerings["asn"][asn].append(peering)
self._update_nodes("add", peering=peering)
self._save_peerings() self._save_peerings()
return True return True, code
def update_peering(self, asn, node, mnt, wg_key, endpoint=None, ipv6ll=None, ipv4=None, ipv6=None, bgp_mp=True, bgp_enh=True): def update_peering(self, asn, node, mnt, wg_key, endpoint=None, ipv6ll=None, ipv4=None, ipv6=None, bgp_mp=True, bgp_enh=True):
# check if this MNT already has a/this asn # check if this MNT already has a/this asn
@ -170,13 +179,15 @@ class PeeringManager:
# ... and add it if it hasn't # ... and add it if it hasn't
self.peerings[mnt].append(asn) self.peerings[mnt].append(asn)
except KeyError: except KeyError:
# ... and cerate it if it doesn't have any yet # ... and create it if it doesn't have any yet
self.peerings["mnter"][mnt] = [asn] self.peerings["mnter"][mnt] = [asn]
try: try:
# there are no peerings for this asn -> can't edit nothing...
if not asn in self.peerings["asn"]: if not asn in self.peerings["asn"]:
return False return False, 404
except KeyError: except KeyError:
return False # this should only happen if "asn" not in self.peerings
return False, 404
success = False success = False
for pNr in range(len(self.peerings["asn"][asn])): for pNr in range(len(self.peerings["asn"][asn])):
@ -185,23 +196,31 @@ class PeeringManager:
new_peering = self.peerings["asn"][asn][pNr] = {"MNT": mnt, "ASN": asn, "node": node, "wg_key": wg_key, new_peering = self.peerings["asn"][asn][pNr] = {"MNT": mnt, "ASN": asn, "node": node, "wg_key": wg_key,
"endpoint": endpoint, "ipv6ll": ipv6ll, "ipv4": ipv4, "ipv6": ipv6, "bgp_mp": bgp_mp, "bgp_enh": bgp_enh} "endpoint": endpoint, "ipv6ll": ipv6ll, "ipv4": ipv4, "ipv6": ipv6, "bgp_mp": bgp_mp, "bgp_enh": bgp_enh}
success = True success = True
peering_number = pNr
if not success: if not success:
return False return False
# notify the node
success, code = self._update_nodes("update", old_peering, new_peering=new_peering)
if not success:
# revert updating peering
self.peerings["asn"][asn][peering_number] = old_peering
return False, code
else:
self._save_peerings() self._save_peerings()
self._update_nodes("update", old_peering, new_peering=new_peering) return True, code
return True
def delete_peering(self, asn, node, mnt, wg_key=None): def delete_peering(self, asn, node, mnt, wg_key=None):
if not self.exists(asn, node, mnt=mnt, wg_key=wg_key): if not self.exists(asn, node, mnt=mnt, wg_key=wg_key):
return False return False, 404
for p in self.peerings["asn"][asn]: for p in self.peerings["asn"][asn]:
if p["node"] == node: if p["node"] == node:
if wg_key and p["wg_key"] != wg_key:
continue
self.peerings["asn"][asn].remove(p) self.peerings["asn"][asn].remove(p)
self._save_peerings() self._save_peerings()
self._update_nodes("delete", peering=p) success, code = self._update_nodes("delete", peering=p)
return True print(f"DELETE: {asn} on {node}: {success}, {code}")
return success, code
# if nothing got found (should have been catched by self.exists) # if nothing got found (should have been catched by self.exists)
return False return False, 404