mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
145 lines
3.9 KiB
Go
145 lines
3.9 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/metacubex/mihomo/component/updater"
|
|
"github.com/metacubex/mihomo/config"
|
|
C "github.com/metacubex/mihomo/constant"
|
|
"github.com/metacubex/mihomo/constant/features"
|
|
"github.com/metacubex/mihomo/hub"
|
|
"github.com/metacubex/mihomo/hub/executor"
|
|
"github.com/metacubex/mihomo/log"
|
|
"github.com/metacubex/mihomo/rules/provider"
|
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
|
)
|
|
|
|
var (
|
|
version bool
|
|
testConfig bool
|
|
geodataMode bool
|
|
homeDir string
|
|
configFile string
|
|
externalUI string
|
|
externalController string
|
|
externalControllerUnix string
|
|
secret string
|
|
)
|
|
|
|
func init() {
|
|
flag.StringVar(&homeDir, "d", os.Getenv("CLASH_HOME_DIR"), "set configuration directory")
|
|
flag.StringVar(&configFile, "f", os.Getenv("CLASH_CONFIG_FILE"), "specify configuration file")
|
|
flag.StringVar(&externalUI, "ext-ui", os.Getenv("CLASH_OVERRIDE_EXTERNAL_UI_DIR"), "override external ui directory")
|
|
flag.StringVar(&externalController, "ext-ctl", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER"), "override external controller address")
|
|
flag.StringVar(&externalControllerUnix, "ext-ctl-unix", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_UNIX"), "override external controller unix address")
|
|
flag.StringVar(&secret, "secret", os.Getenv("CLASH_OVERRIDE_SECRET"), "override secret for RESTful API")
|
|
flag.BoolVar(&geodataMode, "m", false, "set geodata mode")
|
|
flag.BoolVar(&version, "v", false, "show current version of mihomo")
|
|
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
|
|
flag.Parse()
|
|
}
|
|
|
|
func main() {
|
|
_, _ = maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
|
|
|
|
if len(os.Args) > 1 && os.Args[1] == "convert-ruleset" {
|
|
provider.ConvertMain(os.Args[2:])
|
|
return
|
|
}
|
|
|
|
if version {
|
|
fmt.Printf("Mihomo Meta %s %s %s with %s %s\n",
|
|
C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime)
|
|
if tags := features.Tags(); len(tags) != 0 {
|
|
fmt.Printf("Use tags: %s\n", strings.Join(tags, ", "))
|
|
}
|
|
|
|
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)
|
|
}
|
|
} 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 externalUI != "" {
|
|
options = append(options, hub.WithExternalUI(externalUI))
|
|
}
|
|
if externalController != "" {
|
|
options = append(options, hub.WithExternalController(externalController))
|
|
}
|
|
if externalControllerUnix != "" {
|
|
options = append(options, hub.WithExternalControllerUnix(externalControllerUnix))
|
|
}
|
|
if secret != "" {
|
|
options = append(options, hub.WithSecret(secret))
|
|
}
|
|
|
|
if err := hub.Parse(options...); err != nil {
|
|
log.Fatalln("Parse config error: %s", err.Error())
|
|
}
|
|
|
|
if C.GeoAutoUpdate {
|
|
updater.RegisterGeoUpdater()
|
|
}
|
|
|
|
defer executor.Shutdown()
|
|
|
|
termSign := make(chan os.Signal, 1)
|
|
hupSign := make(chan os.Signal, 1)
|
|
signal.Notify(termSign, syscall.SIGINT, syscall.SIGTERM)
|
|
signal.Notify(hupSign, syscall.SIGHUP)
|
|
for {
|
|
select {
|
|
case <-termSign:
|
|
return
|
|
case <-hupSign:
|
|
if cfg, err := executor.ParseWithPath(C.Path.Config()); err == nil {
|
|
hub.ApplyConfig(cfg)
|
|
} else {
|
|
log.Errorln("Parse config error: %s", err.Error())
|
|
}
|
|
}
|
|
}
|
|
}
|