forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
154 lines (135 loc) · 3.74 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"text/tabwriter"
"github.com/whtsky/clash/config"
C "github.com/whtsky/clash/constant"
"github.com/whtsky/clash/hub"
"github.com/whtsky/clash/hub/executor"
"github.com/whtsky/clash/log"
"github.com/whtsky/clash/tunnel"
"go.uber.org/automaxprocs/maxprocs"
)
var (
flagset map[string]bool
version bool
testConfig bool
ruleInspect bool
homeDir string
configFile string
externalUI string
externalController string
secret string
)
func init() {
flag.StringVar(&homeDir, "d", "", "set configuration directory")
flag.StringVar(&configFile, "f", "", "specify configuration file")
flag.StringVar(&externalUI, "ext-ui", "", "override external ui directory")
flag.StringVar(&externalController, "ext-ctl", "", "override external controller address")
flag.StringVar(&secret, "secret", "", "override secret for RESTful API")
flag.BoolVar(&version, "v", false, "show current version of clash")
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
flag.BoolVar(&ruleInspect, "rule-inspect", false, "show counts of different rule types")
flag.Parse()
flagset = map[string]bool{}
flag.Visit(func(f *flag.Flag) {
flagset[f.Name] = true
})
}
func countRules(rules []C.Rule) map[C.RuleType]int {
counts := make(map[C.RuleType]int)
for _, rule := range rules {
ruleType := rule.RuleType()
_, ok := counts[ruleType]
if !ok {
counts[ruleType] = 1
} else {
counts[ruleType]++
}
}
return counts
}
func printRuleCounts(rules []C.Rule) {
result := countRules(rules)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 5, ' ', 0)
for ruleType, count := range result {
fmt.Fprintf(w, "%s\t%d\n", ruleType, count)
}
w.Flush()
}
func printRuleInspect(rules []C.Rule) {
fmt.Println("========== Raw Rules ==========")
printRuleCounts(rules)
fmt.Println("\n\n========== Combined Rules ==========")
printRuleCounts(tunnel.CombineRules(rules))
}
func main() {
maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
if version {
fmt.Printf("Clash %s %s %s with %s %s\n", C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime)
return
}
if homeDir != "" {
if !filepath.IsAbs(homeDir) {
currentDir, _ := os.Getwd()
homeDir = filepath.Join(currentDir, homeDir)
}
C.SetHomeDir(homeDir)
}
if configFile != "" {
if !filepath.IsAbs(configFile) {
currentDir, _ := os.Getwd()
configFile = filepath.Join(currentDir, configFile)
}
C.SetConfig(configFile)
} else {
configFile := filepath.Join(C.Path.HomeDir(), C.Path.Config())
C.SetConfig(configFile)
}
if err := config.Init(C.Path.HomeDir()); err != nil {
log.Fatalln("Initial configuration directory error: %s", err.Error())
}
if testConfig {
config, err := executor.Parse()
if err != nil {
log.Errorln(err.Error())
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
os.Exit(1)
}
fmt.Printf("configuration file %s test is successful\n", C.Path.Config())
if ruleInspect {
printRuleInspect(config.Rules)
}
return
}
if ruleInspect {
config, err := executor.Parse()
if err != nil {
log.Errorln(err.Error())
os.Exit(1)
}
printRuleInspect(config.Rules)
}
var options []hub.Option
if flagset["ext-ui"] {
options = append(options, hub.WithExternalUI(externalUI))
}
if flagset["ext-ctl"] {
options = append(options, hub.WithExternalController(externalController))
}
if flagset["secret"] {
options = append(options, hub.WithSecret(secret))
}
if err := hub.Parse(options...); err != nil {
log.Fatalln("Parse config error: %s", err.Error())
}
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}