clash/hub/executor/executor.go

257 lines
5.9 KiB
Go
Raw Normal View History

2018-11-21 05:47:46 +00:00
package executor
import (
"fmt"
"os"
2020-06-18 10:11:02 +00:00
"sync"
2021-06-10 06:05:56 +00:00
"github.com/Dreamacro/clash/adapter"
"github.com/Dreamacro/clash/adapter/outboundgroup"
"github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/component/iface"
"github.com/Dreamacro/clash/component/profile"
"github.com/Dreamacro/clash/component/profile/cachefile"
"github.com/Dreamacro/clash/component/resolver"
"github.com/Dreamacro/clash/component/trie"
2018-11-21 05:47:46 +00:00
"github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/constant/provider"
2018-12-05 13:13:29 +00:00
"github.com/Dreamacro/clash/dns"
2021-06-13 09:23:10 +00:00
P "github.com/Dreamacro/clash/listener"
authStore "github.com/Dreamacro/clash/listener/auth"
2018-11-21 05:47:46 +00:00
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel"
2018-11-21 05:47:46 +00:00
)
2020-06-18 10:11:02 +00:00
var (
mux sync.Mutex
)
func readConfig(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}
2021-10-09 12:35:06 +00:00
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if len(data) == 0 {
2020-08-25 14:19:59 +00:00
return nil, fmt.Errorf("configuration file %s is empty", path)
}
return data, err
}
2018-11-21 05:47:46 +00: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 05:47:46 +00:00
}
// ApplyConfig dispatch configure to all parts
2018-11-30 09:42:40 +00:00
func ApplyConfig(cfg *config.Config, force bool) {
2020-06-18 10:11:02 +00:00
mux.Lock()
defer mux.Unlock()
updateUsers(cfg.Users)
2019-12-08 04:17:24 +00:00
updateProxies(cfg.Proxies, cfg.Providers)
2018-11-21 05:47:46 +00:00
updateRules(cfg.Rules)
2019-09-11 09:00:55 +00:00
updateHosts(cfg.Hosts)
updateProfile(cfg)
updateGeneral(cfg.General, force)
updateDNS(cfg.DNS)
updateExperimental(cfg)
2018-11-21 05:47:46 +00:00
}
func GetGeneral() *config.General {
ports := P.GetPorts()
2019-06-27 12:45:12 +00:00
authenticator := []string{}
if auth := authStore.Authenticator(); auth != nil {
authenticator = auth.Users()
}
general := &config.General{
2020-06-18 10:11:02 +00:00
Inbound: config.Inbound{
Port: ports.Port,
SocksPort: ports.SocksPort,
RedirPort: ports.RedirPort,
TProxyPort: ports.TProxyPort,
2020-06-18 10:11:02 +00:00
MixedPort: ports.MixedPort,
Authentication: authenticator,
AllowLan: P.AllowLan(),
BindAddress: P.BindAddress(),
},
Mode: tunnel.Mode(),
LogLevel: log.Level(),
IPv6: !resolver.DisableIPv6,
2018-11-21 05:47:46 +00:00
}
2019-06-27 12:45:12 +00:00
return general
2018-11-21 05:47:46 +00:00
}
func updateExperimental(c *config.Config) {}
2018-12-05 13:13:29 +00:00
func updateDNS(c *config.DNS) {
2020-08-25 14:19:59 +00:00
if !c.Enable {
resolver.DefaultResolver = nil
resolver.DefaultHostMapper = nil
dns.ReCreateServer("", nil, nil)
2018-12-05 13:13:29 +00:00
return
}
cfg := dns.Config{
2018-12-05 13:13:29 +00:00
Main: c.NameServer,
Fallback: c.Fallback,
IPv6: c.IPv6,
EnhancedMode: c.EnhancedMode,
2019-05-02 16:05:14 +00:00
Pool: c.FakeIPRange,
Hosts: c.Hosts,
2019-09-15 05:36:45 +00:00
FallbackFilter: dns.FallbackFilter{
2021-08-25 07:15:13 +00:00
GeoIP: c.FallbackFilter.GeoIP,
GeoIPCode: c.FallbackFilter.GeoIPCode,
IPCIDR: c.FallbackFilter.IPCIDR,
Domain: c.FallbackFilter.Domain,
2019-09-15 05:36:45 +00:00
},
Default: c.DefaultNameserver,
Policy: c.NameServerPolicy,
}
r := dns.NewResolver(cfg)
m := dns.NewEnhancer(cfg)
// reuse cache of old host mapper
if old := resolver.DefaultHostMapper; old != nil {
m.PatchFrom(old.(*dns.ResolverEnhancer))
}
resolver.DefaultResolver = r
resolver.DefaultHostMapper = m
if err := dns.ReCreateServer(c.Listen, r, m); 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 13:13:29 +00:00
}
func updateHosts(tree *trie.DomainTrie) {
resolver.DefaultHosts = tree
2019-09-11 09:00:55 +00:00
}
2019-12-08 04:17:24 +00:00
func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.ProxyProvider) {
tunnel.UpdateProxies(proxies, providers)
2018-11-21 05:47:46 +00:00
}
func updateRules(rules []C.Rule) {
tunnel.UpdateRules(rules)
2018-11-21 05:47:46 +00:00
}
2020-06-18 10:11:02 +00:00
func updateGeneral(general *config.General, force bool) {
2018-12-03 15:41:40 +00:00
log.SetLevel(general.LogLevel)
tunnel.SetMode(general.Mode)
2020-06-18 10:11:02 +00:00
resolver.DisableIPv6 = !general.IPv6
if general.Interface != "" {
dialer.DefaultOptions = []dialer.Option{dialer.WithInterface(general.Interface)}
} else {
dialer.DefaultOptions = nil
}
iface.FlushCache()
2020-06-18 10:11:02 +00:00
if !force {
return
}
2018-12-03 15:41:40 +00:00
2018-11-21 05:47:46 +00:00
allowLan := general.AllowLan
P.SetAllowLan(allowLan)
bindAddress := general.BindAddress
P.SetBindAddress(bindAddress)
2021-06-13 09:23:10 +00:00
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
if err := P.ReCreateHTTP(general.Port, tcpIn); err != nil {
2018-11-21 05:47:46 +00:00
log.Errorln("Start HTTP server error: %s", err.Error())
}
2021-06-13 09:23:10 +00:00
if err := P.ReCreateSocks(general.SocksPort, tcpIn, udpIn); err != nil {
log.Errorln("Start SOCKS server error: %s", err.Error())
2018-11-21 05:47:46 +00:00
}
2021-06-13 09:23:10 +00:00
if err := P.ReCreateRedir(general.RedirPort, tcpIn, udpIn); err != nil {
2018-11-21 05:47:46 +00:00
log.Errorln("Start Redir server error: %s", err.Error())
}
2021-06-13 09:23:10 +00:00
if err := P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn); err != nil {
log.Errorln("Start TProxy server error: %s", err.Error())
}
2021-06-13 09:23:10 +00:00
if err := P.ReCreateMixed(general.MixedPort, tcpIn, udpIn); err != nil {
log.Errorln("Start Mixed(http and socks) server error: %s", err.Error())
}
2018-11-21 05:47:46 +00:00
}
func updateUsers(users []auth.AuthUser) {
authenticator := auth.NewAuthenticator(users)
authStore.SetAuthenticator(authenticator)
if authenticator != nil {
log.Infoln("Authentication of local server updated")
}
}
func updateProfile(cfg *config.Config) {
profileCfg := cfg.Profile
profile.StoreSelected.Store(profileCfg.StoreSelected)
if profileCfg.StoreSelected {
patchSelectGroup(cfg.Proxies)
}
}
func patchSelectGroup(proxies map[string]C.Proxy) {
mapping := cachefile.Cache().SelectedMap()
if mapping == nil {
return
}
for name, proxy := range proxies {
2021-06-10 06:05:56 +00:00
outbound, ok := proxy.(*adapter.Proxy)
if !ok {
continue
}
selector, ok := outbound.ProxyAdapter.(*outboundgroup.Selector)
if !ok {
continue
}
selected, exist := mapping[name]
if !exist {
continue
}
selector.Set(selected)
}
}