-
Notifications
You must be signed in to change notification settings - Fork 3
/
clash.go
208 lines (194 loc) · 5.09 KB
/
clash.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"io"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/metacubex/meta-rules-converter/output/sing"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
func init() {
commandClash.PersistentFlags().StringVarP(&inPath, "in", "i", "", "Input Path")
commandClash.PersistentFlags().StringVarP(&outType, "type", "t", "", "Output Type")
commandClash.PersistentFlags().StringVarP(&outDir, "out", "o", "", "Output Path")
mainCommand.AddCommand(commandClash)
}
var commandClash = &cobra.Command{
Use: "clash",
RunE: convertClash,
}
func convertClash(cmd *cobra.Command, args []string) error {
if inPath == "" {
inPath = "."
}
if outDir == "" {
outDir = "rules"
}
if outType == "" {
outType = "sing-box"
}
outDir = strings.TrimSuffix(outDir, "/")
files, err := searchFiles(inPath, ".yaml")
if err != nil {
return err
}
os.MkdirAll(outDir, 0755)
var (
domainFull = make(map[string][]string)
domainSuffix = make(map[string][]string)
domainKeyword = make(map[string][]string)
domainRegex = make(map[string][]string)
ipCIDR = make(map[string][]string)
processName = make(map[string][]string)
packageName = make(map[string][]string)
processPath = make(map[string][]string)
dstPort = make(map[string][]uint16)
)
for _, path := range files {
var (
d []string
ds []string
dk []string
dr []string
ic []string
pn []string
pa []string
pp []string
dp []uint16
)
dirName, content := readFile(path)
for _, line := range content {
rule := strings.Split(line, ",")
switch rule[0] {
case "DOMAIN":
d = append(d, rule[1])
case "DOMAIN-SUFFIX":
ds = append(ds, rule[1])
case "DOMAIN-KEYWORD":
dk = append(dk, rule[1])
case "DOMAIN-REGEX":
dr = append(dr, rule[1])
case "IP-CIDR", "IP-CIDR6":
ic = append(ic, rule[1])
case "PROCESS-NAME":
if strings.Contains(rule[1], ".exe") {
pn = append(pn, rule[1])
} else if strings.Contains(rule[1], ".") {
pa = append(pa, rule[1])
} else {
pn = append(pn, rule[1])
}
case "PROCESS-PATH":
pp = append(pp, rule[1])
case "DST-PORT":
port, _ := strconv.Atoi(rule[1])
dp = append(dp, uint16(port))
}
}
domainFull[dirName] = d
domainSuffix[dirName] = ds
domainKeyword[dirName] = dk
domainRegex[dirName] = dr
ipCIDR[dirName] = ic
processName[dirName] = pn
packageName[dirName] = pa
processPath[dirName] = pp
dstPort[dirName] = dp
}
switch outType {
case "sing-box":
for name, domain := range domainFull {
os.MkdirAll(outDir+"/"+name, 0755)
if len(domain) != 0 || len(domainSuffix[name]) != 0 || len(domainKeyword[name]) != 0 || len(domainRegex[name]) != 0 {
domainRule := []sing.DefaultHeadlessRule{
{
Domain: domain,
DomainKeyword: domainKeyword[name],
DomainSuffix: domainSuffix[name],
DomainRegex: domainRegex[name],
},
}
sing.SaveSingRuleSet(domainRule, outDir+"/"+name+"/domain")
}
if len(ipCIDR[name]) != 0 {
ipRule := []sing.DefaultHeadlessRule{
{
IPCIDR: ipCIDR[name],
},
}
sing.SaveSingRuleSet(ipRule, outDir+"/"+name+"/ip")
}
if len(processName[name]) != 0 || len(packageName[name]) != 0 || len(processPath[name]) != 0 {
processRule := []sing.DefaultHeadlessRule{
{
ProcessName: processName[name],
PackageName: packageName[name],
ProcessPath: processPath[name],
},
}
sing.SaveSingRuleSet(processRule, outDir+"/"+name+"/process")
}
if len(dstPort[name]) != 0 {
otherRule := []sing.DefaultHeadlessRule{
{
Port: dstPort[name],
},
}
sing.SaveSingRuleSet(otherRule, outDir+"/"+name+"/other")
}
classicalRule := []sing.DefaultHeadlessRule{
{
Domain: domainFull[name],
DomainKeyword: domainKeyword[name],
DomainSuffix: domainSuffix[name],
DomainRegex: domainRegex[name],
IPCIDR: ipCIDR[name],
ProcessName: processName[name],
PackageName: packageName[name],
ProcessPath: processPath[name],
Port: dstPort[name],
},
}
sing.SaveSingRuleSet(classicalRule, outDir+"/"+name+"/classical")
}
}
return nil
}
func searchFiles(root string, keyword string) ([]string, error) {
var result []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.Contains(info.Name(), keyword) {
result = append(result, path)
}
return nil
})
return result, err
}
func readFile(path string) (string, []string) {
var dirName string
if runtime.GOOS == "windows" {
lastIndex := strings.LastIndex(path, "\\")
if lastIndex != -1 {
dirName = strings.ToLower(strings.TrimSuffix(path[lastIndex+1:], ".yaml"))
}
} else {
lastIndex := strings.LastIndex(path, "/")
if lastIndex != -1 {
dirName = strings.ToLower(strings.TrimSuffix(path[lastIndex+1:], ".yaml"))
}
}
file, _ := os.Open(path)
data, _ := io.ReadAll(file)
var rules Rules
yaml.Unmarshal(data, &rules)
return dirName, rules.Payload
}
type Rules struct {
Payload []string `yaml:"payload"`
}