From a988b66ccd668d20e91c9de8d717e804d82198a2 Mon Sep 17 00:00:00 2001 From: coward Date: Thu, 21 Mar 2024 17:29:14 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E6=B7=BB=E5=8A=A0=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E6=9C=8D=E5=8A=A1=E7=AB=AF=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E3=80=81=E5=90=AF=E5=8A=A8=E3=80=81=E5=81=9C=E6=AD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- command/wireguard.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 command/wireguard.go diff --git a/command/wireguard.go b/command/wireguard.go new file mode 100644 index 0000000..54db1b0 --- /dev/null +++ b/command/wireguard.go @@ -0,0 +1,69 @@ +package command + +import ( + "fmt" + "gitee.ltd/lxh/logger/log" + "os/exec" + "strings" + "wireguard-dashboard/repository" +) + +// RestartWireguard +// @description: 重启wireguard服务端 +func RestartWireguard() { + configPath, err := repository.System().GetServerSetting() + if err != nil { + log.Errorf("获取服务端配置失败: %v", err.Error()) + return + } + + if configPath.ConfigFilePath != "" { + configPath.ConfigFilePath = strings.Split(configPath.ConfigFilePath, "/")[len(strings.Split(configPath.ConfigFilePath, "/"))-1] + } + + if err = exec.Command(fmt.Sprintf("wg-quick down %s", configPath.ConfigFilePath)).Run(); err != nil { + log.Errorf("停止wireguard服务端失败: %v", err.Error()) + } + + if err = exec.Command(fmt.Sprintf("wg-quick up %s", configPath.ConfigFilePath)).Run(); err != nil { + log.Errorf("启动wireguard服务端失败: %v", err.Error()) + } + + return +} + +// StopWireguard +// @description: 停止服务端 +func StopWireguard() { + configPath, err := repository.System().GetServerSetting() + if err != nil { + log.Errorf("获取服务端配置失败: %v", err.Error()) + return + } + + if configPath.ConfigFilePath != "" { + configPath.ConfigFilePath = strings.Split(configPath.ConfigFilePath, "/")[len(strings.Split(configPath.ConfigFilePath, "/"))-1] + } + + if err = exec.Command(fmt.Sprintf("wg-quick down %s", configPath.ConfigFilePath)).Run(); err != nil { + log.Errorf("停止wireguard服务端失败: %v", err.Error()) + } +} + +// StartWireguard +// @description: 启动服务端 +func StartWireguard() { + configPath, err := repository.System().GetServerSetting() + if err != nil { + log.Errorf("获取服务端配置失败: %v", err.Error()) + return + } + + if configPath.ConfigFilePath != "" { + configPath.ConfigFilePath = strings.Split(configPath.ConfigFilePath, "/")[len(strings.Split(configPath.ConfigFilePath, "/"))-1] + } + + if err = exec.Command(fmt.Sprintf("wg-quick up %s", configPath.ConfigFilePath)).Run(); err != nil { + log.Errorf("启动wireguard服务端失败: %v", err.Error()) + } +}