-
Notifications
You must be signed in to change notification settings - Fork 21
/
main.go
188 lines (162 loc) · 4.46 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
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
// Domain set converter takes a domain set file in v2fly/dlc, plaintext or gob format,
// and converts it to an optimized domain set file in plaintext or gob format.
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"strings"
"github.com/database64128/shadowsocks-go"
"github.com/database64128/shadowsocks-go/bytestrings"
"github.com/database64128/shadowsocks-go/domainset"
"github.com/database64128/shadowsocks-go/mmap"
)
var (
version bool
inDlc string
inText string
inGob string
outText string
outGob string
tag string
)
func init() {
flag.BoolVar(&version, "version", false, "Print the version and exit")
flag.StringVar(&inDlc, "inDlc", "", "Path to input domain set file in v2fly/dlc format")
flag.StringVar(&inText, "inText", "", "Path to input domain set file in plaintext format")
flag.StringVar(&inGob, "inGob", "", "Path to input domain set file in gob format")
flag.StringVar(&outText, "outText", "", "Path to output domain set file in plaintext format")
flag.StringVar(&outGob, "outGob", "", "Path to output domain set file in gob format")
flag.StringVar(&tag, "tag", "", "Select lines with the specified tag. If empty, select all lines. Only applicable to v2fly/dlc format.")
}
func main() {
flag.Parse()
if version {
os.Stdout.WriteString("shadowsocks-go-domain-set-converter " + shadowsocks.Version + "\n")
if info, ok := debug.ReadBuildInfo(); ok {
os.Stdout.WriteString(info.String())
}
return
}
var (
inCount int
inPath string
inFunc func(string) (domainset.Builder, error)
)
if inDlc != "" {
inCount++
inPath = inDlc
inFunc = DomainSetBuilderFromDlc
}
if inText != "" {
inCount++
inPath = inText
inFunc = domainset.BuilderFromText
}
if inGob != "" {
inCount++
inPath = inGob
inFunc = domainset.BuilderFromGobString
}
if inCount != 1 {
fmt.Fprintln(os.Stderr, "Exactly one of -inDlc, -inText, -inGob must be specified.")
flag.Usage()
os.Exit(1)
}
if outText == "" && outGob == "" {
fmt.Fprintln(os.Stderr, "Specify output file paths with -outText and/or -outGob.")
flag.Usage()
os.Exit(1)
}
data, close, err := mmap.ReadFile[string](inPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to read input file:", err)
os.Exit(1)
}
defer close()
dsb, err := inFunc(data)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to parse input file:", err)
return
}
if outText != "" {
fout, err := os.Create(outText)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create output file:", err)
return
}
defer fout.Close()
err = dsb.WriteText(fout)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to write output file:", err)
return
}
}
if outGob != "" {
fout, err := os.Create(outGob)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create output file:", err)
return
}
defer fout.Close()
err = dsb.WriteGob(fout)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to write output file:", err)
return
}
}
}
func DomainSetBuilderFromDlc(text string) (domainset.Builder, error) {
const (
domainPrefix = "full:"
suffixPrefix = "domain:"
keywordPrefix = "keyword:"
regexpPrefix = "regexp:"
domainPrefixLen = len(domainPrefix)
suffixPrefixLen = len(suffixPrefix)
keywordPrefixLen = len(keywordPrefix)
regexpPrefixLen = len(regexpPrefix)
)
dsb := domainset.Builder{
domainset.NewDomainMapMatcher(0),
domainset.NewDomainSuffixTrieMatcherBuilder(0),
domainset.NewKeywordLinearMatcher(0),
domainset.NewRegexpMatcherBuilder(0),
}
for line := range bytestrings.NonEmptyLines(text) {
if line[0] == '#' {
continue
}
end := strings.IndexByte(line, '@')
if end == 0 {
return dsb, fmt.Errorf("invalid line: %q", line)
}
if tag == "" { // select all lines
if end == -1 {
end = len(line)
} else {
end--
}
} else { // select matched tag
if end == -1 || line[end+1:] != tag { // no tag or different tag
continue
} else {
end--
}
}
switch {
case strings.HasPrefix(line, domainPrefix):
dsb.DomainMatcherBuilder().Insert(line[domainPrefixLen:end])
case strings.HasPrefix(line, suffixPrefix):
dsb.SuffixMatcherBuilder().Insert(line[suffixPrefixLen:end])
case strings.HasPrefix(line, keywordPrefix):
dsb.KeywordMatcherBuilder().Insert(line[keywordPrefixLen:end])
case strings.HasPrefix(line, regexpPrefix):
dsb.RegexpMatcherBuilder().Insert(line[regexpPrefixLen:end])
default:
return dsb, fmt.Errorf("invalid line: %q", line)
}
}
return dsb, nil
}