Bläddra i källkod

Merge branch 'reverse-proxy-fix-20210901-1' of tmp/arozos into master

TC 4 år sedan
förälder
incheckning
f8952fa173
1 ändrade filer med 11 tillägg och 1 borttagningar
  1. 11 1
      mod/auth/authlogger/authlogger.go

+ 11 - 1
mod/auth/authlogger/authlogger.go

@@ -49,7 +49,17 @@ func NewLogger() (*Logger, error) {
 func (l *Logger) LogAuth(r *http.Request, loginStatus bool) error {
 	username, _ := mv(r, "username", true)
 	timestamp := time.Now().Unix()
-	return l.LogAuthByRequestInfo(username, r.RemoteAddr, timestamp, loginStatus, "web")
+	//handling the reverse proxy remote IP issue
+	remoteIP := r.Header.Get("X-FORWARDED-FOR")
+	if remoteIP != "" {
+		//grab the last known remote IP from header
+		remoteIPs := strings.Split(remoteIP, ", ")
+		remoteIP = remoteIPs[len(remoteIPs)-1]
+	} else {
+		//if there is no X-FORWARDED-FOR, use default remote IP
+		remoteIP = r.RemoteAddr
+	}
+	return l.LogAuthByRequestInfo(username, remoteIP, timestamp, loginStatus, "web")
 }
 
 //Log the current authentication to record by custom filled information. Use LogAuth if your module is authenticating via web interface