MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / AllowIP

Function AllowIP

internal/iplibrary/list_utils.go:14–62  ·  view source on GitHub ↗

AllowIP 检查IP是否被允许访问 如果一个IP不在任何名单中,则允许访问

(ip string, serverId int64)

Source from the content-addressed store, hash-verified

12// AllowIP 检查IP是否被允许访问
13// 如果一个IP不在任何名单中,则允许访问
14func AllowIP(ip string, serverId int64) (canGoNext bool, inAllowList bool, expiresAt int64) {
15 if !Tea.IsTesting() { // 如果在测试环境,我们不加入一些白名单,以便于可以在本地和局域网正常测试
16 // 放行lo
17 if ip == "127.0.0.1" || ip == "::1" {
18 return true, true, 0
19 }
20
21 // check node
22 nodeConfig, err := nodeconfigs.SharedNodeConfig()
23 if err == nil && nodeConfig.IPIsAutoAllowed(ip) {
24 return true, true, 0
25 }
26 }
27
28 var ipBytes = iputils.ToBytes(ip)
29 if IsZero(ipBytes) {
30 return false, false, 0
31 }
32
33 // check white lists
34 if GlobalWhiteIPList.Contains(ipBytes) {
35 return true, true, 0
36 }
37
38 if serverId > 0 {
39 var list = SharedServerListManager.FindWhiteList(serverId, false)
40 if list != nil && list.Contains(ipBytes) {
41 return true, true, 0
42 }
43 }
44
45 // check black lists
46 expiresAt, ok := GlobalBlackIPList.ContainsExpires(ipBytes)
47 if ok {
48 return false, false, expiresAt
49 }
50
51 if serverId > 0 {
52 var list = SharedServerListManager.FindBlackList(serverId, false)
53 if list != nil {
54 expiresAt, ok = list.ContainsExpires(ipBytes)
55 if ok {
56 return false, false, expiresAt
57 }
58 }
59 }
60
61 return true, false, 0
62}
63
64// IsInWhiteList 检查IP是否在白名单中
65func IsInWhiteList(ip string) bool {

Callers 6

AcceptMethod · 0.92
SetServerIdMethod · 0.92
doRequestLimitMethod · 0.92
doWAFRequestMethod · 0.92
TestIPIsAllowedFunction · 0.85
AllowIPStringsFunction · 0.85

Calls 5

IsZeroFunction · 0.85
FindWhiteListMethod · 0.80
FindBlackListMethod · 0.80
ContainsMethod · 0.45
ContainsExpiresMethod · 0.45

Tested by 1

TestIPIsAllowedFunction · 0.68