From 33d9ab4b0ac6e39f2be5ee62eccac6359b2c78ac Mon Sep 17 00:00:00 2001 From: wyx2685 Date: Mon, 8 Jul 2024 18:12:09 +0900 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E5=BA=94xray1.8.16=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/xray/app/dispatcher/default.go | 58 ++++++++++++-------- core/xray/app/dispatcher/errors.generated.go | 9 --- core/xray/app/dispatcher/fakednssniffer.go | 9 +-- core/xray/app/dispatcher/sniffer.go | 3 +- 4 files changed, 43 insertions(+), 36 deletions(-) delete mode 100644 core/xray/app/dispatcher/errors.generated.go diff --git a/core/xray/app/dispatcher/default.go b/core/xray/app/dispatcher/default.go index 74c2854..bd1afe8 100644 --- a/core/xray/app/dispatcher/default.go +++ b/core/xray/app/dispatcher/default.go @@ -5,6 +5,7 @@ package dispatcher import ( "context" "fmt" + "regexp" "strings" "sync" "time" @@ -14,6 +15,7 @@ import ( "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/buf" + "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/log" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/protocol" @@ -29,7 +31,7 @@ import ( "github.com/xtls/xray-core/transport/pipe" ) -var errSniffingTimeout = newError("timeout on sniffing") +var errSniffingTimeout = errors.New("timeout on sniffing") type cachedReader struct { sync.Mutex @@ -165,24 +167,24 @@ func (d *DefaultDispatcher) getLink(ctx context.Context, network net.Network) (* if user != nil && len(user.Email) > 0 { limit, err = limiter.GetLimiter(sessionInbound.Tag) if err != nil { - newError("get limiter ", sessionInbound.Tag, " error: ", err).AtError().WriteToLog() + errors.LogInfo(ctx, "get limiter ", sessionInbound.Tag, " error: ", err) common.Close(outboundLink.Writer) common.Close(inboundLink.Writer) common.Interrupt(outboundLink.Reader) common.Interrupt(inboundLink.Reader) - return nil, nil, nil, newError("get limiter ", sessionInbound.Tag, " error: ", err) + return nil, nil, nil, errors.New("get limiter ", sessionInbound.Tag, " error: ", err) } // Speed Limit and Device Limit w, reject := limit.CheckLimit(user.Email, sessionInbound.Source.Address.IP().String(), network == net.Network_TCP) if reject { - newError("Limited ", user.Email, " by conn or ip").AtWarning().WriteToLog() + errors.LogInfo(ctx, "Limited ", user.Email, " by conn or ip") common.Close(outboundLink.Writer) common.Close(inboundLink.Writer) common.Interrupt(outboundLink.Reader) common.Interrupt(inboundLink.Reader) - return nil, nil, nil, newError("Limited ", user.Email, " by conn or ip") + return nil, nil, nil, errors.New("Limited ", user.Email, " by conn or ip") } if w != nil { inboundLink.Writer = rate.NewRateLimitWriter(inboundLink.Writer, w) @@ -218,8 +220,20 @@ func (d *DefaultDispatcher) shouldOverride(ctx context.Context, result SniffResu return false } for _, d := range request.ExcludeForDomain { - if strings.ToLower(domain) == d { - return false + if strings.HasPrefix(d, "regexp:") { + pattern := d[7:] + re, err := regexp.Compile(pattern) + if err != nil { + errors.LogInfo(ctx, "Unable to compile regex") + continue + } + if re.MatchString(domain) { + return false + } + } else { + if strings.ToLower(domain) == d { + return false + } } } protocolString := result.Protocol() @@ -232,7 +246,7 @@ func (d *DefaultDispatcher) shouldOverride(ctx context.Context, result SniffResu } if fkr0, ok := d.fdns.(dns.FakeDNSEngineRev0); ok && protocolString != "bittorrent" && p == "fakedns" && destination.Address.Family().IsIP() && fkr0.IsIPInIPPool(destination.Address) { - newError("Using sniffer ", protocolString, " since the fake DNS missed").WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "Using sniffer ", protocolString, " since the fake DNS missed") return true } if resultSubset, ok := result.(SnifferIsProtoSubsetOf); ok { @@ -282,7 +296,7 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin } if err == nil && d.shouldOverride(ctx, result, sniffingRequest, destination) { domain := result.Domain() - newError("sniffed domain: ", domain).WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "sniffed domain: ", domain) destination.Address = net.ParseAddress(domain) protocol := result.Protocol() if resComp, ok := result.(SnifferResultComposite); ok { @@ -307,7 +321,7 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin // DispatchLink implements routing.Dispatcher. func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.Destination, outbound *transport.Link) error { if !destination.IsValid() { - return newError("Dispatcher: Invalid destination.") + return errors.New("Dispatcher: Invalid destination.") } outbounds := session.OutboundsFromContext(ctx) if len(outbounds) == 0 { @@ -336,7 +350,7 @@ func (d *DefaultDispatcher) DispatchLink(ctx context.Context, destination net.De } if err == nil && d.shouldOverride(ctx, result, sniffingRequest, destination) { domain := result.Domain() - newError("sniffed domain: ", domain).WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "sniffed domain: ", domain) destination.Address = net.ParseAddress(domain) protocol := result.Protocol() if resComp, ok := result.(SnifferResultComposite); ok { @@ -434,7 +448,7 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport. var err error l, err = limiter.GetLimiter(sessionInbound.Tag) if err != nil { - newError("get limiter ", sessionInbound.Tag, " error: ", err).AtWarning().WriteToLog(session.ExportIDToError(ctx)) + errors.LogError(ctx, "get limiter ", sessionInbound.Tag, " error: ", err) } } if l != nil { @@ -445,20 +459,20 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport. destStr = destination.Address.IP().String() } if l.CheckDomainRule(destStr) { - newError(fmt.Sprintf( + errors.LogError(ctx, fmt.Sprintf( "User %s access domain %s reject by rule", sessionInbound.User.Email, - destStr)).AtWarning().WriteToLog(session.ExportIDToError(ctx)) + destStr)) common.Close(link.Writer) common.Interrupt(link.Reader) return } if len(protocol) != 0 { if l.CheckProtocolRule(protocol) { - newError(fmt.Sprintf( + errors.LogError(ctx, fmt.Sprintf( "User %s access protocol %s reject by rule", sessionInbound.User.Email, - protocol)).AtWarning().WriteToLog(session.ExportIDToError(ctx)) + protocol)) common.Close(link.Writer) common.Interrupt(link.Reader) return @@ -476,10 +490,10 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport. ctx = session.SetForcedOutboundTagToContext(ctx, "") if h := d.ohm.GetHandler(forcedOutboundTag); h != nil { isPickRoute = 1 - newError("taking platform initialized detour [", forcedOutboundTag, "] for [", destination, "]").WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "taking platform initialized detour [", forcedOutboundTag, "] for [", destination, "]") handler = h } else { - newError("non existing tag for platform initialized detour: ", forcedOutboundTag).AtError().WriteToLog(session.ExportIDToError(ctx)) + errors.LogError(ctx, "non existing tag for platform initialized detour: ", forcedOutboundTag) common.Close(link.Writer) common.Interrupt(link.Reader) return @@ -489,13 +503,13 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport. outTag := route.GetOutboundTag() if h := d.ohm.GetHandler(outTag); h != nil { isPickRoute = 2 - newError("taking detour [", outTag, "] for [", destination, "]").WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "taking detour [", outTag, "] for [", destination, "]") handler = h } else { - newError("non existing outTag: ", outTag).AtWarning().WriteToLog(session.ExportIDToError(ctx)) + errors.LogWarning(ctx, "non existing outTag: ", outTag) } } else { - newError("default route for ", destination).WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "default route for ", destination) } } @@ -508,7 +522,7 @@ func (d *DefaultDispatcher) routedDispatch(ctx context.Context, link *transport. } if handler == nil { - newError("default outbound handler not exist").WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "default outbound handler not exist") common.Close(link.Writer) common.Interrupt(link.Reader) return diff --git a/core/xray/app/dispatcher/errors.generated.go b/core/xray/app/dispatcher/errors.generated.go deleted file mode 100644 index e13ee60..0000000 --- a/core/xray/app/dispatcher/errors.generated.go +++ /dev/null @@ -1,9 +0,0 @@ -package dispatcher - -import "github.com/xtls/xray-core/common/errors" - -type errPathObjHolder struct{} - -func newError(values ...interface{}) *errors.Error { - return errors.New(values...).WithPathObj(errPathObjHolder{}) -} diff --git a/core/xray/app/dispatcher/fakednssniffer.go b/core/xray/app/dispatcher/fakednssniffer.go index cef6e0e..bed9087 100644 --- a/core/xray/app/dispatcher/fakednssniffer.go +++ b/core/xray/app/dispatcher/fakednssniffer.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/session" "github.com/xtls/xray-core/core" @@ -22,7 +23,7 @@ func newFakeDNSSniffer(ctx context.Context) (protocolSnifferWithMetadata, error) } if fakeDNSEngine == nil { - errNotInit := newError("FakeDNSEngine is not initialized, but such a sniffer is used").AtError() + errNotInit := errors.New("FakeDNSEngine is not initialized, but such a sniffer is used").AtError() return protocolSnifferWithMetadata{}, errNotInit } return protocolSnifferWithMetadata{protocolSniffer: func(ctx context.Context, bytes []byte) (SniffResult, error) { @@ -31,7 +32,7 @@ func newFakeDNSSniffer(ctx context.Context) (protocolSnifferWithMetadata, error) if ob.Target.Network == net.Network_TCP || ob.Target.Network == net.Network_UDP { domainFromFakeDNS := fakeDNSEngine.GetDomainFromFakeDNS(ob.Target.Address) if domainFromFakeDNS != "" { - newError("fake dns got domain: ", domainFromFakeDNS, " for ip: ", ob.Target.Address.String()).WriteToLog(session.ExportIDToError(ctx)) + errors.LogInfo(ctx, "fake dns got domain: ", domainFromFakeDNS, " for ip: ", ob.Target.Address.String()) return &fakeDNSSniffResult{domainName: domainFromFakeDNS}, nil } } @@ -109,10 +110,10 @@ func newFakeDNSThenOthers(ctx context.Context, fakeDNSSniffer protocolSnifferWit } return nil, common.ErrNoClue } - newError("ip address not in fake dns range, return as is").AtDebug().WriteToLog() + errors.LogDebug(ctx, "ip address not in fake dns range, return as is") return nil, common.ErrNoClue } - newError("fake dns sniffer did not set address in range option, assume false.").AtWarning().WriteToLog() + errors.LogWarning(ctx, "fake dns sniffer did not set address in range option, assume false.") return nil, common.ErrNoClue }, metadataSniffer: false, diff --git a/core/xray/app/dispatcher/sniffer.go b/core/xray/app/dispatcher/sniffer.go index e23277a..4d62f1b 100644 --- a/core/xray/app/dispatcher/sniffer.go +++ b/core/xray/app/dispatcher/sniffer.go @@ -4,6 +4,7 @@ import ( "context" "github.com/xtls/xray-core/common" + "github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/net" "github.com/xtls/xray-core/common/protocol/bittorrent" "github.com/xtls/xray-core/common/protocol/http" @@ -52,7 +53,7 @@ func NewSniffer(ctx context.Context) *Sniffer { return ret } -var errUnknownContent = newError("unknown content") +var errUnknownContent = errors.New("unknown content") func (s *Sniffer) Sniff(c context.Context, payload []byte, network net.Network) (SniffResult, error) { var pendingSniffer []protocolSnifferWithMetadata