mihomo/main.go

178 lines
5.2 KiB
Go
Raw Permalink Normal View History

2018-06-10 22:50:03 +08:00
package main
import (
"context"
"encoding/base64"
2018-11-14 20:58:10 +08:00
"flag"
2019-03-23 19:24:26 +08:00
"fmt"
"net"
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"
"strings"
2018-06-10 22:50:03 +08:00
"syscall"
2024-09-09 16:08:48 +08:00
"github.com/metacubex/mihomo/component/geodata"
2024-05-17 11:49:09 +08:00
"github.com/metacubex/mihomo/component/updater"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/config"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/constant/features"
2023-11-03 21:01:45 +08:00
"github.com/metacubex/mihomo/hub"
"github.com/metacubex/mihomo/hub/executor"
"github.com/metacubex/mihomo/log"
2024-07-26 22:30:42 +08:00
"github.com/metacubex/mihomo/rules/provider"
2021-10-27 21:27:19 +08:00
"go.uber.org/automaxprocs/maxprocs"
2018-06-10 22:50:03 +08:00
)
var (
version bool
testConfig bool
geodataMode bool
homeDir string
configFile string
configString string
configBytes []byte
externalUI string
externalController string
externalControllerUnix string
externalControllerPipe 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(&configString, "config", os.Getenv("CLASH_CONFIG_STRING"), "specify base64-encoded configuration string")
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(&externalControllerPipe, "ext-ctl-pipe", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_PIPE"), "override external controller pipe address")
flag.StringVar(&secret, "secret", os.Getenv("CLASH_OVERRIDE_SECRET"), "override secret for RESTful API")
2022-03-15 22:25:33 +08:00
flag.BoolVar(&geodataMode, "m", false, "set geodata mode")
2023-11-03 21:01:45 +08:00
flag.BoolVar(&version, "v", false, "show current version of mihomo")
flag.BoolVar(&testConfig, "t", false, "test configuration and exit")
flag.Parse()
}
2018-06-10 22:50:03 +08:00
func main() {
// Defensive programming: panic when code mistakenly calls net.DefaultResolver
net.DefaultResolver.PreferGo = true
net.DefaultResolver.Dial = func(ctx context.Context, network, address string) (net.Conn, error) {
panic("should never be called")
}
_, _ = maxprocs.Set(maxprocs.Logger(func(string, ...any) {}))
2024-07-26 22:30:42 +08:00
if len(os.Args) > 1 && os.Args[1] == "convert-ruleset" {
provider.ConvertMain(os.Args[2:])
return
}
2019-03-23 19:24:26 +08:00
if version {
2023-11-03 21:01:45 +08:00
fmt.Printf("Mihomo Meta %s %s %s with %s %s\n",
C.Version, runtime.GOOS, runtime.GOARCH, runtime.Version(), C.BuildTime)
2023-11-17 23:12:10 +08:00
if tags := features.Tags(); len(tags) != 0 {
fmt.Printf("Use tags: %s\n", strings.Join(tags, ", "))
}
2019-03-23 19:24:26 +08:00
return
}
if homeDir != "" {
if !filepath.IsAbs(homeDir) {
currentDir, _ := os.Getwd()
homeDir = filepath.Join(currentDir, homeDir)
}
C.SetHomeDir(homeDir)
}
2022-03-16 21:47:00 +08:00
if geodataMode {
2024-09-09 16:08:48 +08:00
geodata.SetGeodataMode(true)
2018-11-21 13:47:46 +08:00
}
if configString != "" {
var err error
configBytes, err = base64.StdEncoding.DecodeString(configString)
if err != nil {
log.Fatalln("Initial configuration error: %s", err.Error())
return
}
} else {
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 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
}
if testConfig {
if len(configBytes) != 0 {
if _, err := executor.ParseWithBytes(configBytes); err != nil {
log.Errorln(err.Error())
fmt.Println("configuration test failed")
os.Exit(1)
}
} else {
if _, err := executor.Parse(); err != nil {
log.Errorln(err.Error())
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
os.Exit(1)
}
}
2020-08-25 22:19:59 +08:00
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 externalControllerPipe != "" {
options = append(options, hub.WithExternalControllerPipe(externalControllerPipe))
}
if secret != "" {
options = append(options, hub.WithSecret(secret))
}
if err := hub.Parse(configBytes, 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
}
2024-09-09 16:08:48 +08:00
if updater.GeoAutoUpdate() {
updater.RegisterGeoUpdater()
2024-05-19 15:46:23 +08:00
}
2024-05-19 17:30:39 +08:00
2022-03-28 03:25:55 +08:00
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 err := hub.Parse(configBytes, options...); err != nil {
log.Errorln("Parse config error: %s", err.Error())
}
}
}
2018-06-10 22:50:03 +08:00
}