forked from Loyalsoldier/domain-list-custom
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
112 lines (101 loc) · 3.69 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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"google.golang.org/protobuf/proto"
)
var (
dataPath = flag.String("datapath", filepath.Join("./", "data"), "Path to your custom 'data' directory")
datName = flag.String("datname", "geosite.dat", "Name of the generated dat file")
outputPath = flag.String("outputpath", "./publish", "Output path to the generated files")
exportLists = flag.String("exportlists", "private,microsoft,apple,google,category-games,netflix,disney,hbo,primevideo,apple-tvplus,youtube,tiktok,bilibili,spotify,category-ai-!cn,speedtest,tld-!cn,geolocation-!cn,cn", "Lists to be exported in plaintext format, separated by ',' comma")
excludeAttrs = flag.String("excludeattrs", "private@ads,microsoft@ads,apple@ads,google@ads,category-games@ads,netflix@ads,disney@ads,hbo@ads,primevideo@ads,apple-tvplus@ads,youtube@ads,tiktok@ads,bilibili@ads,spotify@ads,category-ai-!cn@ads,speedtest@ads,tld-!cn@cn@ads,geolocation-!cn@cn@ads,cn@!cn@ads", "Exclude rules with certain attributes in certain lists, seperated by ',' comma, support multiple attributes in one list. Example: geolocation-!cn@cn@ads,geolocation-cn@!cn")
)
func main() {
flag.Parse()
dir := GetDataDir()
listInfoMap := make(ListInfoMap)
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if err := listInfoMap.Marshal(path); err != nil {
return err
}
return nil
}); err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
if err := listInfoMap.FlattenAndGenUniqueDomainList(); err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
// Process and split *excludeRules
excludeAttrsInFile := make(map[fileName]map[attribute]bool)
if *excludeAttrs != "" {
exFilenameAttrSlice := strings.Split(*excludeAttrs, ",")
for _, exFilenameAttr := range exFilenameAttrSlice {
exFilenameAttr = strings.TrimSpace(exFilenameAttr)
exFilenameAttrMap := strings.Split(exFilenameAttr, "@")
filename := fileName(strings.ToUpper(strings.TrimSpace(exFilenameAttrMap[0])))
excludeAttrsInFile[filename] = make(map[attribute]bool)
for _, attr := range exFilenameAttrMap[1:] {
attr = strings.TrimSpace(attr)
if len(attr) > 0 {
excludeAttrsInFile[filename][attribute(attr)] = true
}
}
}
}
// Process and split *exportLists
var exportListsSlice []string
if *exportLists != "" {
tempSlice := strings.Split(*exportLists, ",")
for _, exportList := range tempSlice {
exportList = strings.TrimSpace(exportList)
if len(exportList) > 0 {
exportListsSlice = append(exportListsSlice, exportList)
}
}
}
// Generate dlc.dat
if geositeList := listInfoMap.ToProto(excludeAttrsInFile); geositeList != nil {
protoBytes, err := proto.Marshal(geositeList)
if err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
if err := os.MkdirAll(*outputPath, 0755); err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
}
if err := os.WriteFile(filepath.Join(*outputPath, *datName), protoBytes, 0644); err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
} else {
fmt.Printf("%s has been generated successfully in '%s'.\n", *datName, *outputPath)
}
}
// Generate plaintext list files
if filePlainTextBytesMap, err := listInfoMap.ToPlainText(exportListsSlice); err == nil {
for filename, plaintextBytes := range filePlainTextBytesMap {
filename += ".txt"
if err := os.WriteFile(filepath.Join(*outputPath, filename), plaintextBytes, 0644); err != nil {
fmt.Println("Failed:", err)
os.Exit(1)
} else {
fmt.Printf("%s has been generated successfully in '%s'.\n", filename, *outputPath)
}
}
} else {
fmt.Println("Failed:", err)
os.Exit(1)
}
}