mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-17 04:34:35 +03:00
Some checks failed
Build and Release for Windows 7 / check-assets (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / check-assets (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, android, android-amd64) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Test / check-assets (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Scheduled assets update / geodat (push) Has been cancelled
57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package dns
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/features/dns"
|
|
"github.com/xtls/xray-core/features/routing"
|
|
)
|
|
|
|
// ResolvableContext is an implementation of routing.Context, with domain resolving capability.
|
|
type ResolvableContext struct {
|
|
routing.Context
|
|
dnsClient dns.Client
|
|
cacheIPs []net.IP
|
|
hasError bool
|
|
}
|
|
|
|
// GetTargetIPs overrides original routing.Context's implementation.
|
|
func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
|
|
if len(ctx.cacheIPs) > 0 {
|
|
return ctx.cacheIPs
|
|
}
|
|
|
|
if ctx.hasError {
|
|
return nil
|
|
}
|
|
|
|
if domain := ctx.GetTargetDomain(); len(domain) != 0 {
|
|
ips, _, err := ctx.dnsClient.LookupIP(domain, dns.IPOption{
|
|
IPv4Enable: true,
|
|
IPv6Enable: true,
|
|
FakeEnable: false,
|
|
})
|
|
if err == nil {
|
|
ctx.cacheIPs = ips
|
|
return ips
|
|
}
|
|
errors.LogInfoInner(context.Background(), err, "resolve ip for ", domain)
|
|
}
|
|
|
|
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
|
|
ctx.cacheIPs = ips
|
|
return ips
|
|
}
|
|
|
|
ctx.hasError = true
|
|
return nil
|
|
}
|
|
|
|
// ContextWithDNSClient creates a new routing context with domain resolving capability.
|
|
// Resolved domain IPs can be retrieved by GetTargetIPs().
|
|
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {
|
|
return &ResolvableContext{Context: ctx, dnsClient: client}
|
|
}
|