-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.go
152 lines (137 loc) · 3.29 KB
/
main.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
// GeoIP generator
//
// Before running this file, the GeoIP database must be downloaded and present.
// To download GeoIP database: https://dev.maxmind.com/geoip/geoip2/geolite2/
// Inside you will find block files for IPv4 and IPv6 and country code mapping.
package main
import (
"encoding/csv"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/golang/protobuf/proto"
"v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/infra/conf"
)
var (
countryCodeFile = flag.String("country", "", "Path to the country code file")
ipv4File = flag.String("ipv4", "", "Path to the IPv4 block file")
ipv6File = flag.String("ipv6", "", "Path to the IPv6 block file")
)
func getCountryCodeMap() (map[string]string, error) {
countryCodeReader, err := os.Open(*countryCodeFile)
if err != nil {
return nil, err
}
defer countryCodeReader.Close()
m := make(map[string]string)
reader := csv.NewReader(countryCodeReader)
lines, err := reader.ReadAll()
if err != nil {
return nil, err
}
for _, line := range lines[1:] {
id := line[0]
countryCode := line[4]
if len(countryCode) == 0 {
continue
}
m[id] = strings.ToUpper(countryCode)
}
return m, nil
}
func getCidrPerCountry(file string, m map[string]string, list map[string][]*router.CIDR) error {
fileReader, err := os.Open(file)
if err != nil {
return err
}
defer fileReader.Close()
reader := csv.NewReader(fileReader)
lines, err := reader.ReadAll()
if err != nil {
return err
}
for _, line := range lines[1:] {
cidrStr := line[0]
countryId := line[1]
if countryCode, found := m[countryId]; found {
cidr, err := conf.ParseIP(cidrStr)
if err != nil {
return err
}
cidrs := append(list[countryCode], cidr)
list[countryCode] = cidrs
}
}
return nil
}
func main() {
flag.Parse()
ccMap, err := getCountryCodeMap()
if err != nil {
fmt.Println("Error reading country code map:", err)
return
}
cidrList := make(map[string][]*router.CIDR)
if err := getCidrPerCountry(*ipv4File, ccMap, cidrList); err != nil {
fmt.Println("Error loading IPv4 file:", err)
return
}
if err := getCidrPerCountry(*ipv6File, ccMap, cidrList); err != nil {
fmt.Println("Error loading IPv6 file:", err)
return
}
geoIPList := new(router.GeoIPList)
for cc, cidr := range cidrList {
geoIPList.Entry = append(geoIPList.Entry, &router.GeoIP{
CountryCode: cc,
Cidr: cidr,
})
}
geoIPList.Entry = append(geoIPList.Entry, getLocalIPs())
geoIPBytes, err := proto.Marshal(geoIPList)
if err != nil {
fmt.Println("Error marshalling geoip list:", err)
}
if err := ioutil.WriteFile("geoip.dat", geoIPBytes, 0777); err != nil {
fmt.Println("Error writing geoip to file:", err)
}
}
var (
localIPs = []string{
"0.0.0.0/8",
"10.0.0.0/8",
"100.64.0.0/10",
"127.0.0.0/8",
"169.254.0.0/16",
"172.16.0.0/12",
"192.0.0.0/24",
"192.0.2.0/24",
"192.88.99.0/24",
"192.168.0.0/16",
"198.18.0.0/15",
"198.51.100.0/24",
"203.0.113.0/24",
"224.0.0.0/4",
"240.0.0.0/4",
"255.255.255.255/32",
"::1/128",
"fc00::/7",
"fe80::/10",
}
)
func getLocalIPs() *router.GeoIP {
cidr := make([]*router.CIDR, 0, 16)
for _, ip := range localIPs {
c, err := conf.ParseIP(ip)
common.Must(err)
cidr = append(cidr, c)
}
return &router.GeoIP{
CountryCode: "PRIVATE",
Cidr: cidr,
}
}