From 80b9d289c2f90a48713d6c0bec22cfce0ab45d45 Mon Sep 17 00:00:00 2001 From: laker <935009066@qq.com> Date: Fri, 24 Sep 2021 14:11:17 +0800 Subject: [PATCH] =?UTF-8?q?(update)[=E6=95=B4=E4=BD=93](nginxui=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=96=B0=E5=A2=9E)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/laker/admin/config/LakerConfig.java | 32 +++++ .../sys/controller/NginxController.java | 44 +++++- src/main/resources/application.yaml | 2 +- web/admin/view/system/nginx.html | 125 +++++++++++++++++- web/admin/view/system/onlineUser.html | 2 +- 5 files changed, 196 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/laker/admin/config/LakerConfig.java b/src/main/java/com/laker/admin/config/LakerConfig.java index 508b7af..47e1e2c 100644 --- a/src/main/java/com/laker/admin/config/LakerConfig.java +++ b/src/main/java/com/laker/admin/config/LakerConfig.java @@ -28,6 +28,38 @@ public class LakerConfig { */ private Waf waf = new Waf(); + /** + * nginx + */ + private Nginx nginx = new Nginx(); + + public static class Nginx { + /** + * nginx路径 + */ + private String path; + /** + * nginx配置文件路径 + */ + private String confPath = "file/nginx.conf"; + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getConfPath() { + return confPath; + } + + public void setConfPath(String confPath) { + this.confPath = confPath; + } + } + public static class Waf { private boolean xssEnabled = true; private boolean sqlEnabled = true; diff --git a/src/main/java/com/laker/admin/module/sys/controller/NginxController.java b/src/main/java/com/laker/admin/module/sys/controller/NginxController.java index 55367f7..7e36cef 100644 --- a/src/main/java/com/laker/admin/module/sys/controller/NginxController.java +++ b/src/main/java/com/laker/admin/module/sys/controller/NginxController.java @@ -2,9 +2,11 @@ package com.laker.admin.module.sys.controller; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; +import cn.hutool.core.util.RuntimeUtil; import cn.hutool.core.util.StrUtil; import com.github.odiszapc.nginxparser.NgxConfig; import com.github.odiszapc.nginxparser.NgxDumper; +import com.laker.admin.config.LakerConfig; import com.laker.admin.framework.model.Response; import com.laker.admin.module.sys.pojo.NginxQo; import com.laker.admin.module.sys.service.ISysDeptService; @@ -28,11 +30,16 @@ import java.util.Date; public class NginxController { @Autowired ISysDeptService sysDeptService; + @Autowired + LakerConfig lakerConfig; @GetMapping - public Response get(@RequestParam(required = false, defaultValue = "file/nginx.conf") String path) { + public Response get(@RequestParam(required = false) String path) { NgxConfig conf = null; try { + if (StrUtil.isBlank(path)) { + path = lakerConfig.getNginx().getConfPath(); + } conf = NgxConfig.read(path); } catch (IOException e) { e.printStackTrace(); @@ -45,10 +52,43 @@ public class NginxController { @PostMapping public Response update(@RequestBody NginxQo nginxQo) { if (StrUtil.isBlank(nginxQo.getPath())) { - nginxQo.setPath("file/nginx.conf"); + nginxQo.setPath(lakerConfig.getNginx().getConfPath()); } FileUtil.rename(new File(nginxQo.getPath()), "nginx.conf-bak-" + DateUtil.format(new Date(), "yyyy-MM-dd-HH-mm-ss"), true); FileUtil.writeString(nginxQo.getContext(), new File(nginxQo.getPath()), "utf-8"); return Response.ok(); } + + @PostMapping("/check") + public Response check(@RequestBody NginxQo nginxQo) { + if (StrUtil.isBlank(nginxQo.getPath())) { + nginxQo.setPath(lakerConfig.getNginx().getConfPath()); + } + String res = RuntimeUtil.execForStr("nginx -t -c " + nginxQo.getPath()); + return Response.ok(res); + } + + @PostMapping("/reload") + public Response reload(@RequestBody NginxQo nginxQo) { + if (StrUtil.isBlank(nginxQo.getPath())) { + nginxQo.setPath(lakerConfig.getNginx().getConfPath()); + } + String res = RuntimeUtil.execForStr("nginx -s reload -c " + nginxQo.getPath()); + return Response.ok(res); + } + + @PostMapping("/start") + public Response start(@RequestBody NginxQo nginxQo) { + if (StrUtil.isBlank(nginxQo.getPath())) { + nginxQo.setPath(lakerConfig.getNginx().getConfPath()); + } + String res = RuntimeUtil.execForStr("nginx -c " + nginxQo.getPath()); + return Response.ok(res); + } + + @PostMapping("/stop") + public Response stop() { + String res = RuntimeUtil.execForStr("nginx -s quit"); + return Response.ok(res); + } } \ No newline at end of file diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index d500767..6ca6da3 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -57,4 +57,4 @@ laker: waf: sql-enabled: true xss-enabled: true - excludes: /flow/* \ No newline at end of file + excludes: /flow/*,/sys/nginx/* \ No newline at end of file diff --git a/web/admin/view/system/nginx.html b/web/admin/view/system/nginx.html index c6b0c57..54aba16 100644 --- a/web/admin/view/system/nginx.html +++ b/web/admin/view/system/nginx.html @@ -9,14 +9,16 @@
- - - - + + + +
+
+
-
+
@@ -41,6 +43,7 @@
+ @@ -54,6 +57,118 @@ $("#getConf").click(function () { getConfContent(); }); + + $("#check").click(function () { + easyAdmin.http({ + url: "/sys/nginx/check", + data: JSON.stringify(form.val("nginx")), + dataType: 'json', + contentType: 'application/json', + type: 'post', + success: function (result) { + if (result.success) { + layer.msg(result.msg, + { + icon: 1, + time: 2000, + area: ['100px', '65px'], + content: "执行成功" + }); + } else { + layer.msg(result.msg, + { + icon: 2, + time: 2000, + area: ['220px', '70px'] + } + ); + } + } + }) + }); + $("#start").click(function () { + easyAdmin.http({ + url: "/sys/nginx/start", + data: JSON.stringify(form.val("nginx")), + dataType: 'json', + contentType: 'application/json', + type: 'post', + success: function (result) { + if (result.success) { + layer.msg(result.msg, + { + icon: 1, + time: 2000, + area: ['100px', '65px'], + content: "执行成功" + }); + } else { + layer.msg(result.msg, + { + icon: 2, + time: 2000, + area: ['220px', '70px'] + } + ); + } + } + }) + }); + $("#stop").click(function () { + easyAdmin.http({ + url: "/sys/nginx/stop", + dataType: 'json', + contentType: 'application/json', + type: 'post', + success: function (result) { + if (result.success) { + layer.msg(result.msg, + { + icon: 1, + time: 2000, + area: ['100px', '65px'], + content: "执行成功" + }); + } else { + layer.msg(result.msg, + { + icon: 2, + time: 2000, + area: ['220px', '70px'] + } + ); + } + } + }) + }); + $("#reload").click(function () { + easyAdmin.http({ + url: "/sys/nginx/reload", + data: JSON.stringify(form.val("nginx")), + dataType: 'json', + contentType: 'application/json', + type: 'post', + success: function (result) { + if (result.success) { + layer.msg(result.msg, + { + icon: 1, + time: 2000, + area: ['100px', '65px'], + content: "执行成功" + }); + } else { + layer.msg(result.msg, + { + icon: 2, + time: 2000, + area: ['220px', '70px'] + } + ); + } + } + }) + }); form.on('submit(save)', function (data) { // 转换 easyAdmin.http({ diff --git a/web/admin/view/system/onlineUser.html b/web/admin/view/system/onlineUser.html index 454858f..ba1ef76 100644 --- a/web/admin/view/system/onlineUser.html +++ b/web/admin/view/system/onlineUser.html @@ -14,7 +14,7 @@
- -- GitLab