github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/components/resolver/server.go (about)

     1  package resolver
     2  
     3  import (
     4  	"github.com/Asutorufa/yuhaiin/pkg/log"
     5  	"github.com/Asutorufa/yuhaiin/pkg/net/dns"
     6  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
     7  	pc "github.com/Asutorufa/yuhaiin/pkg/protos/config"
     8  )
     9  
    10  var _ netapi.DNSServer = (*DnsServer)(nil)
    11  
    12  type DnsServer struct {
    13  	netapi.DNSServer
    14  	serverHost string
    15  	resolver   netapi.Resolver
    16  }
    17  
    18  func NewDNSServer(resolver netapi.Resolver) *DnsServer {
    19  	return &DnsServer{netapi.EmptyDNSServer, "", resolver}
    20  }
    21  
    22  func (a *DnsServer) Update(s *pc.Setting) {
    23  	if a.serverHost == s.Dns.Server && a.DNSServer != netapi.EmptyDNSServer {
    24  		return
    25  	}
    26  
    27  	if a.DNSServer != nil {
    28  		if err := a.DNSServer.Close(); err != nil {
    29  			log.Error("close dns server failed", "err", err)
    30  		}
    31  	}
    32  
    33  	a.DNSServer = dns.NewServer(s.Dns.Server, a.resolver)
    34  	a.serverHost = s.Dns.Server
    35  }