🎨一些优化和更改

This commit is contained in:
coward 2024-08-08 15:30:09 +08:00
parent 4cc3c075ed
commit 1cb2a919d1
7 changed files with 67 additions and 10 deletions

View File

@ -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

View File

@ -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())
}

View File

@ -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"`
}

View File

@ -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
}
}

9
http/vo/setting.go Normal file
View File

@ -0,0 +1,9 @@
package vo
// SettingItem
// @description: 设置单项
type SettingItem struct {
Code string `json:"code"`
Data string `json:"data"`
Describe string `json:"describe"`
}

View File

@ -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
}

View File

@ -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