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.
27 lines
438 B
27 lines
438 B
|
8 years ago
|
package rest
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
//根据一个请求,获取ip.
|
||
|
|
func GetIpAddress(r *http.Request) string {
|
||
|
|
var ipAddress string
|
||
|
|
|
||
|
|
ipAddress = r.RemoteAddr
|
||
|
|
|
||
|
|
if ipAddress != "" {
|
||
|
|
ipAddress = strings.Split(ipAddress, ":")[0]
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
|
||
|
|
for _, ip := range strings.Split(r.Header.Get(h), ",") {
|
||
|
|
if ip != "" {
|
||
|
|
ipAddress = ip
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ipAddress
|
||
|
|
}
|