Skip to content

Commit

Permalink
proxyd: Support pattern matching in origin and user agent
Browse files Browse the repository at this point in the history
Adds support for pattern matchin in exempt origins. This should help with some of the issues Synthetix and others are seeing.
  • Loading branch information
mslipper committed Oct 17, 2022
1 parent fd043d8 commit ca45a85
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-trainers-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/proxyd': minor
---

Support pattern matching in exempt origins/user agents
36 changes: 28 additions & 8 deletions proxyd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"math"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -49,8 +50,8 @@ type Server struct {
upgrader *websocket.Upgrader
mainLim FrontendRateLimiter
overrideLims map[string]FrontendRateLimiter
limExemptOrigins map[string]bool
limExemptUserAgents map[string]bool
limExemptOrigins []*regexp.Regexp
limExemptUserAgents []*regexp.Regexp
rpcServer *http.Server
wsServer *http.Server
cache RPCCache
Expand Down Expand Up @@ -104,15 +105,23 @@ func NewServer(
}

var mainLim FrontendRateLimiter
limExemptOrigins := make(map[string]bool)
limExemptUserAgents := make(map[string]bool)
limExemptOrigins := make([]*regexp.Regexp, 0)
limExemptUserAgents := make([]*regexp.Regexp, 0)
if rateLimitConfig.BaseRate > 0 {
mainLim = limiterFactory(time.Duration(rateLimitConfig.BaseInterval), rateLimitConfig.BaseRate, "main")
for _, origin := range rateLimitConfig.ExemptOrigins {
limExemptOrigins[strings.ToLower(origin)] = true
pattern, err := regexp.Compile(origin)
if err != nil {
return nil, err
}
limExemptOrigins = append(limExemptOrigins, pattern)
}
for _, agent := range rateLimitConfig.ExemptUserAgents {
limExemptUserAgents[strings.ToLower(agent)] = true
pattern, err := regexp.Compile(agent)
if err != nil {
return nil, err
}
limExemptUserAgents = append(limExemptUserAgents, pattern)
}
} else {
mainLim = NoopFrontendRateLimiter
Expand Down Expand Up @@ -548,11 +557,22 @@ func (s *Server) populateContext(w http.ResponseWriter, r *http.Request) context
}

func (s *Server) isUnlimitedOrigin(origin string) bool {
return s.limExemptOrigins[strings.ToLower(origin)]
for _, pat := range s.limExemptOrigins {
if pat.MatchString(origin) {
return true
}
}

return false
}

func (s *Server) isUnlimitedUserAgent(origin string) bool {
return s.limExemptUserAgents[strings.ToLower(origin)]
for _, pat := range s.limExemptUserAgents {
if pat.MatchString(origin) {
return true
}
}
return false
}

func setCacheHeader(w http.ResponseWriter, cached bool) {
Expand Down

0 comments on commit ca45a85

Please sign in to comment.