Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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()
22 changes: 22 additions & 0 deletions api/src/routes/ipv6.py
Original file line number Diff line number Diff line change
@@ -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}
57 changes: 57 additions & 0 deletions api/src/utils/ipv6.py
Original file line number Diff line number Diff line change
@@ -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)