Skip to content

Commit 3eaebae

Browse files
committed
update
1 parent 0779e04 commit 3eaebae

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

queue-manager.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,14 @@ func getClientIP(req *http.Request) string {
264264
}
265265

266266
// Fall back to RemoteAddr
267-
return strings.Split(req.RemoteAddr, ":")[0]
267+
remoteAddr := req.RemoteAddr
268+
ipPort := strings.Split(remoteAddr, ":")
269+
if len(ipPort) > 0 {
270+
return ipPort[0]
271+
}
272+
273+
// In case RemoteAddr is in an unexpected format
274+
return remoteAddr
268275
}
269276

270277
// serveQueuePage serves the queue page HTML

queue-manager_test.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func TestGetClientIP(t *testing.T) {
8585
// Test RemoteAddr
8686
req1 := &http.Request{
8787
RemoteAddr: "192.168.1.1:12345",
88+
Header: make(http.Header),
8889
}
8990
if ip := getClientIP(req1); ip != "192.168.1.1" {
9091
t.Errorf("Expected IP 192.168.1.1, got %s", ip)
@@ -93,33 +94,30 @@ func TestGetClientIP(t *testing.T) {
9394
// Test X-Forwarded-For
9495
req2 := &http.Request{
9596
RemoteAddr: "10.0.0.1:12345",
96-
Header: http.Header{
97-
"X-Forwarded-For": []string{"192.168.1.2, 10.0.0.1"},
98-
},
97+
Header: make(http.Header),
9998
}
99+
req2.Header.Set("X-Forwarded-For", "192.168.1.2, 10.0.0.1")
100100
if ip := getClientIP(req2); ip != "192.168.1.2" {
101101
t.Errorf("Expected IP 192.168.1.2, got %s", ip)
102102
}
103103

104104
// Test X-Real-IP
105105
req3 := &http.Request{
106106
RemoteAddr: "10.0.0.1:12345",
107-
Header: http.Header{
108-
"X-Real-IP": []string{"192.168.1.3"},
109-
},
107+
Header: make(http.Header),
110108
}
109+
req3.Header.Set("X-Real-IP", "192.168.1.3")
111110
if ip := getClientIP(req3); ip != "192.168.1.3" {
112111
t.Errorf("Expected IP 192.168.1.3, got %s", ip)
113112
}
114113

115114
// Test precedence: X-Forwarded-For > X-Real-IP > RemoteAddr
116115
req4 := &http.Request{
117116
RemoteAddr: "10.0.0.1:12345",
118-
Header: http.Header{
119-
"X-Forwarded-For": []string{"192.168.1.4, 10.0.0.1"},
120-
"X-Real-IP": []string{"192.168.1.5"},
121-
},
117+
Header: make(http.Header),
122118
}
119+
req4.Header.Set("X-Forwarded-For", "192.168.1.4, 10.0.0.1")
120+
req4.Header.Set("X-Real-IP", "192.168.1.5")
123121
if ip := getClientIP(req4); ip != "192.168.1.4" {
124122
t.Errorf("Expected IP 192.168.1.4, got %s", ip)
125123
}

0 commit comments

Comments
 (0)