hub.go 1.6 KB

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