diff --git a/component/jwt.go b/component/jwt.go index a4eb0a5..c99efd8 100644 --- a/component/jwt.go +++ b/component/jwt.go @@ -16,7 +16,6 @@ const Secret = "IK8MSs76Pb2VJxleTDadf1Wzu3h9QROLv0XtmnCUErYgBG5wAyjk4cioqFZHNpZG type JwtClaims struct { ID string `json:"id"` - Name string `json:"name"` jwt.RegisteredClaims `json:"-"` } diff --git a/http/api/server.go b/http/api/server.go index 8401588..5f59c93 100644 --- a/http/api/server.go +++ b/http/api/server.go @@ -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) } diff --git a/http/api/setting.go b/http/api/setting.go index 3c378fe..7d43046 100644 --- a/http/api/setting.go +++ b/http/api/setting.go @@ -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" diff --git a/http/param/client.go b/http/param/client.go index 74382b7..8df2078 100644 --- a/http/param/client.go +++ b/http/param/client.go @@ -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 diff --git a/http/param/server.go b/http/param/server.go index db47877..0414ee0 100644 --- a/http/param/server.go +++ b/http/param/server.go @@ -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"` } diff --git a/http/param/setting.go b/http/param/setting.go index 0ceab39..e799e32 100644 --- a/http/param/setting.go +++ b/http/param/setting.go @@ -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"` // 配置文件对外输出目录 } diff --git a/model/entity/wireguard.go b/model/entity/wireguard.go index 600975f..206993b 100644 --- a/model/entity/wireguard.go +++ b/model/entity/wireguard.go @@ -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"` } diff --git a/model/vo/client.go b/model/vo/client.go index 7a8bb55..d8d762e 100644 --- a/model/vo/client.go +++ b/model/vo/client.go @@ -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"` } diff --git a/model/vo/wireguard.go b/model/vo/wireguard.go index 7e3dc4c..80227d7 100644 --- a/model/vo/wireguard.go +++ b/model/vo/wireguard.go @@ -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"` } diff --git a/queues/async_wg_config.go b/queues/async_wg_config.go index c1a57d0..4007fb0 100644 --- a/queues/async_wg_config.go +++ b/queues/async_wg_config.go @@ -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), }) } diff --git a/repository/client.go b/repository/client.go index 70d41f5..f7ae4ec 100644 --- a/repository/client.go +++ b/repository/client.go @@ -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 diff --git a/repository/server.go b/repository/server.go index 187d85a..fd6e756 100644 --- a/repository/server.go +++ b/repository/server.go @@ -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 }