|
@@ -15,6 +15,8 @@ from django.utils import timezone
|
|
|
import threading
|
|
import threading
|
|
|
import sys
|
|
import sys
|
|
|
import signal
|
|
import signal
|
|
|
|
|
+import platform
|
|
|
|
|
+import subprocess
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
@@ -479,14 +481,14 @@ def shutdown_system(request):
|
|
|
ip_address = request.META.get('REMOTE_ADDR', '未知IP')
|
|
ip_address = request.META.get('REMOTE_ADDR', '未知IP')
|
|
|
log_success_operation(
|
|
log_success_operation(
|
|
|
request=request,
|
|
request=request,
|
|
|
- operation_content=f"远程关闭系统请求,延迟时间: {delay}秒",
|
|
|
|
|
|
|
+ operation_content=f"延迟时间: {delay}秒",
|
|
|
operation_level="critical",
|
|
operation_level="critical",
|
|
|
module_name="系统管理"
|
|
module_name="系统管理"
|
|
|
)
|
|
)
|
|
|
except Exception as log_error:
|
|
except Exception as log_error:
|
|
|
logger.error(f"关闭请求日志记录失败: {str(log_error)}")
|
|
logger.error(f"关闭请求日志记录失败: {str(log_error)}")
|
|
|
|
|
|
|
|
- logger.critical(f"收到系统关闭请求,将在 {delay} 秒后关闭系统")
|
|
|
|
|
|
|
+ logger.critical(f"将在 {delay} 秒后关闭系统")
|
|
|
|
|
|
|
|
# 启动延迟关闭
|
|
# 启动延迟关闭
|
|
|
_shutdown_system(delay=delay)
|
|
_shutdown_system(delay=delay)
|
|
@@ -512,3 +514,92 @@ def shutdown_system(request):
|
|
|
'status': 'error',
|
|
'status': 'error',
|
|
|
'message': f'处理关闭请求失败: {str(e)}'
|
|
'message': f'处理关闭请求失败: {str(e)}'
|
|
|
}, status=500)
|
|
}, 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)
|