🎨几乎全部完成

This commit is contained in:
coward
2024-08-12 16:52:28 +08:00
parent 49a851ac92
commit 6af6eec866
8 changed files with 304 additions and 21 deletions

View File

@@ -14,6 +14,7 @@ import (
"wireguard-ui/config"
"wireguard-ui/global/client"
"wireguard-ui/model"
"wireguard-ui/service"
"wireguard-ui/template/render_data"
)
@@ -113,20 +114,22 @@ func (w WireguardComponent) GenerateClientFile(clientInfo *model.Client, server
// @receiver w
// @return error
func (w WireguardComponent) ServerControl(filePath string) {
switch config.Config.Wireguard.RestartMode {
case "NOW": // 立即执行
w.watchConfigFile(filePath)
case "DELAY": // 延迟执行
time.Sleep(time.Duration(config.Config.Wireguard.DelayTime) * time.Second)
w.watchConfigFile(filePath)
if filePath == "" {
data, err := service.Setting().GetWGSetForConfig()
if err != nil {
log.Errorf("获取服务端配置失败: %v", err.Error())
return
}
filePath = data.ConfigFilePath
}
w.watchConfigFile(filePath, config.Config.Wireguard.RestartMode, config.Config.Wireguard.DelayTime)
}
// watchConfigFile
// @description: 监听并重新操作配置文件
// @receiver w
// @param filepath
func (w WireguardComponent) watchConfigFile(filepath string) {
func (w WireguardComponent) watchConfigFile(filepath string, mode string, delay int64) {
go func() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
@@ -146,7 +149,14 @@ func (w WireguardComponent) watchConfigFile(filepath string) {
}
if event.Op == fsnotify.Write {
command.RestartWireguard(true)
switch mode {
case "NOW":
command.RestartWireguard(false, filepath)
case "DELAY":
time.Sleep(time.Duration(delay) * time.Second)
command.RestartWireguard(true, filepath)
}
}
// 打印监听事件