2018-11-21 05:47:46 +00:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2019-12-01 05:22:47 +00:00
|
|
|
"fmt"
|
2022-05-19 12:43:41 +00:00
|
|
|
"github.com/Dreamacro/clash/component/process"
|
2022-05-07 23:58:26 +00:00
|
|
|
"github.com/Dreamacro/clash/listener/inner"
|
2022-04-12 12:20:04 +00:00
|
|
|
"net/netip"
|
2019-12-01 05:22:47 +00:00
|
|
|
"os"
|
2021-12-04 09:41:13 +00:00
|
|
|
"runtime"
|
2020-06-18 10:11:02 +00:00
|
|
|
"sync"
|
2019-12-01 05:22:47 +00:00
|
|
|
|
2021-06-10 06:05:56 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter"
|
|
|
|
"github.com/Dreamacro/clash/adapter/outboundgroup"
|
2019-06-27 09:04:25 +00:00
|
|
|
"github.com/Dreamacro/clash/component/auth"
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/component/dialer"
|
2022-02-04 16:51:06 +00:00
|
|
|
G "github.com/Dreamacro/clash/component/geodata"
|
2021-09-06 15:07:34 +00:00
|
|
|
"github.com/Dreamacro/clash/component/iface"
|
2021-02-18 15:41:50 +00:00
|
|
|
"github.com/Dreamacro/clash/component/profile"
|
|
|
|
"github.com/Dreamacro/clash/component/profile/cachefile"
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2022-04-09 14:30:36 +00:00
|
|
|
SNI "github.com/Dreamacro/clash/component/sniffer"
|
2020-05-28 04:13:05 +00:00
|
|
|
"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"
|
2021-07-04 12:32:59 +00:00
|
|
|
"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"
|
2021-07-01 14:49:29 +00:00
|
|
|
"github.com/Dreamacro/clash/listener/tproxy"
|
2018-11-21 05:47:46 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2020-02-15 13:42:46 +00:00
|
|
|
"github.com/Dreamacro/clash/tunnel"
|
2018-11-21 05:47:46 +00:00
|
|
|
)
|
|
|
|
|
2021-10-10 15:44:09 +00:00
|
|
|
var mux sync.Mutex
|
2020-06-18 10:11:02 +00:00
|
|
|
|
2019-12-01 05:22:47 +00:00
|
|
|
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)
|
2019-12-01 05:22:47 +00:00
|
|
|
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)
|
2019-12-01 05:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-12-01 05:22:47 +00:00
|
|
|
buf, err := readConfig(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-25 07:12:11 +00:00
|
|
|
return ParseWithBytes(buf)
|
2019-12-01 05:22:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-25 07:12:11 +00:00
|
|
|
// ParseWithBytes config with buffer
|
2019-12-01 05:22:47 +00:00
|
|
|
func ParseWithBytes(buf []byte) (*config.Config, error) {
|
2019-12-25 07:12:11 +00:00
|
|
|
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()
|
|
|
|
|
2019-06-27 09:04:25 +00:00
|
|
|
updateUsers(cfg.Users)
|
2022-01-18 13:09:36 +00:00
|
|
|
updateProxies(cfg.Proxies, cfg.Providers)
|
|
|
|
updateRules(cfg.Rules, cfg.RuleProviders)
|
2022-04-16 00:29:38 +00:00
|
|
|
updateSniffer(cfg.Sniffer)
|
2022-05-05 13:14:46 +00:00
|
|
|
updateHosts(cfg.Hosts)
|
2022-05-07 23:58:26 +00:00
|
|
|
initInnerTcp()
|
2022-05-16 15:34:27 +00:00
|
|
|
updateDNS(cfg.DNS, cfg.General.IPv6)
|
2022-05-07 23:58:26 +00:00
|
|
|
loadProxyProvider(cfg.Providers)
|
|
|
|
updateProfile(cfg)
|
|
|
|
loadRuleProvider(cfg.RuleProviders)
|
2022-03-21 11:47:21 +00:00
|
|
|
updateGeneral(cfg.General, force)
|
2022-03-21 21:38:42 +00:00
|
|
|
updateIPTables(cfg)
|
2022-05-19 11:19:19 +00:00
|
|
|
updateTun(cfg.Tun)
|
2021-07-10 09:01:40 +00:00
|
|
|
updateExperimental(cfg)
|
2022-01-21 14:38:02 +00:00
|
|
|
|
2022-03-05 10:04:04 +00:00
|
|
|
log.SetLevel(cfg.General.LogLevel)
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-07 23:58:26 +00:00
|
|
|
func initInnerTcp() {
|
|
|
|
inner.New(tunnel.TCPIn())
|
2022-05-07 16:04:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
func GetGeneral() *config.General {
|
|
|
|
ports := P.GetPorts()
|
2022-03-19 18:39:48 +00:00
|
|
|
var authenticator []string
|
2019-06-27 12:45:12 +00:00
|
|
|
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,
|
2020-11-09 02:46:10 +00:00
|
|
|
TProxyPort: ports.TProxyPort,
|
2020-06-18 10:11:02 +00:00
|
|
|
MixedPort: ports.MixedPort,
|
|
|
|
Authentication: authenticator,
|
|
|
|
AllowLan: P.AllowLan(),
|
|
|
|
BindAddress: P.BindAddress(),
|
|
|
|
},
|
2022-02-04 16:51:06 +00:00
|
|
|
Mode: tunnel.Mode(),
|
|
|
|
LogLevel: log.Level(),
|
|
|
|
IPv6: !resolver.DisableIPv6,
|
|
|
|
GeodataLoader: G.LoaderName(),
|
2022-05-03 11:13:37 +00:00
|
|
|
Tun: P.GetTunConf(),
|
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
|
|
|
}
|
|
|
|
|
2020-06-27 06:19:31 +00:00
|
|
|
func updateExperimental(c *config.Config) {}
|
2019-04-24 04:02:52 +00:00
|
|
|
|
2022-05-16 15:34:27 +00:00
|
|
|
func updateDNS(c *config.DNS, generalIPv6 bool) {
|
2022-05-01 01:38:43 +00:00
|
|
|
if !c.Enable {
|
2022-05-16 15:34:27 +00:00
|
|
|
resolver.DisableIPv6 = !generalIPv6
|
2022-05-01 01:38:43 +00:00
|
|
|
resolver.DefaultResolver = nil
|
|
|
|
resolver.DefaultHostMapper = nil
|
2022-05-02 06:21:37 +00:00
|
|
|
resolver.DefaultLocalServer = nil
|
2022-05-01 01:38:43 +00:00
|
|
|
dns.ReCreateServer("", nil, nil)
|
|
|
|
return
|
2022-05-16 15:34:27 +00:00
|
|
|
} else {
|
|
|
|
resolver.DisableIPv6 = !c.IPv6
|
2022-05-01 01:38:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 02:48:42 +00:00
|
|
|
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,
|
2020-08-11 02:28:17 +00:00
|
|
|
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,
|
2021-11-17 08:03:47 +00:00
|
|
|
GeoSite: c.FallbackFilter.GeoSite,
|
2019-09-15 05:36:45 +00:00
|
|
|
},
|
2022-03-27 16:44:13 +00:00
|
|
|
Default: c.DefaultNameserver,
|
|
|
|
Policy: c.NameServerPolicy,
|
|
|
|
ProxyServer: c.ProxyServerNameserver,
|
2020-09-17 02:48:42 +00:00
|
|
|
}
|
2020-08-30 16:32:18 +00:00
|
|
|
|
2020-09-17 02:48:42 +00:00
|
|
|
r := dns.NewResolver(cfg)
|
2022-03-27 16:44:13 +00:00
|
|
|
pr := dns.NewProxyServerHostResolver(r)
|
2020-09-17 02:48:42 +00:00
|
|
|
m := dns.NewEnhancer(cfg)
|
|
|
|
|
|
|
|
// reuse cache of old host mapper
|
|
|
|
if old := resolver.DefaultHostMapper; old != nil {
|
|
|
|
m.PatchFrom(old.(*dns.ResolverEnhancer))
|
2020-08-30 16:32:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-15 13:42:46 +00:00
|
|
|
resolver.DefaultResolver = r
|
2020-09-17 02:48:42 +00:00
|
|
|
resolver.DefaultHostMapper = m
|
2022-05-02 06:21:37 +00:00
|
|
|
resolver.DefaultLocalServer = dns.NewLocalServer(r, m)
|
2020-09-17 02:48:42 +00:00
|
|
|
|
2022-03-27 16:44:13 +00:00
|
|
|
if pr.HasProxyServer() {
|
|
|
|
resolver.ProxyServerHostResolver = pr
|
|
|
|
}
|
|
|
|
|
2022-05-01 01:38:43 +00:00
|
|
|
dns.ReCreateServer(c.Listen, r, m)
|
2018-12-05 13:13:29 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 12:20:04 +00:00
|
|
|
func updateHosts(tree *trie.DomainTrie[netip.Addr]) {
|
2020-02-15 13:42:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-28 00:55:45 +00:00
|
|
|
func updateRules(rules []C.Rule, ruleProviders map[string]provider.RuleProvider) {
|
2021-12-02 14:56:17 +00:00
|
|
|
tunnel.UpdateRules(rules, ruleProviders)
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 00:55:45 +00: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-01-18 13:09:36 +00:00
|
|
|
|
2022-05-07 16:04:16 +00:00
|
|
|
if err := pv.Initial(); err != nil {
|
2022-04-28 00:55:45 +00:00
|
|
|
switch pv.Type() {
|
|
|
|
case provider.Proxy:
|
|
|
|
{
|
|
|
|
log.Warnln("initial proxy provider %s error: %v", (pv).Name(), err)
|
|
|
|
}
|
|
|
|
case provider.Rule:
|
|
|
|
{
|
|
|
|
log.Warnln("initial rule provider %s error: %v", (pv).Name(), err)
|
2022-01-18 13:09:36 +00:00
|
|
|
}
|
2022-04-28 00:55:45 +00:00
|
|
|
|
2022-01-18 13:09:36 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 00:55:45 +00:00
|
|
|
}
|
2022-01-18 13:09:36 +00:00
|
|
|
|
2022-04-28 00:55:45 +00:00
|
|
|
func loadRuleProvider(ruleProviders map[string]provider.RuleProvider) {
|
2022-05-02 08:47:48 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2022-05-08 14:52:46 +00:00
|
|
|
ch := make(chan struct{}, concurrentCount)
|
2022-04-28 00:55:45 +00:00
|
|
|
for _, ruleProvider := range ruleProviders {
|
2022-05-02 08:47:48 +00:00
|
|
|
ruleProvider := ruleProvider
|
|
|
|
wg.Add(1)
|
2022-05-08 14:52:46 +00:00
|
|
|
ch <- struct{}{}
|
2022-05-02 08:47:48 +00:00
|
|
|
go func() {
|
2022-05-08 14:52:46 +00:00
|
|
|
defer func() { <-ch; wg.Done() }()
|
2022-05-02 08:47:48 +00:00
|
|
|
loadProvider(ruleProvider)
|
2022-05-08 14:52:46 +00:00
|
|
|
|
2022-05-02 08:47:48 +00:00
|
|
|
}()
|
2022-01-18 13:09:36 +00:00
|
|
|
}
|
2022-05-02 08:47:48 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
2022-04-28 00:55:45 +00:00
|
|
|
}
|
2022-01-18 13:09:36 +00:00
|
|
|
|
2022-05-02 08:47:48 +00:00
|
|
|
func loadProxyProvider(proxyProviders map[string]provider.ProxyProvider) {
|
2022-05-08 14:52:46 +00:00
|
|
|
// limit concurrent size
|
2022-05-02 08:47:48 +00:00
|
|
|
wg := sync.WaitGroup{}
|
2022-05-08 14:52:46 +00:00
|
|
|
ch := make(chan struct{}, concurrentCount)
|
2022-05-02 08:47:48 +00:00
|
|
|
for _, proxyProvider := range proxyProviders {
|
|
|
|
proxyProvider := proxyProvider
|
|
|
|
wg.Add(1)
|
2022-05-08 14:52:46 +00:00
|
|
|
ch <- struct{}{}
|
2022-05-02 08:47:48 +00:00
|
|
|
go func() {
|
2022-05-08 14:52:46 +00:00
|
|
|
defer func() { <-ch; wg.Done() }()
|
2022-05-02 08:47:48 +00:00
|
|
|
loadProvider(proxyProvider)
|
|
|
|
}()
|
2022-01-18 13:09:36 +00:00
|
|
|
}
|
2022-05-02 08:47:48 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
2022-01-18 13:09:36 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 11:19:19 +00:00
|
|
|
func updateTun(tun *config.Tun) {
|
|
|
|
P.ReCreateTun(tun, tunnel.TCPIn(), tunnel.UDPIn())
|
2022-03-08 21:08:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 12:20:04 +00:00
|
|
|
func updateSniffer(sniffer *config.Sniffer) {
|
|
|
|
if sniffer.Enable {
|
2022-05-03 15:10:59 +00:00
|
|
|
dispatcher, err := SNI.NewSnifferDispatcher(sniffer.Sniffers, sniffer.ForceDomain, sniffer.SkipDomain, sniffer.Ports)
|
2022-04-12 12:20:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln("initial sniffer failed, err:%v", err)
|
|
|
|
}
|
2022-04-16 00:21:31 +00:00
|
|
|
|
|
|
|
tunnel.UpdateSniffer(dispatcher)
|
2022-04-12 12:20:04 +00:00
|
|
|
log.Infoln("Sniffer is loaded and working")
|
2022-04-16 00:21:31 +00:00
|
|
|
} 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 12:20:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 17:11:27 +00:00
|
|
|
func updateGeneral(general *config.General, force bool) {
|
2022-03-19 18:39:48 +00:00
|
|
|
log.SetLevel(general.LogLevel)
|
2022-05-19 12:43:41 +00:00
|
|
|
process.EnableFindProcess(general.EnableProcess)
|
2020-02-15 13:42:46 +00:00
|
|
|
tunnel.SetMode(general.Mode)
|
2022-04-22 17:31:34 +00:00
|
|
|
dialer.DisableIPv6 = !general.IPv6
|
2022-04-22 16:27:22 +00:00
|
|
|
if !dialer.DisableIPv6 {
|
|
|
|
log.Infoln("Use IPv6")
|
2022-04-23 06:21:58 +00:00
|
|
|
} else {
|
|
|
|
resolver.DisableIPv6 = true
|
2022-04-22 16:27:22 +00:00
|
|
|
}
|
2022-03-19 18:39:48 +00:00
|
|
|
|
2022-04-27 13:37:20 +00:00
|
|
|
if general.TCPConcurrent {
|
|
|
|
dialer.SetDial(general.TCPConcurrent)
|
2022-04-22 16:27:22 +00:00
|
|
|
log.Infoln("Use tcp concurrent")
|
|
|
|
}
|
2022-01-05 03:24:00 +00:00
|
|
|
|
2022-04-22 16:27:22 +00:00
|
|
|
adapter.UnifiedDelay.Store(general.UnifiedDelay)
|
2021-11-07 08:48:51 +00:00
|
|
|
dialer.DefaultInterface.Store(general.Interface)
|
2022-03-19 18:39:48 +00:00
|
|
|
|
2022-03-08 21:08:35 +00:00
|
|
|
if dialer.DefaultInterface.Load() != "" {
|
|
|
|
log.Infoln("Use interface name: %s", general.Interface)
|
|
|
|
}
|
2020-06-27 06:19:31 +00:00
|
|
|
|
2022-03-19 18:39:48 +00:00
|
|
|
dialer.DefaultRoutingMark.Store(int32(general.RoutingMark))
|
|
|
|
if general.RoutingMark > 0 {
|
|
|
|
log.Infoln("Use routing mark: %#x", general.RoutingMark)
|
2022-03-02 21:02:17 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2021-09-06 15:07:34 +00:00
|
|
|
iface.FlushCache()
|
|
|
|
|
2020-06-18 10:11:02 +00:00
|
|
|
if !force {
|
|
|
|
return
|
|
|
|
}
|
2018-12-03 15:41:40 +00:00
|
|
|
|
2022-03-14 18:20:19 +00:00
|
|
|
geodataLoader := general.GeodataLoader
|
|
|
|
G.SetLoader(geodataLoader)
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
allowLan := general.AllowLan
|
|
|
|
P.SetAllowLan(allowLan)
|
2019-06-27 09:04:25 +00:00
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
bindAddress := general.BindAddress
|
|
|
|
P.SetBindAddress(bindAddress)
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
tcpIn := tunnel.TCPIn()
|
|
|
|
udpIn := tunnel.UDPIn()
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
P.ReCreateHTTP(general.Port, tcpIn)
|
|
|
|
P.ReCreateSocks(general.SocksPort, tcpIn, udpIn)
|
|
|
|
P.ReCreateRedir(general.RedirPort, tcpIn, udpIn)
|
|
|
|
P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
|
|
|
|
P.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
2019-06-27 09:04:25 +00:00
|
|
|
|
|
|
|
func updateUsers(users []auth.AuthUser) {
|
|
|
|
authenticator := auth.NewAuthenticator(users)
|
|
|
|
authStore.SetAuthenticator(authenticator)
|
|
|
|
if authenticator != nil {
|
|
|
|
log.Infoln("Authentication of local server updated")
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 15:41:50 +00:00
|
|
|
|
|
|
|
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)
|
2021-02-18 15:41:50 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-22 16:40:27 +00:00
|
|
|
selector, ok := outbound.ProxyAdapter.(outboundgroup.SelectAble)
|
2021-02-18 15:41:50 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
selected, exist := mapping[name]
|
|
|
|
if !exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
selector.Set(selected)
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2022-03-21 21:38:42 +00:00
|
|
|
func updateIPTables(cfg *config.Config) {
|
|
|
|
tproxy.CleanupTProxyIPTables()
|
2022-03-08 21:08:35 +00:00
|
|
|
|
2022-03-21 21:38:42 +00:00
|
|
|
iptables := cfg.IPTables
|
|
|
|
if runtime.GOOS != "linux" || !iptables.Enable {
|
2022-03-08 21:08:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-03-21 21:38:42 +00:00
|
|
|
log.Errorln("[IPTABLES] setting iptables failed: %s", err.Error())
|
2022-03-08 21:08:35 +00:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-03-23 12:37:46 +00:00
|
|
|
if cfg.Tun.Enable {
|
|
|
|
err = fmt.Errorf("when tun is enabled, iptables cannot be set automatically")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:38:42 +00:00
|
|
|
var (
|
|
|
|
inboundInterface = "lo"
|
2022-03-23 03:04:43 +00:00
|
|
|
bypass = iptables.Bypass
|
2022-03-21 21:38:42 +00:00
|
|
|
tProxyPort = cfg.General.TProxyPort
|
|
|
|
dnsCfg = cfg.DNS
|
|
|
|
)
|
|
|
|
|
|
|
|
if tProxyPort == 0 {
|
|
|
|
err = fmt.Errorf("tproxy-port must be greater than zero")
|
2022-03-08 21:08:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:38:42 +00:00
|
|
|
if !dnsCfg.Enable {
|
|
|
|
err = fmt.Errorf("DNS server must be enable")
|
2021-12-04 09:41:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
dnsPort, err := netip.ParseAddrPort(dnsCfg.Listen)
|
2021-12-04 09:41:13 +00:00
|
|
|
if err != nil {
|
2022-03-22 16:46:37 +00:00
|
|
|
err = fmt.Errorf("DNS server must be correct")
|
2021-07-01 14:49:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:38:42 +00:00
|
|
|
if iptables.InboundInterface != "" {
|
|
|
|
inboundInterface = iptables.InboundInterface
|
2021-12-04 09:41:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-05 10:04:04 +00:00
|
|
|
if dialer.DefaultRoutingMark.Load() == 0 {
|
|
|
|
dialer.DefaultRoutingMark.Store(2158)
|
2021-12-04 09:41:13 +00:00
|
|
|
}
|
2022-03-05 10:04:04 +00:00
|
|
|
|
2022-04-23 18:07:57 +00:00
|
|
|
err = tproxy.SetTProxyIPTables(inboundInterface, bypass, uint16(tProxyPort), dnsPort.Port())
|
2022-03-21 21:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
2022-03-18 17:11:27 +00:00
|
|
|
}
|
2022-03-21 21:38:42 +00:00
|
|
|
|
|
|
|
log.Infoln("[IPTABLES] Setting iptables completed")
|
2021-12-04 09:41:13 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2022-03-27 19:25:55 +00:00
|
|
|
func Shutdown() {
|
2022-05-19 11:19:19 +00:00
|
|
|
P.Cleanup(false)
|
2022-03-21 21:38:42 +00:00
|
|
|
tproxy.CleanupTProxyIPTables()
|
2022-04-13 08:47:47 +00:00
|
|
|
resolver.StoreFakePoolState()
|
2022-03-27 19:25:55 +00:00
|
|
|
|
|
|
|
log.Warnln("Clash shutting down")
|
2021-11-17 08:03:47 +00:00
|
|
|
}
|