flowerstonezl hace 2 meses
padre
commit
cfd48b46b9
Se han modificado 2 ficheros con 94 adiciones y 2 borrados
  1. 1 0
      backup/urls.py
  2. 93 2
      backup/views.py

+ 1 - 0
backup/urls.py

@@ -6,4 +6,5 @@ urlpatterns = [
     path('list/', views.list_backups, name='list_backups'),
     path('point/', views.restore_to_point, name='restore_to_point'),
     path('shutdown/', views.shutdown_system, name='shutdown_system'),
+    path('poweroff/', views.shutdown_computer, name='poweroff'),
 ]

+ 93 - 2
backup/views.py

@@ -15,6 +15,8 @@ from django.utils import timezone
 import threading
 import sys
 import signal
+import platform
+import subprocess
 
 logger = logging.getLogger(__name__)
 
@@ -479,14 +481,14 @@ def shutdown_system(request):
             ip_address = request.META.get('REMOTE_ADDR', '未知IP')
             log_success_operation(
                 request=request,
-                operation_content=f"远程关闭系统请求,延迟时间: {delay}秒",
+                operation_content=f"延迟时间: {delay}秒",
                 operation_level="critical",
                 module_name="系统管理"
             )
         except Exception as log_error:
             logger.error(f"关闭请求日志记录失败: {str(log_error)}")
         
-        logger.critical(f"收到系统关闭请求,将在 {delay} 秒后关闭系统")
+        logger.critical(f"将在 {delay} 秒后关闭系统")
         
         # 启动延迟关闭
         _shutdown_system(delay=delay)
@@ -512,3 +514,92 @@ def shutdown_system(request):
             'status': 'error',
             'message': f'处理关闭请求失败: {str(e)}'
         }, status=500)
+
+@csrf_exempt
+@require_POST
+def shutdown_computer(request):
+    
+    try:
+        data = json.loads(request.body) if request.body else {}
+        delay = int(data.get('delay', 60)) 
+        
+        # 验证延迟时间范围(0-600秒,即10分钟)
+        if delay < 0 or delay > 600:
+            delay = 60
+        
+        # 根据操作系统执行不同的关闭命令
+        system = platform.system()
+        try:
+            if system == "Windows":
+                
+                subprocess.run(["shutdown", "/s", "/t", str(delay), "/f"], check=True)
+            elif system == "Linux":
+                
+                subprocess.run(["shutdown", "-h", "+" + str(delay // 60) if delay >= 60 else "now"], check=True)
+            elif system == "Darwin":
+               
+                subprocess.run(["sudo", "shutdown", "-h", "+" + str(delay // 60) if delay >= 60 else "now"], check=True)
+            else:
+                return JsonResponse({
+                    'status': 'error',
+                    'message': f'不支持的操作系统: {system}'
+                }, status=400)
+            
+            return JsonResponse({
+                'status': 'success',
+                'message': f' {delay} 秒',
+                'delay': delay,
+                'system': system
+            })
+            
+        except subprocess.CalledProcessError as e:
+            logger.error(f"执行失败: {str(e)}")
+            try:
+                log_failure_operation(
+                    request=request,
+                    operation_content=f"系统命令失败 - {str(e)}",
+                    operation_level="critical",
+                    module_name="系统管理"
+                )
+            except Exception as log_error:
+                logger.error(f"关闭失败日志记录失败: {str(log_error)}")
+            return JsonResponse({
+                'status': 'error',
+                'message': f'执行关闭命令失败: {str(e)}'
+            }, status=500)
+        except PermissionError:
+            logger.error("关闭电脑失败: 权限不足")
+            try:
+                log_failure_operation(
+                    request=request,
+                    operation_content=" 权限不足",
+                    operation_level="critical",
+                    module_name="系统管理"
+                )
+            except Exception as log_error:
+                logger.error(f"日志记录失败: {str(log_error)}")
+            return JsonResponse({
+                'status': 'error',
+                'message': '权限不足,无法关闭电脑'
+            }, status=403)
+        
+    except ValueError:
+        return JsonResponse({
+            'status': 'error',
+            'message': '无效的延迟时间参数'
+        }, status=400)
+    except Exception as e:
+        logger.error(f"处理关闭电脑请求失败: {str(e)}", exc_info=True)
+        try:
+            log_failure_operation(
+                request=request,
+                operation_content=f"远程关闭电脑请求失败: {str(e)}",
+                operation_level="critical",
+                module_name="系统管理"
+            )
+        except Exception as log_error:
+            logger.error(f"关闭失败日志记录失败: {str(log_error)}")
+        return JsonResponse({
+            'status': 'error',
+            'message': f'处理关闭请求失败: {str(e)}'
+        }, status=500)