🎨做了一些bug修复和变动

This commit is contained in:
coward 2024-05-24 15:19:26 +08:00
parent e216725096
commit e172b3c838
12 changed files with 64 additions and 49 deletions

View File

@ -16,7 +16,6 @@ const Secret = "IK8MSs76Pb2VJxleTDadf1Wzu3h9QROLv0XtmnCUErYgBG5wAyjk4cioqFZHNpZG
type JwtClaims struct {
ID string `json:"id"`
Name string `json:"name"`
jwt.RegisteredClaims `json:"-"`
}

View File

@ -4,7 +4,6 @@ import (
"gitee.ltd/lxh/logger/log"
"github.com/gin-gonic/gin"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"strings"
"wireguard-dashboard/command"
"wireguard-dashboard/http/param"
"wireguard-dashboard/model/entity"
@ -45,7 +44,7 @@ func (server) SaveServer(c *gin.Context) {
}
publicKey := privateKey.PublicKey()
serverInfo := &entity.Server{
IpScope: strings.Join(p.IpScope, ","),
IpScope: p.IpScope,
ListenPort: p.ListenPort,
PrivateKey: privateKey.String(),
PublicKey: publicKey.String(),
@ -84,10 +83,6 @@ func (server) GetServer(c *gin.Context) {
log.Errorf("获取服务端信息失败: %v", err.Error())
}
if data.IpScopeStr != "" {
data.IpScope = strings.Split(data.IpScopeStr, ",")
}
utils.GinResponse(c).OKWithData(data)
}

View File

@ -51,7 +51,7 @@ func (setting) SetServerGlobal(c *gin.Context) {
return
}
data, _ := json.Marshal(p.Data)
data, _ := json.Marshal(p)
var ent entity.Setting
ent.Code = "SERVER_SETTING"

View File

@ -5,6 +5,11 @@ import "wireguard-dashboard/model/template_data"
// ClientList
// @description: 客户端列表
type ClientList struct {
Name string `json:"name" form:"name"`
Email string `json:"email" form:"email"`
Ip string `json:"ip" form:"ip"`
CreateUser string `json:"createUser" form:"createUser"`
Enabled *int `json:"enabled" form:"enabled"`
page
}
@ -26,10 +31,10 @@ type SaveClient struct {
AllowedIPS []string `json:"allowedIPS" form:"allowedIPS" binding:"required"`
ExtraAllowedIPS []string `json:"extraAllowedIPS" form:"extraAllowedIPS" binding:"omitempty"`
Endpoint string `json:"endpoint" form:"endpoint" binding:"omitempty"`
UseServerDNS int `json:"useServerDNS" form:"useServerDNS" binding:"required,oneof=1 0"`
EnabledAfterCreation int `json:"enabledAfterCreation" form:"enabledAfterCreation" binding:"required,oneof=1 0"`
UseServerDNS *int `json:"useServerDNS" form:"useServerDNS" binding:"required,oneof=1 0"`
EnabledAfterCreation *int `json:"enableAfterCreation" form:"enableAfterCreation" binding:"required,oneof=1 0"`
Keys *template_data.Keys `json:"keys" form:"keys" binding:"omitempty"`
Enabled int `json:"enabled" form:"enabled" binding:"required,oneof=1 0"`
Enabled *int `json:"enabled" form:"enabled" binding:"required,oneof=1 0"`
}
// ControlServer

View File

@ -1,10 +1,10 @@
package param
type SaveServer struct {
Id string `json:"id" form:"id" binding:"omitempty"` // id
IpScope []string `json:"ipScope" form:"ipScope" binding:"required"` // 内网ip范围段
ListenPort int `json:"listenPort" form:"listenPort" binding:"required"` // 监听端口
PostUpScript string `json:"postUpScript" form:"postUpScript" binding:"omitempty"`
PreDownScript string `json:"preDownScript" form:"preDownScript" binding:"omitempty"`
PostDownScript string `json:"postDownScript" form:"postDownScript" binding:"omitempty"`
Id string `json:"id" form:"id" binding:"omitempty"` // id
IpScope string `json:"ipScope" form:"ipScope" binding:"required"` // 内网ip范围段
ListenPort int `json:"listenPort" form:"listenPort" binding:"required"` // 监听端口
PostUpScript string `json:"postUpScript" form:"postUpScript" binding:"omitempty"`
PreDownScript string `json:"preDownScript" form:"preDownScript" binding:"omitempty"`
PostDownScript string `json:"postDownScript" form:"postDownScript" binding:"omitempty"`
}

View File

@ -11,13 +11,11 @@ type SetSetting struct {
// SetServerGlobal
// @description: 设置服务端全局配置
type SetServerGlobal struct {
Data struct {
EndpointAddress string `json:"endpointAddress" binding:"required"` // 服务公网IP
DnsServer []string `json:"dnsServer" binding:"required"` // DNS列表
MTU int `json:"MTU" binding:"required"`
PersistentKeepalive int `json:"persistentKeepalive" binding:"omitempty"`
FirewallMark string `json:"firewallMark" binding:"omitempty"`
Table string `json:"table" binding:"omitempty"`
ConfigFilePath string `json:"configFilePath" binding:"required"` // 配置文件对外输出目录
} `json:"data"`
EndpointAddress string `json:"endpointAddress" binding:"required"` // 服务公网IP
DnsServer []string `json:"dnsServer" binding:"required"` // DNS列表
MTU int `json:"MTU" binding:"required"`
PersistentKeepalive int `json:"persistentKeepalive" binding:"omitempty"`
FirewallMark string `json:"firewallMark" binding:"omitempty"`
Table string `json:"table" binding:"omitempty"`
ConfigFilePath string `json:"configFilePath" binding:"required"` // 配置文件对外输出目录
}

View File

@ -30,11 +30,11 @@ type Client struct {
AllowedIps string `json:"allowedIps" gorm:"type:varchar(255);not null;comment:'允许访问的ip'"`
ExtraAllowedIps string `json:"extraAllowedIps" gorm:"type:varchar(255);default null;comment:'额外允许的ip范围'"`
Endpoint string `json:"endpoint" gorm:"type:varchar(255);default null;comment:'端点'"`
UseServerDns int `json:"useServerDns" gorm:"type:int(1);default 1;comment:'是否使用服务端dns'"`
EnableAfterCreation int `json:"enableAfterCreation" gorm:"type:int(1);default 1;comment:'是否创建后启用'"`
UseServerDns *int `json:"useServerDns" gorm:"type:int(1);default 1;comment:'是否使用服务端dns'"`
EnableAfterCreation *int `json:"enableAfterCreation" gorm:"type:int(1);default 1;comment:'是否创建后启用'"`
Keys string `json:"keys" gorm:"type:text;default null;comment:'公钥和密钥的json串'"`
UserId string `json:"userId" gorm:"type:char(36);not null;comment:'创建人id'"`
Enabled bool `json:"enabled" gorm:"type:tinyint(1);default 1;comment:'状态0 - 禁用 | 1 - 正常)'"`
Enabled *int `json:"enabled" gorm:"type:tinyint(1);default 1;comment:'状态0 - 禁用 | 1 - 正常)'"`
User *User `json:"user" gorm:"foreignKey:UserId"`
Server *Server `json:"server" gorm:"foreignKey:ServerId"`
}

View File

@ -14,8 +14,8 @@ type Client struct {
IpAllocationStr string `json:"-" gorm:"ipAllocationStr"`
AllowedIps []string `json:"allowedIPS" gorm:"-"`
AllowedIpsStr string `json:"-" gorm:"allowedIPSStr"`
ExtraAllowedIps []string `json:"extraAllowedIPS"`
ExtraAllowedIpsStr string `json:"-" gorm:"extraAllowedIPSStr"`
ExtraAllowedIps []string `json:"extraAllowedIPS" gorm:"-"`
ExtraAllowedIpsStr string `json:"-" gorm:"extraAllowedIPSStr"` // extra_allowed_ips_str
Endpoint string `json:"endpoint"`
UseServerDNS int `json:"useServerDNS"`
EnableAfterCreation int `json:"enableAfterCreation"`
@ -23,7 +23,7 @@ type Client struct {
Keys template_data.Keys `json:"keys" gorm:"-"`
CreateUser string `json:"createUser"`
Enabled bool `json:"enabled"`
CreatedAt entity.JsonTime `json:"createAt"`
CreatedAt entity.JsonTime `json:"createdAt"`
UpdatedAt entity.JsonTime `json:"updatedAt"`
}

View File

@ -3,13 +3,12 @@ package vo
// Server
// @description: 服务端返回信息
type Server struct {
Id string `json:"id"` // id
IpScope []string `json:"ipScope" gorm:"-"` // ip范围
IpScopeStr string `json:"-" gorm:"ipScope"`
ListenPort int `json:"listenPort"` // 服务监听端口
PrivateKey string `json:"privateKey"` // 私钥
PublicKey string `json:"publicKey"` // 公钥
PostUpScript string `json:"postUpScript"`
PreDownScript string `json:"preDownScript"`
PostDownScript string `json:"postDownScript"`
Id string `json:"id"` // id
IpScope string `json:"ipScope"` // ip范围
ListenPort int `json:"listenPort"` // 服务监听端口
PrivateKey string `json:"privateKey"` // 私钥
PublicKey string `json:"publicKey"` // 公钥
PostUpScript string `json:"postUpScript"`
PreDownScript string `json:"preDownScript"`
PostDownScript string `json:"postDownScript"`
}

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"gitee.ltd/lxh/logger/log"
"github.com/spf13/cast"
"os"
"strconv"
"wireguard-dashboard/client"
@ -95,7 +96,7 @@ func asyncWireguardConfigFile() {
CreatedAt: v.CreatedAt.String(),
UpdatedAt: v.UpdatedAt.String(),
CreateUser: createUserName,
Enabled: v.Enabled,
Enabled: cast.ToBool(v.Enabled),
})
}

View File

@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"gitee.ltd/lxh/logger/log"
"github.com/spf13/cast"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"gorm.io/gorm"
"strings"
@ -34,10 +33,29 @@ func Client() clientRepo {
// @return total
// @return err
func (r clientRepo) List(p param.ClientList) (data []vo.Client, total int64, err error) {
err = r.Table("t_wg_client as twc").Scopes(utils.Page(p.Current, p.Size)).Joins("LEFT JOIN t_user as tu ON twc.user_id = tu.id").
sel := r.Table("t_wg_client as twc").
Scopes(utils.Page(p.Current, p.Size)).
Joins("LEFT JOIN t_user as tu ON twc.user_id = tu.id").
Select("twc.id", "twc.created_at", "twc.updated_at", "twc.name", "twc.email", "twc.subnet_range", "twc.ip_allocation as ip_allocation_str", "twc.allowed_ips as allowed_ips_str",
"twc.extra_allowed_ips as extra_allowed_ips_str", "twc.endpoint", "twc.use_server_dns", "twc.enable_after_creation", "twc.enabled", "twc.keys as keys_str", "tu.name as create_user").
Order("twc.created_at DESC").Find(&data).Offset(-1).Limit(-1).Count(&total).Error
"twc.extra_allowed_ips as extra_allowed_ips_str", "twc.endpoint", "twc.use_server_dns", "twc.enable_after_creation", "twc.enabled", "twc.keys as keys_str", "tu.name as create_user")
if p.Name != "" {
sel.Where("twc.name LIKE ?", "%"+p.Name+"%")
}
if p.Email != "" {
sel.Where("twc.email = ?", p.Email)
}
if p.Ip != "" {
sel.Where("twc.ip_allocation LIKE ?", "%"+p.Ip+"%")
}
if p.Enabled != nil {
sel.Where("twc.enabled = ?", p.Enabled)
}
err = sel.Order("twc.created_at DESC").Find(&data).Offset(-1).Limit(-1).Count(&total).Error
if err != nil {
return
@ -84,7 +102,7 @@ func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Cli
UseServerDns: p.UseServerDNS,
EnableAfterCreation: p.EnabledAfterCreation,
UserId: adminId,
Enabled: cast.ToBool(p.Enabled),
Enabled: p.Enabled,
}
// id不为空更新信息
@ -138,7 +156,7 @@ func (r clientRepo) Save(p param.SaveClient, adminId string) (client *entity.Cli
EnableAfterCreation: p.EnabledAfterCreation,
Keys: string(keysStr),
UserId: adminId,
Enabled: true,
Enabled: p.Enabled,
}
err = r.Model(&entity.Client{}).Create(ent).Error

View File

@ -24,7 +24,7 @@ func Server() server {
// @return data
// @return err
func (r server) GetServer() (data *vo.Server, err error) {
err = r.Model(&entity.Server{}).Select("id", "ip_scope as ip_scope_str", "listen_port", "private_key", "public_key", "post_up_script", "pre_down_script", "post_down_script").First(&data).Error
err = r.Model(&entity.Server{}).Select("id", "ip_scope", "listen_port", "private_key", "public_key", "post_up_script", "pre_down_script", "post_down_script").First(&data).Error
return
}