@@ -160,7 +160,10 @@ func (p *Proxy) handleWrite(resp http.ResponseWriter, req *http.Request, lc *laz
160160 // Pipe request
161161 n , err := io .Copy (connOut , req .Body )
162162 if p .OnBytesReceived != nil && n > 0 {
163- p .OnBytesReceived (clientIpFor (req ), lc .addr , req , n )
163+ clientIp := clientIpFor (req )
164+ if clientIp != "" {
165+ p .OnBytesReceived (clientIp , lc .addr , req , n )
166+ }
164167 }
165168 if err != nil && err != io .EOF {
166169 badGateway (resp , fmt .Sprintf ("Unable to write to connOut: %s" , err ))
@@ -222,7 +225,7 @@ func (p *Proxy) handleRead(resp http.ResponseWriter, req *http.Request, lc *lazy
222225
223226 // Write if necessary
224227 if n > 0 {
225- if p .OnBytesSent != nil && n > 0 {
228+ if clientIp != "" && p .OnBytesSent != nil && n > 0 {
226229 p .OnBytesSent (clientIp , lc .addr , req , int64 (n ))
227230 }
228231
@@ -303,7 +306,12 @@ func (p *Proxy) getLazyConn(id string, addr string) (l *lazyConn, isNew bool) {
303306func clientIpFor (req * http.Request ) string {
304307 clientIp := req .Header .Get ("X-Forwarded-For" )
305308 if clientIp == "" {
306- clientIp = strings .Split (req .RemoteAddr , ":" )[0 ]
309+ clientIp , _ , err := net .SplitHostPort (req .RemoteAddr )
310+ if err != nil {
311+ log .Debugf ("Unable to split RemoteAddr %v: %v" , err )
312+ return ""
313+ }
314+ return clientIp
307315 }
308316 // clientIp may contain multiple ips, use the first
309317 ips := strings .Split (clientIp , "," )
0 commit comments