diff --git a/KeeneticPy/BGPTools.py b/KeeneticPy/BGPTools.py index 9b95b0d..4aa92e5 100644 --- a/KeeneticPy/BGPTools.py +++ b/KeeneticPy/BGPTools.py @@ -4,6 +4,13 @@ from httpx import Client from parsel import Selector +def cidr2mask(cidr:str) -> str: + ip, prefix = cidr.split("/") + prefix = int(prefix) + mask = (0xFFFFFFFF >> (32 - prefix)) << (32 - prefix) + + return f"{(mask >> 24) & 0xFF}.{(mask >> 16) & 0xFF}.{(mask >> 8) & 0xFF}.{mask & 0xFF}" + def asn2cidr(asn:int) -> dict: if not isinstance(asn, int): return {} @@ -25,5 +32,5 @@ def asn2cidr(asn:int) -> dict: ] } -def domain2ip(domain): +def domain2ip(domain:str) -> dict: return Domain2IP(domain).bilgi \ No newline at end of file diff --git a/KeeneticPy/Keenetic.py b/KeeneticPy/Keenetic.py index acd415d..33bebd3 100644 --- a/KeeneticPy/Keenetic.py +++ b/KeeneticPy/Keenetic.py @@ -7,7 +7,7 @@ from os import remove, listdir from datetime import datetime from Kekik import slugify -from . import asn2cidr, domain2ip +from . import cidr2mask, asn2cidr, domain2ip class Keenetic: def __init__(self, user:str="admin", password:str="", ip:str="192.168.1.1"): @@ -158,15 +158,6 @@ def get_static_routes(self) -> list[dict[str, str]]: json = {"show":{"ip":{"route":{}}}} ).json().get("ip", {}).get("route", []) - @staticmethod - def cidr_to_mask(cidr): - """ https://bgp.tools/as/32934#prefixes """ - ip, prefix = cidr.split("/") - prefix = int(prefix) - mask = (0xFFFFFFFF >> (32 - prefix)) << (32 - prefix) - - return f"{(mask >> 24) & 0xFF}.{(mask >> 16) & 0xFF}.{(mask >> 8) & 0xFF}.{mask & 0xFF}" - def add_static_route(self, comment:str, host:str=None, network:str=None, mask:str=None, interface:str="Wireguard2") -> bool: payload = None @@ -204,7 +195,7 @@ def add_route_with_asn(self, asn:int, interface:str="Wireguard2") -> bool: assert False, f"ASN {asn} için CIDR bilgisine ulaşılamıyor." for prefix in asn_data["prefixes"]: - veri = {"comment": asn_data["company"], "network": prefix.split("/")[0], "mask": Keenetic.cidr_to_mask(prefix), "interface": interface} + veri = {"comment": asn_data["company"], "network": prefix.split("/")[0], "mask": cidr2mask(prefix), "interface": interface} return self.add_static_route(**veri) def add_route_with_domain(self, domain:str, interface:str="Wireguard2") -> bool: @@ -218,7 +209,7 @@ def add_route_with_domain(self, domain:str, interface:str="Wireguard2") -> bool: if domain_data.get("subnetler"): for subnet in domain_data["subnetler"]: - veri = {"comment": domain_data["domain"], "network": subnet.split("/")[0], "mask": Keenetic.cidr_to_mask(subnet), "interface": interface} + veri = {"comment": domain_data["domain"], "network": subnet.split("/")[0], "mask": cidr2mask(subnet), "interface": interface} return self.add_static_route(**veri) for ip in domain_data["ipler"]: diff --git a/KeeneticPy/__init__.py b/KeeneticPy/__init__.py index ed4184a..f7a6402 100644 --- a/KeeneticPy/__init__.py +++ b/KeeneticPy/__init__.py @@ -1,4 +1,4 @@ # Bu araç @keyiflerolsun tarafından | @KekikAkademi için yazılmıştır. from .Keenetic import Keenetic -from .BGPTools import asn2cidr, domain2ip \ No newline at end of file +from .BGPTools import cidr2mask, asn2cidr, domain2ip \ No newline at end of file diff --git a/README.md b/README.md index 3d0b0a6..45fc191 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,6 @@ for route in modem.get_static_routes(): konsol.log(route) if route.get("comment") == "bakalim.io": konsol.print(modem.del_static_route(**route)) - break konsol.print(modem.add_route_with_asn(asn=32934, interface="Wireguard2")) diff --git a/setup.py b/setup.py index 79d3bd3..945dc70 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( # ? Genel Bilgiler name = "KeeneticPy", - version = "1.0", + version = "1.1", url = "https://github.com/keyiflerolsun/KeeneticPy", description = "Python Lib for Keenetic Routers", keywords = ["KeeneticPy", "KekikAkademi", "keyiflerolsun"],