-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathipCountryTest.go
55 lines (51 loc) · 1.01 KB
/
ipCountryTest.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
51
52
53
54
55
package go_utils
import (
"embed"
"net"
"strings"
"sync"
)
//go:embed ipdb/*
var IpDbs embed.FS
var (
CtIps = map[string][]*net.IPNet{}
ipLc sync.Mutex
)
func GetEmbedPay(fs embed.FS, s string) []byte {
if data, err := fs.ReadFile(s); nil == err {
return data
}
return nil
}
func init() {
if fs, err := IpDbs.ReadDir("ipdb"); nil == err {
for _, x := range fs {
if strings.HasSuffix(x.Name(), ".txt") {
if data := GetEmbedPay(IpDbs, "ipdb/"+x.Name()); nil != data {
a := strings.Split(strings.TrimSpace(string(data)), "\n")
k1 := strings.Split(x.Name(), ".")[0]
var a1 = CtIps[k1]
for _, x := range a {
if _, n1, err := net.ParseCIDR(x); nil == err {
a1 = append(a1, n1)
}
}
CtIps[k1] = a1
}
}
}
}
}
func CheckIpIsCountry(ip, ctName string) bool {
ipLc.Lock()
defer ipLc.Unlock()
ip1 := net.ParseIP(ip)
if a, ok := CtIps[strings.ToUpper(ctName)]; ok {
for _, x := range a {
if x.Contains(ip1) {
return true
}
}
}
return false
}