wireguard-dashboard/command/wireguard.go

70 lines
2.0 KiB
Go

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