From 1cb2a919d192378490625dfe9a0e8ad58ccdb9a0 Mon Sep 17 00:00:00 2001 From: coward Date: Thu, 8 Aug 2024 15:30:09 +0800 Subject: [PATCH] =?UTF-8?q?:art:=E4=B8=80=E4=BA=9B=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=92=8C=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/api/client.go | 8 +++++++- http/api/setting.go | 31 +++++++++++++++++++++++++++++-- http/param/setting.go | 5 +++-- http/router/setting.go | 8 +++++--- http/vo/setting.go | 9 +++++++++ service/setting.go | 14 +++++++++++++- utils/network.go | 2 +- 7 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 http/vo/setting.go diff --git a/http/api/client.go b/http/api/client.go index abf94dd..de3efce 100644 --- a/http/api/client.go +++ b/http/api/client.go @@ -162,7 +162,13 @@ func (ClientApi) GenerateIP(c *gin.Context) { } ips := utils.Network().GenerateIPByIPS(serverInfo.Address, assignIPS...) - response.R(c).OkWithData(ips) + + clientIPS := ips + serverIPS := serverInfo.Address + response.R(c).OkWithData(map[string]any{ + "clientIPS": clientIPS, + "serverIPS": serverIPS, + }) } // Download diff --git a/http/api/setting.go b/http/api/setting.go index 9cafa91..4f3e755 100644 --- a/http/api/setting.go +++ b/http/api/setting.go @@ -6,6 +6,7 @@ import ( "wireguard-ui/http/response" "wireguard-ui/model" "wireguard-ui/service" + "wireguard-ui/utils" ) type setting struct{} @@ -26,8 +27,9 @@ func (setting) Set(c *gin.Context) { } if err := service.Setting().SetData(&model.Setting{ - Code: p.Code, - Data: p.Data, + Code: p.Code, + Data: p.Data, + Describe: p.Describe, }); err != nil { response.R(c).FailedWithError(err) return @@ -74,3 +76,28 @@ func (setting) GetSetting(c *gin.Context) { response.R(c).OkWithData(data.Data) } + +// GetAllSetting +// @description: 获取全部配置 +// @receiver setting +// @param c +func (setting) GetAllSetting(c *gin.Context) { + // 不查询的配置 + var blackList = []string{"WG_SETTING", "WG_SERVER"} + + data, err := service.Setting().GetAllSetting(blackList) + if err != nil { + response.R(c).FailedWithError("获取配置失败") + return + } + + response.R(c).OkWithData(data) +} + +// GetPublicAddr +// @description: 获取公网地址 +// @receiver setting +// @param c +func (setting) GetPublicAddr(c *gin.Context) { + response.R(c).OkWithData(utils.Network().GetHostPublicIP()) +} diff --git a/http/param/setting.go b/http/param/setting.go index 06a0d33..401ae99 100644 --- a/http/param/setting.go +++ b/http/param/setting.go @@ -3,6 +3,7 @@ package param // SetSetting // @description: 添加/编辑设置 type SetSetting struct { - Code string `json:"code" form:"code" binding:"required"` - Data string `json:"data" form:"data" binding:"required"` + Code string `json:"code" form:"code" binding:"required"` + Data string `json:"data" form:"data" binding:"required"` + Describe string `json:"describe" form:"describe" binding:"omitempty"` } diff --git a/http/router/setting.go b/http/router/setting.go index eb69407..723b0f5 100644 --- a/http/router/setting.go +++ b/http/router/setting.go @@ -11,8 +11,10 @@ import ( func SettingApi(r *gin.RouterGroup) { setting := r.Group("setting") { - setting.POST("", api.Setting().Set) // 新增/编辑设置 - setting.DELETE("/:code", api.Setting().Delete) // 删除配置 - setting.GET("", api.Setting().GetSetting) // 获取指定配置 + setting.POST("", api.Setting().Set) // 新增/编辑设置 + setting.DELETE("/:code", api.Setting().Delete) // 删除配置 + setting.GET("", api.Setting().GetSetting) // 获取指定配置 + setting.GET("/all", api.Setting().GetAllSetting) // 获取全部配置 + setting.GET("/public-addr", api.Setting().GetPublicAddr) // 获取公网IP } } diff --git a/http/vo/setting.go b/http/vo/setting.go new file mode 100644 index 0000000..aece1d6 --- /dev/null +++ b/http/vo/setting.go @@ -0,0 +1,9 @@ +package vo + +// SettingItem +// @description: 设置单项 +type SettingItem struct { + Code string `json:"code"` + Data string `json:"data"` + Describe string `json:"describe"` +} diff --git a/service/setting.go b/service/setting.go index 87baf02..c7a2fd4 100644 --- a/service/setting.go +++ b/service/setting.go @@ -4,6 +4,7 @@ import ( "encoding/json" "gorm.io/gorm" gdb "wireguard-ui/global/client" + "wireguard-ui/http/vo" "wireguard-ui/model" "wireguard-ui/template/render_data" ) @@ -23,7 +24,7 @@ func (s setting) SetData(data *model.Setting) error { // 存在就更新,反之新增 if count > 0 { - return s.Save(&data).Error + return s.Where("code = ?", data.Code).Updates(&data).Error } return s.Create(&data).Error @@ -72,3 +73,14 @@ func (s setting) GetWGServerForConfig() (data *render_data.Server, err error) { data.Table = gs.Table return } + +// GetAllSetting +// @description: 获取全部配置 +// @receiver s +// @param blackList +// @return data +// @return err +func (s setting) GetAllSetting(blackList []string) (data []vo.SettingItem, err error) { + err = s.Model(&model.Setting{}).Select("code, data, describe").Where("code not in ?", blackList).Find(&data).Error + return +} diff --git a/utils/network.go b/utils/network.go index 4068268..1285d3b 100644 --- a/utils/network.go +++ b/utils/network.go @@ -121,7 +121,7 @@ func (n network) GenerateIPByIPS(ips []string, assignedIPS ...string) []string { } } suffix = cast.ToString(cast.ToInt64(suffix) + 1) - oips = append(oips, fmt.Sprintf("%s.%s", prefix, suffix)) + oips = append(oips, fmt.Sprintf("%s.%s/32", prefix, suffix)) } return oips