Skip to content

Commit

Permalink
Insert requests from IP filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
KingAkeem committed Oct 12, 2023
1 parent c1fe8e1 commit f1a22cd
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions plugins/ipfilter/requestfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func init() {
}

type RequestFilterPlugin struct {
db *database.Database
Settings map[string]interface{}
activeMode bool
ipBlacklist []interface{}
Expand All @@ -44,6 +45,7 @@ func NewRequestFilterPlugin(settings map[string]interface{}, db *database.Databa
regionBlacklist, _ := settings["region-blacklist"].([]interface{})

return &RequestFilterPlugin{
db: db,
Settings: settings,
activeMode: settings["active_mode"].(bool),
ipBlacklist: ipBlacklist,
Expand Down Expand Up @@ -81,6 +83,11 @@ func (p *RequestFilterPlugin) Handle(r *http.Request) error {
if len(p.ipBlacklist) > 0 {
for _, blacklistedIP := range p.ipBlacklist {
if ip == blacklistedIP {
req, err := database.NewRequest(r, "ipblacklist")
if err != nil {
return err
}
p.db.Insert(req)
return errors.New("IP address is blacklisted")
}
}
Expand All @@ -90,6 +97,11 @@ func (p *RequestFilterPlugin) Handle(r *http.Request) error {
isWhitelisted := false
for _, whitelistedIP := range p.ipWhitelist {
if ip == whitelistedIP {
req, err := database.NewRequest(r, "ipwhitelist")
if err != nil {
return err
}
p.db.Insert(req)
isWhitelisted = true
break
}
Expand All @@ -105,6 +117,11 @@ func (p *RequestFilterPlugin) Handle(r *http.Request) error {

for _, restrictedRegion := range p.regionBlacklist {
if region == restrictedRegion {
req, err := database.NewRequest(r, "regionblacklist")
if err != nil {
return err
}
p.db.Insert(req)
log.Printf("region %s is restricted by policy", region)
return errors.New("access from this region is restricted by policy")
}
Expand All @@ -115,6 +132,11 @@ func (p *RequestFilterPlugin) Handle(r *http.Request) error {
isRegionWhitelisted := false
for _, allowedRegion := range p.regionWhitelist {
if region == allowedRegion {
req, err := database.NewRequest(r, "regionwhitelist")
if err != nil {
return err
}
p.db.Insert(req)
isRegionWhitelisted = true
break
}
Expand Down

0 comments on commit f1a22cd

Please sign in to comment.