Merge remote-tracking branch 'origin/Alpha' into Alpha

This commit is contained in:
adlyq 2022-04-28 11:51:57 +08:00
commit b23a333acf
10 changed files with 49 additions and 47 deletions

View file

@ -33,15 +33,10 @@ func NewInner(conn net.Conn, dst string, host string) *context.ConnContext {
metadata.Host = host metadata.Host = host
metadata.AddrType = C.AtypDomainName metadata.AddrType = C.AtypDomainName
metadata.Process = C.ClashName metadata.Process = C.ClashName
if ip, port, err := parseAddr(dst); err == nil { if h, port, err := net.SplitHostPort(dst); err == nil {
metadata.DstPort = port metadata.DstPort = port
if host == "" { if host == "" {
metadata.DstIP = ip metadata.Host = h
if ip.Is4() {
metadata.AddrType = C.AtypIPv6
} else {
metadata.AddrType = C.AtypIPv4
}
} }
} }

View file

@ -2,7 +2,6 @@ package provider
import ( import (
"context" "context"
"github.com/Dreamacro/clash/component/dialer"
"github.com/Dreamacro/clash/listener/inner" "github.com/Dreamacro/clash/listener/inner"
"io" "io"
"net" "net"
@ -86,10 +85,6 @@ func (h *HTTPVehicle) Read() ([]byte, error) {
client := http.Client{Transport: transport} client := http.Client{Transport: transport}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
transport.DialContext = func(ctx context.Context, network, address string) (net.Conn, error) {
return dialer.DialContext(ctx, network, address)
}
resp, err = client.Do(req)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -151,7 +151,7 @@ type Config struct {
Users []auth.AuthUser Users []auth.AuthUser
Proxies map[string]C.Proxy Proxies map[string]C.Proxy
Providers map[string]providerTypes.ProxyProvider Providers map[string]providerTypes.ProxyProvider
RuleProviders map[string]*providerTypes.RuleProvider RuleProviders map[string]providerTypes.RuleProvider
Sniffer *Sniffer Sniffer *Sniffer
} }
@ -523,8 +523,8 @@ func parseProxies(cfg *RawConfig) (proxies map[string]C.Proxy, providersMap map[
return proxies, providersMap, nil return proxies, providersMap, nil
} }
func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]*providerTypes.RuleProvider, error) { func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[string]providerTypes.RuleProvider, error) {
ruleProviders := map[string]*providerTypes.RuleProvider{} ruleProviders := map[string]providerTypes.RuleProvider{}
log.Infoln("Geodata Loader mode: %s", geodata.LoaderName()) log.Infoln("Geodata Loader mode: %s", geodata.LoaderName())
// parse rule provider // parse rule provider
for name, mapping := range cfg.RuleProvider { for name, mapping := range cfg.RuleProvider {
@ -533,7 +533,7 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, map[strin
return nil, nil, err return nil, nil, err
} }
ruleProviders[name] = &rp ruleProviders[name] = rp
RP.SetRuleProvider(rp) RP.SetRuleProvider(rp)
} }

View file

@ -82,8 +82,9 @@ func ApplyConfig(cfg *config.Config, force bool) {
updateTun(cfg.Tun, cfg.DNS) updateTun(cfg.Tun, cfg.DNS)
updateExperimental(cfg) updateExperimental(cfg)
updateHosts(cfg.Hosts) updateHosts(cfg.Hosts)
loadProvider(cfg.RuleProviders, cfg.Providers) loadProxyProvider(cfg.Providers)
updateProfile(cfg) updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
log.SetLevel(cfg.General.LogLevel) log.SetLevel(cfg.General.LogLevel)
} }
@ -181,39 +182,41 @@ func updateProxies(proxies map[string]C.Proxy, providers map[string]provider.Pro
tunnel.UpdateProxies(proxies, providers) tunnel.UpdateProxies(proxies, providers)
} }
func updateRules(rules []C.Rule, ruleProviders map[string]*provider.RuleProvider) { func updateRules(rules []C.Rule, ruleProviders map[string]provider.RuleProvider) {
tunnel.UpdateRules(rules, ruleProviders) tunnel.UpdateRules(rules, ruleProviders)
} }
func loadProvider(ruleProviders map[string]*provider.RuleProvider, proxyProviders map[string]provider.ProxyProvider) { func loadProvider(pv provider.Provider) {
load := func(pv provider.Provider) { if pv.VehicleType() == provider.Compatible {
if pv.VehicleType() == provider.Compatible { log.Infoln("Start initial compatible provider %s", pv.Name())
log.Infoln("Start initial compatible provider %s", pv.Name()) } else {
} else { log.Infoln("Start initial provider %s", (pv).Name())
log.Infoln("Start initial provider %s", (pv).Name()) }
}
if err := (pv).Initial(); err != nil {
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)
}
if err := (pv).Initial(); err != nil {
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)
}
} }
} }
}
for _, proxyProvider := range proxyProviders { func loadRuleProvider(ruleProviders map[string]provider.RuleProvider) {
load(proxyProvider)
}
for _, ruleProvider := range ruleProviders { for _, ruleProvider := range ruleProviders {
load(*ruleProvider) loadProvider(ruleProvider)
}
}
func loadProxyProvider(ruleProviders map[string]provider.ProxyProvider) {
for _, ruleProvider := range ruleProviders {
loadProvider(ruleProvider)
} }
} }

View file

@ -1,6 +1,7 @@
package nat package nat
import ( import (
log "github.com/sirupsen/logrus"
"io" "io"
"net" "net"
"net/netip" "net/netip"
@ -43,6 +44,7 @@ func Start(device io.ReadWriter, gateway, portal, broadcast netip.Addr) (*TCP, *
for { for {
n, err := device.Read(buf) n, err := device.Read(buf)
if err != nil { if err != nil {
log.Warnln("system error:%s", err.Error())
continue continue
} }

View file

@ -32,6 +32,7 @@ func (c *classicalStrategy) ShouldResolveIP() bool {
func (c *classicalStrategy) OnUpdate(rules []string) { func (c *classicalStrategy) OnUpdate(rules []string) {
var classicalRules []C.Rule var classicalRules []C.Rule
shouldResolveIP := false shouldResolveIP := false
count := 0
for _, rawRule := range rules { for _, rawRule := range rules {
ruleType, rule, params := ruleParse(rawRule) ruleType, rule, params := ruleParse(rawRule)
r, err := parseRule(ruleType, rule, "", params) r, err := parseRule(ruleType, rule, "", params)
@ -43,11 +44,12 @@ func (c *classicalStrategy) OnUpdate(rules []string) {
} }
classicalRules = append(classicalRules, r) classicalRules = append(classicalRules, r)
c.count++ count++
} }
} }
c.rules = classicalRules c.rules = classicalRules
c.count = count
} }
func NewClassicalStrategy() *classicalStrategy { func NewClassicalStrategy() *classicalStrategy {

View file

@ -27,16 +27,18 @@ func (d *domainStrategy) ShouldResolveIP() bool {
func (d *domainStrategy) OnUpdate(rules []string) { func (d *domainStrategy) OnUpdate(rules []string) {
domainTrie := trie.New[bool]() domainTrie := trie.New[bool]()
count := 0
for _, rule := range rules { for _, rule := range rules {
err := domainTrie.Insert(rule, true) err := domainTrie.Insert(rule, true)
if err != nil { if err != nil {
log.Warnln("invalid domain:[%s]", rule) log.Warnln("invalid domain:[%s]", rule)
} else { } else {
d.count++ count++
} }
} }
d.domainRules = domainTrie d.domainRules = domainTrie
d.count = count
} }
func ruleParse(ruleRaw string) (string, string, []string) { func ruleParse(ruleRaw string) (string, string, []string) {

View file

@ -100,6 +100,7 @@ func (f *fetcher) Update() (interface{}, bool, error) {
hash := md5.Sum(buf) hash := md5.Sum(buf)
if bytes.Equal(f.hash[:], hash[:]) { if bytes.Equal(f.hash[:], hash[:]) {
f.updatedAt = &now f.updatedAt = &now
os.Chtimes(f.vehicle.Path(), now, now)
return nil, true, nil return nil, true, nil
} }

View file

@ -26,16 +26,18 @@ func (i *ipcidrStrategy) ShouldResolveIP() bool {
func (i *ipcidrStrategy) OnUpdate(rules []string) { func (i *ipcidrStrategy) OnUpdate(rules []string) {
ipCidrTrie := trie.NewIpCidrTrie() ipCidrTrie := trie.NewIpCidrTrie()
count := 0
for _, rule := range rules { for _, rule := range rules {
err := ipCidrTrie.AddIpCidrForString(rule) err := ipCidrTrie.AddIpCidrForString(rule)
if err != nil { if err != nil {
log.Warnln("invalid Ipcidr:[%s]", rule) log.Warnln("invalid Ipcidr:[%s]", rule)
} else { } else {
i.count++ count++
} }
} }
i.trie = ipCidrTrie i.trie = ipCidrTrie
i.count = count
i.shouldResolveIP = i.count > 0 i.shouldResolveIP = i.count > 0
} }

View file

@ -30,7 +30,7 @@ var (
rules []C.Rule rules []C.Rule
proxies = make(map[string]C.Proxy) proxies = make(map[string]C.Proxy)
providers map[string]provider.ProxyProvider providers map[string]provider.ProxyProvider
ruleProviders map[string]*provider.RuleProvider ruleProviders map[string]provider.RuleProvider
configMux sync.RWMutex configMux sync.RWMutex
// Outbound Rule // Outbound Rule
@ -62,7 +62,7 @@ func Rules() []C.Rule {
} }
// UpdateRules handle update rules // UpdateRules handle update rules
func UpdateRules(newRules []C.Rule, rp map[string]*provider.RuleProvider) { func UpdateRules(newRules []C.Rule, rp map[string]provider.RuleProvider) {
configMux.Lock() configMux.Lock()
rules = newRules rules = newRules
ruleProviders = rp ruleProviders = rp
@ -80,7 +80,7 @@ func Providers() map[string]provider.ProxyProvider {
} }
// RuleProviders return all loaded rule providers // RuleProviders return all loaded rule providers
func RuleProviders() map[string]*provider.RuleProvider { func RuleProviders() map[string]provider.RuleProvider {
return ruleProviders return ruleProviders
} }