clash/main.go

107 lines
2.7 KiB
Go
Raw Normal View History

2018-06-10 14:50:03 +00:00
package main
import (
2018-11-14 12:58:10 +00:00
"flag"
2019-03-23 11:24:26 +00:00
"fmt"
2018-06-10 14:50:03 +00:00
"os"
"os/signal"
2018-11-14 12:58:10 +00:00
"path/filepath"
2019-03-23 11:24:26 +00:00
"runtime"
2018-06-10 14:50:03 +00:00
"syscall"
"github.com/Dreamacro/clash/config"
2018-11-14 12:58:10 +00:00
C "github.com/Dreamacro/clash/constant"
2018-06-18 03:31:49 +00:00
"github.com/Dreamacro/clash/hub"
"github.com/Dreamacro/clash/hub/executor"
2019-12-20 09:22:24 +00:00
"github.com/Dreamacro/clash/log"
2021-10-27 13:27:19 +00:00
"go.uber.org/automaxprocs/maxprocs"
2018-06-10 14:50:03 +00:00
)
var (
flagset map[string]bool
version bool
testConfig 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")
2019-03-23 11:24:26 +00:00
flag.BoolVar(&version, "v", false, "show current version of clash")
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
flag.Parse()
flagset = map[string]bool{}
flag.Visit(func(f *flag.Flag) {
flagset[f.Name] = true
})
}
2018-06-10 14:50:03 +00:00
func main() {
2021-10-27 13:27:19 +00:00
maxprocs.Set(maxprocs.Logger(func(string, ...interface{}) {}))
2019-03-23 11:24:26 +00:00
if version {
2021-04-04 09:36:22 +00:00
fmt.Printf("Clash %s %s %s with %s %s\n", C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime)
2019-03-23 11:24:26 +00:00
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)
}
2018-11-21 05:47:46 +00:00
if err := config.Init(C.Path.HomeDir()); err != nil {
2019-12-20 09:22:24 +00:00
log.Fatalln("Initial configuration directory error: %s", err.Error())
2018-11-21 05:47:46 +00:00
}
if testConfig {
if _, err := executor.Parse(); err != nil {
log.Errorln(err.Error())
2020-08-25 14:19:59 +00:00
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
os.Exit(1)
}
2020-08-25 14:19:59 +00:00
fmt.Printf("configuration file %s test is successful\n", C.Path.Config())
return
}
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 {
2019-12-20 09:22:24 +00:00
log.Fatalln("Parse config error: %s", err.Error())
2018-06-18 03:31:49 +00:00
}
2018-06-10 14:50:03 +00:00
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
}