30 lines
572 B
Go
30 lines
572 B
Go
|
/*
|
||
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||
|
*/
|
||
|
|
||
|
package http
|
||
|
|
||
|
import (
|
||
|
"net"
|
||
|
"net/http"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func RemoteAddr(r *http.Request) string {
|
||
|
address, _, _ := net.SplitHostPort(r.RemoteAddr)
|
||
|
if address != "localhost" && address != "127.0.0.1" && address != "::1" {
|
||
|
return address
|
||
|
}
|
||
|
forwarded := r.Header.Get("X-Forwarded-For")
|
||
|
if forwarded == "" {
|
||
|
return address
|
||
|
}
|
||
|
ips := strings.Split(forwarded, ", ")
|
||
|
forwarded = ips[0]
|
||
|
if forwarded == "" {
|
||
|
return address
|
||
|
}
|
||
|
return forwarded
|
||
|
}
|