github.com/metacubex/mihomo@v1.18.5/dns/patch_android.go (about)

     1  //go:build android && cmfa
     2  
     3  package dns
     4  
     5  import (
     6  	"context"
     7  
     8  	D "github.com/miekg/dns"
     9  
    10  	"github.com/metacubex/mihomo/common/lru"
    11  	"github.com/metacubex/mihomo/component/dhcp"
    12  	"github.com/metacubex/mihomo/component/resolver"
    13  )
    14  
    15  const SystemDNSPlaceholder = "system"
    16  
    17  var systemResolver *Resolver
    18  var isolateHandler handler
    19  
    20  var _ dnsClient = (*dhcpClient)(nil)
    21  
    22  type dhcpClient struct {
    23  	enable bool
    24  }
    25  
    26  func (d *dhcpClient) Address() string {
    27  	return SystemDNSPlaceholder
    28  }
    29  
    30  func (d *dhcpClient) Exchange(m *D.Msg) (msg *D.Msg, err error) {
    31  	return d.ExchangeContext(context.Background(), m)
    32  }
    33  
    34  func (d *dhcpClient) ExchangeContext(ctx context.Context, m *D.Msg) (msg *D.Msg, err error) {
    35  	if s := systemResolver; s != nil {
    36  		return s.ExchangeContext(ctx, m)
    37  	}
    38  
    39  	return nil, dhcp.ErrNotFound
    40  }
    41  
    42  func ServeDNSWithDefaultServer(msg *D.Msg) (*D.Msg, error) {
    43  	if h := isolateHandler; h != nil {
    44  		return handlerWithContext(context.Background(), h, msg)
    45  	}
    46  
    47  	return nil, D.ErrTime
    48  }
    49  
    50  func FlushCacheWithDefaultResolver() {
    51  	if r := resolver.DefaultResolver; r != nil {
    52  		r.(*Resolver).cache = lru.New(lru.WithSize[string, *D.Msg](4096), lru.WithStale[string, *D.Msg](true))
    53  	}
    54  }
    55  
    56  func UpdateSystemDNS(addr []string) {
    57  	if len(addr) == 0 {
    58  		systemResolver = nil
    59  	}
    60  
    61  	ns := make([]NameServer, 0, len(addr))
    62  	for _, d := range addr {
    63  		ns = append(ns, NameServer{Addr: d})
    64  	}
    65  
    66  	systemResolver = NewResolver(Config{Main: ns})
    67  }
    68  
    69  func UpdateIsolateHandler(resolver *Resolver, mapper *ResolverEnhancer) {
    70  	if resolver == nil {
    71  		isolateHandler = nil
    72  
    73  		return
    74  	}
    75  
    76  	isolateHandler = NewHandler(resolver, mapper)
    77  }
    78  
    79  func newDHCPClient(ifaceName string) *dhcpClient {
    80  	return &dhcpClient{enable: ifaceName == SystemDNSPlaceholder}
    81  }