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

ip rl: improved method of getting the IP address #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 17 additions & 5 deletions plugins/go-pre-ip-rate-limiter/CustomGoPlugin.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

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

Expand All @@ -21,7 +22,7 @@ func init() {
// IPRateLimit
func IPRateLimit(rw http.ResponseWriter, r *http.Request) {
// Get the client IP address
clientIP := getIP(r.RemoteAddr)
clientIP := getIP(r)

logger.Info("Request came in from " + clientIP)

Expand All @@ -39,9 +40,20 @@ func IPRateLimit(rw http.ResponseWriter, r *http.Request) {
ctx.SetSession(r, session, true)
}

func getIP(remoteAddr string) string {
if ip := strings.Split(remoteAddr, ":"); len(ip) > 0 {
return ip[0]
func getIP(r *http.Request) string {

for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
for _, addr := range strings.Split(r.Header.Get(h), ",") {
addr = strings.TrimSpace(addr)
// header can contain spaces too, strip those out.
realIP := net.ParseIP(addr)
if !realIP.IsGlobalUnicast() {
// bad address - we should alsp eventually check for private IP ranges
continue
}
return addr
}
}
return ""
// If no valid IP found, return the remote address
return r.RemoteAddr
}