From 8899bb02bfc95d8d664491b3d3afd0a970fa35de Mon Sep 17 00:00:00 2001
From: tw <987146971@qq.com>
Date: Fri, 21 Jun 2024 14:33:42 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=9B=B4=E6=96=B0?=
=?UTF-8?q?=E8=AF=81=E4=B9=A6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.gitignore | 6 ++++
main.go | 50 +++++++++++++++++-----------
templates/index.tmpl | 79 +++++++++++++++++++++++++++++++++++++++-----
3 files changed, 106 insertions(+), 29 deletions(-)
create mode 100644 .gitignore
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b707f3b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+.idea
+.vscode
+
+bin
+ovpn.db
+openvpn-web
\ No newline at end of file
diff --git a/main.go b/main.go
index 10ea509..daa1a32 100644
--- a/main.go
+++ b/main.go
@@ -436,33 +436,43 @@ func main() {
ovpn.POST("/server", func(c *gin.Context) {
a := c.PostForm("action")
- if a == "settings" {
+ switch a {
+ case "settings":
k := c.PostForm("key")
+ v := c.PostForm("value")
+
if k == "auth-user" {
- restartCmd := "supervisorctl stop openvpn && sleep 2 && supervisorctl start openvpn"
- if v := c.PostForm("value"); v == "true" {
- cmd := exec.Command("sh", "-c", fmt.Sprintf("sed -i 's/^#auth-user-pass-verify/auth-user-pass-verify/' $OVPN_DATA/server.conf && %s", restartCmd))
-
- if out, err := cmd.CombinedOutput(); err != nil {
- if out == nil {
- out = []byte(err.Error())
- }
- logger.Error(context.Background(), string(out))
- c.JSON(http.StatusInternalServerError, gin.H{"message": "启用用户认证失败"})
- } else {
- c.JSON(http.StatusOK, gin.H{"message": "启用用户认证成功"})
+ msg := "停用"
+ if v == "true" {
+ msg = "启用"
+ }
+ cmd := exec.Command("sh", "-c", fmt.Sprintf("/usr/bin/docker-entrypoint.sh auth %s", v))
+ if out, err := cmd.CombinedOutput(); err != nil {
+ if out == nil {
+ out = []byte(err.Error())
}
+ logger.Error(context.Background(), string(out))
+ c.JSON(http.StatusInternalServerError, gin.H{"message": fmt.Sprintf("%s用户认证失败", msg)})
} else {
- cmd := exec.Command("sh", "-c", fmt.Sprintf("sed -i 's/^auth-user-pass-verify/#&/' $OVPN_DATA/server.conf && %s", restartCmd))
- if out, err := cmd.CombinedOutput(); err != nil {
- logger.Error(context.Background(), string(out))
- c.JSON(http.StatusInternalServerError, gin.H{"message": "停用用户认证失败"})
- } else {
- c.JSON(http.StatusOK, gin.H{"message": "停用用户认证成功"})
- }
+ c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("%s用户认证成功", msg)})
+ }
+ }
+ case "renewcert":
+ cmd := exec.Command("sh", "-c", "/usr/bin/docker-entrypoint.sh renewcert")
+ if out, err := cmd.CombinedOutput(); err != nil {
+ if out == nil {
+ out = []byte(err.Error())
}
+ logger.Error(context.Background(), string(out))
+ c.JSON(http.StatusInternalServerError, gin.H{"message": "更新证书失败"})
+ return
}
+
+ c.JSON(http.StatusOK, gin.H{"message": "更新证书成功"})
+ default:
+ c.JSON(http.StatusUnprocessableEntity, gin.H{"message": "未知操作"})
}
+
})
ovpn.POST("/kill", func(c *gin.Context) {
diff --git a/templates/index.tmpl b/templates/index.tmpl
index e8218ff..ea902f3 100644
--- a/templates/index.tmpl
+++ b/templates/index.tmpl
@@ -14,6 +14,10 @@