2018-11-21 13:47:46 +08:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/config"
|
|
|
|
"github.com/metacubex/mihomo/hub/executor"
|
|
|
|
"github.com/metacubex/mihomo/hub/route"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2018-11-21 13:47:46 +08:00
|
|
|
)
|
|
|
|
|
2020-04-27 22:23:09 +08:00
|
|
|
type Option func(*config.Config)
|
|
|
|
|
|
|
|
func WithExternalUI(externalUI string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
|
|
|
cfg.General.ExternalUI = externalUI
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExternalController(externalController string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
|
|
|
cfg.General.ExternalController = externalController
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:08:54 +08:00
|
|
|
func WithExternalControllerUnix(externalControllerUnix string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
|
|
|
cfg.General.ExternalControllerUnix = externalControllerUnix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:23:09 +08:00
|
|
|
func WithSecret(secret string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
|
|
|
cfg.General.Secret = secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
// Parse call at the beginning of mihomo
|
2020-04-27 22:23:09 +08:00
|
|
|
func Parse(options ...Option) error {
|
2018-11-21 13:47:46 +08:00
|
|
|
cfg, err := executor.Parse()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:23:09 +08:00
|
|
|
for _, option := range options {
|
|
|
|
option(cfg)
|
|
|
|
}
|
|
|
|
|
2018-12-20 01:29:13 +08:00
|
|
|
if cfg.General.ExternalUI != "" {
|
|
|
|
route.SetUIPath(cfg.General.ExternalUI)
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
if cfg.General.ExternalController != "" {
|
2023-01-14 21:08:06 +08:00
|
|
|
go route.Start(cfg.General.ExternalController, cfg.General.ExternalControllerTLS,
|
2023-03-12 15:00:59 +08:00
|
|
|
cfg.General.Secret, cfg.TLS.Certificate, cfg.TLS.PrivateKey, cfg.General.LogLevel == log.DEBUG)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-04-17 09:39:00 +08:00
|
|
|
if cfg.General.ExternalControllerUnix != "" {
|
|
|
|
go route.StartUnix(cfg.General.ExternalControllerUnix, cfg.General.LogLevel == log.DEBUG)
|
|
|
|
}
|
|
|
|
|
2018-11-30 17:42:40 +08:00
|
|
|
executor.ApplyConfig(cfg, true)
|
2018-11-21 13:47:46 +08:00
|
|
|
return nil
|
|
|
|
}
|