]> git.ipfire.org Git - pbs.git/commitdiff
api: Move function to fetch the client's IP address
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 3 Jul 2025 08:13:02 +0000 (08:13 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 3 Jul 2025 08:13:02 +0000 (08:13 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/api/distros.py
src/api/downloads.py
src/api/util.py [new file with mode: 0644]

index 5177de4631cc8055a0a69d7735cd5b89500bf6dd..24567ece9fea8994faa44b75decac5bf029a0111 100644 (file)
@@ -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
 
index bd2c07a75e6f8b720869929a6b0d4e24ffb98228..1ff9bd88468b259f781a0dba771082e2139ea980 100644 (file)
@@ -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():
index f329e86598857d89b77c346ea07f4798fbf99409..f34eccb03ffe6b9d78e46a29f2850ee3a73e7771 100644 (file)
@@ -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 (file)
index 0000000..efed660
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+
+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)