🐛导入配置时,获取当前系统的公网IP,避免手动去更改
All checks were successful
continuous-integration/drone/tag Build is passing

This commit is contained in:
coward 2024-09-24 10:23:20 +08:00
parent 13e4006592
commit 72b8473591

View File

@ -2,6 +2,7 @@ package service
import (
"encoding/json"
"fmt"
slog "gitee.ltd/lxh/logger/log"
"gorm.io/gorm"
"strings"
@ -10,6 +11,7 @@ import (
"wireguard-ui/http/vo"
"wireguard-ui/model"
"wireguard-ui/template/render_data"
"wireguard-ui/utils"
)
type setting struct{ *gorm.DB }
@ -151,6 +153,9 @@ func (s setting) Export() (data vo.Export, err error) {
// @param data
// @return err
func (s setting) Import(data *vo.Export, loginUser *vo.User) (err error) {
// 获取导入系统的公网IP地址
pubAddr := utils.Network().GetHostPublicIP()
data.Global.EndpointAddress = pubAddr
// 先更新global配置
gst, _ := json.Marshal(data.Global)
gs := &model.Setting{
@ -179,20 +184,24 @@ func (s setting) Import(data *vo.Export, loginUser *vo.User) (err error) {
PublicKey: v.Keys.PublicKey,
PresharedKey: v.Keys.PresharedKey,
}
cc := param.SaveClient{
Name: v.Name,
Email: v.Email,
IpAllocation: v.IpAllocation,
AllowedIps: v.AllowedIps,
ExtraAllowedIps: v.ExtraAllowedIps,
Endpoint: v.Endpoint,
UseServerDns: v.UseServerDns,
Keys: keys,
Enabled: v.Enabled,
OfflineMonitoring: v.OfflineMonitoring,
}
if v.Endpoint != "" {
port := strings.Split(v.Endpoint, ":")[1]
endpoint := fmt.Sprintf("%s:%s", pubAddr, port)
cc.Endpoint = endpoint
}
if err := Client().SaveClient(cc, loginUser); err != nil {
slog.Errorf("客户端[%s]导入失败: %v", v.Name, err)
continue