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"
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2018-10-16 13:12:36 +08:00
|
|
|
var (
|
2019-10-14 18:11:22 +08:00
|
|
|
version bool
|
|
|
|
homeDir string
|
|
|
|
configFile 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")
|
2019-03-23 19:24:26 +08:00
|
|
|
flag.BoolVar(&version, "v", false, "show current version of clash")
|
2018-10-16 13:12:36 +08:00
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
func main() {
|
2019-03-23 19:24:26 +08:00
|
|
|
if version {
|
|
|
|
fmt.Printf("Clash %s %s %s %s\n", C.Version, runtime.GOOS, runtime.GOARCH, C.BuildTime)
|
|
|
|
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 {
|
|
|
|
configFile := filepath.Join(C.Path.HomeDir(), C.Path.Config())
|
|
|
|
C.SetConfig(configFile)
|
2018-10-16 13:12:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
if err := config.Init(C.Path.HomeDir()); err != nil {
|
|
|
|
log.Fatalf("Initial configuration directory error: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hub.Parse(); err != nil {
|
2018-07-26 00:04:59 +08:00
|
|
|
log.Fatalf("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)
|
|
|
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
<-sigCh
|
|
|
|
}
|