Skip to content

Commit 33ab80a

Browse files
committed
client ip logic updated
1 parent 7799544 commit 33ab80a

File tree

3 files changed

+31
-34
lines changed

3 files changed

+31
-34
lines changed

header_test.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,15 @@ func TestParseContentType(t *testing.T) {
186186
}
187187

188188
func createRequest(hdrKey, value string) *Request {
189+
return &Request{Request: createRawHTTPRequest(hdrKey, value)}
190+
}
191+
192+
func createRawHTTPRequest(hdrKey, value string) *http.Request {
189193
hdr := http.Header{}
190194
hdr.Set(hdrKey, value)
191195
url, _ := url.Parse("http://localhost:8080/testpath")
192-
return &Request{Request: &http.Request{
196+
return &http.Request{
193197
URL: url,
194198
Header: hdr,
195-
}}
199+
}
196200
}

request.go

+15-20
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,29 @@ type Request struct {
1818
}
1919

2020
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
21-
// Request methods
21+
// Unexported methods
2222
//___________________________________
2323

24-
// ClientIP returns IP address from HTTP request, typically known as Client IP or
24+
// clientIP returns IP address from HTTP request, typically known as Client IP or
2525
// Remote IP. It parses the IP in the order of X-Forwarded-For, X-Real-IP
2626
// and finally `http.Request.RemoteAddr`.
27-
//
28-
// Set aah application configuration `http.proxy = true`, if you're running
29-
// application behind proxy server like nginx, haproxy, apache, etc. otherwise
30-
// this method might return inaccurate Client IP address.
31-
func (r *Request) ClientIP(proxy bool) string {
32-
if proxy {
33-
// Header X-Forwarded-For
34-
if fwdFor := r.Header.Get(HeaderXForwardedFor); !ess.IsStrEmpty(fwdFor) {
35-
index := strings.Index(fwdFor, ",")
36-
if index == -1 {
37-
return strings.TrimSpace(fwdFor)
38-
}
39-
return strings.TrimSpace(fwdFor[:index])
27+
func clientIP(req *http.Request) string {
28+
// Header X-Forwarded-For
29+
if fwdFor := req.Header.Get(HeaderXForwardedFor); !ess.IsStrEmpty(fwdFor) {
30+
index := strings.Index(fwdFor, ",")
31+
if index == -1 {
32+
return strings.TrimSpace(fwdFor)
4033
}
34+
return strings.TrimSpace(fwdFor[:index])
35+
}
4136

42-
// Header X-Real-Ip
43-
if realIP := r.Header.Get(HeaderXRealIP); !ess.IsStrEmpty(realIP) {
44-
return strings.TrimSpace(realIP)
45-
}
37+
// Header X-Real-Ip
38+
if realIP := req.Header.Get(HeaderXRealIP); !ess.IsStrEmpty(realIP) {
39+
return strings.TrimSpace(realIP)
4640
}
4741

48-
if remoteAddr, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
42+
// Remote Address
43+
if remoteAddr, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
4944
return strings.TrimSpace(remoteAddr)
5045
}
5146

request_test.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ import (
1212
)
1313

1414
func TestClientIP(t *testing.T) {
15-
req1 := createRequest(HeaderXForwardedFor, "10.0.0.1, 10.0.0.2")
16-
ipAddress := req1.ClientIP(true)
15+
req1 := createRawHTTPRequest(HeaderXForwardedFor, "10.0.0.1, 10.0.0.2")
16+
ipAddress := clientIP(req1)
1717
assert.Equal(t, "10.0.0.1", ipAddress)
1818

19-
req2 := createRequest(HeaderXForwardedFor, "10.0.0.2")
20-
ipAddress = req2.ClientIP(true)
19+
req2 := createRawHTTPRequest(HeaderXForwardedFor, "10.0.0.2")
20+
ipAddress = clientIP(req2)
2121
assert.Equal(t, "10.0.0.2", ipAddress)
2222

23-
req3 := createRequest(HeaderXRealIP, "10.0.0.3")
24-
ipAddress = req3.ClientIP(true)
23+
req3 := createRawHTTPRequest(HeaderXRealIP, "10.0.0.3")
24+
ipAddress = clientIP(req3)
2525
assert.Equal(t, "10.0.0.3", ipAddress)
2626

2727
req4 := createRequestWithHost("127.0.0.1:8080", "192.168.0.1:1234")
28-
ipAddress = req4.ClientIP(false)
28+
ipAddress = clientIP(req4)
2929
assert.Equal(t, "192.168.0.1", ipAddress)
3030

3131
req5 := createRequestWithHost("127.0.0.1:8080", "")
32-
ipAddress = req5.ClientIP(false)
32+
ipAddress = clientIP(req5)
3333
assert.Equal(t, "", ipAddress)
3434
}
3535

36-
func createRequestWithHost(host, remote string) *Request {
37-
return &Request{
38-
Request: &http.Request{Host: host, RemoteAddr: remote},
39-
}
36+
func createRequestWithHost(host, remote string) *http.Request {
37+
return &http.Request{Host: host, RemoteAddr: remote}
4038
}

0 commit comments

Comments
 (0)