2018-11-21 13:47:46 +08:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
2024-08-31 19:23:40 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/config"
|
2024-08-31 19:23:40 +08:00
|
|
|
"github.com/metacubex/mihomo/constant/features"
|
2023-11-03 21:01:45 +08:00
|
|
|
"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) {
|
2024-08-24 20:49:12 +08:00
|
|
|
cfg.Controller.ExternalUI = externalUI
|
2020-04-27 22:23:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithExternalController(externalController string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
2024-08-24 20:49:12 +08:00
|
|
|
cfg.Controller.ExternalController = externalController
|
2020-04-27 22:23:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 10:08:54 +08:00
|
|
|
func WithExternalControllerUnix(externalControllerUnix string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
2024-08-24 20:49:12 +08:00
|
|
|
cfg.Controller.ExternalControllerUnix = externalControllerUnix
|
2024-04-17 10:08:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:23:09 +08:00
|
|
|
func WithSecret(secret string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
2024-08-24 20:49:12 +08:00
|
|
|
cfg.Controller.Secret = secret
|
2020-04-27 22:23:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-31 19:23:40 +08:00
|
|
|
// ApplyConfig dispatch configure to all parts include ExternalController
|
|
|
|
func ApplyConfig(cfg *config.Config) {
|
|
|
|
applyRoute(cfg)
|
|
|
|
executor.ApplyConfig(cfg, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyRoute(cfg *config.Config) {
|
|
|
|
if features.CMFA && strings.HasSuffix(cfg.Controller.ExternalUI, ":0") {
|
|
|
|
// CMFA have set its default override value to end with ":0" for security.
|
|
|
|
// so we direct return at here
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if cfg.Controller.ExternalUI != "" {
|
|
|
|
route.SetUIPath(cfg.Controller.ExternalUI)
|
|
|
|
}
|
|
|
|
route.ReCreateServer(&route.Config{
|
|
|
|
Addr: cfg.Controller.ExternalController,
|
|
|
|
TLSAddr: cfg.Controller.ExternalControllerTLS,
|
|
|
|
UnixAddr: cfg.Controller.ExternalControllerUnix,
|
|
|
|
Secret: cfg.Controller.Secret,
|
|
|
|
Certificate: cfg.TLS.Certificate,
|
|
|
|
PrivateKey: cfg.TLS.PrivateKey,
|
|
|
|
DohServer: cfg.Controller.ExternalDohServer,
|
|
|
|
IsDebug: cfg.General.LogLevel == log.DEBUG,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-31 19:23:40 +08:00
|
|
|
ApplyConfig(cfg)
|
2018-11-21 13:47:46 +08:00
|
|
|
return nil
|
|
|
|
}
|