forked from projectdiscovery/simplehttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule.go
More file actions
65 lines (55 loc) · 1.62 KB
/
rule.go
File metadata and controls
65 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package tcpserver
import (
"regexp"
"strings"
)
// RulesConfiguration from yaml
type RulesConfiguration struct {
Rules []Rule `yaml:"rules"`
}
// Rule to apply to various requests
type Rule struct {
Name string `yaml:"name,omitempty"`
Match string `yaml:"match,omitempty"`
MatchContains string `yaml:"match-contains,omitempty"`
matchRegex *regexp.Regexp
Response string `yaml:"response,omitempty"`
}
// NewRule creates a new Rule - default is regex
func NewRule(match, response string) (*Rule, error) {
return NewRegexRule(match, response)
}
// NewRegexRule returns a new regex-match Rule
func NewRegexRule(match, response string) (*Rule, error) {
regxp, err := regexp.Compile(match)
if err != nil {
return nil, err
}
return &Rule{Match: match, matchRegex: regxp, Response: response}, nil
}
// NewLiteralRule returns a new literal-match Rule
func NewLiteralRule(match, response string) (*Rule, error) {
return &Rule{MatchContains: match, Response: response}, nil
}
// NewRuleFromTemplate "copies" a new Rule
func NewRuleFromTemplate(r Rule) (newRule *Rule, err error) {
newRule = &Rule{
Name: r.Name,
Response: r.Response,
MatchContains: r.MatchContains,
Match: r.Match,
}
if newRule.Match != "" {
newRule.matchRegex, err = regexp.Compile(newRule.Match)
}
return
}
// MatchInput returns if the input was matches with one of the matchers
func (r *Rule) MatchInput(input []byte) bool {
if r.matchRegex != nil && r.matchRegex.Match(input) {
return true
} else if r.MatchContains != "" && strings.Contains(string(input), r.MatchContains) {
return true
}
return false
}