2021-06-01 16:43:13 +08:00
|
|
|
package configutils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/binary"
|
|
|
|
|
"github.com/cespare/xxhash/v2"
|
|
|
|
|
"math"
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// IP2Long 将IP转换为整型
|
|
|
|
|
// 注意IPv6没有顺序
|
|
|
|
|
func IP2Long(ip string) uint64 {
|
|
|
|
|
if len(ip) == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
s := net.ParseIP(ip)
|
|
|
|
|
if len(s) == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if strings.Contains(ip, ":") {
|
|
|
|
|
return math.MaxUint32 + xxhash.Sum64(s)
|
|
|
|
|
}
|
|
|
|
|
return uint64(binary.BigEndian.Uint32(s.To4()))
|
|
|
|
|
}
|
2021-07-20 10:55:52 +08:00
|
|
|
|
|
|
|
|
// QuoteIP 为IPv6加上括号
|
|
|
|
|
func QuoteIP(ip string) string {
|
|
|
|
|
if len(ip) == 0 {
|
|
|
|
|
return ip
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(ip, ":") {
|
|
|
|
|
return ip
|
|
|
|
|
}
|
|
|
|
|
if ip[0] != '[' {
|
|
|
|
|
return "[" + ip + "]"
|
|
|
|
|
}
|
|
|
|
|
return ip
|
|
|
|
|
}
|