diff --git a/core/scripts/webpanel/routers/api/v1/config/hysteria.py b/core/scripts/webpanel/routers/api/v1/config/hysteria.py index 2d05c38..7eecc0b 100644 --- a/core/scripts/webpanel/routers/api/v1/config/hysteria.py +++ b/core/scripts/webpanel/routers/api/v1/config/hysteria.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, HTTPException -from ..schema.config.hysteria import ConfigFile +from ..schema.config.hysteria import ConfigFile, GetPortResponse, GetSniResponse from ..schema.response import DetailResponse # from ..schema.config.hysteria import InstallInputBody import cli_api @@ -65,6 +65,26 @@ async def update(): raise HTTPException(status_code=400, detail=f'Error: {str(e)}') +@router.get('/get-port', response_model=GetPortResponse, summary='Get Hysteria2 port') +async def get_port(): + """ + Retrieves the port for Hysteria2. + + Returns: + A GetPortResponse containing the Hysteria2 port. + + Raises: + HTTPException: if an error occurs while getting the Hysteria2 port. + """ + try: + if port := cli_api.get_hysteria2_port(): + return GetPortResponse(port=port) + else: + raise HTTPException(status_code=404, detail='Hysteria2 port not found.') + except Exception as e: + raise HTTPException(status_code=400, detail=f'Error: {str(e)}') + + @router.get('/set-port/{port}', response_model=DetailResponse, summary='Set Hysteria2 port') async def set_port(port: int): """ @@ -86,6 +106,26 @@ async def set_port(port: int): raise HTTPException(status_code=400, detail=f'Error: {str(e)}') +@router.get('/get-sni', response_model=GetSniResponse, summary='Get Hysteria2 SNI') +async def get_sni(): + ''' + Retrieves the SNI for Hysteria2. + + Returns: + A GetSniResponse containing the Hysteria2 SNI. + + Raises: + HTTPException: if an error occurs while getting the Hysteria2 SNI. + ''' + try: + if sni := cli_api.get_hysteria2_sni(): + return GetSniResponse(sni=sni) + else: + raise HTTPException(status_code=404, detail='Hysteria2 SNI not found.') + except Exception as e: + raise HTTPException(status_code=400, detail=f'Error: {str(e)}') + + @router.get('/set-sni/{sni}', response_model=DetailResponse, summary='Set Hysteria2 SNI') async def set_sni(sni: str): """ diff --git a/core/scripts/webpanel/routers/api/v1/schema/config/hysteria.py b/core/scripts/webpanel/routers/api/v1/schema/config/hysteria.py index b319433..82e51a4 100644 --- a/core/scripts/webpanel/routers/api/v1/schema/config/hysteria.py +++ b/core/scripts/webpanel/routers/api/v1/schema/config/hysteria.py @@ -1,4 +1,4 @@ -from pydantic import RootModel +from pydantic import BaseModel, RootModel from typing import Any # Change: Installing and uninstalling Hysteria2 is possible only through the CLI @@ -10,3 +10,11 @@ from typing import Any # TODO: Define supported fields of the config file class ConfigFile(RootModel): # type: ignore root: dict[str, Any] + + +class GetPortResponse(BaseModel): + port: int + + +class GetSniResponse(BaseModel): + sni: str