Clash.Meta/hub/executor/executor.go

194 lines
4.5 KiB
Go
Raw Normal View History

2018-11-21 13:47:46 +08:00
package executor
import (
"fmt"
"io/ioutil"
"os"
2020-06-18 18:11:02 +08:00
"sync"
2019-12-08 12:17:24 +08:00
"github.com/Dreamacro/clash/adapters/provider"
"github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/component/trie"
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"
authStore "github.com/Dreamacro/clash/proxy/auth"
"github.com/Dreamacro/clash/tunnel"
2018-11-21 13:47:46 +08:00
)
2020-06-18 18:11:02 +08:00
var (
mux sync.Mutex
)
func readConfig(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if len(data) == 0 {
2020-08-25 22:19:59 +08:00
return nil, fmt.Errorf("configuration file %s is empty", path)
}
return data, err
}
2018-11-21 13:47:46 +08:00
// 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) {
buf, err := readConfig(path)
if err != nil {
return nil, err
}
return ParseWithBytes(buf)
}
// ParseWithBytes config with buffer
func ParseWithBytes(buf []byte) (*config.Config, error) {
return config.Parse(buf)
2018-11-21 13:47:46 +08:00
}
// ApplyConfig dispatch configure to all parts
2018-11-30 17:42:40 +08:00
func ApplyConfig(cfg *config.Config, force bool) {
2020-06-18 18:11:02 +08:00
mux.Lock()
defer mux.Unlock()
updateUsers(cfg.Users)
2020-06-18 18:11:02 +08:00
updateGeneral(cfg.General, force)
2019-12-08 12:17:24 +08:00
updateProxies(cfg.Proxies, cfg.Providers)
2018-11-21 13:47:46 +08:00
updateRules(cfg.Rules)
2018-12-05 21:13:29 +08:00
updateDNS(cfg.DNS)
2019-09-11 17:00:55 +08:00
updateHosts(cfg.Hosts)
updateExperimental(cfg)
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{
2020-06-18 18:11:02 +08:00
Inbound: config.Inbound{
Port: ports.Port,
SocksPort: ports.SocksPort,
RedirPort: ports.RedirPort,
MixedPort: ports.MixedPort,
Authentication: authenticator,
AllowLan: P.AllowLan(),
BindAddress: P.BindAddress(),
},
Mode: tunnel.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
}
func updateExperimental(c *config.Config) {}
2018-12-05 21:13:29 +08:00
func updateDNS(c *config.DNS) {
2020-08-25 22:19:59 +08:00
if !c.Enable {
resolver.DefaultResolver = nil
tunnel.SetResolver(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,
Hosts: c.Hosts,
2019-09-15 13:36:45 +08:00
FallbackFilter: dns.FallbackFilter{
GeoIP: c.FallbackFilter.GeoIP,
IPCIDR: c.FallbackFilter.IPCIDR,
},
Default: c.DefaultNameserver,
2018-12-05 21:13:29 +08:00
})
resolver.DefaultResolver = r
tunnel.SetResolver(r)
if err := dns.ReCreateServer(c.Listen, r); err != nil {
log.Errorln("Start DNS server error: %s", err.Error())
return
}
if c.Listen != "" {
log.Infoln("DNS server listening at: %s", c.Listen)
}
2018-12-05 21:13:29 +08:00
}
func updateHosts(tree *trie.DomainTrie) {
resolver.DefaultHosts = tree
2019-09-11 17:00:55 +08:00
}
2019-12-08 12:17:24 +08:00
func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.ProxyProvider) {
tunnel.UpdateProxies(proxies, providers)
2018-11-21 13:47:46 +08:00
}
func updateRules(rules []C.Rule) {
tunnel.UpdateRules(rules)
2018-11-21 13:47:46 +08:00
}
2020-06-18 18:11:02 +08:00
func updateGeneral(general *config.General, force bool) {
2018-12-03 23:41:40 +08:00
log.SetLevel(general.LogLevel)
tunnel.SetMode(general.Mode)
2020-06-18 18:11:02 +08:00
resolver.DisableIPv6 = !general.IPv6
if general.Interface != "" {
dialer.DialHook = dialer.DialerWithInterface(general.Interface)
dialer.ListenPacketHook = dialer.ListenPacketWithInterface(general.Interface)
} else {
dialer.DialHook = nil
dialer.ListenPacketHook = nil
}
2020-06-18 18:11:02 +08:00
if !force {
return
}
2018-12-03 23:41:40 +08:00
2018-11-21 13:47:46 +08:00
allowLan := general.AllowLan
P.SetAllowLan(allowLan)
bindAddress := general.BindAddress
P.SetBindAddress(bindAddress)
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())
}
if err := P.ReCreateMixed(general.MixedPort); err != nil {
log.Errorln("Start Mixed(http and socks5) server error: %s", err.Error())
}
2018-11-21 13:47:46 +08:00
}
func updateUsers(users []auth.AuthUser) {
authenticator := auth.NewAuthenticator(users)
authStore.SetAuthenticator(authenticator)
if authenticator != nil {
log.Infoln("Authentication of local server updated")
}
}