mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-11-05 23:41:47 +08:00
22 lines
368 B
Go
22 lines
368 B
Go
package netutils2
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func GetHttpRequestIp(r *http.Request) string {
|
|
ipStr := r.Header.Get("X-Real-Ip")
|
|
if len(ipStr) == 0 {
|
|
ipStr = r.Header.Get("X-Forwarded-For")
|
|
if len(ipStr) == 0 {
|
|
ipStr = r.RemoteAddr
|
|
colonPos := strings.Index(ipStr, ":")
|
|
if colonPos > 0 {
|
|
ipStr = ipStr[:colonPos]
|
|
}
|
|
}
|
|
}
|
|
return ipStr
|
|
}
|