Skip to content

Commit

Permalink
added cidrIgnore setting
Browse files Browse the repository at this point in the history
  • Loading branch information
martensson committed Apr 1, 2019
1 parent 7b12c30 commit f8727c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
51 changes: 31 additions & 20 deletions f5elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

"github.com/BurntSushi/toml"
"github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru"
"github.com/olivere/elastic"
"github.com/oschwald/geoip2-golang"
"gopkg.in/mcuadros/go-syslog.v2"
Expand All @@ -43,16 +43,17 @@ type Request struct {
}

type Config struct {
Address string
Port string
Nodes []string
Index string
Workers int
Bulk int
Buffer int
Timeout int
Geoip string
Salt string
Address string
Port string
Nodes []string
Index string
Workers int
Bulk int
Buffer int
Timeout int
Geoip string
Salt string
CidrIgnore []string
}

type Worker struct {
Expand Down Expand Up @@ -124,15 +125,25 @@ func (w Worker) NewRequest(msg string) (Request, error) {
}
request.Timestamp = time.Now().UTC().Format("2006-01-02T15:04:05Z")
if config.Salt != "" {
// hash client ip with secret salt.
if val, ok := hashcache.Get(request.Client); ok {
request.Client = val.(string)
} else {
h := sha256.New()
h.Write([]byte(request.Client + config.Salt))
hexhash := hex.EncodeToString(h.Sum(nil))[0:16]
hashcache.Add(request.Client, hexhash)
request.Client = hexhash
ip := net.ParseIP(request.Client)
var found bool
for _, cidr := range config.CidrIgnore {
_, ipnet, _ := net.ParseCIDR(cidr)
if ipnet.Contains(ip) {
found = true
}
}
if !found {
// hash client ip with secret salt.
if val, ok := hashcache.Get(request.Client); ok {
request.Client = val.(string)
} else {
h := sha256.New()
h.Write([]byte(request.Client + config.Salt))
hexhash := hex.EncodeToString(h.Sum(nil))[0:16]
hashcache.Add(request.Client, hexhash)
request.Client = hexhash
}
}
}
return request, nil
Expand Down
4 changes: 3 additions & 1 deletion f5elastic.toml-example
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ timeout = 5
# Geoip2/GeoLite2 database
geoip = "GeoLite2-City.mmdb"
# generate sha256 hash to hide client ip (disabled if empty)
salt = ""
salt = ""
# cidr networks to ignore hashing
cidrIgnore = ["10.0.0.0/8"]

0 comments on commit f8727c3

Please sign in to comment.