github.com/igoogolx/clash@v1.19.8/hub/hub.go (about)

     1  package hub
     2  
     3  import (
     4  	"github.com/igoogolx/clash/config"
     5  	"github.com/igoogolx/clash/hub/executor"
     6  	"github.com/igoogolx/clash/hub/route"
     7  )
     8  
     9  type Option func(*config.Config)
    10  
    11  func WithExternalUI(externalUI string) Option {
    12  	return func(cfg *config.Config) {
    13  		cfg.General.ExternalUI = externalUI
    14  	}
    15  }
    16  
    17  func WithExternalController(externalController string) Option {
    18  	return func(cfg *config.Config) {
    19  		cfg.General.ExternalController = externalController
    20  	}
    21  }
    22  
    23  func WithSecret(secret string) Option {
    24  	return func(cfg *config.Config) {
    25  		cfg.General.Secret = secret
    26  	}
    27  }
    28  
    29  // Parse call at the beginning of clash
    30  func Parse(options ...Option) error {
    31  	cfg, err := executor.Parse()
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	for _, option := range options {
    37  		option(cfg)
    38  	}
    39  
    40  	if cfg.General.ExternalUI != "" {
    41  		route.SetUIPath(cfg.General.ExternalUI)
    42  	}
    43  
    44  	if cfg.General.ExternalController != "" {
    45  		go route.Start(cfg.General.ExternalController, cfg.General.Secret)
    46  	}
    47  
    48  	executor.ApplyConfig(cfg, true)
    49  	return nil
    50  }