feat: add type and default fields to rule group requests and update related logic
This commit is contained in:
parent
40c24fbc85
commit
994cc4bebb
@ -89,16 +89,20 @@ type (
|
|||||||
CreateRuleGroupRequest {
|
CreateRuleGroupRequest {
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
|
Type string `json:"type"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Rules string `json:"rules"`
|
Rules string `json:"rules"`
|
||||||
|
Default bool `json:"default"`
|
||||||
Enable bool `json:"enable"`
|
Enable bool `json:"enable"`
|
||||||
}
|
}
|
||||||
UpdateRuleGroupRequest {
|
UpdateRuleGroupRequest {
|
||||||
Id int64 `json:"id" validate:"required"`
|
Id int64 `json:"id" validate:"required"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
|
Type string `json:"type"`
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
Rules string `json:"rules"`
|
Rules string `json:"rules"`
|
||||||
|
Default bool `json:"default"`
|
||||||
Enable bool `json:"enable"`
|
Enable bool `json:"enable"`
|
||||||
}
|
}
|
||||||
DeleteRuleGroupRequest {
|
DeleteRuleGroupRequest {
|
||||||
|
|||||||
3
initialize/migrate/database/02007_adapte_rule.down.sql
Normal file
3
initialize/migrate/database/02007_adapte_rule.down.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE `server_rule_group`
|
||||||
|
DROP COLUMN `default`,
|
||||||
|
DROP COLUMN `type`;
|
||||||
3
initialize/migrate/database/02007_adapte_rule.up.sql
Normal file
3
initialize/migrate/database/02007_adapte_rule.up.sql
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
ALTER TABLE `server_rule_group`
|
||||||
|
ADD COLUMN `default` TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Is Default Group',
|
||||||
|
ADD COLUMN `type` VARCHAR(100) NOT NULL DEFAULT '' COMMENT 'Rule Group Type';
|
||||||
@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/perfect-panel/server/pkg/result"
|
"github.com/perfect-panel/server/pkg/result"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Create rule group
|
// CreateRuleGroupHandler Create rule group
|
||||||
func CreateRuleGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
func CreateRuleGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
var req types.CreateRuleGroupRequest
|
var req types.CreateRuleGroupRequest
|
||||||
|
|||||||
@ -55,11 +55,13 @@ func (l *CreateRuleGroupLogic) CreateRuleGroup(req *types.CreateRuleGroupRequest
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = l.svcCtx.ServerModel.InsertRuleGroup(l.ctx, &server.RuleGroup{
|
err = l.svcCtx.ServerModel.InsertRuleGroup(l.ctx, &server.RuleGroup{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Icon: req.Icon,
|
Icon: req.Icon,
|
||||||
Tags: tool.StringSliceToString(req.Tags),
|
Type: req.Type,
|
||||||
Rules: strings.Join(rs, "\n"),
|
Tags: tool.StringSliceToString(req.Tags),
|
||||||
Enable: req.Enable,
|
Rules: strings.Join(rs, "\n"),
|
||||||
|
Default: req.Default,
|
||||||
|
Enable: req.Enable,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("[CreateRuleGroup] Insert Database Error: ", logger.Field("error", err.Error()))
|
l.Errorw("[CreateRuleGroup] Insert Database Error: ", logger.Field("error", err.Error()))
|
||||||
|
|||||||
@ -36,12 +36,14 @@ func (l *UpdateRuleGroupLogic) UpdateRuleGroup(req *types.UpdateRuleGroupRequest
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = l.svcCtx.ServerModel.UpdateRuleGroup(l.ctx, &server.RuleGroup{
|
err = l.svcCtx.ServerModel.UpdateRuleGroup(l.ctx, &server.RuleGroup{
|
||||||
Id: req.Id,
|
Id: req.Id,
|
||||||
Icon: req.Icon,
|
Icon: req.Icon,
|
||||||
Name: req.Name,
|
Type: req.Type,
|
||||||
Tags: tool.StringSliceToString(req.Tags),
|
Name: req.Name,
|
||||||
Rules: strings.Join(rs, "\n"),
|
Tags: tool.StringSliceToString(req.Tags),
|
||||||
Enable: req.Enable,
|
Rules: strings.Join(rs, "\n"),
|
||||||
|
Default: req.Default,
|
||||||
|
Enable: req.Enable,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
|
||||||
|
|||||||
@ -12,6 +12,8 @@ const (
|
|||||||
RelayModeNone = "none"
|
RelayModeNone = "none"
|
||||||
RelayModeAll = "all"
|
RelayModeAll = "all"
|
||||||
RelayModeRandom = "random"
|
RelayModeRandom = "random"
|
||||||
|
RuleGroupType = "ban"
|
||||||
|
RuleGroupAuto = "auto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerFilter struct {
|
type ServerFilter struct {
|
||||||
@ -178,9 +180,11 @@ type RuleGroup struct {
|
|||||||
Id int64 `gorm:"primary_key"`
|
Id int64 `gorm:"primary_key"`
|
||||||
Icon string `gorm:"type:MEDIUMTEXT;comment:Rule Group Icon"`
|
Icon string `gorm:"type:MEDIUMTEXT;comment:Rule Group Icon"`
|
||||||
Name string `gorm:"type:varchar(100);not null;default:'';comment:Rule Group Name"`
|
Name string `gorm:"type:varchar(100);not null;default:'';comment:Rule Group Name"`
|
||||||
|
Type string `gorm:"type:varchar(100);not null;default:'';comment:Rule Group Type"`
|
||||||
Tags string `gorm:"type:text;comment:Selected Node Tags"`
|
Tags string `gorm:"type:text;comment:Selected Node Tags"`
|
||||||
Rules string `gorm:"type:MEDIUMTEXT;comment:Rules"`
|
Rules string `gorm:"type:MEDIUMTEXT;comment:Rules"`
|
||||||
Enable bool `gorm:"type:tinyint(1);not null;default:1;comment:Rule Group Enable"`
|
Enable bool `gorm:"type:tinyint(1);not null;default:1;comment:Rule Group Enable"`
|
||||||
|
Default bool `gorm:"type:tinyint(1);not null;default:0;comment:Rule Group is Default"`
|
||||||
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
CreatedAt time.Time `gorm:"<-:create;comment:Creation Time"`
|
||||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||||
}
|
}
|
||||||
|
|||||||
@ -32,6 +32,11 @@ type Announcement struct {
|
|||||||
UpdatedAt int64 `json:"updated_at"`
|
UpdatedAt int64 `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AnyTLS struct {
|
||||||
|
Port int `json:"port" validate:"required"`
|
||||||
|
SecurityConfig SecurityConfig `json:"security_config"`
|
||||||
|
}
|
||||||
|
|
||||||
type AppAuthCheckRequest struct {
|
type AppAuthCheckRequest struct {
|
||||||
Method string `json:"method" validate:"required" validate:"required,oneof=device email mobile"`
|
Method string `json:"method" validate:"required" validate:"required,oneof=device email mobile"`
|
||||||
Account string `json:"account"`
|
Account string `json:"account"`
|
||||||
@ -441,11 +446,13 @@ type CreatePaymentMethodRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CreateRuleGroupRequest struct {
|
type CreateRuleGroupRequest struct {
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
Tags []string `json:"tags"`
|
Type string `json:"type"`
|
||||||
Rules string `json:"rules"`
|
Tags []string `json:"tags"`
|
||||||
Enable bool `json:"enable"`
|
Rules string `json:"rules"`
|
||||||
|
Default bool `json:"default"`
|
||||||
|
Enable bool `json:"enable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateSubscribeGroupRequest struct {
|
type CreateSubscribeGroupRequest struct {
|
||||||
@ -1955,12 +1962,14 @@ type UpdatePaymentMethodRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UpdateRuleGroupRequest struct {
|
type UpdateRuleGroupRequest struct {
|
||||||
Id int64 `json:"id" validate:"required"`
|
Id int64 `json:"id" validate:"required"`
|
||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
Name string `json:"name" validate:"required"`
|
Type string `json:"type"`
|
||||||
Tags []string `json:"tags"`
|
Name string `json:"name" validate:"required"`
|
||||||
Rules string `json:"rules"`
|
Tags []string `json:"tags"`
|
||||||
Enable bool `json:"enable"`
|
Rules string `json:"rules"`
|
||||||
|
Default bool `json:"default"`
|
||||||
|
Enable bool `json:"enable"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UpdateSubscribeGroupRequest struct {
|
type UpdateSubscribeGroupRequest struct {
|
||||||
|
|||||||
@ -27,7 +27,7 @@ func NewAdapter(cfg *Config) *Adapter {
|
|||||||
// 转换服务器列表
|
// 转换服务器列表
|
||||||
proxies := adapterProxies(cfg.Nodes)
|
proxies := adapterProxies(cfg.Nodes)
|
||||||
// 生成代理组
|
// 生成代理组
|
||||||
proxyGroup, region := generateProxyGroup(proxies)
|
proxyGroup, nodes := generateProxyGroup(proxies)
|
||||||
|
|
||||||
// 转换规则组
|
// 转换规则组
|
||||||
g, r := adapterRules(cfg.Rules)
|
g, r := adapterRules(cfg.Rules)
|
||||||
@ -35,7 +35,8 @@ func NewAdapter(cfg *Config) *Adapter {
|
|||||||
// 加入兜底节点
|
// 加入兜底节点
|
||||||
for i, group := range g {
|
for i, group := range g {
|
||||||
if len(group.Proxies) == 0 {
|
if len(group.Proxies) == 0 {
|
||||||
g[i].Proxies = append([]string{"DIRECT"}, region...)
|
p := append([]string{"智能线路"}, nodes...)
|
||||||
|
g[i].Proxies = append(p, "REJECT", "DIRECT")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +49,7 @@ func NewAdapter(cfg *Config) *Adapter {
|
|||||||
Proxies: proxies,
|
Proxies: proxies,
|
||||||
Group: proxyGroup,
|
Group: proxyGroup,
|
||||||
Rules: r,
|
Rules: r,
|
||||||
Region: region,
|
Nodes: nodes,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,9 @@ package proxy
|
|||||||
type Adapter struct {
|
type Adapter struct {
|
||||||
Proxies []Proxy
|
Proxies []Proxy
|
||||||
Group []Group
|
Group []Group
|
||||||
Rules []string
|
Rules []string // rule
|
||||||
Region []string
|
Nodes []string // all node
|
||||||
|
Default string // Default Node
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxy represents a proxy server
|
// Proxy represents a proxy server
|
||||||
|
|||||||
@ -121,7 +121,7 @@ func adapterTags(tags map[string][]*server.Server, group []proxy.Group) (proxyGr
|
|||||||
return group
|
return group
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateProxyGroup(servers []proxy.Proxy) (proxyGroup []proxy.Group, region []string) {
|
func generateProxyGroup(servers []proxy.Proxy) (proxyGroup []proxy.Group, nodes []string) {
|
||||||
// 设置手动选择分组
|
// 设置手动选择分组
|
||||||
proxyGroup = append(proxyGroup, []proxy.Group{
|
proxyGroup = append(proxyGroup, []proxy.Group{
|
||||||
{
|
{
|
||||||
@ -139,17 +139,13 @@ func generateProxyGroup(servers []proxy.Proxy) (proxyGroup []proxy.Group, region
|
|||||||
}...)
|
}...)
|
||||||
|
|
||||||
for _, node := range servers {
|
for _, node := range servers {
|
||||||
if node.Country != "" {
|
proxyGroup = addProxyToGroup(node.Name, "智能线路", proxyGroup)
|
||||||
proxyGroup = addProxyToGroup(node.Name, node.Country, proxyGroup)
|
|
||||||
region = append(region, node.Country)
|
|
||||||
|
|
||||||
proxyGroup = addProxyToGroup(node.Country, "智能线路", proxyGroup)
|
|
||||||
}
|
|
||||||
|
|
||||||
proxyGroup = addProxyToGroup(node.Name, "手动选择", proxyGroup)
|
proxyGroup = addProxyToGroup(node.Name, "手动选择", proxyGroup)
|
||||||
|
nodes = append(nodes, node.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
proxyGroup = addProxyToGroup("DIRECT", "手动选择", proxyGroup)
|
proxyGroup = addProxyToGroup("DIRECT", "手动选择", proxyGroup)
|
||||||
return proxyGroup, tool.RemoveDuplicateElements(region...)
|
return proxyGroup, tool.RemoveDuplicateElements(nodes...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func adapterProxies(servers []*server.Server) []proxy.Proxy {
|
func adapterProxies(servers []*server.Server) []proxy.Proxy {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user