Skip to content

Commit

Permalink
Add geoip data to info log
Browse files Browse the repository at this point in the history
  • Loading branch information
t94j0 committed Mar 14, 2022
1 parent 9fca2b2 commit ae3e1d9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
25 changes: 25 additions & 0 deletions satellite/handlers/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"crypto/md5"
"encoding/hex"
"io"
"net"
"strings"

log "github.com/sirupsen/logrus"
"github.com/t94j0/satellite/net/http"
"github.com/t94j0/satellite/satellite/geoip"
"github.com/t94j0/satellite/satellite/path"
"github.com/t94j0/satellite/satellite/util"
)
Expand Down Expand Up @@ -76,8 +79,29 @@ func getJA3(req *http.Request) string {
return string(out)
}

func parseRemoteAddr(ipPort string) net.IP {
targetIP := strings.Split(ipPort, ":")[0]
return net.ParseIP(targetIP)
}

func getCountryCode(remoteAddr string, gip *geoip.DB) (string, error) {
targetHost := parseRemoteAddr(remoteAddr)
if gip.HasDB() {
cc, err := gip.CountryCode(targetHost)
if err != nil {
return "", err
}
return cc, nil
}
return "", nil
}

func (h RootHandler) log(req *http.Request, respCode int) {
ja3 := getJA3(req)
cc, err := getCountryCode(req.RemoteAddr, &h.paths.GeoipDB)
if err != nil {
log.Error(err)
}
log.WithFields(log.Fields{
"method": req.Method,
"host": req.Host,
Expand All @@ -86,5 +110,6 @@ func (h RootHandler) log(req *http.Request, respCode int) {
"ja3": ja3,
"response": respCode,
"user_agent": req.UserAgent(),
"geo_ip": cc,
}).Info("request")
}
2 changes: 1 addition & 1 deletion satellite/path/client_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (c *ClientID) Match(ip net.IP, targetList []string) bool {
return false
}

lastSubset := list[len(list)-len(targetList) : len(list)]
lastSubset := list[len(list)-len(targetList) : ]

for i := range lastSubset {
if lastSubset[i] != targetList[i] {
Expand Down
2 changes: 1 addition & 1 deletion satellite/path/conditionals.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ func (c *RequestConditions) geoipMatch(req *http.Request, gip geoip.DB) bool {
if err != nil {
log.WithFields(log.Fields{
"error": err,
}).Debug("Error getting country code")
}).Error("Error getting country code")
return false
}

Expand Down
31 changes: 31 additions & 0 deletions satellite/path/conditionals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,3 +1198,34 @@ func TestRequestConditions_ShouldHost_geoip_blacklist(t *testing.T) {
t.Error(err)
}
}

func TestRequestConditions_ShouldHost_geoip_blacklist_accept(t *testing.T) {
// Create HTTP Request
mockRequest := &http.Request{RemoteAddr: "5.250.176.20:54321"}

state, file, err := TemporaryDB()
if err != nil {
t.Error(err)
}

gip, err := createGeoIP()
if err != nil {
t.Error(err)
}

data := `geoip:
blacklist_countries:
- US`

conditions, err := NewRequestConditions([]byte(data))
if err != nil {
t.Error(err)
}
if !conditions.ShouldHost(mockRequest, state, gip) {
t.Fail()
}

if err := RemoveDB(file); err != nil {
t.Error(err)
}
}
10 changes: 7 additions & 3 deletions satellite/path/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Paths struct {
globalConditionsPath string

state *State
geoipDB geoip.DB
GeoipDB geoip.DB
list []*Path
}

Expand Down Expand Up @@ -66,7 +66,7 @@ func (paths *Paths) AddGeoIP(path string) error {
if err != nil {
return err
}
paths.geoipDB = db
paths.GeoipDB = db

return nil
}
Expand Down Expand Up @@ -120,6 +120,10 @@ func (paths *Paths) collectConditionalsDirectory(targetPath string) (RequestCond
condsResult := make([]RequestConditions, 0)

collectWalkFunc := func(oPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if info.IsDir() {
return nil
}
Expand Down Expand Up @@ -233,7 +237,7 @@ func (paths *Paths) MatchAndServe(w http.ResponseWriter, req *http.Request) (boo

paths.applyGlobalConditionals(matchedPath)

shouldHost := matchedPath.ShouldHost(req, paths.state, paths.geoipDB)
shouldHost := matchedPath.ShouldHost(req, paths.state, paths.GeoipDB)
if shouldHost {
if err := matchedPath.ServeHTTP(w, req, paths.base); err != nil {
return false, err
Expand Down

0 comments on commit ae3e1d9

Please sign in to comment.