-
Notifications
You must be signed in to change notification settings - Fork 79
/
data.go
166 lines (137 loc) · 4.19 KB
/
data.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bufio"
"encoding/csv"
"strings"
"log"
"github.com/maxmind/mmdbwriter/mmdbtype"
"os"
"path/filepath"
"strconv"
)
var (
chinaIpList []string
chunzhenIpList []string
clangIpV4List []string
clangIpV6List []string
cloudCNListV4List []string
cloudCNListV6List []string
chinaOperatorIpV4List []string
chinaOperatorIpV6List []string
countryChina = mmdbtype.Map{
"iso_code": mmdbtype.String("CN"),
"geoname_id": mmdbtype.Uint32(1814991),
"is_in_european_union": mmdbtype.Bool(false),
"names": mmdbtype.Map{
"de": mmdbtype.String("China"),
"en": mmdbtype.String("China"),
"es": mmdbtype.String("China"),
"fr": mmdbtype.String("Chine"),
"ja": mmdbtype.String("中国"),
"pt-BR": mmdbtype.String("China"),
"ru": mmdbtype.String("Китай"),
"zh-CN": mmdbtype.String("中国"),
},
}
cnData = mmdbtype.Map{
"continent": mmdbtype.Map{
"code": mmdbtype.String("AS"),
"geoname_id": mmdbtype.Uint32(6255147),
"names": mmdbtype.Map{
"de": mmdbtype.String("Asien"),
"en": mmdbtype.String("Asia"),
"es": mmdbtype.String("Asia"),
"fr": mmdbtype.String("Asie"),
"ja": mmdbtype.String("アジア"),
"pt-BR": mmdbtype.String("Ásia"),
"ru": mmdbtype.String("Азия"),
"zh-CN": mmdbtype.String("亚洲"),
},
},
"registered_country": countryChina,
"country": countryChina,
}
liteCountryMap map[uint64]mmdbtype.Map
)
func init() {
chunzhenIpList = readFileToStringArray("chunzhen_cn.txt")
chinaIpList = readFileToStringArray("china_ip_list.txt")
clangIpV4List = readFileToStringArray("all_cn.txt")
clangIpV6List = readFileToStringArray("all_cn_ipv6.txt")
chinaOperatorIpV4List = readFileToStringArray("china_operator_ipv4.txt")
chinaOperatorIpV6List = readFileToStringArray("china_operator_ipv6.txt")
initLiteCountryMap()
initCloudCN()
log.Printf("chunzhenIpList size, %d\n", len(chunzhenIpList))
log.Printf("chinaIpList size, %d\n", len(chinaIpList))
log.Printf("clangIpV4List size, %d\n", len(clangIpV4List))
log.Printf("clangIpV6List size, %d\n", len(clangIpV6List))
log.Printf("chinaOperatorIpV4List size, %d\n", len(chinaOperatorIpV4List))
log.Printf("chinaOperatorIpV6List size, %d\n", len(chinaOperatorIpV6List))
}
func readFileToStringArray(filePath string) []string {
var strList []string
fh, err := os.Open(filepath.Join(workDir, filePath))
if err != nil {
log.Printf("fail to open %s\n", err)
return strList
}
scanner := bufio.NewScanner(fh)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
strList = append(strList, scanner.Text())
}
return strList
}
func initLiteCountryMap() {
csvFile, err := os.Open(filepath.Join(workDir, "mindmax", "GeoLite2-Country-Locations-zh-CN.csv"))
if err != nil {
log.Fatalf("fail to open %s\n", err)
}
reader := csv.NewReader(csvFile)
countryLocationsZhCn, err := reader.ReadAll()
if err != nil {
log.Printf("fail to read csv %s\n", err)
return
}
liteCountryMap = make(map[uint64]mmdbtype.Map)
for index, value := range countryLocationsZhCn {
if index == 0 {
continue
}
if len(value[4]) == 0 {
continue
}
geoNameId, _ := strconv.ParseUint(value[0], 10, 32)
liteRecord := mmdbtype.Map{
"country": mmdbtype.Map{
"geoname_id": mmdbtype.Uint32(geoNameId),
"iso_code": mmdbtype.String(value[4]),
},
}
liteCountryMap[geoNameId] = liteRecord
}
//content, _ := json.Marshal(liteCountryMap)
//log.Printf("%v\n", string(content))
}
func initCloudCN() {
fh, err := os.Open(filepath.Join(workDir, "cloud_cn.list"))
if err != nil {
log.Printf("fail to open %s\n", err)
return
}
scanner := bufio.NewScanner(fh)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
clashRule := scanner.Text()
if strings.HasPrefix(clashRule, "IP-CIDR,") {
tmp := strings.Replace(clashRule, "IP-CIDR,", "", -1)
ipCidr := strings.Replace(tmp, ",no-resolve", "", -1)
cloudCNListV4List = append(cloudCNListV4List, ipCidr)
} else if strings.HasPrefix(clashRule, "IP-CIDR6,") {
tmp := strings.Replace(clashRule, "IP-CIDR6,", "", -1)
ipCidr := strings.Replace(tmp, ",no-resolve", "", -1)
cloudCNListV6List = append(cloudCNListV6List, ipCidr)
}
}
}