Skip to content

Commit

Permalink
Merge pull request #2 from DylanGore/main
Browse files Browse the repository at this point in the history
Add config option to disable logging for local requests
  • Loading branch information
PascalMinder authored Apr 11, 2021
2 parents f51a43a + 1f06ac2 commit 188dbb7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions .traefik.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ summary: 'block request based on their country of origin'

testData:
allowlocalrequests: false
loglocalrequests: true
api: "https://get.geojs.io/v1/ip/country/{ip}"
countries:
- CH
12 changes: 10 additions & 2 deletions geoblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
// Config the plugin configuration.
type Config struct {
AllowLocalRequests bool `yaml:"allowlocalrequests"`
LogLocalRequests bool `yaml:"loglocalrequests"`
Api string `yaml:"api"`
Countries []string `yaml:"countries,omitempty"`
}
Expand All @@ -38,6 +39,7 @@ func CreateConfig() *Config {
type GeoBlock struct {
next http.Handler
AllowLocalRequests bool
LogLocalRequests bool
apiUri string
countries []string
privateIPRanges []*net.IPNet
Expand All @@ -57,11 +59,13 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h

log.Println("API uri: ", config.Api)
log.Println("allow local IPs: ", config.AllowLocalRequests)
log.Println("log local requests: ", config.LogLocalRequests)
log.Println("allowed countries: ", config.Countries)

return &GeoBlock{
next: next,
AllowLocalRequests: config.AllowLocalRequests,
LogLocalRequests: config.LogLocalRequests,
apiUri: config.Api,
countries: config.Countries,
privateIPRanges: InitPrivateIPBlocks(),
Expand All @@ -86,10 +90,14 @@ func (a *GeoBlock) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

if isPrivateIp {
if a.AllowLocalRequests {
log.Println("Local ip allowed: ", ipAddress)
if a.LogLocalRequests {
log.Println("Local ip allowed: ", ipAddress)
}
a.next.ServeHTTP(rw, req)
} else {
log.Println("Local ip denied: ", ipAddress)
if a.LogLocalRequests {
log.Println("Local ip denied: ", ipAddress)
}
rw.WriteHeader(http.StatusForbidden)
}

Expand Down

0 comments on commit 188dbb7

Please sign in to comment.