@@ -7,46 +7,67 @@ import (
77 "flag"
88 "fmt"
99 "os"
10+ "runtime/debug"
1011 "strings"
1112
13+ "github.com/database64128/shadowsocks-go"
1214 "github.com/database64128/shadowsocks-go/bytestrings"
1315 "github.com/database64128/shadowsocks-go/domainset"
1416 "github.com/database64128/shadowsocks-go/mmap"
1517)
1618
1719var (
18- inDlc = flag .String ("inDlc" , "" , "Path to input domain set file in v2fly/dlc format." )
19- inText = flag .String ("inText" , "" , "Path to input domain set file in plaintext format." )
20- inGob = flag .String ("inGob" , "" , "Path to input domain set file in gob format." )
21- outText = flag .String ("outText" , "" , "Path to output domain set file in plaintext format." )
22- outGob = flag .String ("outGob" , "" , "Path to output domain set file in gob format." )
23- tag = flag .String ("tag" , "" , "Select lines with the specified tag. If empty, select all lines. Only applicable to v2fly/dlc format." )
20+ version bool
21+ inDlc string
22+ inText string
23+ inGob string
24+ outText string
25+ outGob string
26+ tag string
2427)
2528
29+ func init () {
30+ flag .BoolVar (& version , "version" , false , "Print the version and exit" )
31+ flag .StringVar (& inDlc , "inDlc" , "" , "Path to input domain set file in v2fly/dlc format" )
32+ flag .StringVar (& inText , "inText" , "" , "Path to input domain set file in plaintext format" )
33+ flag .StringVar (& inGob , "inGob" , "" , "Path to input domain set file in gob format" )
34+ flag .StringVar (& outText , "outText" , "" , "Path to output domain set file in plaintext format" )
35+ flag .StringVar (& outGob , "outGob" , "" , "Path to output domain set file in gob format" )
36+ flag .StringVar (& tag , "tag" , "" , "Select lines with the specified tag. If empty, select all lines. Only applicable to v2fly/dlc format." )
37+ }
38+
2639func main () {
2740 flag .Parse ()
2841
42+ if version {
43+ os .Stdout .WriteString ("shadowsocks-go-domain-set-converter " + shadowsocks .Version + "\n " )
44+ if info , ok := debug .ReadBuildInfo (); ok {
45+ os .Stdout .WriteString (info .String ())
46+ }
47+ return
48+ }
49+
2950 var (
3051 inCount int
3152 inPath string
3253 inFunc func (string ) (domainset.Builder , error )
3354 )
3455
35- if * inDlc != "" {
56+ if inDlc != "" {
3657 inCount ++
37- inPath = * inDlc
58+ inPath = inDlc
3859 inFunc = DomainSetBuilderFromDlc
3960 }
4061
41- if * inText != "" {
62+ if inText != "" {
4263 inCount ++
43- inPath = * inText
64+ inPath = inText
4465 inFunc = domainset .BuilderFromText
4566 }
4667
47- if * inGob != "" {
68+ if inGob != "" {
4869 inCount ++
49- inPath = * inGob
70+ inPath = inGob
5071 inFunc = domainset .BuilderFromGobString
5172 }
5273
@@ -56,7 +77,7 @@ func main() {
5677 os .Exit (1 )
5778 }
5879
59- if * outText == "" && * outGob == "" {
80+ if outText == "" && outGob == "" {
6081 fmt .Fprintln (os .Stderr , "Specify output file paths with -outText and/or -outGob." )
6182 flag .Usage ()
6283 os .Exit (1 )
@@ -75,8 +96,8 @@ func main() {
7596 return
7697 }
7798
78- if * outText != "" {
79- fout , err := os .Create (* outText )
99+ if outText != "" {
100+ fout , err := os .Create (outText )
80101 if err != nil {
81102 fmt .Fprintln (os .Stderr , "Failed to create output file:" , err )
82103 return
@@ -90,8 +111,8 @@ func main() {
90111 }
91112 }
92113
93- if * outGob != "" {
94- fout , err := os .Create (* outGob )
114+ if outGob != "" {
115+ fout , err := os .Create (outGob )
95116 if err != nil {
96117 fmt .Fprintln (os .Stderr , "Failed to create output file:" , err )
97118 return
@@ -135,14 +156,14 @@ func DomainSetBuilderFromDlc(text string) (domainset.Builder, error) {
135156 return dsb , fmt .Errorf ("invalid line: %q" , line )
136157 }
137158
138- if * tag == "" { // select all lines
159+ if tag == "" { // select all lines
139160 if end == - 1 {
140161 end = len (line )
141162 } else {
142163 end --
143164 }
144165 } else { // select matched tag
145- if end == - 1 || line [end + 1 :] != * tag { // no tag or different tag
166+ if end == - 1 || line [end + 1 :] != tag { // no tag or different tag
146167 continue
147168 } else {
148169 end --
0 commit comments