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()) + } +}