diff --git a/service/setting.go b/service/setting.go index c833c3e..0764f5f 100644 --- a/service/setting.go +++ b/service/setting.go @@ -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