github.com/kelleygo/clashcore@v1.0.2/hub/hub.go (about)

     1  package hub
     2  
     3  import (
     4  	"github.com/kelleygo/clashcore/config"
     5  	"github.com/kelleygo/clashcore/hub/executor"
     6  	"github.com/kelleygo/clashcore/hub/route"
     7  	"github.com/kelleygo/clashcore/log"
     8  )
     9  
    10  type Option func(*config.Config)
    11  
    12  func WithExternalUI(externalUI string) Option {
    13  	return func(cfg *config.Config) {
    14  		cfg.General.ExternalUI = externalUI
    15  	}
    16  }
    17  
    18  func WithExternalController(externalController string) Option {
    19  	return func(cfg *config.Config) {
    20  		cfg.General.ExternalController = externalController
    21  	}
    22  }
    23  
    24  func WithExternalControllerUnix(externalControllerUnix string) Option {
    25  	return func(cfg *config.Config) {
    26  		cfg.General.ExternalControllerUnix = externalControllerUnix
    27  	}
    28  }
    29  
    30  func WithSecret(secret string) Option {
    31  	return func(cfg *config.Config) {
    32  		cfg.General.Secret = secret
    33  	}
    34  }
    35  
    36  // Parse call at the beginning of yiclashcore
    37  func Parse(options ...Option) error {
    38  	cfg, err := executor.Parse()
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	for _, option := range options {
    44  		option(cfg)
    45  	}
    46  
    47  	if cfg.General.ExternalUI != "" {
    48  		route.SetUIPath(cfg.General.ExternalUI)
    49  	}
    50  
    51  	if cfg.General.ExternalController != "" {
    52  		go route.Start(cfg.General.ExternalController, cfg.General.ExternalControllerTLS,
    53  			cfg.General.Secret, cfg.TLS.Certificate, cfg.TLS.PrivateKey, cfg.General.LogLevel == log.DEBUG)
    54  	}
    55  
    56  	if cfg.General.ExternalControllerUnix != "" {
    57  		go route.StartUnix(cfg.General.ExternalControllerUnix, cfg.General.LogLevel == log.DEBUG)
    58  	}
    59  
    60  	executor.ApplyConfig(cfg, true)
    61  	return nil
    62  }