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

This commit is contained in:
gVisor bot 2022-08-12 03:36:15 +08:00
commit 40cab0094e
12 changed files with 56 additions and 175 deletions

View file

@ -10,8 +10,10 @@ import (
"strings" "strings"
) )
var encRaw = base64.RawStdEncoding var (
var enc = base64.StdEncoding encRaw = base64.RawStdEncoding
enc = base64.StdEncoding
)
func DecodeBase64(buf []byte) []byte { func DecodeBase64(buf []byte) []byte {
dBuf := make([]byte, encRaw.DecodedLen(len(buf))) dBuf := make([]byte, encRaw.DecodedLen(len(buf)))
@ -149,7 +151,7 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
vless["skip-cert-verify"] = false vless["skip-cert-verify"] = false
vless["tls"] = false vless["tls"] = false
tls := strings.ToLower(query.Get("security")) tls := strings.ToLower(query.Get("security"))
if strings.Contains(tls, "tls") { if strings.HasSuffix(tls, "tls") {
vless["tls"] = true vless["tls"] = true
} }
sni := query.Get("sni") sni := query.Get("sni")
@ -244,7 +246,11 @@ func ConvertsV2Ray(buf []byte) ([]map[string]any, error) {
vmess["server"] = values["add"] vmess["server"] = values["add"]
vmess["port"] = values["port"] vmess["port"] = values["port"]
vmess["uuid"] = values["id"] vmess["uuid"] = values["id"]
vmess["alterId"] = values["aid"] if alterId, ok := values["aid"]; ok {
vmess["alterId"] = alterId
} else {
vmess["alterId"] = 0
}
vmess["cipher"] = "auto" vmess["cipher"] = "auto"
vmess["udp"] = true vmess["udp"] = true
vmess["tls"] = false vmess["tls"] = false

View file

@ -1,3 +1,5 @@
//go:build !android && linux
package ebpf package ebpf
import ( import (

View file

@ -1,4 +1,4 @@
//go:build !linux //go:build !linux || android
package ebpf package ebpf

View file

@ -83,6 +83,4 @@ type Rule interface {
Payload() string Payload() string
ShouldResolveIP() bool ShouldResolveIP() bool
ShouldFindProcess() bool ShouldFindProcess() bool
RuleExtra() *RuleExtra
SetRuleExtra(re *RuleExtra)
} }

View file

@ -1,48 +1,9 @@
package constant package constant
import ( import (
"net/netip"
"strings"
"github.com/Dreamacro/clash/component/geodata/router" "github.com/Dreamacro/clash/component/geodata/router"
) )
type RuleExtra struct {
Network NetWork
SourceIPs []*netip.Prefix
ProcessNames []string
}
func (re *RuleExtra) NotMatchNetwork(network NetWork) bool {
return re.Network != ALLNet && re.Network != network
}
func (re *RuleExtra) NotMatchSourceIP(srcIP netip.Addr) bool {
if re.SourceIPs == nil {
return false
}
for _, ips := range re.SourceIPs {
if ips.Contains(srcIP) {
return false
}
}
return true
}
func (re *RuleExtra) NotMatchProcessName(processName string) bool {
if re.ProcessNames == nil {
return false
}
for _, pn := range re.ProcessNames {
if strings.EqualFold(pn, processName) {
return false
}
}
return true
}
type RuleGeoSite interface { type RuleGeoSite interface {
GetDomainMatcher() *router.DomainMatcher GetDomainMatcher() *router.DomainMatcher
} }

View file

@ -335,7 +335,6 @@ func updateGeneral(general *config.General, force bool) {
P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn) P.ReCreateTProxy(general.TProxyPort, tcpIn, udpIn)
P.ReCreateMixed(general.MixedPort, tcpIn, udpIn) P.ReCreateMixed(general.MixedPort, tcpIn, udpIn)
P.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn) P.ReCreateAutoRedir(general.EBpf.AutoRedir, tcpIn, udpIn)
P.ReCreateRedirToTun(general.EBpf.RedirectToTun)
} }
func updateUsers(users []auth.AuthUser) { func updateUsers(users []auth.AuthUser) {

View file

@ -41,7 +41,6 @@ var (
mixedListener *mixed.Listener mixedListener *mixed.Listener
mixedUDPLister *socks.UDPListener mixedUDPLister *socks.UDPListener
tunStackListener ipstack.Stack tunStackListener ipstack.Stack
tcProgram *ebpf.TcEBpfProgram
autoRedirListener *autoredir.Listener autoRedirListener *autoredir.Listener
autoRedirProgram *ebpf.TcEBpfProgram autoRedirProgram *ebpf.TcEBpfProgram
@ -371,9 +370,9 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
var err error var err error
defer func() { defer func() {
if err != nil { if err != nil {
if redirListener != nil { if autoRedirListener != nil {
_ = redirListener.Close() _ = autoRedirListener.Close()
redirListener = nil autoRedirListener = nil
} }
if autoRedirProgram != nil { if autoRedirProgram != nil {
autoRedirProgram.Close() autoRedirProgram.Close()
@ -387,10 +386,10 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
slices.Sort(nicArr) slices.Sort(nicArr)
nicArr = slices.Compact(nicArr) nicArr = slices.Compact(nicArr)
if redirListener != nil && autoRedirProgram != nil { if autoRedirListener != nil && autoRedirProgram != nil {
_ = redirListener.Close() _ = autoRedirListener.Close()
autoRedirProgram.Close() autoRedirProgram.Close()
redirListener = nil autoRedirListener = nil
autoRedirProgram = nil autoRedirProgram = nil
} }
@ -420,37 +419,6 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs()) log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs())
} }
func ReCreateRedirToTun(ifaceNames []string) {
tcMux.Lock()
defer tcMux.Unlock()
nicArr := ifaceNames
slices.Sort(nicArr)
nicArr = slices.Compact(nicArr)
if tcProgram != nil {
tcProgram.Close()
tcProgram = nil
}
if len(nicArr) == 0 {
return
}
if lastTunConf == nil || !lastTunConf.Enable {
return
}
program, err := ebpf.NewTcEBpfProgram(nicArr, lastTunConf.Device)
if err != nil {
log.Errorln("Attached tc ebpf program error: %v", err)
return
}
tcProgram = program
log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs())
}
// GetPorts return the ports of proxy servers // GetPorts return the ports of proxy servers
func GetPorts() *Ports { func GetPorts() *Ports {
ports := &Ports{} ports := &Ports{}

View file

@ -2,10 +2,6 @@ package common
import ( import (
"errors" "errors"
"net/netip"
"strings"
C "github.com/Dreamacro/clash/constant"
) )
var ( var (
@ -15,15 +11,6 @@ var (
) )
type Base struct { type Base struct {
ruleExtra *C.RuleExtra
}
func (b *Base) RuleExtra() *C.RuleExtra {
return b.ruleExtra
}
func (b *Base) SetRuleExtra(re *C.RuleExtra) {
b.ruleExtra = re
} }
func (b *Base) ShouldFindProcess() bool { func (b *Base) ShouldFindProcess() bool {
@ -42,47 +29,3 @@ func HasNoResolve(params []string) bool {
} }
return false return false
} }
func FindNetwork(params []string) C.NetWork {
for _, p := range params {
if strings.EqualFold(p, "tcp") {
return C.TCP
} else if strings.EqualFold(p, "udp") {
return C.UDP
}
}
return C.ALLNet
}
func FindSourceIPs(params []string) []*netip.Prefix {
var ips []*netip.Prefix
for _, p := range params {
if p == noResolve || len(p) < 7 {
continue
}
ipnet, err := netip.ParsePrefix(p)
if err != nil {
continue
}
ips = append(ips, &ipnet)
}
if len(ips) > 0 {
return ips
}
return nil
}
func FindProcessName(params []string) []string {
var processNames []string
for _, p := range params {
if strings.HasPrefix(p, "P:") {
processNames = append(processNames, strings.TrimPrefix(p, "P:"))
}
}
if len(processNames) > 0 {
return processNames
}
return nil
}

View file

@ -10,8 +10,8 @@ import (
type Domain struct { type Domain struct {
*Base *Base
domain string domain string
rawDomain string
adapter string adapter string
isIDNA bool
} }
func (d *Domain) RuleType() C.RuleType { func (d *Domain) RuleType() C.RuleType {
@ -30,7 +30,11 @@ func (d *Domain) Adapter() string {
} }
func (d *Domain) Payload() string { func (d *Domain) Payload() string {
return d.rawDomain domain := d.domain
if d.isIDNA {
domain, _ = idna.ToUnicode(domain)
}
return domain
} }
func NewDomain(domain string, adapter string) *Domain { func NewDomain(domain string, adapter string) *Domain {
@ -39,7 +43,7 @@ func NewDomain(domain string, adapter string) *Domain {
Base: &Base{}, Base: &Base{},
domain: strings.ToLower(actualDomain), domain: strings.ToLower(actualDomain),
adapter: adapter, adapter: adapter,
rawDomain: domain, isIDNA: actualDomain != domain,
} }
} }

View file

@ -11,7 +11,7 @@ type DomainKeyword struct {
*Base *Base
keyword string keyword string
adapter string adapter string
rawKeyword string isIDNA bool
} }
func (dk *DomainKeyword) RuleType() C.RuleType { func (dk *DomainKeyword) RuleType() C.RuleType {
@ -31,7 +31,11 @@ func (dk *DomainKeyword) Adapter() string {
} }
func (dk *DomainKeyword) Payload() string { func (dk *DomainKeyword) Payload() string {
return dk.rawKeyword keyword := dk.keyword
if dk.isIDNA {
keyword, _ = idna.ToUnicode(keyword)
}
return keyword
} }
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword { func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
@ -40,7 +44,7 @@ func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
Base: &Base{}, Base: &Base{},
keyword: strings.ToLower(actualDomainKeyword), keyword: strings.ToLower(actualDomainKeyword),
adapter: adapter, adapter: adapter,
rawKeyword: keyword, isIDNA: keyword != actualDomainKeyword,
} }
} }

View file

@ -11,7 +11,7 @@ type DomainSuffix struct {
*Base *Base
suffix string suffix string
adapter string adapter string
rawSuffix string isIDNA bool
} }
func (ds *DomainSuffix) RuleType() C.RuleType { func (ds *DomainSuffix) RuleType() C.RuleType {
@ -31,16 +31,20 @@ func (ds *DomainSuffix) Adapter() string {
} }
func (ds *DomainSuffix) Payload() string { func (ds *DomainSuffix) Payload() string {
return ds.rawSuffix suffix := ds.suffix
if ds.isIDNA {
suffix, _ = idna.ToUnicode(suffix)
}
return suffix
} }
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix { func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
actualDomainKeyword, _ := idna.ToASCII(suffix) actualDomainSuffix, _ := idna.ToASCII(suffix)
return &DomainSuffix{ return &DomainSuffix{
Base: &Base{}, Base: &Base{},
suffix: strings.ToLower(actualDomainKeyword), suffix: strings.ToLower(actualDomainSuffix),
adapter: adapter, adapter: adapter,
rawSuffix: suffix, isIDNA: suffix != actualDomainSuffix,
} }
} }

View file

@ -65,13 +65,5 @@ func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, pars
return nil, parseErr return nil, parseErr
} }
ruleExtra := &C.RuleExtra{
Network: RC.FindNetwork(params),
SourceIPs: RC.FindSourceIPs(params),
ProcessNames: RC.FindProcessName(params),
}
parsed.SetRuleExtra(ruleExtra)
return return
} }