2018-11-21 13:47:46 +08:00
|
|
|
package executor
|
|
|
|
|
|
|
|
import (
|
2019-12-01 13:22:47 +08:00
|
|
|
"fmt"
|
2023-09-01 03:11:35 +08:00
|
|
|
"net"
|
2022-04-12 20:20:04 +08:00
|
|
|
"net/netip"
|
2019-12-01 13:22:47 +08:00
|
|
|
"os"
|
2021-12-04 17:41:13 +08:00
|
|
|
"runtime"
|
2023-09-01 03:11:35 +08:00
|
|
|
"strconv"
|
2020-06-18 18:11:02 +08:00
|
|
|
"sync"
|
2023-09-01 03:11:35 +08:00
|
|
|
"time"
|
2024-10-05 19:23:01 +08:00
|
|
|
_ "unsafe"
|
2019-12-01 13:22:47 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/adapter"
|
|
|
|
"github.com/metacubex/mihomo/adapter/inbound"
|
|
|
|
"github.com/metacubex/mihomo/adapter/outboundgroup"
|
|
|
|
"github.com/metacubex/mihomo/component/auth"
|
|
|
|
"github.com/metacubex/mihomo/component/ca"
|
|
|
|
"github.com/metacubex/mihomo/component/dialer"
|
2024-10-05 19:23:01 +08:00
|
|
|
"github.com/metacubex/mihomo/component/geodata"
|
2024-09-09 16:08:48 +08:00
|
|
|
mihomoHttp "github.com/metacubex/mihomo/component/http"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/iface"
|
2024-10-05 13:58:49 +08:00
|
|
|
"github.com/metacubex/mihomo/component/keepalive"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/profile"
|
|
|
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
2024-09-22 14:41:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/resource"
|
2024-08-27 20:33:43 +08:00
|
|
|
"github.com/metacubex/mihomo/component/sniffer"
|
2024-08-24 20:49:12 +08:00
|
|
|
tlsC "github.com/metacubex/mihomo/component/tls"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/trie"
|
2024-08-13 14:19:34 +08:00
|
|
|
"github.com/metacubex/mihomo/component/updater"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/config"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/constant/provider"
|
|
|
|
"github.com/metacubex/mihomo/dns"
|
|
|
|
"github.com/metacubex/mihomo/listener"
|
|
|
|
authStore "github.com/metacubex/mihomo/listener/auth"
|
|
|
|
LC "github.com/metacubex/mihomo/listener/config"
|
|
|
|
"github.com/metacubex/mihomo/listener/inner"
|
|
|
|
"github.com/metacubex/mihomo/listener/tproxy"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2023-11-13 17:42:25 +08:00
|
|
|
"github.com/metacubex/mihomo/ntp"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/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
|
|
|
|
2019-12-01 13:22:47 +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)
|
2019-12-01 13:22:47 +08:00
|
|
|
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)
|
2019-12-01 13:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2019-12-01 13:22:47 +08:00
|
|
|
buf, err := readConfig(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-25 15:12:11 +08:00
|
|
|
return ParseWithBytes(buf)
|
2019-12-01 13:22:47 +08:00
|
|
|
}
|
|
|
|
|
2019-12-25 15:12:11 +08:00
|
|
|
// ParseWithBytes config with buffer
|
2019-12-01 13:22:47 +08:00
|
|
|
func ParseWithBytes(buf []byte) (*config.Config, error) {
|
2019-12-25 15:12:11 +08:00
|
|
|
return config.Parse(buf)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2024-08-31 19:23:40 +08:00
|
|
|
// ApplyConfig dispatch configure to all parts without ExternalController
|
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()
|
2024-09-09 16:08:48 +08:00
|
|
|
log.SetLevel(cfg.General.LogLevel)
|
2023-03-14 22:37:07 +08:00
|
|
|
|
|
|
|
tunnel.OnSuspend()
|
|
|
|
|
2023-09-22 14:45:34 +08:00
|
|
|
ca.ResetCertificate()
|
2023-03-14 22:37:07 +08:00
|
|
|
for _, c := range cfg.TLS.CustomTrustCert {
|
2023-09-22 14:45:34 +08:00
|
|
|
if err := ca.AddCertificate(c); err != nil {
|
2023-03-14 22:37:07 +08:00
|
|
|
log.Warnln("%s\nadd error: %s", c, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-24 20:49:12 +08:00
|
|
|
updateExperimental(cfg.Experimental)
|
2019-06-27 17:04:25 +08:00
|
|
|
updateUsers(cfg.Users)
|
2022-01-18 21:09:36 +08:00
|
|
|
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)
|
2022-05-05 21:14:46 +08:00
|
|
|
updateHosts(cfg.Hosts)
|
2024-10-05 19:23:01 +08:00
|
|
|
updateGeneral(cfg.General, true)
|
2023-09-01 03:11:35 +08:00
|
|
|
updateNTP(cfg.NTP)
|
2024-06-17 22:04:51 +08:00
|
|
|
updateDNS(cfg.DNS, cfg.General.IPv6)
|
2023-02-22 22:32:04 +08:00
|
|
|
updateListeners(cfg.General, cfg.Listeners, force)
|
2024-08-26 20:52:56 +08:00
|
|
|
updateTun(cfg.General) // tun should not care "force"
|
2022-03-22 05:38:42 +08:00
|
|
|
updateIPTables(cfg)
|
2022-11-18 22:57:33 +08:00
|
|
|
updateTunnels(cfg.Tunnels)
|
2022-01-21 22:38:02 +08:00
|
|
|
|
2023-03-14 22:37:07 +08:00
|
|
|
tunnel.OnInnerLoading()
|
|
|
|
|
|
|
|
initInnerTcp()
|
|
|
|
loadProxyProvider(cfg.Providers)
|
|
|
|
updateProfile(cfg)
|
|
|
|
loadRuleProvider(cfg.RuleProviders)
|
2023-04-01 11:53:39 +08:00
|
|
|
runtime.GC()
|
2023-03-14 22:37:07 +08:00
|
|
|
tunnel.OnRunning()
|
2023-12-09 18:58:36 +08:00
|
|
|
hcCompatibleProvider(cfg.Providers)
|
2024-10-05 13:58:49 +08:00
|
|
|
updateUpdater(cfg)
|
2024-09-27 20:09:35 +08:00
|
|
|
|
|
|
|
resolver.ResetConnection()
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-08 07:58:26 +08:00
|
|
|
func initInnerTcp() {
|
2023-09-28 18:59:31 +08:00
|
|
|
inner.New(tunnel.Tunnel)
|
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
|
2024-09-27 18:10:05 +08:00
|
|
|
if auth := authStore.Default.Authenticator(); auth != nil {
|
2019-06-27 20:45:12 +08:00
|
|
|
authenticator = auth.Users()
|
|
|
|
}
|
|
|
|
|
|
|
|
general := &config.General{
|
2020-06-18 18:11:02 +08:00
|
|
|
Inbound: config.Inbound{
|
2022-11-11 20:56:08 +08:00
|
|
|
Port: ports.Port,
|
|
|
|
SocksPort: ports.SocksPort,
|
|
|
|
RedirPort: ports.RedirPort,
|
|
|
|
TProxyPort: ports.TProxyPort,
|
|
|
|
MixedPort: ports.MixedPort,
|
2023-09-02 01:14:26 +08:00
|
|
|
Tun: listener.GetTunConf(),
|
2022-12-06 10:13:05 +08:00
|
|
|
TuicServer: listener.GetTuicConf(),
|
2022-11-11 20:56:08 +08:00
|
|
|
ShadowSocksConfig: ports.ShadowSocksConfig,
|
|
|
|
VmessConfig: ports.VmessConfig,
|
|
|
|
Authentication: authenticator,
|
2023-10-10 19:43:26 +08:00
|
|
|
SkipAuthPrefixes: inbound.SkipAuthPrefixes(),
|
2023-12-12 20:39:11 +08:00
|
|
|
LanAllowedIPs: inbound.AllowedIPs(),
|
|
|
|
LanDisAllowedIPs: inbound.DisAllowedIPs(),
|
2022-11-28 20:04:56 +08:00
|
|
|
AllowLan: listener.AllowLan(),
|
|
|
|
BindAddress: listener.BindAddress(),
|
2024-08-24 20:49:12 +08:00
|
|
|
InboundTfo: inbound.Tfo(),
|
|
|
|
InboundMPTCP: inbound.MPTCP(),
|
2020-06-18 18:11:02 +08:00
|
|
|
},
|
2024-08-24 20:49:12 +08:00
|
|
|
Mode: tunnel.Mode(),
|
|
|
|
UnifiedDelay: adapter.UnifiedDelay.Load(),
|
|
|
|
LogLevel: log.Level(),
|
|
|
|
IPv6: !resolver.DisableIPv6,
|
|
|
|
Interface: dialer.DefaultInterface.Load(),
|
|
|
|
RoutingMark: int(dialer.DefaultRoutingMark.Load()),
|
|
|
|
GeoXUrl: config.GeoXUrl{
|
2024-10-05 19:23:01 +08:00
|
|
|
GeoIp: geodata.GeoIpUrl(),
|
|
|
|
Mmdb: geodata.MmdbUrl(),
|
|
|
|
ASN: geodata.ASNUrl(),
|
|
|
|
GeoSite: geodata.GeoSiteUrl(),
|
2024-08-24 20:49:12 +08:00
|
|
|
},
|
2024-09-09 16:08:48 +08:00
|
|
|
GeoAutoUpdate: updater.GeoAutoUpdate(),
|
|
|
|
GeoUpdateInterval: updater.GeoUpdateInterval(),
|
2024-10-05 19:23:01 +08:00
|
|
|
GeodataMode: geodata.GeodataMode(),
|
|
|
|
GeodataLoader: geodata.LoaderName(),
|
|
|
|
GeositeMatcher: geodata.SiteMatcherName(),
|
2024-08-24 20:49:12 +08:00
|
|
|
TCPConcurrent: dialer.GetTcpConcurrent(),
|
|
|
|
FindProcessMode: tunnel.FindProcessMode(),
|
|
|
|
Sniffing: tunnel.IsSniffing(),
|
|
|
|
GlobalClientFingerprint: tlsC.GetGlobalFingerprint(),
|
2024-09-09 16:08:48 +08:00
|
|
|
GlobalUA: mihomoHttp.UA(),
|
2024-09-22 14:41:45 +08:00
|
|
|
ETagSupport: resource.ETag(),
|
2024-10-05 13:58:49 +08:00
|
|
|
KeepAliveInterval: int(keepalive.KeepAliveInterval() / time.Second),
|
|
|
|
KeepAliveIdle: int(keepalive.KeepAliveIdle() / time.Second),
|
|
|
|
DisableKeepAlive: keepalive.DisableKeepAlive(),
|
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
|
|
|
}
|
|
|
|
|
2023-02-22 22:32:04 +08:00
|
|
|
func updateListeners(general *config.General, listeners map[string]C.InboundListener, force bool) {
|
2023-09-28 18:59:31 +08:00
|
|
|
listener.PatchInboundListeners(listeners, tunnel.Tunnel, true)
|
2023-02-22 22:32:04 +08:00
|
|
|
if !force {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
allowLan := general.AllowLan
|
|
|
|
listener.SetAllowLan(allowLan)
|
2023-10-10 19:43:26 +08:00
|
|
|
inbound.SetSkipAuthPrefixes(general.SkipAuthPrefixes)
|
2023-12-12 20:39:11 +08:00
|
|
|
inbound.SetAllowedIPs(general.LanAllowedIPs)
|
|
|
|
inbound.SetDisAllowedIPs(general.LanDisAllowedIPs)
|
2023-02-22 22:32:04 +08:00
|
|
|
|
|
|
|
bindAddress := general.BindAddress
|
|
|
|
listener.SetBindAddress(bindAddress)
|
2023-09-28 18:59:31 +08:00
|
|
|
listener.ReCreateHTTP(general.Port, tunnel.Tunnel)
|
|
|
|
listener.ReCreateSocks(general.SocksPort, tunnel.Tunnel)
|
|
|
|
listener.ReCreateRedir(general.RedirPort, tunnel.Tunnel)
|
|
|
|
listener.ReCreateTProxy(general.TProxyPort, tunnel.Tunnel)
|
|
|
|
listener.ReCreateMixed(general.MixedPort, tunnel.Tunnel)
|
|
|
|
listener.ReCreateShadowSocks(general.ShadowSocksConfig, tunnel.Tunnel)
|
|
|
|
listener.ReCreateVmess(general.VmessConfig, tunnel.Tunnel)
|
|
|
|
listener.ReCreateTuic(general.TuicServer, tunnel.Tunnel)
|
2024-08-26 20:52:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateTun(general *config.General) {
|
2024-08-24 13:25:23 +08:00
|
|
|
listener.ReCreateTun(general.Tun, tunnel.Tunnel)
|
2022-12-04 13:37:14 +08:00
|
|
|
}
|
|
|
|
|
2024-08-24 20:49:12 +08:00
|
|
|
func updateExperimental(c *config.Experimental) {
|
|
|
|
if c.QUICGoDisableGSO {
|
2023-10-01 09:10:11 +08:00
|
|
|
_ = os.Setenv("QUIC_GO_DISABLE_GSO", strconv.FormatBool(true))
|
|
|
|
}
|
2024-08-24 20:49:12 +08:00
|
|
|
if c.QUICGoDisableECN {
|
2023-10-01 09:10:11 +08:00
|
|
|
_ = os.Setenv("QUIC_GO_DISABLE_ECN", strconv.FormatBool(true))
|
2023-09-08 22:58:59 +08:00
|
|
|
}
|
2024-08-24 20:49:12 +08:00
|
|
|
dialer.GetIP4PEnable(c.IP4PEnable)
|
2022-06-14 22:50:57 +08:00
|
|
|
}
|
2019-04-24 12:02:52 +08:00
|
|
|
|
2023-09-01 03:11:35 +08:00
|
|
|
func updateNTP(c *config.NTP) {
|
|
|
|
if c.Enable {
|
2023-09-25 09:11:20 +08:00
|
|
|
ntp.ReCreateNTPService(
|
|
|
|
net.JoinHostPort(c.Server, strconv.Itoa(c.Port)),
|
|
|
|
time.Duration(c.Interval),
|
|
|
|
c.DialerProxy,
|
|
|
|
c.WriteToSystem,
|
|
|
|
)
|
2023-09-01 03:11:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
func updateDNS(c *config.DNS, generalIPv6 bool) {
|
2022-05-01 09:38:43 +08:00
|
|
|
if !c.Enable {
|
|
|
|
resolver.DefaultResolver = nil
|
|
|
|
resolver.DefaultHostMapper = nil
|
2022-05-02 14:21:37 +08:00
|
|
|
resolver.DefaultLocalServer = nil
|
2024-10-04 13:19:41 +08:00
|
|
|
resolver.ProxyServerHostResolver = nil
|
|
|
|
resolver.DirectHostResolver = nil
|
2022-05-01 09:38:43 +08:00
|
|
|
dns.ReCreateServer("", nil, nil)
|
|
|
|
return
|
|
|
|
}
|
2020-09-17 10:48:42 +08:00
|
|
|
cfg := dns.Config{
|
2024-08-15 20:04:24 +08:00
|
|
|
Main: c.NameServer,
|
|
|
|
Fallback: c.Fallback,
|
|
|
|
IPv6: c.IPv6 && generalIPv6,
|
|
|
|
IPv6Timeout: c.IPv6Timeout,
|
|
|
|
EnhancedMode: c.EnhancedMode,
|
|
|
|
Pool: c.FakeIPRange,
|
|
|
|
Hosts: c.Hosts,
|
|
|
|
FallbackIPFilter: c.FallbackIPFilter,
|
|
|
|
FallbackDomainFilter: c.FallbackDomainFilter,
|
|
|
|
Default: c.DefaultNameserver,
|
|
|
|
Policy: c.NameServerPolicy,
|
|
|
|
ProxyServer: c.ProxyServerNameserver,
|
2024-10-04 13:19:41 +08:00
|
|
|
DirectServer: c.DirectNameServer,
|
|
|
|
DirectFollowPolicy: c.DirectFollowPolicy,
|
2024-08-15 20:04:24 +08:00
|
|
|
CacheAlgorithm: c.CacheAlgorithm,
|
2020-09-17 10:48:42 +08:00
|
|
|
}
|
2020-08-31 00:32:18 +08:00
|
|
|
|
2024-10-04 13:19:41 +08:00
|
|
|
r := dns.NewResolver(cfg)
|
2020-09-17 10:48:42 +08:00
|
|
|
m := dns.NewEnhancer(cfg)
|
|
|
|
|
|
|
|
// reuse cache of old host mapper
|
|
|
|
if old := resolver.DefaultHostMapper; old != nil {
|
|
|
|
m.PatchFrom(old.(*dns.ResolverEnhancer))
|
2020-08-31 00:32:18 +08:00
|
|
|
}
|
|
|
|
|
2020-02-15 21:42:46 +08:00
|
|
|
resolver.DefaultResolver = r
|
2020-09-17 10:48:42 +08:00
|
|
|
resolver.DefaultHostMapper = m
|
2024-10-04 13:19:41 +08:00
|
|
|
resolver.DefaultLocalServer = dns.NewLocalServer(r.Resolver, m)
|
2024-05-06 14:03:29 +08:00
|
|
|
resolver.UseSystemHosts = c.UseSystemHosts
|
2020-09-17 10:48:42 +08:00
|
|
|
|
2024-10-04 13:19:41 +08:00
|
|
|
if r.ProxyResolver.Invalid() {
|
|
|
|
resolver.ProxyServerHostResolver = r.ProxyResolver
|
|
|
|
} else {
|
|
|
|
resolver.ProxyServerHostResolver = r.Resolver
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.DirectResolver.Invalid() {
|
|
|
|
resolver.DirectHostResolver = r.DirectResolver
|
|
|
|
} else {
|
|
|
|
resolver.DirectHostResolver = r.Resolver
|
2022-03-28 00:44:13 +08:00
|
|
|
}
|
|
|
|
|
2024-10-04 13:19:41 +08:00
|
|
|
dns.ReCreateServer(c.Listen, r.Resolver, m)
|
2018-12-05 21:13:29 +08:00
|
|
|
}
|
|
|
|
|
2023-03-12 15:00:59 +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
|
|
|
}
|
|
|
|
|
2022-04-28 08:55:45 +08:00
|
|
|
func loadProvider(pv provider.Provider) {
|
|
|
|
if pv.VehicleType() == provider.Compatible {
|
2023-12-09 18:58:36 +08:00
|
|
|
return
|
2022-04-28 08:55:45 +08:00
|
|
|
} else {
|
|
|
|
log.Infoln("Start initial provider %s", (pv).Name())
|
|
|
|
}
|
2022-01-18 21:09:36 +08:00
|
|
|
|
2022-05-08 00:04:16 +08:00
|
|
|
if err := pv.Initial(); err != nil {
|
2022-04-28 08:55:45 +08:00
|
|
|
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)
|
2022-04-28 08:55:45 +08:00
|
|
|
}
|
|
|
|
case provider.Rule:
|
|
|
|
{
|
2023-01-23 11:14:45 +08:00
|
|
|
log.Errorln("initial rule provider %s error: %v", (pv).Name(), err)
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
2022-04-28 08:55:45 +08:00
|
|
|
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
|
|
|
}
|
2022-04-28 08:55:45 +08:00
|
|
|
}
|
2022-01-18 21:09:36 +08:00
|
|
|
|
2022-04-28 08:55:45 +08:00
|
|
|
func loadRuleProvider(ruleProviders map[string]provider.RuleProvider) {
|
2022-05-02 16:47:48 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2022-05-08 22:52:46 +08:00
|
|
|
ch := make(chan struct{}, concurrentCount)
|
2022-04-28 08:55:45 +08:00
|
|
|
for _, ruleProvider := range ruleProviders {
|
2022-05-02 16:47:48 +08:00
|
|
|
ruleProvider := ruleProvider
|
|
|
|
wg.Add(1)
|
2022-05-08 22:52:46 +08:00
|
|
|
ch <- struct{}{}
|
2022-05-02 16:47:48 +08:00
|
|
|
go func() {
|
2022-05-08 22:52:46 +08:00
|
|
|
defer func() { <-ch; wg.Done() }()
|
2022-05-02 16:47:48 +08:00
|
|
|
loadProvider(ruleProvider)
|
2022-05-08 22:52:46 +08:00
|
|
|
|
2022-05-02 16:47:48 +08:00
|
|
|
}()
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
2022-05-02 16:47:48 +08:00
|
|
|
|
|
|
|
wg.Wait()
|
2022-04-28 08:55:45 +08:00
|
|
|
}
|
2022-01-18 21:09:36 +08:00
|
|
|
|
2022-05-02 16:47:48 +08:00
|
|
|
func loadProxyProvider(proxyProviders map[string]provider.ProxyProvider) {
|
2022-05-08 22:52:46 +08:00
|
|
|
// limit concurrent size
|
2022-05-02 16:47:48 +08:00
|
|
|
wg := sync.WaitGroup{}
|
2022-05-08 22:52:46 +08:00
|
|
|
ch := make(chan struct{}, concurrentCount)
|
2022-05-02 16:47:48 +08:00
|
|
|
for _, proxyProvider := range proxyProviders {
|
|
|
|
proxyProvider := proxyProvider
|
|
|
|
wg.Add(1)
|
2022-05-08 22:52:46 +08:00
|
|
|
ch <- struct{}{}
|
2022-05-02 16:47:48 +08:00
|
|
|
go func() {
|
2022-05-08 22:52:46 +08:00
|
|
|
defer func() { <-ch; wg.Done() }()
|
2022-05-02 16:47:48 +08:00
|
|
|
loadProvider(proxyProvider)
|
|
|
|
}()
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
2022-05-02 16:47:48 +08:00
|
|
|
|
|
|
|
wg.Wait()
|
2022-01-18 21:09:36 +08:00
|
|
|
}
|
2023-12-09 18:58:36 +08:00
|
|
|
func hcCompatibleProvider(proxyProviders map[string]provider.ProxyProvider) {
|
|
|
|
// limit concurrent size
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
ch := make(chan struct{}, concurrentCount)
|
|
|
|
for _, proxyProvider := range proxyProviders {
|
|
|
|
proxyProvider := proxyProvider
|
|
|
|
if proxyProvider.VehicleType() == provider.Compatible {
|
|
|
|
log.Infoln("Start initial Compatible provider %s", proxyProvider.Name())
|
|
|
|
wg.Add(1)
|
|
|
|
ch <- struct{}{}
|
|
|
|
go func() {
|
|
|
|
defer func() { <-ch; wg.Done() }()
|
|
|
|
if err := proxyProvider.Initial(); err != nil {
|
|
|
|
log.Errorln("initial Compatible provider %s error: %v", proxyProvider.Name(), err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2022-01-18 21:09:36 +08:00
|
|
|
|
2023-12-09 18:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-03-09 05:08:35 +08:00
|
|
|
|
2024-08-27 20:33:43 +08:00
|
|
|
func updateSniffer(snifferConfig *sniffer.Config) {
|
|
|
|
dispatcher, err := sniffer.NewDispatcher(snifferConfig)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("initial sniffer failed, err:%v", err)
|
|
|
|
}
|
2022-04-16 08:21:31 +08:00
|
|
|
|
2024-08-27 20:33:43 +08:00
|
|
|
tunnel.UpdateSniffer(dispatcher)
|
|
|
|
|
|
|
|
if snifferConfig.Enable {
|
2022-04-12 20:20:04 +08:00
|
|
|
log.Infoln("Sniffer is loaded and working")
|
2022-04-16 08:21:31 +08:00
|
|
|
} else {
|
|
|
|
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) {
|
2023-09-28 18:59:31 +08:00
|
|
|
listener.PatchTunnel(tunnels, tunnel.Tunnel)
|
2022-11-18 22:57:33 +08:00
|
|
|
}
|
|
|
|
|
2024-10-05 13:58:49 +08:00
|
|
|
func updateUpdater(cfg *config.Config) {
|
|
|
|
general := cfg.General
|
|
|
|
updater.SetGeoAutoUpdate(general.GeoAutoUpdate)
|
|
|
|
updater.SetGeoUpdateInterval(general.GeoUpdateInterval)
|
|
|
|
|
|
|
|
controller := cfg.Controller
|
2024-10-05 13:40:00 +08:00
|
|
|
updater.DefaultUiUpdater = updater.NewUiUpdater(controller.ExternalUI, controller.ExternalUIURL, controller.ExternalUIName)
|
|
|
|
updater.DefaultUiUpdater.AutoDownloadUI()
|
2024-08-13 14:19:34 +08:00
|
|
|
}
|
|
|
|
|
2024-10-05 19:23:01 +08:00
|
|
|
//go:linkname temporaryUpdateGeneral github.com/metacubex/mihomo/config.temporaryUpdateGeneral
|
|
|
|
func temporaryUpdateGeneral(general *config.General) func() {
|
|
|
|
oldGeneral := GetGeneral()
|
|
|
|
updateGeneral(general, false)
|
|
|
|
return func() {
|
|
|
|
updateGeneral(oldGeneral, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateGeneral(general *config.General, logging bool) {
|
2020-02-15 21:42:46 +08:00
|
|
|
tunnel.SetMode(general.Mode)
|
2023-01-20 16:29:08 +08:00
|
|
|
tunnel.SetFindProcessMode(general.FindProcessMode)
|
2023-03-06 23:23:05 +08:00
|
|
|
resolver.DisableIPv6 = !general.IPv6
|
2022-03-20 02:39:48 +08:00
|
|
|
|
2024-10-05 19:23:01 +08:00
|
|
|
dialer.SetTcpConcurrent(general.TCPConcurrent)
|
|
|
|
if logging && general.TCPConcurrent {
|
2022-04-23 00:27:22 +08:00
|
|
|
log.Infoln("Use tcp concurrent")
|
|
|
|
}
|
2022-01-05 11:24:00 +08:00
|
|
|
|
2023-02-24 14:19:50 +08:00
|
|
|
inbound.SetTfo(general.InboundTfo)
|
2023-08-09 17:09:03 +08:00
|
|
|
inbound.SetMPTCP(general.InboundMPTCP)
|
2023-02-24 14:19:50 +08:00
|
|
|
|
2024-10-05 13:58:49 +08:00
|
|
|
keepalive.SetKeepAliveIdle(time.Duration(general.KeepAliveIdle) * time.Second)
|
|
|
|
keepalive.SetKeepAliveInterval(time.Duration(general.KeepAliveInterval) * time.Second)
|
|
|
|
keepalive.SetDisableKeepAlive(general.DisableKeepAlive)
|
|
|
|
|
2022-04-23 00:27:22 +08:00
|
|
|
adapter.UnifiedDelay.Store(general.UnifiedDelay)
|
2020-06-27 14:19:31 +08:00
|
|
|
|
2023-03-14 22:37:07 +08:00
|
|
|
dialer.DefaultInterface.Store(general.Interface)
|
2022-03-20 02:39:48 +08:00
|
|
|
dialer.DefaultRoutingMark.Store(int32(general.RoutingMark))
|
2024-10-05 19:23:01 +08:00
|
|
|
if logging && general.RoutingMark > 0 {
|
2022-03-20 02:39:48 +08:00
|
|
|
log.Infoln("Use routing mark: %#x", general.RoutingMark)
|
2022-03-03 05:02:17 +08:00
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
|
2021-09-06 23:07:34 +08:00
|
|
|
iface.FlushCache()
|
2024-10-05 19:23:01 +08:00
|
|
|
|
|
|
|
geodata.SetGeodataMode(general.GeodataMode)
|
|
|
|
geodata.SetLoader(general.GeodataLoader)
|
|
|
|
geodata.SetSiteMatcher(general.GeositeMatcher)
|
|
|
|
geodata.SetGeoIpUrl(general.GeoXUrl.GeoIp)
|
|
|
|
geodata.SetGeoSiteUrl(general.GeoXUrl.GeoSite)
|
|
|
|
geodata.SetMmdbUrl(general.GeoXUrl.Mmdb)
|
|
|
|
geodata.SetASNUrl(general.GeoXUrl.ASN)
|
|
|
|
mihomoHttp.SetUA(general.GlobalUA)
|
|
|
|
resource.SetETag(general.ETagSupport)
|
|
|
|
|
|
|
|
tlsC.SetGlobalUtlsClient(general.GlobalClientFingerprint)
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
2019-06-27 17:04:25 +08:00
|
|
|
|
|
|
|
func updateUsers(users []auth.AuthUser) {
|
|
|
|
authenticator := auth.NewAuthenticator(users)
|
2024-09-27 18:10:05 +08:00
|
|
|
authStore.Default.SetAuthenticator(authenticator)
|
2019-06-27 17:04:25 +08:00
|
|
|
if authenticator != nil {
|
|
|
|
log.Infoln("Authentication of local server updated")
|
|
|
|
}
|
|
|
|
}
|
2021-02-18 23:41:50 +08: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 {
|
2024-09-27 21:33:37 +08:00
|
|
|
outbound, ok := proxy.(C.Proxy)
|
2021-02-18 23:41:50 +08:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-09-27 21:33:37 +08:00
|
|
|
selector, ok := outbound.Adapter().(outboundgroup.SelectAble)
|
2021-02-18 23:41:50 +08:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
selected, exist := mapping[name]
|
|
|
|
if !exist {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-24 08:07:17 +08:00
|
|
|
selector.ForceSet(selected)
|
2021-02-18 23:41:50 +08:00
|
|
|
}
|
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
|
2022-03-22 05:38:42 +08:00
|
|
|
func updateIPTables(cfg *config.Config) {
|
|
|
|
tproxy.CleanupTProxyIPTables()
|
2022-03-09 05:08:35 +08:00
|
|
|
|
2022-03-22 05:38:42 +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 {
|
2022-03-22 05:38:42 +08:00
|
|
|
log.Errorln("[IPTABLES] setting iptables failed: %s", err.Error())
|
2022-03-09 05:08:35 +08:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-04 08:52:30 +08:00
|
|
|
if cfg.General.Tun.Enable {
|
2022-03-23 20:37:46 +08:00
|
|
|
err = fmt.Errorf("when tun is enabled, iptables cannot be set automatically")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 05:38:42 +08:00
|
|
|
var (
|
|
|
|
inboundInterface = "lo"
|
2022-03-23 11:04:43 +08:00
|
|
|
bypass = iptables.Bypass
|
2022-03-22 05:38:42 +08:00
|
|
|
tProxyPort = cfg.General.TProxyPort
|
|
|
|
dnsCfg = cfg.DNS
|
2024-03-07 00:52:20 +08:00
|
|
|
DnsRedirect = iptables.DnsRedirect
|
|
|
|
|
|
|
|
dnsPort netip.AddrPort
|
2022-03-22 05:38:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if tProxyPort == 0 {
|
|
|
|
err = fmt.Errorf("tproxy-port must be greater than zero")
|
2022-03-09 05:08:35 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-07 00:52:20 +08:00
|
|
|
if DnsRedirect {
|
|
|
|
if !dnsCfg.Enable {
|
|
|
|
err = fmt.Errorf("DNS server must be enable")
|
|
|
|
return
|
|
|
|
}
|
2021-12-04 17:41:13 +08:00
|
|
|
|
2024-03-07 00:52:20 +08:00
|
|
|
dnsPort, err = netip.ParseAddrPort(dnsCfg.Listen)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("DNS server must be correct")
|
|
|
|
return
|
|
|
|
}
|
2021-07-01 22:49:29 +08:00
|
|
|
}
|
|
|
|
|
2022-03-22 05:38:42 +08:00
|
|
|
if iptables.InboundInterface != "" {
|
|
|
|
inboundInterface = iptables.InboundInterface
|
2021-12-04 17:41:13 +08:00
|
|
|
}
|
|
|
|
|
2024-06-17 22:04:51 +08:00
|
|
|
dialer.DefaultRoutingMark.CompareAndSwap(0, 2158)
|
2022-03-05 18:04:04 +08:00
|
|
|
|
2024-03-07 00:52:20 +08:00
|
|
|
err = tproxy.SetTProxyIPTables(inboundInterface, bypass, uint16(tProxyPort), DnsRedirect, dnsPort.Port())
|
2022-03-22 05:38:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
2022-03-19 01:11:27 +08:00
|
|
|
}
|
2022-03-22 05:38:42 +08:00
|
|
|
|
|
|
|
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()
|
2022-03-22 05:38:42 +08:00
|
|
|
tproxy.CleanupTProxyIPTables()
|
2022-04-13 16:47:47 +08:00
|
|
|
resolver.StoreFakePoolState()
|
2022-03-28 03:25:55 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
log.Warnln("Mihomo shutting down")
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|