-
Notifications
You must be signed in to change notification settings - Fork 87
/
main.go
95 lines (81 loc) · 2.07 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"strings"
"github.com/gogo/protobuf/proto"
"v2ray.com/core/app/router"
)
var (
ruleSitesDomains []*router.Domain
)
func GetSitesList(fileName string) []*router.Domain {
d, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err)
}
domains := strings.Split(string(d), "\n")
ruleSitesDomains = make([]*router.Domain, len(domains))
for idx, pattern := range domains {
ruleSitesDomains[idx] = &router.Domain{
Type: router.Domain_Domain,
Value: pattern,
}
}
return ruleSitesDomains
}
func main() {
datfile := flag.String("dat", "geosite.dat", "datfile's name")
sitefile := flag.String("dir", "sites", "sites file folder")
flag.CommandLine.Usage = func() {
fmt.Println(`
./v2sitedat -dat geosite.dat -dir sites
rules :
{
"type": "field",
"domain": ["geosite:reject"],
"outboundTag": "block"
},{
"type": "field",
"domain": ["geosite:proxy"],
"outboundTag": "proxy"
},{
"type": "field",
"domain": ["geosite:proxyjp"],
"outboundTag": "proxyjp"
},{
"type": "field",
"domain": ["geosite:direct"],
"outboundTag": "direct"
},{
"type": "field",
"domain": ["geosite:cn"],
"outboundTag": "direct"
},
`)
}
flag.Parse()
v2SiteList := new(router.GeoSiteList)
rulefiles, err := ioutil.ReadDir(*sitefile)
if err != nil {
panic(err)
}
for _, rf := range rulefiles {
filename := rf.Name()
fmt.Println(filename)
v2SiteList.Entry = append(v2SiteList.Entry, &router.GeoSite{
CountryCode: strings.ToUpper(filename),
Domain: GetSitesList(*sitefile + "/" + filename),
})
}
v2SiteListBytes, err := proto.Marshal(v2SiteList)
if err != nil {
fmt.Println("failed to marshal v2sites:", err)
return
}
if err := ioutil.WriteFile(*datfile, v2SiteListBytes, 0777); err != nil {
fmt.Println("failed to write %s.", *datfile, err)
}
fmt.Println(*sitefile, "-->", *datfile)
}