2018-11-21 13:47:46 +08:00
|
|
|
package hub
|
|
|
|
|
|
|
|
import (
|
2022-12-03 12:25:10 +08:00
|
|
|
"errors"
|
|
|
|
|
2020-05-28 12:13:05 +08:00
|
|
|
"github.com/Dreamacro/clash/config"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/hub/executor"
|
|
|
|
"github.com/Dreamacro/clash/hub/route"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithSecret(secret string) Option {
|
|
|
|
return func(cfg *config.Config) {
|
|
|
|
cfg.General.Secret = secret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
// Parse call at the beginning of clash
|
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 != "" {
|
2022-12-03 12:25:10 +08:00
|
|
|
if cfg.General.TLSPort != 0 && (len(cfg.General.PrivateKey) == 0 || len(cfg.General.Cert) == 0) {
|
|
|
|
return errors.New("Must be provided certificates and keys, for tls controller")
|
|
|
|
}
|
|
|
|
|
|
|
|
go route.Start(cfg.General.ExternalController, cfg.General.Secret, cfg.General.TLSPort,
|
|
|
|
cfg.General.Cert, cfg.General.PrivateKey)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2018-11-30 17:42:40 +08:00
|
|
|
executor.ApplyConfig(cfg, true)
|
2018-11-21 13:47:46 +08:00
|
|
|
return nil
|
|
|
|
}
|