-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathmain.go
102 lines (83 loc) · 2.04 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
package main
import (
"runtime"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var clashHome string
var clashConfig string
var clashUI string
var mmdb bool
var commit string
var conf *Conf
var rootCmd = &cobra.Command{
Use: "tpclash",
Short: "Transparent proxy tool for Clash",
Version: commit,
}
var runCmd = &cobra.Command{
Use: "run",
Short: "Run tpclash",
Run: func(cmd *cobra.Command, args []string) {
run()
},
}
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean tpclash iptables and route config",
Run: func(cmd *cobra.Command, args []string) {
cleanIPTables()
cleanRoute()
},
}
var extractCmd = &cobra.Command{
Use: "extract",
Short: "Extract embed files",
Run: func(cmd *cobra.Command, args []string) {
copyFiles()
},
}
func init() {
cobra.EnableCommandSorting = false
cobra.OnInitialize(tpClashInit)
rootCmd.PersistentFlags().StringVarP(&clashHome, "home", "d", "/data/clash", "clash home dir")
rootCmd.PersistentFlags().StringVarP(&clashConfig, "config", "c", "/etc/clash.yaml", "clash config path")
rootCmd.PersistentFlags().StringVarP(&clashUI, "ui", "u", "yacd", "clash dashboard(official/yacd)")
rootCmd.PersistentFlags().BoolVar(&mmdb, "mmdb", true, "extract Country.mmdb file")
rootCmd.AddCommand(runCmd, cleanCmd, extractCmd)
}
func main() {
cobra.CheckErr(rootCmd.Execute())
}
func tpClashInit() {
// init logrus
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05",
PadLevelText: true,
})
// os check
if runtime.GOOS != "linux" {
logrus.Fatal("only support linux system")
}
// init config
viper.SetConfigFile(clashConfig)
viper.SetEnvPrefix("TPCLASH")
viper.AutomaticEnv()
logrus.Info("[main] load clash config...")
err := viper.ReadInConfig()
if err != nil {
logrus.Fatalf("failed to load config: %v", err)
}
conf, err = parseConf()
if err != nil {
logrus.Fatal(err)
}
if conf.Debug {
logrus.SetLevel(logrus.DebugLevel)
}
// copy static files
createUser()
mkHomeDir()
}