github.com/EagleQL/Xray-core@v1.4.3/app/dispatcher/fakednssniffer.go (about)

     1  package dispatcher
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/xtls/xray-core/common"
     7  	"github.com/xtls/xray-core/common/net"
     8  	"github.com/xtls/xray-core/common/session"
     9  	"github.com/xtls/xray-core/core"
    10  	"github.com/xtls/xray-core/features/dns"
    11  )
    12  
    13  // newFakeDNSSniffer Create a Fake DNS metadata sniffer
    14  func newFakeDNSSniffer(ctx context.Context) (protocolSnifferWithMetadata, error) {
    15  	var fakeDNSEngine dns.FakeDNSEngine
    16  	err := core.RequireFeatures(ctx, func(fdns dns.FakeDNSEngine) {
    17  		fakeDNSEngine = fdns
    18  	})
    19  	if err != nil {
    20  		return protocolSnifferWithMetadata{}, err
    21  	}
    22  	if fakeDNSEngine == nil {
    23  		errNotInit := newError("FakeDNSEngine is not initialized, but such a sniffer is used").AtError()
    24  		return protocolSnifferWithMetadata{}, errNotInit
    25  	}
    26  	return protocolSnifferWithMetadata{protocolSniffer: func(ctx context.Context, bytes []byte) (SniffResult, error) {
    27  		Target := session.OutboundFromContext(ctx).Target
    28  		if Target.Network == net.Network_TCP || Target.Network == net.Network_UDP {
    29  			domainFromFakeDNS := fakeDNSEngine.GetDomainFromFakeDNS(Target.Address)
    30  			if domainFromFakeDNS != "" {
    31  				newError("fake dns got domain: ", domainFromFakeDNS, " for ip: ", Target.Address.String()).WriteToLog(session.ExportIDToError(ctx))
    32  				return &fakeDNSSniffResult{domainName: domainFromFakeDNS}, nil
    33  			}
    34  		}
    35  		return nil, common.ErrNoClue
    36  	}, metadataSniffer: true}, nil
    37  }
    38  
    39  type fakeDNSSniffResult struct {
    40  	domainName string
    41  }
    42  
    43  func (fakeDNSSniffResult) Protocol() string {
    44  	return "fakedns"
    45  }
    46  
    47  func (f fakeDNSSniffResult) Domain() string {
    48  	return f.domainName
    49  }