From: Michael Tremer Date: Thu, 3 Jul 2025 08:13:02 +0000 (+0000) Subject: api: Move function to fetch the client's IP address X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=73cba6e6c038618119ca82baa55132c2d2030aa5;p=pbs.git api: Move function to fetch the client's IP address Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 5177de46..24567ece 100644 --- a/Makefile.am +++ b/Makefile.am @@ -129,7 +129,8 @@ api_PYTHON = \ src/api/events.py \ src/api/mirrors.py \ src/api/uploads.py \ - src/api/users.py + src/api/users.py \ + src/api/util.py apidir = $(pkgpythondir)/api diff --git a/src/api/distros.py b/src/api/distros.py index bd2c07a7..1ff9bd88 100644 --- a/src/api/distros.py +++ b/src/api/distros.py @@ -27,7 +27,7 @@ import typing from . import apiv1 from . import app from . import backend -from .downloads import get_current_address +from . import util from ..distros import Distro from ..repos import Repo @@ -122,7 +122,8 @@ class MirrorList(pydantic.BaseModel): async def get_mirrorlist( arch: str, repo = fastapi.Depends(get_repo_from_path), - current_address: ipaddress.IPv6Address | ipaddress.IPv4Address = fastapi.Depends(get_current_address), + current_address: ipaddress.IPv6Address | ipaddress.IPv4Address = \ + fastapi.Depends(util.get_current_address), ) -> MirrorList: # Check if we support the architecture if not arch in pakfire.supported_arches(): diff --git a/src/api/downloads.py b/src/api/downloads.py index f329e865..f34eccb0 100644 --- a/src/api/downloads.py +++ b/src/api/downloads.py @@ -25,6 +25,7 @@ import stat from . import app from . import backend +from . import util # Create a new router for all endpoints router = fastapi.APIRouter( @@ -58,16 +59,11 @@ async def head(path: str) -> fastapi.Response: }, ) -async def get_current_address( - request: fastapi.Request -) -> ipaddress.IPv6Address | ipaddress.IPv4Address: - return ipaddress.ip_address(request.client.host) - - @router.get("/{path:path}", include_in_schema=False) async def get( - path: str, current_address: ipaddress.IPv6Address | ipaddress.IPv4Address = \ - fastapi.Depends(get_current_address), + path: str, + current_address: ipaddress.IPv6Address | ipaddress.IPv4Address = \ + fastapi.Depends(util.get_current_address), ) -> fastapi.responses.RedirectResponse: """ Handle any downloads @@ -76,8 +72,6 @@ async def get( if not await backend.stat(path, stat.S_IFREG): raise fastapi.HTTPException(404) - print(path) - # Tell the clients to never cache the redirect headers = { "Cache-Control" : "no-store", diff --git a/src/api/util.py b/src/api/util.py new file mode 100644 index 00000000..efed660e --- /dev/null +++ b/src/api/util.py @@ -0,0 +1,30 @@ +############################################################################### +# # +# Pakfire - The IPFire package management system # +# Copyright (C) 2025 Pakfire development team # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +import fastapi +import ipaddress + +async def get_current_address( + request: fastapi.Request +) -> ipaddress.IPv6Address | ipaddress.IPv4Address: + """ + Returns the IP address of the client + """ + return ipaddress.ip_address(request.client.host)