2018-06-10 22:50:03 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-11-14 20:58:10 +08:00
|
|
|
"flag"
|
2019-03-23 19:24:26 +08:00
|
|
|
"fmt"
|
2018-06-10 22:50:03 +08:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2018-11-14 20:58:10 +08:00
|
|
|
"path/filepath"
|
2019-03-23 19:24:26 +08:00
|
|
|
"runtime"
|
2018-06-10 22:50:03 +08:00
|
|
|
"syscall"
|
|
|
|
|
2018-07-26 00:04:59 +08:00
|
|
|
"github.com/Dreamacro/clash/config"
|
2018-11-14 20:58:10 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-06-18 11:31:49 +08:00
|
|
|
"github.com/Dreamacro/clash/hub"
|
2020-02-29 17:48:26 +08:00
|
|
|
"github.com/Dreamacro/clash/hub/executor"
|
2019-12-20 17:22:24 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2021-10-27 21:27:19 +08:00
|
|
|
|
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
2018-06-10 22:50:03 +08:00
|
|
|
)
|
|
|
|
|
2018-10-16 13:12:36 +08:00
|
|
|
var (
|
2020-04-27 22:23:09 +08:00
|
|
|
flagset map[string]bool
|
|
|
|
version bool
|
|
|
|
testConfig bool
|
2022-03-15 22:25:33 +08:00
|
|
|
geodataMode bool
|
2020-04-27 22:23:09 +08:00
|
|
|
homeDir string
|
|
|
|
configFile string
|
|
|
|
externalUI string
|
|
|
|
externalController string
|
|
|
|
secret string
|
2018-10-16 13:12:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-10-14 18:11:22 +08:00
|
|
|
flag.StringVar(&homeDir, "d", "", "set configuration directory")
|
|
|
|
flag.StringVar(&configFile, "f", "", "specify configuration file")
|
2020-04-27 22:23:09 +08:00
|
|
|
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")
|
2022-03-15 22:25:33 +08:00
|
|
|
flag.BoolVar(&geodataMode, "m", false, "set geodata mode")
|
2019-03-23 19:24:26 +08:00
|
|
|
flag.BoolVar(&version, "v", false, "show current version of clash")
|
2020-02-29 17:48:26 +08:00
|
|
|
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
|
2018-10-16 13:12:36 +08:00
|
|
|
flag.Parse()
|
2020-04-27 22:23:09 +08:00
|
|
|
|
|
|
|
flagset = map[string]bool{}
|
|
|
|
flag.Visit(func(f *flag.Flag) {
|
|
|
|
flagset[f.Name] = true
|
|
|
|
})
|
2018-10-16 13:12:36 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
func main() {
|
2022-03-16 12:10:13 +08:00
|
|
|
maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
|
2019-03-23 19:24:26 +08:00
|
|
|
if version {
|
2021-12-03 20:38:40 +08:00
|
|
|
fmt.Printf("Clash Meta %s %s %s with %s %s\n", C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime)
|
2019-03-23 19:24:26 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-14 18:11:22 +08:00
|
|
|
if homeDir != "" {
|
|
|
|
if !filepath.IsAbs(homeDir) {
|
2018-10-16 13:12:36 +08:00
|
|
|
currentDir, _ := os.Getwd()
|
2019-10-14 18:11:22 +08:00
|
|
|
homeDir = filepath.Join(currentDir, homeDir)
|
2018-10-16 13:12:36 +08:00
|
|
|
}
|
2019-10-14 18:11:22 +08:00
|
|
|
C.SetHomeDir(homeDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
if configFile != "" {
|
|
|
|
if !filepath.IsAbs(configFile) {
|
|
|
|
currentDir, _ := os.Getwd()
|
|
|
|
configFile = filepath.Join(currentDir, configFile)
|
|
|
|
}
|
|
|
|
C.SetConfig(configFile)
|
|
|
|
} else {
|
2021-10-28 11:36:11 +08:00
|
|
|
configFile = filepath.Join(C.Path.HomeDir(), C.Path.Config())
|
2019-10-14 18:11:22 +08:00
|
|
|
C.SetConfig(configFile)
|
2018-10-16 13:12:36 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 21:47:00 +08:00
|
|
|
if geodataMode {
|
2022-03-16 17:29:09 +08:00
|
|
|
C.GeodataMode = true
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2022-03-16 00:43:08 +08:00
|
|
|
if err := config.Init(C.Path.HomeDir()); err != nil {
|
|
|
|
log.Fatalln("Initial configuration directory error: %s", err.Error())
|
2022-03-15 22:25:33 +08:00
|
|
|
}
|
|
|
|
|
2020-02-29 17:48:26 +08:00
|
|
|
if testConfig {
|
|
|
|
if _, err := executor.Parse(); err != nil {
|
|
|
|
log.Errorln(err.Error())
|
2020-08-25 22:19:59 +08:00
|
|
|
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
|
2020-02-29 17:48:26 +08:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-08-25 22:19:59 +08:00
|
|
|
fmt.Printf("configuration file %s test is successful\n", C.Path.Config())
|
2020-02-29 17:48:26 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:23:09 +08:00
|
|
|
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 17:22:24 +08:00
|
|
|
log.Fatalln("Parse config error: %s", err.Error())
|
2018-06-18 11:31:49 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
sigCh := make(chan os.Signal, 1)
|
2021-11-17 16:03:47 +08:00
|
|
|
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
2018-06-10 22:50:03 +08:00
|
|
|
<-sigCh
|
2021-11-17 16:03:47 +08:00
|
|
|
|
2022-03-05 18:04:04 +08:00
|
|
|
// cleanup
|
|
|
|
log.Warnln("Clash cleanup")
|
|
|
|
hub.Cleanup()
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
log.Warnln("Clash shutting down")
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|