Skip to content

Commit

Permalink
better rate limiting by IP
Browse files Browse the repository at this point in the history
  • Loading branch information
lanrat committed Jan 9, 2021
1 parent c3281b8 commit 421f893
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 23 deletions.
23 changes: 1 addition & 22 deletions server/ip_utils.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
package server

import (
"net"
"net/http"
"strings"
)

//custom vary by to use real remote IP without port
type ipVaryBy struct{}

func (ip ipVaryBy) Key(r *http.Request) string {
return getIPAddress(r)
}

func getIPAddress(r *http.Request) string {
hdr := r.Header
hdrRealIP := hdr.Get("X-Real-Ip")
hdrForwardedFor := hdr.Get("X-Forwarded-For")
if hdrRealIP == "" && hdrForwardedFor == "" {
hdrRealIP, _, _ := net.SplitHostPort(r.RemoteAddr)
return hdrRealIP
}
if hdrForwardedFor != "" {
// X-Forwarded-For is potentially a list of addresses separated with ","
parts := strings.Split(hdrForwardedFor, ",")
for i, p := range parts {
parts[i] = strings.TrimSpace(p)
}
return parts[0]
}
return hdrRealIP
return r.RemoteAddr
}
1 change: 0 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func (s *Server) Start() error {

// rate limiting
// TODO add rate limiting after static handler and possible the main page
// TODO use IP set by proxyheaders!
throttleHandler := makeThrottleHandler(
s.apiConfig.APIRequestsPerMinute,
s.apiConfig.APIRequestsBurst,
Expand Down

0 comments on commit 421f893

Please sign in to comment.