实现基础的DDoS防护

This commit is contained in:
刘祥超
2022-05-18 21:03:51 +08:00
parent 45620dcdb7
commit 9bdd9a433c
31 changed files with 2605 additions and 58 deletions

View File

@@ -5,9 +5,12 @@ import (
"github.com/cespare/xxhash"
"math"
"net"
"regexp"
"strings"
)
var ipv4Reg = regexp.MustCompile(`\d+\.`)
// IP2Long 将IP转换为整型
// 注意IPv6没有顺序
func IP2Long(ip string) uint64 {
@@ -54,3 +57,24 @@ func IsLocalIP(ipString string) bool {
return false
}
// IsIPv4 是否为IPv4
func IsIPv4(ip string) bool {
var data = net.ParseIP(ip)
if data == nil {
return false
}
if strings.Contains(ip, ":") {
return false
}
return data.To4() != nil
}
// IsIPv6 是否为IPv6
func IsIPv6(ip string) bool {
var data = net.ParseIP(ip)
if data == nil {
return false
}
return !IsIPv4(ip)
}