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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package routing
import (
"errors"
"fmt"
"net/netip"
"os"
"strings"
)
// The geo files are v2ray's protobuf lists (v2ray-core app/router config):
//
// GeoSiteList { repeated GeoSite entry = 1; }
// GeoSite { string country_code = 1; repeated Domain domain = 2; }
// Domain { Type type = 1; string value = 2; ... } // Plain, Regex, Domain, Full
// GeoIPList { repeated GeoIP entry = 1; }
// GeoIP { string country_code = 1; repeated CIDR cidr = 2; bool reverse_match = 3; }
// CIDR { bytes ip = 1; uint32 prefix = 2; }
//
// Only the wire format is needed, so it is read here without a protobuf
// library. Only the requested tags are decoded, which keeps loading a
// large file fast.
// v2ray Domain types.
const (
domainPlain = 0 // keyword: the value appears anywhere in the name
domainRegex = 1
domainSuffix = 2 // the name or any name under it
domainFull = 3
)
var errTruncated = errors.New("truncated protobuf data")
// wire reads protobuf fields from a byte slice.
type wire struct{ b []byte }
func (w *wire) varint() (uint64, error) {
var v uint64
for shift := uint(0); shift < 64; shift += 7 {
if len(w.b) == 0 {
return 0, errTruncated
}
c := w.b[0]
w.b = w.b[1:]
v |= uint64(c&0x7f) << shift
if c < 0x80 {
return v, nil
}
}
return 0, errors.New("varint overflow")
}
// next returns the next field number, wire type, and, for length-delimited
// fields, the bytes; varint values are returned in n.
func (w *wire) next() (field int, typ int, data []byte, n uint64, err error) {
key, err := w.varint()
if err != nil {
return 0, 0, nil, 0, err
}
field, typ = int(key>>3), int(key&7)
switch typ {
case 0:
n, err = w.varint()
case 1:
if len(w.b) < 8 {
return 0, 0, nil, 0, errTruncated
}
w.b = w.b[8:]
case 2:
var size uint64
if size, err = w.varint(); err == nil {
if uint64(len(w.b)) < size {
return 0, 0, nil, 0, errTruncated
}
data, w.b = w.b[:size], w.b[size:]
}
case 5:
if len(w.b) < 4 {
return 0, 0, nil, 0, errTruncated
}
w.b = w.b[4:]
default:
err = fmt.Errorf("unsupported protobuf wire type %d", typ)
}
return field, typ, data, n, err
}
// entries calls fn with the tag and body of every entry of a GeoSiteList
// or GeoIPList whose tag (lower case) is wanted.
func entries(data []byte, want func(tag string) bool, fn func(tag string, body []byte) error) error {
list := wire{data}
for len(list.b) > 0 {
field, typ, entry, _, err := list.next()
if err != nil {
return err
}
if field != 1 || typ != 2 {
continue
}
// The tag is the first field in practice; read it without decoding
// the rest of an unwanted entry.
e := wire{entry}
tag := ""
for len(e.b) > 0 {
f, t, value, _, err := e.next()
if err != nil {
return err
}
if f == 1 && t == 2 {
tag = strings.ToLower(string(value))
break
}
}
if want(tag) {
if err := fn(tag, entry); err != nil {
return fmt.Errorf("%s: %w", tag, err)
}
}
}
return nil
}
// Tags lists the tags of a geo file, for messages.
func Tags(path string) ([]string, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var tags []string
err = entries(data, func(string) bool { return true }, func(tag string, _ []byte) error {
tags = append(tags, tag)
return nil
})
return tags, err
}
// loadGeoSite reads the domains of the wanted tags of a geosite file into
// sets by tag.
func loadGeoSite(path string, wanted map[string]bool) (map[string]*DomainSet, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
sets := make(map[string]*DomainSet)
err = entries(data, func(tag string) bool { return wanted[tag] }, func(tag string, body []byte) error {
set := sets[tag]
if set == nil {
set = newDomainSet()
sets[tag] = set
}
e := wire{body}
for len(e.b) > 0 {
field, typ, domain, _, err := e.next()
if err != nil {
return err
}
if field != 2 || typ != 2 {
continue
}
kind, value, err := readDomain(domain)
if err != nil {
return err
}
if err := set.add(kind, value); err != nil {
return err
}
}
return nil
})
return sets, err
}
func readDomain(data []byte) (kind int, value string, err error) {
d := wire{data}
for len(d.b) > 0 {
field, typ, bytes, n, err := d.next()
if err != nil {
return 0, "", err
}
switch {
case field == 1 && typ == 0:
kind = int(n)
case field == 2 && typ == 2:
value = string(bytes)
}
}
return kind, value, nil
}
// loadGeoIP reads the networks of the wanted tags of a geoip file into
// sets by tag.
func loadGeoIP(path string, wanted map[string]bool) (map[string]*IPSet, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
sets := make(map[string]*IPSet)
err = entries(data, func(tag string) bool { return wanted[tag] }, func(tag string, body []byte) error {
set := sets[tag]
if set == nil {
set = &IPSet{}
sets[tag] = set
}
e := wire{body}
for len(e.b) > 0 {
field, typ, cidr, n, err := e.next()
if err != nil {
return err
}
switch {
case field == 2 && typ == 2:
prefix, err := readCIDR(cidr)
if err != nil {
return err
}
set.add(prefix)
case field == 3 && typ == 0 && n != 0:
return errors.New("reverse_match lists are not supported")
}
}
return nil
})
for _, set := range sets {
set.finish()
}
return sets, err
}
func readCIDR(data []byte) (netip.Prefix, error) {
var ip []byte
var bits uint64
c := wire{data}
for len(c.b) > 0 {
field, typ, value, n, err := c.next()
if err != nil {
return netip.Prefix{}, err
}
switch {
case field == 1 && typ == 2:
ip = value
case field == 2 && typ == 0:
bits = n
}
}
addr, ok := netip.AddrFromSlice(ip)
if !ok || int(bits) > addr.BitLen() {
return netip.Prefix{}, fmt.Errorf("invalid CIDR %x/%d", ip, bits)
}
if addr.Is4In6() {
if bits < 96 {
return netip.Prefix{}, fmt.Errorf("invalid CIDR %x/%d", ip, bits)
}
addr, bits = addr.Unmap(), bits-96
}
return netip.PrefixFrom(addr, int(bits)).Masked(), nil
}