2018-11-21 13:47:46 +08:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2019-06-27 17:04:25 +08:00
|
|
|
"github.com/Dreamacro/clash/component/auth"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/config"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2018-12-05 21:13:29 +08:00
|
|
|
"github.com/Dreamacro/clash/dns"
|
2018-11-21 13:47:46 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
P "github.com/Dreamacro/clash/proxy"
|
2019-06-27 17:04:25 +08:00
|
|
|
authStore "github.com/Dreamacro/clash/proxy/auth"
|
2018-11-21 13:47:46 +08:00
|
|
|
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 17:42:40 +08:00
|
|
|
func ApplyConfig(cfg *config.Config, force bool) {
|
2019-06-27 17:04:25 +08:00
|
|
|
updateUsers(cfg.Users)
|
2018-11-30 17:42:40 +08:00
|
|
|
if force {
|
|
|
|
updateGeneral(cfg.General)
|
|
|
|
}
|
2018-11-21 13:47:46 +08:00
|
|
|
updateProxies(cfg.Proxies)
|
|
|
|
updateRules(cfg.Rules)
|
2018-12-05 21:13:29 +08:00
|
|
|
updateDNS(cfg.DNS)
|
2019-04-24 12:02:52 +08:00
|
|
|
updateExperimental(cfg.Experimental)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetGeneral() *config.General {
|
|
|
|
ports := P.GetPorts()
|
2019-06-27 20:45:12 +08:00
|
|
|
authenticator := []string{}
|
|
|
|
if auth := authStore.Authenticator(); auth != nil {
|
|
|
|
authenticator = auth.Users()
|
|
|
|
}
|
|
|
|
|
|
|
|
general := &config.General{
|
2019-06-27 17:04:25 +08:00
|
|
|
Port: ports.Port,
|
|
|
|
SocksPort: ports.SocksPort,
|
|
|
|
RedirPort: ports.RedirPort,
|
2019-06-27 20:45:12 +08:00
|
|
|
Authentication: authenticator,
|
2019-06-27 17:04:25 +08:00
|
|
|
AllowLan: P.AllowLan(),
|
|
|
|
Mode: T.Instance().Mode(),
|
|
|
|
LogLevel: log.Level(),
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
2019-06-27 20:45:12 +08:00
|
|
|
|
|
|
|
return general
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2019-04-24 12:02:52 +08:00
|
|
|
func updateExperimental(c *config.Experimental) {
|
|
|
|
T.Instance().UpdateExperimental(c.IgnoreResolveFail)
|
|
|
|
}
|
|
|
|
|
2018-12-05 21:13:29 +08:00
|
|
|
func updateDNS(c *config.DNS) {
|
|
|
|
if c.Enable == false {
|
2019-06-29 00:58:59 +08:00
|
|
|
dns.DefaultResolver = nil
|
2018-12-05 21:13:29 +08:00
|
|
|
dns.ReCreateServer("", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r := dns.New(dns.Config{
|
|
|
|
Main: c.NameServer,
|
|
|
|
Fallback: c.Fallback,
|
|
|
|
IPv6: c.IPv6,
|
|
|
|
EnhancedMode: c.EnhancedMode,
|
2019-05-03 00:05:14 +08:00
|
|
|
Pool: c.FakeIPRange,
|
2018-12-05 21:13:29 +08:00
|
|
|
})
|
2019-06-29 00:58:59 +08:00
|
|
|
dns.DefaultResolver = r
|
2019-01-06 14:31:42 +08:00
|
|
|
if err := dns.ReCreateServer(c.Listen, r); err != nil {
|
|
|
|
log.Errorln("Start DNS server error: %s", err.Error())
|
2019-02-02 21:37:36 +08:00
|
|
|
return
|
2019-01-06 14:31:42 +08:00
|
|
|
}
|
2019-06-29 00:58:59 +08:00
|
|
|
|
|
|
|
if c.Listen != "" {
|
|
|
|
log.Infoln("DNS server listening at: %s", c.Listen)
|
|
|
|
}
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func updateProxies(proxies map[string]C.Proxy) {
|
2018-12-05 18:19:30 +08:00
|
|
|
tunnel := T.Instance()
|
|
|
|
oldProxies := tunnel.Proxies()
|
|
|
|
|
2019-03-16 00:43:16 +08:00
|
|
|
// close proxy group goroutine
|
2018-12-05 18:19:30 +08:00
|
|
|
for _, proxy := range oldProxies {
|
2019-03-16 00:43:16 +08:00
|
|
|
proxy.Destroy()
|
2018-12-05 18:19:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tunnel.UpdateProxies(proxies)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateRules(rules []C.Rule) {
|
|
|
|
T.Instance().UpdateRules(rules)
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateGeneral(general *config.General) {
|
2018-12-03 23:41:40 +08:00
|
|
|
log.SetLevel(general.LogLevel)
|
|
|
|
T.Instance().SetMode(general.Mode)
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
allowLan := general.AllowLan
|
|
|
|
|
|
|
|
P.SetAllowLan(allowLan)
|
2019-06-27 17:04:25 +08:00
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 17:04:25 +08:00
|
|
|
|
|
|
|
func updateUsers(users []auth.AuthUser) {
|
|
|
|
authenticator := auth.NewAuthenticator(users)
|
|
|
|
authStore.SetAuthenticator(authenticator)
|
|
|
|
if authenticator != nil {
|
|
|
|
log.Infoln("Authentication of local server updated")
|
|
|
|
}
|
|
|
|
}
|