clash/hub/hub.go

53 lines
1.1 KiB
Go
Raw Normal View History

2018-11-21 05:47:46 +00:00
package hub
import (
"github.com/Dreamacro/clash/config"
2018-11-21 05:47:46 +00:00
"github.com/Dreamacro/clash/hub/executor"
"github.com/Dreamacro/clash/hub/route"
"github.com/Dreamacro/clash/log"
2018-11-21 05:47:46 +00: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
}
}
func WithSecret(secret string) Option {
return func(cfg *config.Config) {
cfg.General.Secret = secret
}
}
2018-11-21 05:47:46 +00:00
// Parse call at the beginning of clash
func Parse(options ...Option) error {
2018-11-21 05:47:46 +00:00
cfg, err := executor.Parse()
if err != nil {
return err
}
for _, option := range options {
option(cfg)
}
2018-12-19 17:29:13 +00:00
if cfg.General.ExternalUI != "" {
route.SetUIPath(cfg.General.ExternalUI)
}
2018-11-21 05:47:46 +00:00
if cfg.General.ExternalController != "" {
go route.Start(cfg.General.ExternalController, cfg.General.ExternalControllerTLS,
cfg.General.Secret, cfg.TLS.Certificate, cfg.TLS.PrivateKey, cfg.General.LogLevel == log.DEBUG)
2018-11-21 05:47:46 +00:00
}
2018-11-30 09:42:40 +00:00
executor.ApplyConfig(cfg, true)
2018-11-21 05:47:46 +00:00
return nil
}