From 87926a8acd52e641641816f6eba58b115b2dced4 Mon Sep 17 00:00:00 2001 From: Aaron3S Date: Thu, 12 Mar 2026 19:43:15 +0800 Subject: [PATCH] feat: add hostname api --- apps/jumpserver/api/__init__.py | 3 ++- apps/jumpserver/api/hostname.py | 17 +++++++++++++++++ apps/jumpserver/urls.py | 1 + 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 apps/jumpserver/api/hostname.py diff --git a/apps/jumpserver/api/__init__.py b/apps/jumpserver/api/__init__.py index d60681e7a..d0bc9b7bd 100644 --- a/apps/jumpserver/api/__init__.py +++ b/apps/jumpserver/api/__init__.py @@ -1,4 +1,5 @@ from .aggregate import * from .dashboard import IndexApi from .health import PrometheusMetricsApi, HealthCheckView -from .search import GlobalSearchView \ No newline at end of file +from .search import GlobalSearchView +from .hostname import HostnameView diff --git a/apps/jumpserver/api/hostname.py b/apps/jumpserver/api/hostname.py new file mode 100644 index 000000000..ce266ea24 --- /dev/null +++ b/apps/jumpserver/api/hostname.py @@ -0,0 +1,17 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework import status +from rest_framework.permissions import AllowAny + + +## 此 api 返回 /etc/hostname 的值, 可以匿名访问 +class HostnameView(APIView): + permission_classes = (AllowAny,) + + def get(self, request): + try: + with open('/etc/hostname', 'r') as f: + hostname = f.read().strip() or "Unknown" + return Response({"hostname": hostname}) + except Exception as e: + return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/apps/jumpserver/urls.py b/apps/jumpserver/urls.py index 6e53b62d9..979c88c3d 100644 --- a/apps/jumpserver/urls.py +++ b/apps/jumpserver/urls.py @@ -66,6 +66,7 @@ urlpatterns = [ path('api/v1/', include(api_v1)), path('api/health/', api.HealthCheckView.as_view(), name="health"), path('api/v1/health/', api.HealthCheckView.as_view(), name="health_v1"), + path('api/v1/hostname/', api.HostnameView.as_view(), name="hostname"), # External apps url path('core/auth/captcha/', include('captcha.urls')), path('core/', include(app_view_patterns)),