Clash.Meta/hub/executor/executor.go

500 lines
12 KiB
Go
Raw Normal View History

2018-11-21 13:47:46 +08:00
package executor
import (
"fmt"
2023-09-01 03:11:35 +08:00
"github.com/Dreamacro/clash/ntp"
"net"
2022-04-12 20:20:04 +08:00
"net/netip"
"os"
2021-12-04 17:41:13 +08:00
"runtime"
2023-09-01 03:11:35 +08:00
"strconv"
"strings"
2020-06-18 18:11:02 +08:00
"sync"
2023-09-01 03:11:35 +08:00
"time"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter"
"github.com/Dreamacro/clash/adapter/inbound"
2021-06-10 14:05:56 +08:00
"github.com/Dreamacro/clash/adapter/outboundgroup"
"github.com/Dreamacro/clash/component/auth"
"github.com/Dreamacro/clash/component/dialer"
2022-02-05 00:51:06 +08:00
G "github.com/Dreamacro/clash/component/geodata"
"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"
SNI "github.com/Dreamacro/clash/component/sniffer"
CTLS "github.com/Dreamacro/clash/component/tls"
"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"
"github.com/Dreamacro/clash/constant/provider"
2018-12-05 21:13:29 +08:00
"github.com/Dreamacro/clash/dns"
2022-11-18 22:57:33 +08:00
"github.com/Dreamacro/clash/listener"
2021-06-13 17:23:10 +08:00
authStore "github.com/Dreamacro/clash/listener/auth"
LC "github.com/Dreamacro/clash/listener/config"
2022-11-16 10:43:16 +08:00
"github.com/Dreamacro/clash/listener/inner"
2021-07-01 22:49:29 +08:00
"github.com/Dreamacro/clash/listener/tproxy"
2018-11-21 13:47:46 +08:00
"github.com/Dreamacro/clash/log"
"github.com/Dreamacro/clash/tunnel"
2018-11-21 13:47:46 +08:00
)
2021-10-10 23:44:09 +08:00
var mux sync.Mutex
2020-06-18 18:11:02 +08:00
func readConfig(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}
2021-10-09 20:35:06 +08:00
data, err := os.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()
tunnel.OnSuspend()
CTLS.ResetCertificate()
for _, c := range cfg.TLS.CustomTrustCert {
if err := CTLS.AddCertificate(c); err != nil {
log.Warnln("%s\nadd error: %s", c, err.Error())
}
}
updateUsers(cfg.Users)
updateProxies(cfg.Proxies, cfg.Providers)
2022-12-04 13:37:14 +08:00
updateRules(cfg.Rules, cfg.SubRules, cfg.RuleProviders)
2022-04-16 08:29:38 +08:00
updateSniffer(cfg.Sniffer)
updateHosts(cfg.Hosts)
updateGeneral(cfg.General)
2023-09-01 03:11:35 +08:00
updateNTP(cfg.NTP)
updateDNS(cfg.DNS, cfg.RuleProviders, cfg.General.IPv6)
updateListeners(cfg.General, cfg.Listeners, force)
updateIPTables(cfg)
updateTun(cfg.General)
updateExperimental(cfg)
2022-11-18 22:57:33 +08:00
updateTunnels(cfg.Tunnels)
2022-01-21 22:38:02 +08:00
tunnel.OnInnerLoading()
initInnerTcp()
loadProxyProvider(cfg.Providers)
updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
runtime.GC()
tunnel.OnRunning()
2022-03-05 18:04:04 +08:00
log.SetLevel(cfg.General.LogLevel)
2018-11-21 13:47:46 +08:00
}
func initInnerTcp() {
inner.New(tunnel.TCPIn())
2022-05-08 00:04:16 +08:00
}
2018-11-21 13:47:46 +08:00
func GetGeneral() *config.General {
2022-11-18 22:57:33 +08:00
ports := listener.GetPorts()
2022-03-20 02:39:48 +08:00
var authenticator []string
2019-06-27 20:45:12 +08:00
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,
TProxyPort: ports.TProxyPort,
MixedPort: ports.MixedPort,
Tun: listener.GetTunConf(),
2022-12-06 10:13:05 +08:00
TuicServer: listener.GetTuicConf(),
ShadowSocksConfig: ports.ShadowSocksConfig,
VmessConfig: ports.VmessConfig,
Authentication: authenticator,
AllowLan: listener.AllowLan(),
BindAddress: listener.BindAddress(),
2020-06-18 18:11:02 +08:00
},
2022-02-05 00:51:06 +08:00
Mode: tunnel.Mode(),
LogLevel: log.Level(),
IPv6: !resolver.DisableIPv6,
GeodataLoader: G.LoaderName(),
Interface: dialer.DefaultInterface.Load(),
Sniffing: tunnel.IsSniffing(),
2023-03-06 23:23:05 +08:00
TCPConcurrent: dialer.GetTcpConcurrent(),
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 updateListeners(general *config.General, listeners map[string]C.InboundListener, force bool) {
2022-12-04 13:37:14 +08:00
tcpIn := tunnel.TCPIn()
udpIn := tunnel.UDPIn()
2023-02-18 13:16:07 +08:00
natTable := tunnel.NatTable()
2023-02-18 13:16:07 +08:00
listener.PatchInboundListeners(listeners, tcpIn, udpIn, natTable, true)
if !force {
return
}
allowLan := general.AllowLan
listener.SetAllowLan(allowLan)
bindAddress := general.BindAddress
listener.SetBindAddress(bindAddress)
listener.ReCreateHTTP(general.Port, tcpIn)
listener.ReCreateSocks(general.SocksPort, tcpIn, udpIn)
listener.ReCreateRedir(general.RedirPort, tcpIn, udpIn, natTable)
listener.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn)
listener.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn, natTable)
listener.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
listener.ReCreateShadowSocks(general.ShadowSocksConfig, tcpIn, udpIn)
listener.ReCreateVmess(general.VmessConfig, tcpIn, udpIn)
listener.ReCreateTuic(LC.TuicServer(general.TuicServer), tcpIn, udpIn)
2022-12-04 13:37:14 +08:00
}
func updateExperimental(c *config.Config) {
}
2023-09-01 03:11:35 +08:00
func updateNTP(c *config.NTP) {
if c.Enable {
ntp.ReCreateNTPService(net.JoinHostPort(c.Server, strconv.Itoa(c.Port)),
time.Duration(c.Interval))
}
}
func updateDNS(c *config.DNS, ruleProvider map[string]provider.RuleProvider, generalIPv6 bool) {
if !c.Enable {
resolver.DefaultResolver = nil
resolver.DefaultHostMapper = nil
2022-05-02 14:21:37 +08:00
resolver.DefaultLocalServer = nil
dns.ReCreateServer("", nil, nil)
return
}
policy := make(map[string][]dns.NameServer)
domainSetPolicies := make(map[provider.RuleProvider][]dns.NameServer)
for key, nameservers := range c.NameServerPolicy {
temp := strings.Split(key, ":")
if len(temp) == 2 {
prefix := temp[0]
key := temp[1]
switch strings.ToLower(prefix) {
case "rule-set":
if p, ok := ruleProvider[key]; ok {
domainSetPolicies[p] = nameservers
}
case "geosite":
// TODO:
}
} else {
policy[key] = nameservers
}
}
cfg := dns.Config{
2018-12-05 21:13:29 +08:00
Main: c.NameServer,
Fallback: c.Fallback,
2023-01-21 22:31:07 +08:00
IPv6: c.IPv6 && generalIPv6,
2023-03-10 23:38:16 +08:00
IPv6Timeout: c.IPv6Timeout,
2018-12-05 21:13:29 +08:00
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{
2021-08-25 15:15:13 +08:00
GeoIP: c.FallbackFilter.GeoIP,
GeoIPCode: c.FallbackFilter.GeoIPCode,
IPCIDR: c.FallbackFilter.IPCIDR,
Domain: c.FallbackFilter.Domain,
2021-11-17 16:03:47 +08:00
GeoSite: c.FallbackFilter.GeoSite,
2019-09-15 13:36:45 +08:00
},
Default: c.DefaultNameserver,
Policy: c.NameServerPolicy,
ProxyServer: c.ProxyServerNameserver,
DomainSetPolicy: domainSetPolicies,
}
r := dns.NewResolver(cfg)
2022-03-28 00:44:13 +08:00
pr := dns.NewProxyServerHostResolver(r)
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
2022-05-02 14:21:37 +08:00
resolver.DefaultLocalServer = dns.NewLocalServer(r, m)
if pr.Invalid() {
2022-03-28 00:44:13 +08:00
resolver.ProxyServerHostResolver = pr
}
dns.ReCreateServer(c.Listen, r, m)
2018-12-05 21:13:29 +08:00
}
func updateHosts(tree *trie.DomainTrie[resolver.HostValue]) {
resolver.DefaultHosts = resolver.NewHosts(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
}
2022-12-04 13:37:14 +08:00
func updateRules(rules []C.Rule, subRules map[string][]C.Rule, ruleProviders map[string]provider.RuleProvider) {
tunnel.UpdateRules(rules, subRules, ruleProviders)
2018-11-21 13:47:46 +08:00
}
func loadProvider(pv provider.Provider) {
if pv.VehicleType() == provider.Compatible {
log.Infoln("Start initial compatible provider %s", pv.Name())
} else {
log.Infoln("Start initial provider %s", (pv).Name())
}
2022-05-08 00:04:16 +08:00
if err := pv.Initial(); err != nil {
switch pv.Type() {
case provider.Proxy:
{
2023-01-23 11:14:45 +08:00
log.Errorln("initial proxy provider %s error: %v", (pv).Name(), err)
}
case provider.Rule:
{
2023-01-23 11:14:45 +08:00
log.Errorln("initial rule provider %s error: %v", (pv).Name(), err)
}
}
}
}
func loadRuleProvider(ruleProviders map[string]provider.RuleProvider) {
2022-05-02 16:47:48 +08:00
wg := sync.WaitGroup{}
ch := make(chan struct{}, concurrentCount)
for _, ruleProvider := range ruleProviders {
2022-05-02 16:47:48 +08:00
ruleProvider := ruleProvider
wg.Add(1)
ch <- struct{}{}
2022-05-02 16:47:48 +08:00
go func() {
defer func() { <-ch; wg.Done() }()
2022-05-02 16:47:48 +08:00
loadProvider(ruleProvider)
2022-05-02 16:47:48 +08:00
}()
}
2022-05-02 16:47:48 +08:00
wg.Wait()
}
2022-05-02 16:47:48 +08:00
func loadProxyProvider(proxyProviders map[string]provider.ProxyProvider) {
// limit concurrent size
2022-05-02 16:47:48 +08:00
wg := sync.WaitGroup{}
ch := make(chan struct{}, concurrentCount)
2022-05-02 16:47:48 +08:00
for _, proxyProvider := range proxyProviders {
proxyProvider := proxyProvider
wg.Add(1)
ch <- struct{}{}
2022-05-02 16:47:48 +08:00
go func() {
defer func() { <-ch; wg.Done() }()
2022-05-02 16:47:48 +08:00
loadProvider(proxyProvider)
}()
}
2022-05-02 16:47:48 +08:00
wg.Wait()
}
func updateTun(general *config.General) {
if general == nil {
2022-11-03 18:56:03 +08:00
return
}
listener.ReCreateTun(LC.Tun(general.Tun), tunnel.TCPIn(), tunnel.UDPIn())
listener.ReCreateRedirToTun(general.Tun.RedirectToTun)
2022-03-09 05:08:35 +08:00
}
2022-04-12 20:20:04 +08:00
func updateSniffer(sniffer *config.Sniffer) {
if sniffer.Enable {
2022-10-14 08:42:28 +08:00
dispatcher, err := SNI.NewSnifferDispatcher(
2023-01-23 13:16:25 +08:00
sniffer.Sniffers, sniffer.ForceDomain, sniffer.SkipDomain,
2022-10-14 08:42:28 +08:00
sniffer.ForceDnsMapping, sniffer.ParsePureIp,
)
2022-04-12 20:20:04 +08:00
if err != nil {
log.Warnln("initial sniffer failed, err:%v", err)
}
tunnel.UpdateSniffer(dispatcher)
2022-04-12 20:20:04 +08:00
log.Infoln("Sniffer is loaded and working")
} else {
dispatcher, err := SNI.NewCloseSnifferDispatcher()
if err != nil {
log.Warnln("initial sniffer failed, err:%v", err)
}
tunnel.UpdateSniffer(dispatcher)
log.Infoln("Sniffer is closed")
2022-04-12 20:20:04 +08:00
}
}
2022-12-06 10:13:05 +08:00
func updateTunnels(tunnels []LC.Tunnel) {
2022-11-18 22:57:33 +08:00
listener.PatchTunnel(tunnels, tunnel.TCPIn(), tunnel.UDPIn())
}
func updateGeneral(general *config.General) {
tunnel.SetMode(general.Mode)
tunnel.SetFindProcessMode(general.FindProcessMode)
2023-03-06 23:23:05 +08:00
resolver.DisableIPv6 = !general.IPv6
2022-03-20 02:39:48 +08:00
2022-04-27 21:37:20 +08:00
if general.TCPConcurrent {
2023-03-06 23:23:05 +08:00
dialer.SetTcpConcurrent(general.TCPConcurrent)
log.Infoln("Use tcp concurrent")
}
2023-02-24 14:19:50 +08:00
inbound.SetTfo(general.InboundTfo)
inbound.SetMPTCP(general.InboundMPTCP)
2023-02-24 14:19:50 +08:00
adapter.UnifiedDelay.Store(general.UnifiedDelay)
dialer.DefaultInterface.Store(general.Interface)
2022-03-20 02:39:48 +08:00
dialer.DefaultRoutingMark.Store(int32(general.RoutingMark))
if general.RoutingMark > 0 {
log.Infoln("Use routing mark: %#x", general.RoutingMark)
2022-03-03 05:02:17 +08:00
}
2021-11-17 16:03:47 +08:00
iface.FlushCache()
2022-03-15 02:20:19 +08:00
geodataLoader := general.GeodataLoader
G.SetLoader(geodataLoader)
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")
}
}
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 14:05:56 +08:00
outbound, ok := proxy.(*adapter.Proxy)
if !ok {
continue
}
2022-05-23 00:40:27 +08:00
selector, ok := outbound.ProxyAdapter.(outboundgroup.SelectAble)
if !ok {
continue
}
selected, exist := mapping[name]
if !exist {
continue
}
selector.ForceSet(selected)
}
}
2021-11-17 16:03:47 +08:00
func updateIPTables(cfg *config.Config) {
tproxy.CleanupTProxyIPTables()
2022-03-09 05:08:35 +08:00
iptables := cfg.IPTables
if runtime.GOOS != "linux" || !iptables.Enable {
2022-03-09 05:08:35 +08:00
return
}
var err error
defer func() {
if err != nil {
log.Errorln("[IPTABLES] setting iptables failed: %s", err.Error())
2022-03-09 05:08:35 +08:00
os.Exit(2)
}
}()
if cfg.General.Tun.Enable {
err = fmt.Errorf("when tun is enabled, iptables cannot be set automatically")
return
}
var (
inboundInterface = "lo"
2022-03-23 11:04:43 +08:00
bypass = iptables.Bypass
tProxyPort = cfg.General.TProxyPort
dnsCfg = cfg.DNS
)
if tProxyPort == 0 {
err = fmt.Errorf("tproxy-port must be greater than zero")
2022-03-09 05:08:35 +08:00
return
}
if !dnsCfg.Enable {
err = fmt.Errorf("DNS server must be enable")
2021-12-04 17:41:13 +08:00
return
}
2022-04-24 02:07:57 +08:00
dnsPort, err := netip.ParseAddrPort(dnsCfg.Listen)
2021-12-04 17:41:13 +08:00
if err != nil {
err = fmt.Errorf("DNS server must be correct")
2021-07-01 22:49:29 +08:00
return
}
if iptables.InboundInterface != "" {
inboundInterface = iptables.InboundInterface
2021-12-04 17:41:13 +08:00
}
2022-03-05 18:04:04 +08:00
if dialer.DefaultRoutingMark.Load() == 0 {
dialer.DefaultRoutingMark.Store(2158)
2021-12-04 17:41:13 +08:00
}
2022-03-05 18:04:04 +08:00
2022-04-24 02:07:57 +08:00
err = tproxy.SetTProxyIPTables(inboundInterface, bypass, uint16(tProxyPort), dnsPort.Port())
if err != nil {
return
}
log.Infoln("[IPTABLES] Setting iptables completed")
2021-12-04 17:41:13 +08:00
}
2021-11-17 16:03:47 +08:00
2022-03-28 03:25:55 +08:00
func Shutdown() {
2023-09-02 14:10:26 +08:00
listener.Cleanup()
tproxy.CleanupTProxyIPTables()
2022-04-13 16:47:47 +08:00
resolver.StoreFakePoolState()
2022-03-28 03:25:55 +08:00
log.Warnln("Clash shutting down")
2021-11-17 16:03:47 +08:00
}