From 72b847359169e56efffd281ecf6981d8f5be7c91 Mon Sep 17 00:00:00 2001 From: coward Date: Tue, 24 Sep 2024 10:23:20 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=E5=AF=BC=E5=85=A5=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E7=9A=84=E5=85=AC=E7=BD=91IP=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E6=89=8B=E5=8A=A8=E5=8E=BB=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/setting.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) 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