:art:新增用户头像更换、客户端邮件通知

This commit is contained in:
coward
2024-06-13 11:28:02 +08:00
parent d1bb49c208
commit 67f394f136
12 changed files with 226 additions and 143 deletions

View File

@@ -1,13 +1,19 @@
package utils
import (
"encoding/json"
"errors"
"fmt"
"github.com/spf13/cast"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"math/rand"
"os"
"slices"
"strings"
"wireguard-dashboard/client"
"wireguard-dashboard/model/entity"
"wireguard-dashboard/model/template_data"
"wireguard-dashboard/model/vo"
)
type wireguard struct{}
@@ -68,6 +74,58 @@ func (w wireguard) GenerateClientIP(serverIP, rule string, assignedIPS ...string
return fmt.Sprintf("%s.%s", prefix, suffix)
}
// GenerateClientFile
// @description: 生成客户端临时配置文件
// @receiver w
// @param clientInfo
// @param setting
// @return tmpFilePath
// @return err
func (w wireguard) GenerateClientFile(clientInfo *entity.Client, setting *vo.ServerSetting) (tmpFilePath string, err error) {
var keys template_data.Keys
_ = json.Unmarshal([]byte(clientInfo.Keys), &keys)
var serverDNS []string
if *clientInfo.UseServerDns == 1 {
serverDNS = setting.DnsServer
}
// 处理一下数据
execData := template_data.ClientConfig{
PrivateKey: keys.PrivateKey,
IpAllocation: clientInfo.IpAllocation,
MTU: setting.MTU,
DNS: strings.Join(serverDNS, ","),
PublicKey: clientInfo.Server.PublicKey,
PresharedKey: keys.PresharedKey,
AllowedIPS: clientInfo.AllowedIps,
Endpoint: setting.EndpointAddress,
ListenPort: clientInfo.Server.ListenPort,
PersistentKeepalive: setting.PersistentKeepalive,
}
// 不同环境下处理文件路径
var outPath = "/tmp/" + fmt.Sprintf("%s.conf", clientInfo.Name)
var templatePath = "./template/wg.client.conf"
if os.Getenv("GIN_MODE") != "release" {
outPath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\" + fmt.Sprintf("%s.conf", clientInfo.Name)
templatePath = "E:\\Workspace\\Go\\wireguard-dashboard\\template\\wg.client.conf"
}
// 渲染数据
parseTemplate, err := Template().Parse(templatePath)
if err != nil {
return "", errors.New("读取模板文件失败")
}
err = Template().Execute(parseTemplate, execData, outPath)
if err != nil {
return "", errors.New("文件渲染失败")
}
return outPath, nil
}
// random
// @description: 随机模式
// @receiver w