-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathget_real_ip_test.go
149 lines (136 loc) · 3.53 KB
/
get_real_ip_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package traefik_get_real_ip_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
plugin "github.com/Paxxs/traefik-get-real-ip"
)
func TestNew(t *testing.T) {
cfg := plugin.CreateConfig()
cfg.Proxy = []plugin.Proxy{
{
ProxyHeadername: "X-From-Cdn",
ProxyHeadervalue: "1",
RealIP: "X-Forwarded-For",
},
{
ProxyHeadername: "X-From-Cdn",
ProxyHeadervalue: "2",
RealIP: "Client-Ip",
},
{
ProxyHeadername: "X-From-Cdn",
ProxyHeadervalue: "3",
RealIP: "Cf-Connecting-Ip",
},
{
ProxyHeadername: "X-From-Cdn",
ProxyHeadervalue: "4",
RealIP: "X-Forwarded-For",
},
{
ProxyHeadername: "X-From-Cdn",
ProxyHeadervalue: "5",
RealIP: "Client-Ip",
},
{
ProxyHeadername: "*",
ProxyHeadervalue: "6",
RealIP: "RemoteAddr",
},
}
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {})
handler, err := plugin.New(ctx, next, cfg, "traefik-get-real-ip")
if err != nil {
t.Fatal(err)
}
testCases := []struct {
xff string // X-Forwarded-For
xFromProxy string // cdn标识
realIPHeader string // CDN传递IP字段
realIP string // CDN传递IP字段值
desc string
expected string
remoteAddr string
}{
{
xff: "奇怪的,东西🤣,10.0.0.1, 2.2.2.2,3.3.3.3",
xFromProxy: "1",
realIPHeader: "Client-Ip",
realIP: "10.0.0.2",
desc: "Proxy 1 通过 xff 传递IP",
remoteAddr: "172.18.0.1:1000",
expected: "10.0.0.1",
},
{
xff: "10.0.1.2",
xFromProxy: "2",
realIPHeader: "Client-Ip",
realIP: "10.0.1.1",
desc: "Proxy 2 通过 Client-Ip 传递IP",
remoteAddr: "172.18.0.2:2000",
expected: "10.0.1.1",
},
{
xff: "10.0.0.2",
xFromProxy: "3",
realIPHeader: "Cf-Connecting-Ip",
realIP: "10.0.2.1",
desc: "Proxy 3 通过 Cf-Connecting-Ip 传递IP",
remoteAddr: "172.18.0.3:3000",
expected: "10.0.2.1",
},
{
xff: "奇怪的,东西🤣,10.0.3.1:2345, 2.2.2.2,3.3.3.3",
xFromProxy: "4",
realIPHeader: "Client-Ip",
realIP: "10.0.3.2",
desc: "Proxy 4 通过 xff 传递IP带端口号",
remoteAddr: "172.18.0.4:4000",
expected: "10.0.3.1",
},
{
xff: "10.0.5.1",
xFromProxy: "5",
realIP: "RemoteAddr",
desc: "Proxy 5 取远程地址",
remoteAddr: "172.18.0.5:55122",
expected: "172.18.0.5",
},
{
xff: "sss",
xFromProxy: "6",
realIPHeader: "Client-Ip",
realIP: "6",
desc: "Proxy 6 未正确传递",
remoteAddr: "172.18.0.6:6000",
expected: "172.18.0.6",
},
}
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
reorder := httptest.NewRecorder()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
fmt.Println("\n😊 测试:", test.desc)
fmt.Println(test)
req.RemoteAddr = test.remoteAddr
req.Header.Set(test.realIPHeader, test.realIP)
req.Header.Set("X-From-Cdn", test.xFromProxy)
req.Header.Set("X-Forwarded-For", test.xff)
handler.ServeHTTP(reorder, req)
assertHeader(t, req, "X-Real-Ip", test.expected)
})
}
}
func assertHeader(t *testing.T, req *http.Request, key, expected string) {
t.Helper()
if req.Header.Get(key) != expected {
t.Errorf("invalid header value: got %s, want %s", req.Header.Get(key), expected)
}
}