2024-03-21 17:29:14 +08:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gitee.ltd/lxh/logger/log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
"wireguard-dashboard/repository"
|
|
|
|
)
|
|
|
|
|
2024-03-22 10:42:36 +08:00
|
|
|
// getConfigFileName
|
|
|
|
// @description: 获取服务端配置文件名称
|
|
|
|
// @return string
|
|
|
|
func getConfigFileName() string {
|
|
|
|
data, err := repository.System().GetServerSetting()
|
2024-03-21 17:29:14 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("获取服务端配置失败: %v", err.Error())
|
2024-03-22 10:42:36 +08:00
|
|
|
return ""
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|
|
|
|
|
2024-03-22 10:42:36 +08:00
|
|
|
if data.ConfigFilePath != "" {
|
|
|
|
data.ConfigFilePath = strings.Split(data.ConfigFilePath, "/")[len(strings.Split(data.ConfigFilePath, "/"))-1] // 这里取到的是wg0.conf
|
|
|
|
data.ConfigFilePath = strings.Split(data.ConfigFilePath, ".conf")[0] // 这里取到的就是wg0
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|
|
|
|
|
2024-03-22 10:42:36 +08:00
|
|
|
return data.ConfigFilePath
|
|
|
|
}
|
2024-03-21 17:29:14 +08:00
|
|
|
|
2024-03-22 10:42:36 +08:00
|
|
|
// RestartWireguard
|
|
|
|
// @description: 是否重启
|
|
|
|
// @param isAsync // 是否异步执行
|
|
|
|
func RestartWireguard(isAsync bool) {
|
|
|
|
if isAsync {
|
|
|
|
go func() {
|
|
|
|
StopWireguard() // 停止
|
|
|
|
StartWireguard() // 启动
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
StopWireguard() // 停止
|
|
|
|
StartWireguard() // 启动
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopWireguard
|
|
|
|
// @description: 停止服务端
|
|
|
|
func StopWireguard() {
|
2024-03-22 10:42:36 +08:00
|
|
|
configFileName := getConfigFileName()
|
2024-03-21 17:29:14 +08:00
|
|
|
|
2024-06-05 11:40:34 +08:00
|
|
|
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("wg-quick down %s", configFileName))
|
2024-06-05 12:04:22 +08:00
|
|
|
if err := cmd.Run(); err == nil {
|
2024-06-05 11:40:34 +08:00
|
|
|
log.Infof("停止wireguard[%s]服务端成功", configFileName)
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|
2024-03-22 10:42:36 +08:00
|
|
|
|
|
|
|
return
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartWireguard
|
|
|
|
// @description: 启动服务端
|
|
|
|
func StartWireguard() {
|
2024-03-22 10:42:36 +08:00
|
|
|
configFileName := getConfigFileName()
|
2024-03-21 17:29:14 +08:00
|
|
|
|
2024-06-05 11:40:34 +08:00
|
|
|
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf("wg-quick up %s", configFileName))
|
2024-06-05 12:04:22 +08:00
|
|
|
if err := cmd.Run(); err == nil {
|
2024-06-05 11:40:34 +08:00
|
|
|
log.Infof("启动wireguard[%s]服务端成功", configFileName)
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|
2024-03-22 10:42:36 +08:00
|
|
|
return
|
2024-03-21 17:29:14 +08:00
|
|
|
}
|