Skip to content

Commit

Permalink
🕊
Browse files Browse the repository at this point in the history
🕊
  • Loading branch information
keyiflerolsun committed Aug 7, 2024
1 parent 656d8de commit df49a33
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
9 changes: 8 additions & 1 deletion KeeneticPy/BGPTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
Expand All @@ -25,5 +32,5 @@ def asn2cidr(asn:int) -> dict:
]
}

def domain2ip(domain):
def domain2ip(domain:str) -> dict:
return Domain2IP(domain).bilgi
15 changes: 3 additions & 12 deletions KeeneticPy/Keenetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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"]:
Expand Down
2 changes: 1 addition & 1 deletion KeeneticPy/__init__.py
Original file line number Diff line number Diff line change
@@ -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
from .BGPTools import cidr2mask, asn2cidr, domain2ip
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down

0 comments on commit df49a33

Please sign in to comment.