You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

28 lines
507 B

  1. package ratelimit
  2. import (
  3. "net"
  4. "net/http"
  5. "strings"
  6. )
  7. // IP returns unique key per request IP.
  8. func IP(r *http.Request) string {
  9. ip, _, _ := net.SplitHostPort(r.RemoteAddr)
  10. if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
  11. if i := strings.IndexAny(xff, ",;"); i != -1 {
  12. xff = xff[:i]
  13. }
  14. ip += "," + xff
  15. }
  16. if xrip := r.Header.Get("X-Real-IP"); xrip != "" {
  17. ip += "," + xrip
  18. }
  19. return ip
  20. }
  21. // NOP returns empty key for each request.
  22. func NOP(r *http.Request) string {
  23. return ""
  24. }