clash/hub/executor/executor.go

83 lines
1.9 KiB
Go
Raw Normal View History

2018-11-21 05:47:46 +00:00
package executor
import (
2018-12-05 10:19:30 +00:00
adapters "github.com/Dreamacro/clash/adapters/outbound"
2018-11-21 05:47:46 +00:00
"github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
P "github.com/Dreamacro/clash/proxy"
T "github.com/Dreamacro/clash/tunnel"
)
// Parse config with default config path
func Parse() (*config.Config, error) {
return ParseWithPath(C.Path.Config())
}
// ParseWithPath parse config with custom config path
func ParseWithPath(path string) (*config.Config, error) {
return config.Parse(path)
}
// ApplyConfig dispatch configure to all parts
2018-11-30 09:42:40 +00:00
func ApplyConfig(cfg *config.Config, force bool) {
if force {
updateGeneral(cfg.General)
}
2018-11-21 05:47:46 +00:00
updateProxies(cfg.Proxies)
updateRules(cfg.Rules)
}
func GetGeneral() *config.General {
ports := P.GetPorts()
return &config.General{
Port: ports.Port,
SocksPort: ports.SocksPort,
RedirPort: ports.RedirPort,
AllowLan: P.AllowLan(),
Mode: T.Instance().Mode(),
LogLevel: log.Level(),
}
}
func updateProxies(proxies map[string]C.Proxy) {
2018-12-05 10:19:30 +00:00
tunnel := T.Instance()
oldProxies := tunnel.Proxies()
// close old goroutine
for _, proxy := range oldProxies {
switch raw := proxy.(type) {
case *adapters.URLTest:
raw.Close()
case *adapters.Fallback:
raw.Close()
}
}
tunnel.UpdateProxies(proxies)
2018-11-21 05:47:46 +00:00
}
func updateRules(rules []C.Rule) {
T.Instance().UpdateRules(rules)
}
func updateGeneral(general *config.General) {
2018-12-03 15:41:40 +00:00
log.SetLevel(general.LogLevel)
T.Instance().SetMode(general.Mode)
2018-11-21 05:47:46 +00:00
allowLan := general.AllowLan
P.SetAllowLan(allowLan)
if err := P.ReCreateHTTP(general.Port); err != nil {
log.Errorln("Start HTTP server error: %s", err.Error())
}
if err := P.ReCreateSocks(general.SocksPort); err != nil {
log.Errorln("Start SOCKS5 server error: %s", err.Error())
}
if err := P.ReCreateRedir(general.RedirPort); err != nil {
log.Errorln("Start Redir server error: %s", err.Error())
}
}