2021-11-17 16:16:09 +08:00
|
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
package iplibrary
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-21 21:32:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
2021-11-17 16:16:09 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// AllowIP 检查IP是否被允许访问
|
2022-01-10 19:54:10 +08:00
|
|
|
|
// 如果一个IP不在任何名单中,则允许访问
|
2022-03-06 19:40:26 +08:00
|
|
|
|
func AllowIP(ip string, serverId int64) (canGoNext bool, inAllowList bool) {
|
2022-06-05 17:15:02 +08:00
|
|
|
|
// 放行lo
|
|
|
|
|
|
if ip == "127.0.0.1" {
|
|
|
|
|
|
return true, true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-17 16:16:09 +08:00
|
|
|
|
var ipLong = utils.IP2Long(ip)
|
|
|
|
|
|
if ipLong == 0 {
|
2022-03-06 19:40:26 +08:00
|
|
|
|
return false, false
|
2021-11-17 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-21 21:32:10 +08:00
|
|
|
|
// check node
|
|
|
|
|
|
nodeConfig, err := nodeconfigs.SharedNodeConfig()
|
|
|
|
|
|
if err == nil && nodeConfig.IPIsAutoAllowed(ip) {
|
|
|
|
|
|
return true, true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-17 16:16:09 +08:00
|
|
|
|
// check white lists
|
|
|
|
|
|
if GlobalWhiteIPList.Contains(ipLong) {
|
2022-03-06 19:40:26 +08:00
|
|
|
|
return true, true
|
2021-11-17 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if serverId > 0 {
|
|
|
|
|
|
var list = SharedServerListManager.FindWhiteList(serverId, false)
|
|
|
|
|
|
if list != nil && list.Contains(ipLong) {
|
2022-03-06 19:40:26 +08:00
|
|
|
|
return true, true
|
2021-11-17 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// check black lists
|
|
|
|
|
|
if GlobalBlackIPList.Contains(ipLong) {
|
2022-03-06 19:40:26 +08:00
|
|
|
|
return false, false
|
2021-11-17 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if serverId > 0 {
|
|
|
|
|
|
var list = SharedServerListManager.FindBlackList(serverId, false)
|
|
|
|
|
|
if list != nil && list.Contains(ipLong) {
|
2022-03-06 19:40:26 +08:00
|
|
|
|
return false, false
|
2021-11-17 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 19:40:26 +08:00
|
|
|
|
return true, false
|
2021-11-17 16:16:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-10 19:54:10 +08:00
|
|
|
|
// IsInWhiteList 检查IP是否在白名单中
|
|
|
|
|
|
func IsInWhiteList(ip string) bool {
|
|
|
|
|
|
var ipLong = utils.IP2Long(ip)
|
|
|
|
|
|
if ipLong == 0 {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// check white lists
|
|
|
|
|
|
return GlobalWhiteIPList.Contains(ipLong)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-17 16:16:09 +08:00
|
|
|
|
// AllowIPStrings 检查一组IP是否被允许访问
|
|
|
|
|
|
func AllowIPStrings(ipStrings []string, serverId int64) bool {
|
|
|
|
|
|
if len(ipStrings) == 0 {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, ip := range ipStrings {
|
2022-03-06 19:40:26 +08:00
|
|
|
|
isAllowed, _ := AllowIP(ip, serverId)
|
2021-11-17 16:16:09 +08:00
|
|
|
|
if !isAllowed {
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|