Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func lexProtocol(l *lexer) stateFn {
case r == ' ':
l.emit(itemProtocol, true)
return lexSourceAddress
case !(unicode.IsLetter(r) || unicode.IsDigit(r) || (l.len() > 0 && r == '-')):
case !unicode.IsLetter(r) && !unicode.IsDigit(r) && l.len() > 0 && r != '-':
return l.errorf("invalid character %q for a rule protocol", r)
}
}
Expand Down
24 changes: 12 additions & 12 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func parseContent(content string) ([]byte, error) {

b = hexRE.ReplaceAllStringFunc(b,
func(h string) string {
r, err := hex.DecodeString(strings.Replace(strings.Trim(h, "|"), " ", "", -1))
r, err := hex.DecodeString(strings.ReplaceAll(strings.Trim(h, "|"), " ", ""))
if err != nil {
panic("invalid hexRE regexp")
}
Expand Down Expand Up @@ -408,7 +408,7 @@ func unquote(s string) string {
if strings.IndexByte(s, '"') < 0 {
return s
}
return strings.Replace(s, `\"`, `"`, -1)
return strings.ReplaceAll(s, `\"`, `"`)
}

func inSlice(str string, strings []string) bool {
Expand All @@ -421,7 +421,7 @@ func inSlice(str string, strings []string) bool {
}

// comment decodes a comment (commented rule, or just a comment.)
func (r *Rule) comment(key item, l *lexer) error {
func (r *Rule) comment(key item) error {
if key.typ != itemComment {
panic("item is not a comment")
}
Expand All @@ -445,7 +445,7 @@ func (r *Rule) comment(key item, l *lexer) error {
}

// action decodes an IDS rule option based on its key.
func (r *Rule) action(key item, l *lexer) error {
func (r *Rule) action(key item) error {
if key.typ != itemAction {
panic("item is not an action")
}
Expand All @@ -457,7 +457,7 @@ func (r *Rule) action(key item, l *lexer) error {
}

// protocol decodes an IDS rule protocol based on its key.
func (r *Rule) protocol(key item, l *lexer) error {
func (r *Rule) protocol(key item) error {
if key.typ != itemProtocol {
panic("item is not a protocol")
}
Expand All @@ -469,7 +469,7 @@ func (r *Rule) protocol(key item, l *lexer) error {
}

// network decodes an IDS rule network (networks and ports) based on its key.
func (r *Rule) network(key item, l *lexer) error {
func (r *Rule) network(key item) error {
// Identify if the whole network component is negated.
tmp := strings.TrimPrefix(key.value, "!")
negated := len(tmp) < len(key.value)
Expand Down Expand Up @@ -598,7 +598,7 @@ func validNetworks(nets []string) bool {
}

// direction decodes an IDS rule direction based on its key.
func (r *Rule) direction(key item, l *lexer) error {
func (r *Rule) direction(key item) error {
if key.typ != itemDirection {
panic("item is not a direction")
}
Expand Down Expand Up @@ -930,7 +930,7 @@ func parseRuleAux(rule string, commented bool) (*Rule, error) {
// Ignore comment ending rule.
return r, nil
}
err = r.comment(item, l)
err = r.comment(item)
// Error here means that the comment was not a commented rule.
// So we're not parsing a rule and we need to break out.
if err != nil {
Expand All @@ -939,13 +939,13 @@ func parseRuleAux(rule string, commented bool) (*Rule, error) {
// This line was a commented rule.
return r, nil
case itemAction:
err = r.action(item, l)
err = r.action(item)
case itemProtocol:
err = r.protocol(item, l)
err = r.protocol(item)
case itemSourceAddress, itemDestinationAddress, itemSourcePort, itemDestinationPort:
err = r.network(item, l)
err = r.network(item)
case itemDirection:
err = r.direction(item, l)
err = r.direction(item)
case itemOptionKey:
err = r.option(item, l)
// We will continue to parse a rule with unsupported options.
Expand Down
2 changes: 1 addition & 1 deletion rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ func (p PCRE) String() string {

// escape quote signs, if necessary
if bytes.IndexByte(pattern, '"') > -1 {
pattern = bytes.Replace(pattern, []byte(`"`), []byte(`\"`), -1)
pattern = bytes.ReplaceAll(pattern, []byte(`"`), []byte(`\"`))
}

var s strings.Builder
Expand Down