2021-03-07 06:39:50 +02:00
|
|
|
package dispatcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-20 08:15:49 +03:00
|
|
|
"strings"
|
2021-03-07 06:39:50 +02:00
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common"
|
2024-06-29 21:32:57 +03:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2021-03-07 06:39:50 +02:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/session"
|
2021-03-14 17:05:08 +02:00
|
|
|
"github.com/xtls/xray-core/core"
|
2021-03-07 06:39:50 +02:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
)
|
|
|
|
|
2022-01-11 06:39:58 +02:00
|
|
|
// newFakeDNSSniffer Creates a Fake DNS metadata sniffer
|
2021-03-07 06:39:50 +02:00
|
|
|
func newFakeDNSSniffer(ctx context.Context) (protocolSnifferWithMetadata, error) {
|
|
|
|
var fakeDNSEngine dns.FakeDNSEngine
|
2022-04-28 07:22:05 +03:00
|
|
|
{
|
2022-04-30 06:22:42 +03:00
|
|
|
fakeDNSEngineFeat := core.MustFromContext(ctx).GetFeature((*dns.FakeDNSEngine)(nil))
|
2022-04-28 07:22:05 +03:00
|
|
|
if fakeDNSEngineFeat != nil {
|
|
|
|
fakeDNSEngine = fakeDNSEngineFeat.(dns.FakeDNSEngine)
|
|
|
|
}
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|
2022-04-28 07:22:05 +03:00
|
|
|
|
2021-03-07 06:39:50 +02:00
|
|
|
if fakeDNSEngine == nil {
|
2024-06-29 21:32:57 +03:00
|
|
|
errNotInit := errors.New("FakeDNSEngine is not initialized, but such a sniffer is used").AtError()
|
2021-03-07 06:39:50 +02:00
|
|
|
return protocolSnifferWithMetadata{}, errNotInit
|
|
|
|
}
|
|
|
|
return protocolSnifferWithMetadata{protocolSniffer: func(ctx context.Context, bytes []byte) (SniffResult, error) {
|
2024-05-14 04:52:24 +03:00
|
|
|
outbounds := session.OutboundsFromContext(ctx)
|
2024-11-09 13:16:11 +02:00
|
|
|
ob := outbounds[len(outbounds)-1]
|
2024-05-14 04:52:24 +03:00
|
|
|
if ob.Target.Network == net.Network_TCP || ob.Target.Network == net.Network_UDP {
|
|
|
|
domainFromFakeDNS := fakeDNSEngine.GetDomainFromFakeDNS(ob.Target.Address)
|
2021-03-07 06:39:50 +02:00
|
|
|
if domainFromFakeDNS != "" {
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogInfo(ctx, "fake dns got domain: ", domainFromFakeDNS, " for ip: ", ob.Target.Address.String())
|
2021-03-07 06:39:50 +02:00
|
|
|
return &fakeDNSSniffResult{domainName: domainFromFakeDNS}, nil
|
|
|
|
}
|
|
|
|
}
|
2021-10-20 08:15:49 +03:00
|
|
|
|
|
|
|
if ipAddressInRangeValueI := ctx.Value(ipAddressInRange); ipAddressInRangeValueI != nil {
|
|
|
|
ipAddressInRangeValue := ipAddressInRangeValueI.(*ipAddressInRangeOpt)
|
|
|
|
if fkr0, ok := fakeDNSEngine.(dns.FakeDNSEngineRev0); ok {
|
2024-05-14 04:52:24 +03:00
|
|
|
inPool := fkr0.IsIPInIPPool(ob.Target.Address)
|
2021-10-20 08:15:49 +03:00
|
|
|
ipAddressInRangeValue.addressInRange = &inPool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 06:39:50 +02:00
|
|
|
return nil, common.ErrNoClue
|
|
|
|
}, metadataSniffer: true}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeDNSSniffResult struct {
|
|
|
|
domainName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fakeDNSSniffResult) Protocol() string {
|
|
|
|
return "fakedns"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fakeDNSSniffResult) Domain() string {
|
|
|
|
return f.domainName
|
|
|
|
}
|
2021-10-20 08:15:49 +03:00
|
|
|
|
|
|
|
type fakeDNSExtraOpts int
|
|
|
|
|
|
|
|
const ipAddressInRange fakeDNSExtraOpts = 1
|
|
|
|
|
|
|
|
type ipAddressInRangeOpt struct {
|
|
|
|
addressInRange *bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type DNSThenOthersSniffResult struct {
|
|
|
|
domainName string
|
|
|
|
protocolOriginalName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f DNSThenOthersSniffResult) IsProtoSubsetOf(protocolName string) bool {
|
|
|
|
return strings.HasPrefix(protocolName, f.protocolOriginalName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (DNSThenOthersSniffResult) Protocol() string {
|
|
|
|
return "fakedns+others"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f DNSThenOthersSniffResult) Domain() string {
|
|
|
|
return f.domainName
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFakeDNSThenOthers(ctx context.Context, fakeDNSSniffer protocolSnifferWithMetadata, others []protocolSnifferWithMetadata) (
|
2022-05-18 10:29:01 +03:00
|
|
|
protocolSnifferWithMetadata, error,
|
|
|
|
) { // nolint: unparam
|
2021-10-20 08:15:49 +03:00
|
|
|
// ctx may be used in the future
|
|
|
|
_ = ctx
|
|
|
|
return protocolSnifferWithMetadata{
|
|
|
|
protocolSniffer: func(ctx context.Context, bytes []byte) (SniffResult, error) {
|
|
|
|
ipAddressInRangeValue := &ipAddressInRangeOpt{}
|
|
|
|
ctx = context.WithValue(ctx, ipAddressInRange, ipAddressInRangeValue)
|
|
|
|
result, err := fakeDNSSniffer.protocolSniffer(ctx, bytes)
|
|
|
|
if err == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
if ipAddressInRangeValue.addressInRange != nil {
|
|
|
|
if *ipAddressInRangeValue.addressInRange {
|
|
|
|
for _, v := range others {
|
|
|
|
if v.metadataSniffer || bytes != nil {
|
|
|
|
if result, err := v.protocolSniffer(ctx, bytes); err == nil {
|
|
|
|
return DNSThenOthersSniffResult{domainName: result.Domain(), protocolOriginalName: result.Protocol()}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, common.ErrNoClue
|
|
|
|
}
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogDebug(ctx, "ip address not in fake dns range, return as is")
|
2021-10-20 08:15:49 +03:00
|
|
|
return nil, common.ErrNoClue
|
|
|
|
}
|
2024-06-29 21:32:57 +03:00
|
|
|
errors.LogWarning(ctx, "fake dns sniffer did not set address in range option, assume false.")
|
2021-10-20 08:15:49 +03:00
|
|
|
return nil, common.ErrNoClue
|
|
|
|
},
|
|
|
|
metadataSniffer: false,
|
|
|
|
}, nil
|
|
|
|
}
|