123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package route
- import (
- "context"
- "net/netip"
- "strings"
- "github.com/sagernet/sing-box/adapter"
- C "github.com/sagernet/sing-box/constant"
- "github.com/sagernet/sing-box/log"
- "github.com/sagernet/sing-dns"
- E "github.com/sagernet/sing/common/exceptions"
- F "github.com/sagernet/sing/common/format"
- mDNS "github.com/miekg/dns"
- )
- func (r *Router) matchDNS(ctx context.Context) (context.Context, dns.Transport, dns.DomainStrategy) {
- metadata := adapter.ContextFrom(ctx)
- if metadata == nil {
- panic("no context")
- }
- for i, rule := range r.dnsRules {
- if rule.Match(metadata) {
- if rule.DisableCache() {
- ctx = dns.ContextWithDisableCache(ctx, true)
- }
- detour := rule.Outbound()
- r.dnsLogger.DebugContext(ctx, "match[", i, "] ", rule.String(), " => ", detour)
- if transport, loaded := r.transportMap[detour]; loaded {
- if domainStrategy, dsLoaded := r.transportDomainStrategy[transport]; dsLoaded {
- return ctx, transport, domainStrategy
- } else {
- return ctx, transport, r.defaultDomainStrategy
- }
- }
- r.dnsLogger.ErrorContext(ctx, "transport not found: ", detour)
- }
- }
- if domainStrategy, dsLoaded := r.transportDomainStrategy[r.defaultTransport]; dsLoaded {
- return ctx, r.defaultTransport, domainStrategy
- } else {
- return ctx, r.defaultTransport, r.defaultDomainStrategy
- }
- }
- func (r *Router) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
- if len(message.Question) > 0 {
- r.dnsLogger.DebugContext(ctx, "exchange ", formatQuestion(message.Question[0].String()))
- }
- ctx, metadata := adapter.AppendContext(ctx)
- if len(message.Question) > 0 {
- metadata.QueryType = message.Question[0].Qtype
- switch metadata.QueryType {
- case mDNS.TypeA:
- metadata.IPVersion = 4
- case mDNS.TypeAAAA:
- metadata.IPVersion = 6
- }
- metadata.Domain = fqdnToDomain(message.Question[0].Name)
- }
- ctx, transport, strategy := r.matchDNS(ctx)
- ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
- defer cancel()
- response, err := r.dnsClient.Exchange(ctx, transport, message, strategy)
- if err != nil && len(message.Question) > 0 {
- r.dnsLogger.ErrorContext(ctx, E.Cause(err, "exchange failed for ", formatQuestion(message.Question[0].String())))
- }
- if len(message.Question) > 0 && response != nil {
- LogDNSAnswers(r.dnsLogger, ctx, message.Question[0].Name, response.Answer)
- }
- return response, err
- }
- func (r *Router) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
- r.dnsLogger.DebugContext(ctx, "lookup domain ", domain)
- ctx, metadata := adapter.AppendContext(ctx)
- metadata.Domain = domain
- ctx, transport, transportStrategy := r.matchDNS(ctx)
- if strategy == dns.DomainStrategyAsIS {
- strategy = transportStrategy
- }
- ctx, cancel := context.WithTimeout(ctx, C.DNSTimeout)
- defer cancel()
- addrs, err := r.dnsClient.Lookup(ctx, transport, domain, strategy)
- if len(addrs) > 0 {
- r.dnsLogger.InfoContext(ctx, "lookup succeed for ", domain, ": ", strings.Join(F.MapToString(addrs), " "))
- } else {
- r.dnsLogger.ErrorContext(ctx, E.Cause(err, "lookup failed for ", domain))
- if err == nil {
- err = dns.RCodeNameError
- }
- }
- return addrs, err
- }
- func (r *Router) LookupDefault(ctx context.Context, domain string) ([]netip.Addr, error) {
- return r.Lookup(ctx, domain, dns.DomainStrategyAsIS)
- }
- func LogDNSAnswers(logger log.ContextLogger, ctx context.Context, domain string, answers []mDNS.RR) {
- for _, answer := range answers {
- logger.InfoContext(ctx, "exchanged ", domain, " ", mDNS.Type(answer.Header().Rrtype).String(), " ", formatQuestion(answer.String()))
- }
- }
- func fqdnToDomain(fqdn string) string {
- if mDNS.IsFqdn(fqdn) {
- return fqdn[:len(fqdn)-1]
- }
- return fqdn
- }
- func formatQuestion(string string) string {
- if strings.HasPrefix(string, ";") {
- string = string[1:]
- }
- string = strings.ReplaceAll(string, "\t", " ")
- for strings.Contains(string, " ") {
- string = strings.ReplaceAll(string, " ", " ")
- }
- return string
- }
|