-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample_playground_test.go
76 lines (64 loc) · 2.89 KB
/
example_playground_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
package realclientip_test
import (
"fmt"
"net/http"
"github.com/realclientip/realclientip-go"
)
func Example_playground() {
// We'll make a fake request
req, _ := http.NewRequest("GET", "https://example.com", nil)
req.Header.Add("X-Forwarded-For", "1.1.1.1, 2001:db8:cafe::99%eth0, 3.3.3.3, 192.168.1.1")
req.Header.Add("Forwarded", `For=fe80::abcd;By=fe80::1234, Proto=https;For=::ffff:188.0.2.128, For="[2001:db8:cafe::17]:4848", For=fc00::1`)
req.Header.Add("X-Real-IP", "4.4.4.4")
req.RemoteAddr = "192.168.1.2:8888"
var strat realclientip.Strategy
strat = realclientip.RemoteAddrStrategy{}
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 192.168.1.2
strat, _ = realclientip.NewSingleIPHeaderStrategy("X-Real-IP")
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 4.4.4.4
strat, _ = realclientip.NewLeftmostNonPrivateStrategy("Forwarded")
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 188.0.2.128
strat, _ = realclientip.NewRightmostNonPrivateStrategy("X-Forwarded-For")
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 3.3.3.3
strat, _ = realclientip.NewRightmostTrustedCountStrategy("Forwarded", 2)
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 2001:db8:cafe::17
trustedRanges, _ := realclientip.AddressesAndRangesToIPNets([]string{"192.168.0.0/16", "3.3.3.3"}...)
strat, _ = realclientip.NewRightmostTrustedRangeStrategy("X-Forwarded-For", trustedRanges)
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 2001:db8:cafe::99%eth0
ipAddr, _ := realclientip.ParseIPAddr(strat.ClientIP(req.Header, req.RemoteAddr))
fmt.Println(ipAddr.IP) // 2001:db8:cafe::99
strat = realclientip.NewChainStrategy(
realclientip.Must(realclientip.NewSingleIPHeaderStrategy("Cf-Connecting-IP")),
realclientip.RemoteAddrStrategy{},
)
fmt.Printf("\n%T: %+v\n", strat, strat)
fmt.Println(strat.ClientIP(req.Header, req.RemoteAddr)) // 192.168.1.2
// Output:
// realclientip.RemoteAddrStrategy: {}
// 192.168.1.2
//
// realclientip.SingleIPHeaderStrategy: {headerName:X-Real-Ip}
// 4.4.4.4
//
// realclientip.LeftmostNonPrivateStrategy: {headerName:Forwarded}
// 188.0.2.128
//
// realclientip.RightmostNonPrivateStrategy: {headerName:X-Forwarded-For}
// 3.3.3.3
//
// realclientip.RightmostTrustedCountStrategy: {headerName:Forwarded trustedCount:2}
// 2001:db8:cafe::17
//
// realclientip.RightmostTrustedRangeStrategy: {headerName:X-Forwarded-For trustedRanges:[192.168.0.0/16 3.3.3.3/32]
// 2001:db8:cafe::99%eth0
// 2001:db8:cafe::99
//
// realclientip.ChainStrategy: {strategies:[realclientip.SingleIPHeaderStrategy{headerName:Cf-Connecting-Ip} realclientip.RemoteAddrStrategy{}]}
// 192.168.1.2
}