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 {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Type string `json:"type"`
|
||||
Tags []string `json:"tags"`
|
||||
Rules string `json:"rules"`
|
||||
Default bool `json:"default"`
|
||||
Enable bool `json:"enable"`
|
||||
}
|
||||
UpdateRuleGroupRequest {
|
||||
Id int64 `json:"id" validate:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Rules string `json:"rules"`
|
||||
Default bool `json:"default"`
|
||||
Enable bool `json:"enable"`
|
||||
}
|
||||
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"
|
||||
)
|
||||
|
||||
// Create rule group
|
||||
// CreateRuleGroupHandler Create rule group
|
||||
func CreateRuleGroupHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
var req types.CreateRuleGroupRequest
|
||||
|
||||
@ -55,11 +55,13 @@ func (l *CreateRuleGroupLogic) CreateRuleGroup(req *types.CreateRuleGroupRequest
|
||||
}
|
||||
|
||||
err = l.svcCtx.ServerModel.InsertRuleGroup(l.ctx, &server.RuleGroup{
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
Tags: tool.StringSliceToString(req.Tags),
|
||||
Rules: strings.Join(rs, "\n"),
|
||||
Enable: req.Enable,
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
Type: req.Type,
|
||||
Tags: tool.StringSliceToString(req.Tags),
|
||||
Rules: strings.Join(rs, "\n"),
|
||||
Default: req.Default,
|
||||
Enable: req.Enable,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorw("[CreateRuleGroup] Insert Database Error: ", logger.Field("error", err.Error()))
|
||||
|
||||
@ -36,12 +36,14 @@ func (l *UpdateRuleGroupLogic) UpdateRuleGroup(req *types.UpdateRuleGroupRequest
|
||||
return err
|
||||
}
|
||||
err = l.svcCtx.ServerModel.UpdateRuleGroup(l.ctx, &server.RuleGroup{
|
||||
Id: req.Id,
|
||||
Icon: req.Icon,
|
||||
Name: req.Name,
|
||||
Tags: tool.StringSliceToString(req.Tags),
|
||||
Rules: strings.Join(rs, "\n"),
|
||||
Enable: req.Enable,
|
||||
Id: req.Id,
|
||||
Icon: req.Icon,
|
||||
Type: req.Type,
|
||||
Name: req.Name,
|
||||
Tags: tool.StringSliceToString(req.Tags),
|
||||
Rules: strings.Join(rs, "\n"),
|
||||
Default: req.Default,
|
||||
Enable: req.Enable,
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), err.Error())
|
||||
|
||||
@ -12,6 +12,8 @@ const (
|
||||
RelayModeNone = "none"
|
||||
RelayModeAll = "all"
|
||||
RelayModeRandom = "random"
|
||||
RuleGroupType = "ban"
|
||||
RuleGroupAuto = "auto"
|
||||
)
|
||||
|
||||
type ServerFilter struct {
|
||||
@ -178,9 +180,11 @@ type RuleGroup struct {
|
||||
Id int64 `gorm:"primary_key"`
|
||||
Icon string `gorm:"type:MEDIUMTEXT;comment:Rule Group Icon"`
|
||||
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"`
|
||||
Rules string `gorm:"type:MEDIUMTEXT;comment:Rules"`
|
||||
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"`
|
||||
UpdatedAt time.Time `gorm:"comment:Update Time"`
|
||||
}
|
||||
|
||||
@ -32,6 +32,11 @@ type Announcement struct {
|
||||
UpdatedAt int64 `json:"updated_at"`
|
||||
}
|
||||
|
||||
type AnyTLS struct {
|
||||
Port int `json:"port" validate:"required"`
|
||||
SecurityConfig SecurityConfig `json:"security_config"`
|
||||
}
|
||||
|
||||
type AppAuthCheckRequest struct {
|
||||
Method string `json:"method" validate:"required" validate:"required,oneof=device email mobile"`
|
||||
Account string `json:"account"`
|
||||
@ -441,11 +446,13 @@ type CreatePaymentMethodRequest struct {
|
||||
}
|
||||
|
||||
type CreateRuleGroupRequest struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Tags []string `json:"tags"`
|
||||
Rules string `json:"rules"`
|
||||
Enable bool `json:"enable"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Type string `json:"type"`
|
||||
Tags []string `json:"tags"`
|
||||
Rules string `json:"rules"`
|
||||
Default bool `json:"default"`
|
||||
Enable bool `json:"enable"`
|
||||
}
|
||||
|
||||
type CreateSubscribeGroupRequest struct {
|
||||
@ -1955,12 +1962,14 @@ type UpdatePaymentMethodRequest struct {
|
||||
}
|
||||
|
||||
type UpdateRuleGroupRequest struct {
|
||||
Id int64 `json:"id" validate:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Rules string `json:"rules"`
|
||||
Enable bool `json:"enable"`
|
||||
Id int64 `json:"id" validate:"required"`
|
||||
Icon string `json:"icon"`
|
||||
Type string `json:"type"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Tags []string `json:"tags"`
|
||||
Rules string `json:"rules"`
|
||||
Default bool `json:"default"`
|
||||
Enable bool `json:"enable"`
|
||||
}
|
||||
|
||||
type UpdateSubscribeGroupRequest struct {
|
||||
|
||||
@ -27,7 +27,7 @@ func NewAdapter(cfg *Config) *Adapter {
|
||||
// 转换服务器列表
|
||||
proxies := adapterProxies(cfg.Nodes)
|
||||
// 生成代理组
|
||||
proxyGroup, region := generateProxyGroup(proxies)
|
||||
proxyGroup, nodes := generateProxyGroup(proxies)
|
||||
|
||||
// 转换规则组
|
||||
g, r := adapterRules(cfg.Rules)
|
||||
@ -35,7 +35,8 @@ func NewAdapter(cfg *Config) *Adapter {
|
||||
// 加入兜底节点
|
||||
for i, group := range g {
|
||||
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,
|
||||
Group: proxyGroup,
|
||||
Rules: r,
|
||||
Region: region,
|
||||
Nodes: nodes,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,8 +4,9 @@ package proxy
|
||||
type Adapter struct {
|
||||
Proxies []Proxy
|
||||
Group []Group
|
||||
Rules []string
|
||||
Region []string
|
||||
Rules []string // rule
|
||||
Nodes []string // all node
|
||||
Default string // Default Node
|
||||
}
|
||||
|
||||
// Proxy represents a proxy server
|
||||
|
||||
@ -121,7 +121,7 @@ func adapterTags(tags map[string][]*server.Server, group []proxy.Group) (proxyGr
|
||||
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{
|
||||
{
|
||||
@ -139,17 +139,13 @@ func generateProxyGroup(servers []proxy.Proxy) (proxyGroup []proxy.Group, region
|
||||
}...)
|
||||
|
||||
for _, node := range servers {
|
||||
if node.Country != "" {
|
||||
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)
|
||||
return proxyGroup, tool.RemoveDuplicateElements(region...)
|
||||
return proxyGroup, tool.RemoveDuplicateElements(nodes...)
|
||||
}
|
||||
|
||||
func adapterProxies(servers []*server.Server) []proxy.Proxy {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user