Files
hetty/pkg/scope/scope.go

181 lines
2.9 KiB
Go
Raw Permalink Normal View History

2020-10-01 21:46:35 +02:00
package scope
import (
2022-01-21 11:45:54 +01:00
"bytes"
"encoding/gob"
2020-10-01 21:46:35 +02:00
"net/http"
"regexp"
"sync"
)
type Scope struct {
rules []Rule
2022-01-21 11:45:54 +01:00
mu sync.RWMutex
2020-10-01 21:46:35 +02:00
}
type Rule struct {
URL *regexp.Regexp
Header Header
Body *regexp.Regexp
}
type Header struct {
Key *regexp.Regexp
Value *regexp.Regexp
}
func (s *Scope) Rules() []Rule {
s.mu.RLock()
defer s.mu.RUnlock()
2021-04-25 16:23:53 +02:00
2020-10-01 21:46:35 +02:00
return s.rules
}
2022-01-21 11:45:54 +01:00
func (s *Scope) SetRules(rules []Rule) {
2020-10-29 20:54:17 +01:00
s.mu.Lock()
defer s.mu.Unlock()
2020-10-01 21:46:35 +02:00
s.rules = rules
}
func (s *Scope) Match(req *http.Request, body []byte) bool {
s.mu.RLock()
defer s.mu.RUnlock()
2021-04-25 16:23:53 +02:00
2020-10-01 21:46:35 +02:00
for _, rule := range s.rules {
if matches := rule.Match(req, body); matches {
return true
}
}
return false
}
func (r Rule) Match(req *http.Request, body []byte) bool {
if r.URL != nil {
if matches := r.URL.MatchString(req.URL.String()); matches {
return true
}
}
for key, values := range req.Header {
var keyMatches, valueMatches bool
2021-04-25 16:23:53 +02:00
2020-10-01 21:46:35 +02:00
if r.Header.Key != nil {
if matches := r.Header.Key.MatchString(key); matches {
keyMatches = true
}
}
2021-04-25 16:23:53 +02:00
2020-10-01 21:46:35 +02:00
if r.Header.Value != nil {
for _, value := range values {
if matches := r.Header.Value.MatchString(value); matches {
valueMatches = true
break
}
}
}
// When only key or value is set, match on whatever is set.
// When both are set, both must match.
switch {
case r.Header.Key != nil && r.Header.Value == nil && keyMatches:
return true
case r.Header.Key == nil && r.Header.Value != nil && valueMatches:
return true
case r.Header.Key != nil && r.Header.Value != nil && keyMatches && valueMatches:
return true
}
}
if r.Body != nil {
if matches := r.Body.Match(body); matches {
return true
}
}
return false
}
2020-10-29 20:54:17 +01:00
2022-01-21 11:45:54 +01:00
func regexpToString(r *regexp.Regexp) string {
if r == nil {
return ""
}
2020-10-29 20:54:17 +01:00
2022-01-21 11:45:54 +01:00
return r.String()
}
func stringToRegexp(s string) (*regexp.Regexp, error) {
if s == "" {
return nil, nil
}
return regexp.Compile(s)
}
type ruleDTO struct {
URL string
Header struct {
Key string
Value string
}
Body string
}
func (r Rule) MarshalBinary() ([]byte, error) {
2020-10-29 20:54:17 +01:00
dto := ruleDTO{
2022-01-21 11:45:54 +01:00
URL: regexpToString(r.URL),
2020-10-29 20:54:17 +01:00
Body: regexpToString(r.Body),
}
2022-01-21 11:45:54 +01:00
dto.Header.Key = regexpToString(r.Header.Key)
dto.Header.Value = regexpToString(r.Header.Value)
buf := bytes.Buffer{}
2020-10-29 20:54:17 +01:00
2022-01-21 11:45:54 +01:00
err := gob.NewEncoder(&buf).Encode(dto)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
2020-10-29 20:54:17 +01:00
}
2022-01-21 11:45:54 +01:00
func (r *Rule) UnmarshalBinary(data []byte) error {
dto := ruleDTO{}
2020-10-29 20:54:17 +01:00
2022-01-21 11:45:54 +01:00
err := gob.NewDecoder(bytes.NewReader(data)).Decode(&dto)
if err != nil {
2020-10-29 20:54:17 +01:00
return err
}
url, err := stringToRegexp(dto.URL)
if err != nil {
return err
}
2021-04-25 16:23:53 +02:00
2020-10-29 20:54:17 +01:00
headerKey, err := stringToRegexp(dto.Header.Key)
if err != nil {
return err
}
2021-04-25 16:23:53 +02:00
2020-10-29 20:54:17 +01:00
headerValue, err := stringToRegexp(dto.Header.Value)
if err != nil {
return err
}
2021-04-25 16:23:53 +02:00
2020-10-29 20:54:17 +01:00
body, err := stringToRegexp(dto.Body)
if err != nil {
return err
}
*r = Rule{
URL: url,
Header: Header{
Key: headerKey,
Value: headerValue,
},
Body: body,
}
return nil
}