Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filters based on allowed user agent (regexp) #232

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func main() {
proxy.Register(&plugins.BalanceTracker{})
proxy.Register(&plugins.LeakyBucketPlugin{"APP"})
proxy.Register(&plugins.NoopFilter{proxy.LifecycleMask(proxy.AccountLookup|proxy.RateLimit|proxy.BalanceCheck)})
proxy.Register(&plugins.UserAgentFilter{})
proxy.Register(proxy.NewReconciler(300)) // seconds

gateway()
Expand Down
2 changes: 1 addition & 1 deletion gateway/plugins/leaky.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (l *LeakyBucketPlugin) getBucketsForScope(ctx context.Context, app *db.App)
return buckets
}
for _, rule := range rules {
if rule.RuleType != "rate-limits" {
if rule.RuleType != "rate-limits" || !rule.Active {
continue
}
rate, err := utils.ParseRate(rule.Value)
Expand Down
78 changes: 78 additions & 0 deletions gateway/plugins/useragent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package plugins

import (
"context"
"log"
"net/http"
"regexp"

"porters/db"
"porters/proxy"
)

const (
UA_TYPE_ID string = "allowed-user-agents"
)

type UserAgentFilter struct {

}

func (u *UserAgentFilter) Name() string {
return "User Agent Filter"
}

func (u *UserAgentFilter) Key() string {
return "USERAGENT"
}

func (u *UserAgentFilter) Load() {
log.Println("Loading", u.Name())
}

func (u *UserAgentFilter) HandleRequest(req *http.Request) error {
ctx := req.Context()
ua := req.UserAgent()
appId := proxy.PluckAppId(req)
app := &db.App{Id: appId}
err := app.Lookup(ctx)
if err != nil {
return proxy.NewHTTPError(http.StatusNotFound)
}

rules := u.getRulesForScope(ctx, app)
success := (len(rules) == 0)

for _, rule := range rules {
if (rule.MatchString(ua)) {
success = true
break
}
}

if !success {
return proxy.NewHTTPError(http.StatusUnauthorized)
}
return nil
}

func (u *UserAgentFilter) getRulesForScope(ctx context.Context, app *db.App) []regexp.Regexp {
useragents := make([]regexp.Regexp, 0)
rules, err := app.Rules(ctx)
if err != nil {
log.Println("couldn't get rules", err)
} else {
for _, rule := range rules {
if rule.RuleType != UA_TYPE_ID || !rule.Active {
continue
}
matcher, err := regexp.Compile(rule.Value)
if err != nil {
log.Println("unable to compile regexp", err)
} else {
useragents = append(useragents, *matcher)
}
}
}
return useragents
}
Loading