-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.go
50 lines (39 loc) · 1004 Bytes
/
util.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
package ratelimit
import (
"net"
"net/http"
"strings"
)
// IsWhitelistIPAddress check whether an ip is in whitelist
func IsWhitelistIPAddress(address string, localIPNets []*net.IPNet) bool {
ip := net.ParseIP(address)
if ip != nil {
for _, ipNet := range localIPNets {
if ipNet.Contains(ip) {
return true
}
}
}
return false
}
// GetRemoteIP returns the ip of requester
// Doesn't care if the ip is real or not
func GetRemoteIP(r *http.Request) (string, error) {
host, _, err := net.SplitHostPort(r.RemoteAddr)
return host, err
}
// MatchMethod check whether the request method is in the methods list
func MatchMethod(methods, method string) bool {
methods = strings.ToUpper(methods)
if methods == "*" || strings.Contains(methods, method) {
return true
}
return false
}
// MatchStatus check whether the upstream response status code is in the status list
func MatchStatus(status, s string) bool {
if strings.Contains(status, s) {
return true
}
return false
}