30f1b29257
# Conflicts: # .github/workflows/codeql-analysis.yml # .github/workflows/linter.yml # .github/workflows/release.yml # Makefile # README.md # adapter/outbound/vless.go # component/geodata/memconservative/cache.go # component/geodata/router/condition.go # component/geodata/router/condition_geoip.go # component/geodata/standard/standard.go # component/geodata/utils.go # config/config.go # config/initial.go # constant/metadata.go # constant/path.go # constant/rule.go # constant/rule_extra.go # dns/client.go # dns/filters.go # dns/resolver.go # go.mod # go.sum # hub/executor/executor.go # hub/route/configs.go # listener/listener.go # listener/tproxy/tproxy_linux_iptables.go # listener/tun/dev/dev.go # listener/tun/dev/dev_darwin.go # listener/tun/dev/dev_linux.go # listener/tun/dev/dev_windows.go # listener/tun/dev/wintun/config.go # listener/tun/dev/wintun/dll_windows.go # listener/tun/dev/wintun/session_windows.go # listener/tun/dev/wintun/wintun_windows.go # listener/tun/ipstack/commons/dns.go # listener/tun/ipstack/gvisor/tun.go # listener/tun/ipstack/gvisor/tundns.go # listener/tun/ipstack/gvisor/utils.go # listener/tun/ipstack/stack_adapter.go # listener/tun/ipstack/system/dns.go # listener/tun/ipstack/system/tcp.go # listener/tun/ipstack/system/tun.go # listener/tun/tun_adapter.go # main.go # rule/common/base.go # rule/common/domain.go # rule/common/domain_keyword.go # rule/common/domain_suffix.go # rule/common/final.go # rule/common/geoip.go # rule/common/geosite.go # rule/common/ipcidr.go # rule/common/port.go # rule/parser.go # rule/process.go # test/go.mod # test/go.sum # transport/vless/xtls.go # tunnel/tunnel.go
118 lines
2.9 KiB
Go
118 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"runtime"
|
|
"syscall"
|
|
|
|
"github.com/Dreamacro/clash/config"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/hub"
|
|
"github.com/Dreamacro/clash/hub/executor"
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
|
)
|
|
|
|
var (
|
|
flagset map[string]bool
|
|
version bool
|
|
testConfig bool
|
|
geodataMode 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(&geodataMode, "m", false, "set geodata mode")
|
|
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
|
|
})
|
|
}
|
|
|
|
func main() {
|
|
maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
|
|
if version {
|
|
fmt.Printf("Clash Meta %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 geodataMode {
|
|
C.GeodataMode = true
|
|
}
|
|
|
|
if err := config.Init(C.Path.HomeDir()); err != nil {
|
|
log.Fatalln("Initial configuration directory error: %s", err.Error())
|
|
}
|
|
|
|
if testConfig {
|
|
if _, err := executor.Parse(); 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())
|
|
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 {
|
|
log.Fatalln("Parse config error: %s", err.Error())
|
|
}
|
|
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
|
|
<-sigCh
|
|
|
|
// cleanup
|
|
log.Warnln("Clash cleanup")
|
|
hub.Cleanup()
|
|
|
|
log.Warnln("Clash shutting down")
|
|
}
|