From c40da42b150478a2bbbb78c960e5bdd5ec4fcdf8 Mon Sep 17 00:00:00 2001 From: Iam54r1n4 Date: Fri, 7 Feb 2025 03:21:22 +0000 Subject: [PATCH] Implement ip APIs --- .../routers/api/v1/config/__init__.py | 2 ++ .../webpanel/routers/api/v1/config/ip.py | 29 +++++++++++++++++++ .../routers/api/v1/schema/config/ip.py | 7 +++++ 3 files changed, 38 insertions(+) create mode 100644 core/scripts/webpanel/routers/api/v1/config/ip.py create mode 100644 core/scripts/webpanel/routers/api/v1/schema/config/ip.py diff --git a/core/scripts/webpanel/routers/api/v1/config/__init__.py b/core/scripts/webpanel/routers/api/v1/config/__init__.py index 2dd031c..02812a3 100644 --- a/core/scripts/webpanel/routers/api/v1/config/__init__.py +++ b/core/scripts/webpanel/routers/api/v1/config/__init__.py @@ -4,6 +4,7 @@ from . import warp from . import telegram from . import normalsub from . import singbox +from . import ip from . import misc router = APIRouter() @@ -14,4 +15,5 @@ router.include_router(warp.router, prefix='/warp') router.include_router(telegram.router, prefix='/telegram') router.include_router(normalsub.router, prefix='/normalsub') router.include_router(singbox.router, prefix='/singbox') +router.include_router(ip.router, prefix='/ip') router.include_router(misc.router) diff --git a/core/scripts/webpanel/routers/api/v1/config/ip.py b/core/scripts/webpanel/routers/api/v1/config/ip.py new file mode 100644 index 0000000..497a788 --- /dev/null +++ b/core/scripts/webpanel/routers/api/v1/config/ip.py @@ -0,0 +1,29 @@ +from fastapi import APIRouter, HTTPException +from ..schema.response import DetailResponse + + +from ..schema.config.ip import EditInputBody +import cli_api + +router = APIRouter() + + +@router.get('/add') +async def add(): + try: + cli_api.add_ip_address() + return DetailResponse(detail='IP addresses added successfully.') + except Exception as e: + raise HTTPException(status_code=400, detail=f'Error: {str(e)}') + + +@router.post('/edit', response_model=DetailResponse) +async def edit(body: EditInputBody): + try: + if not body.ipv4 and not body.ipv6: + raise HTTPException(status_code=400, detail='Error: You must specify either ipv4 or ipv6') + + cli_api.edit_ip_address(str(body.ipv4), str(body.ipv6)) + return DetailResponse(detail='IP address edited successfully.') + except Exception as e: + raise HTTPException(status_code=400, detail=f'Error: {str(e)}') diff --git a/core/scripts/webpanel/routers/api/v1/schema/config/ip.py b/core/scripts/webpanel/routers/api/v1/schema/config/ip.py new file mode 100644 index 0000000..0dd0948 --- /dev/null +++ b/core/scripts/webpanel/routers/api/v1/schema/config/ip.py @@ -0,0 +1,7 @@ +from pydantic import BaseModel +from ipaddress import IPv4Address, IPv6Address + + +class EditInputBody(BaseModel): + ipv4: IPv4Address | None = None + ipv6: IPv6Address | None = None