diff --git a/api/src/app.py b/api/src/app.py index 8dfdef4..f2402ba 100644 --- a/api/src/app.py +++ b/api/src/app.py @@ -4,7 +4,7 @@ from dependencies.common_key_header import common_key_header from middlewares import client_auth from models import db -from routes import auth, users +from routes import auth, users, ipv6 app = FastAPI(title="NetWorkers API", version="1.0.0") @@ -23,5 +23,7 @@ async def get_info() -> dict: dependencies=[Depends(common_key_header)]) app.include_router(users.router, prefix="/users", tags=["users"], dependencies=[Depends(common_key_header)]) +app.include_router(ipv6.router, prefix="/ipv6", tags=["ipv6"], + dependencies=[Depends(common_key_header)]) db.init_db() diff --git a/api/src/routes/ipv6.py b/api/src/routes/ipv6.py new file mode 100644 index 0000000..920e33a --- /dev/null +++ b/api/src/routes/ipv6.py @@ -0,0 +1,22 @@ +"""Users routes module.""" + +from fastapi import APIRouter, Depends + +import utils.ipv6 as ipv6_utils +from dependencies.jwt import jwt_bearer + +router = APIRouter() + +@router.get("/simplify/{ipv6}", summary="Get the simplification of the IPV6",\ + dependencies=[Depends(jwt_bearer)]) +async def get_simplify(ipv6: str) -> dict: + """Get simplification of the IPV6.""" + res = ipv6_utils.simplify(ipv6) + return {"ipv6": res} + +@router.get("/extend/{ipv6}", summary="Get the extend of the IPV6",\ + dependencies=[Depends(jwt_bearer)]) +async def get_extend(ipv6: str) -> dict: + """Get extend of the IPV6.""" + res = ipv6_utils.extend(ipv6) + return {"ipv6": res} diff --git a/api/src/utils/ipv6.py b/api/src/utils/ipv6.py new file mode 100644 index 0000000..9ba04a3 --- /dev/null +++ b/api/src/utils/ipv6.py @@ -0,0 +1,57 @@ +"""Module for IPv6 utilities.""" + +def simplify(ipv6: str) -> str: + """Simplify an IPv6 address. + + Args: + ipv6 (str): The IPv6 address to simplify. + + Returns: + str: The simplified IPv6 address. + + """ + parts = ipv6.split(":") + for part in parts: + if part == "0000": + parts[parts.index(part)] = "0" + + is_zero = False + i = 0 + while i < len(parts): + part = parts[i] + if part == "0": + parts.pop(i) + is_zero = True + elif is_zero: + parts.insert(i, "") + i = len(parts) # break + else: + i += 1 + + return ":".join(parts) + +def extend(ipv6: str) -> str: + """Extend an IPv6 address. + + Args: + ipv6 (str): The IPv6 address to extend. + + Returns: + str: The extended IPv6 address. + + """ + parts = ipv6.split(":") + i = 0 + while i < len(parts): + if parts[i] == "": + parts[i] = "0000" + for _j in range(8 - len(parts)): + parts.insert(i, "0000") + i += 8 - len(parts) + elif parts[i] == "0": + parts[i] = "0000" + i += 1 + else: + i += 1 + + return ":".join(parts)