fix some bug and standardize the code (#2)

This commit is contained in:
Li4n0
2021-04-22 16:40:22 +08:00
committed by GitHub
parent 960b2ab5ea
commit 72c6150517
10 changed files with 45 additions and 55 deletions
+19 -31
View File
@@ -25,41 +25,29 @@ type BaseRule struct {
Notice bool `gorm:"default:false;not null;" form:"notice" json:"notice"`
}
func compileCatcher(flagFormat string) (reg *regexp.Regexp, err error) {
if reg, err := regexp.Compile(flagFormat); err == nil {
return reg, nil
} else {
// * meaning record all connections.
if flagFormat != "*" {
return nil, err
} else {
return nil, nil
}
}
}
func (br BaseRule) Match(s string) (flag, flagGroup string) {
if br.flagCatcher == nil && br.FlagFormat != "*" {
// compile rule flags
catcher, err := compileCatcher(br.FlagFormat)
if err != nil {
log.Error("%s(rule:%s)", err.Error(), br.Name)
if br.flagCatcher == nil {
if br.FlagFormat == "*" {
flag = "*"
return
} else {
if catcher, err := regexp.Compile(br.FlagFormat); err != nil {
log.Error("%s(rule:%s)", err.Error(), br.Name)
return
} else {
br.flagCatcher = catcher
}
}
br.flagCatcher = catcher
}
if br.flagCatcher == nil {
// capture all connection.
flag = "*"
} else {
matched := br.flagCatcher.FindStringSubmatch(s)
if len(matched) == 0 {
return
}
flag = matched[0]
if len(matched) > 1 {
flagGroup = matched[1]
}
matched := br.flagCatcher.FindStringSubmatch(s)
if len(matched) == 0 {
return
}
flag = matched[0]
if len(matched) > 1 {
flagGroup = matched[1]
}
return flag, flagGroup
}