adding a RealIP func
This commit is contained in:
parent
e462a16204
commit
de330d561a
1 changed files with 18 additions and 1 deletions
|
@ -13,7 +13,7 @@ func LogRequest(r *http.Request, statusCode int) {
|
|||
var user_agent string
|
||||
|
||||
user_agent = ""
|
||||
addr = r.RemoteAddr
|
||||
addr = RealIP(r)
|
||||
|
||||
for k, v := range r.Header {
|
||||
if k == "User-Agent" {
|
||||
|
@ -33,3 +33,20 @@ func LogRequest(r *http.Request, statusCode int) {
|
|||
statusCode,
|
||||
r.ContentLength)
|
||||
}
|
||||
|
||||
func RealIP(r *http.Request) (ip string) {
|
||||
ip = r.RemoteAddr
|
||||
|
||||
port_pos := strings.LastIndex(ip, ":")
|
||||
if port_pos != -1 {
|
||||
ip = ip[0:port_pos]
|
||||
}
|
||||
|
||||
for k, v := range r.Header {
|
||||
if k == "X-Forwarded-For" {
|
||||
ip = strings.Join(v, " ")
|
||||
}
|
||||
}
|
||||
|
||||
return ip
|
||||
}
|
Loading…
Reference in a new issue