mirror of
https://github.com/netbirdio/netbird.git
synced 2025-08-18 19:09:09 +02:00
Yep, even more cleanup
This commit is contained in:
@@ -21,7 +21,7 @@ const dnsTimeout = 5 * time.Second
|
|||||||
// Resolver caches critical NetBird infrastructure domains
|
// Resolver caches critical NetBird infrastructure domains
|
||||||
type Resolver struct {
|
type Resolver struct {
|
||||||
records map[dns.Question][]dns.RR
|
records map[dns.Question][]dns.RR
|
||||||
managementDomain *domain.Domain
|
mgmtDomain *domain.Domain
|
||||||
mutex sync.RWMutex
|
mutex sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +93,8 @@ func (m *Resolver) continueToNext(w dns.ResponseWriter, r *dns.Msg) {
|
|||||||
|
|
||||||
// AddDomain manually adds a domain to cache by resolving it.
|
// AddDomain manually adds a domain to cache by resolving it.
|
||||||
func (m *Resolver) AddDomain(ctx context.Context, d domain.Domain) error {
|
func (m *Resolver) AddDomain(ctx context.Context, d domain.Domain) error {
|
||||||
|
dnsName := strings.ToLower(dns.Fqdn(d.PunycodeString()))
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(ctx, dnsTimeout)
|
ctx, cancel := context.WithTimeout(ctx, dnsTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@@ -106,7 +108,7 @@ func (m *Resolver) AddDomain(ctx context.Context, d domain.Domain) error {
|
|||||||
if ip.Is4() {
|
if ip.Is4() {
|
||||||
rr := &dns.A{
|
rr := &dns.A{
|
||||||
Hdr: dns.RR_Header{
|
Hdr: dns.RR_Header{
|
||||||
Name: strings.ToLower(dns.Fqdn(d.PunycodeString())),
|
Name: dnsName,
|
||||||
Rrtype: dns.TypeA,
|
Rrtype: dns.TypeA,
|
||||||
Class: dns.ClassINET,
|
Class: dns.ClassINET,
|
||||||
Ttl: 300,
|
Ttl: 300,
|
||||||
@@ -117,7 +119,7 @@ func (m *Resolver) AddDomain(ctx context.Context, d domain.Domain) error {
|
|||||||
} else if ip.Is6() {
|
} else if ip.Is6() {
|
||||||
rr := &dns.AAAA{
|
rr := &dns.AAAA{
|
||||||
Hdr: dns.RR_Header{
|
Hdr: dns.RR_Header{
|
||||||
Name: strings.ToLower(dns.Fqdn(d.PunycodeString())),
|
Name: dnsName,
|
||||||
Rrtype: dns.TypeAAAA,
|
Rrtype: dns.TypeAAAA,
|
||||||
Class: dns.ClassINET,
|
Class: dns.ClassINET,
|
||||||
Ttl: 300,
|
Ttl: 300,
|
||||||
@@ -132,7 +134,7 @@ func (m *Resolver) AddDomain(ctx context.Context, d domain.Domain) error {
|
|||||||
|
|
||||||
if len(aRecords) > 0 {
|
if len(aRecords) > 0 {
|
||||||
aQuestion := dns.Question{
|
aQuestion := dns.Question{
|
||||||
Name: strings.ToLower(dns.Fqdn(d.PunycodeString())),
|
Name: dnsName,
|
||||||
Qtype: dns.TypeA,
|
Qtype: dns.TypeA,
|
||||||
Qclass: dns.ClassINET,
|
Qclass: dns.ClassINET,
|
||||||
}
|
}
|
||||||
@@ -141,7 +143,7 @@ func (m *Resolver) AddDomain(ctx context.Context, d domain.Domain) error {
|
|||||||
|
|
||||||
if len(aaaaRecords) > 0 {
|
if len(aaaaRecords) > 0 {
|
||||||
aaaaQuestion := dns.Question{
|
aaaaQuestion := dns.Question{
|
||||||
Name: strings.ToLower(dns.Fqdn(d.PunycodeString())),
|
Name: dnsName,
|
||||||
Qtype: dns.TypeAAAA,
|
Qtype: dns.TypeAAAA,
|
||||||
Qclass: dns.ClassINET,
|
Qclass: dns.ClassINET,
|
||||||
}
|
}
|
||||||
@@ -168,7 +170,7 @@ func (m *Resolver) PopulateFromConfig(ctx context.Context, mgmtURL *url.URL) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
m.managementDomain = &d
|
m.mgmtDomain = &d
|
||||||
m.mutex.Unlock()
|
m.mutex.Unlock()
|
||||||
|
|
||||||
if err := m.AddDomain(ctx, d); err != nil {
|
if err := m.AddDomain(ctx, d); err != nil {
|
||||||
@@ -180,18 +182,20 @@ func (m *Resolver) PopulateFromConfig(ctx context.Context, mgmtURL *url.URL) err
|
|||||||
|
|
||||||
// RemoveDomain removes a domain from the cache.
|
// RemoveDomain removes a domain from the cache.
|
||||||
func (m *Resolver) RemoveDomain(d domain.Domain) error {
|
func (m *Resolver) RemoveDomain(d domain.Domain) error {
|
||||||
|
dnsName := strings.ToLower(dns.Fqdn(d.PunycodeString()))
|
||||||
|
|
||||||
m.mutex.Lock()
|
m.mutex.Lock()
|
||||||
defer m.mutex.Unlock()
|
defer m.mutex.Unlock()
|
||||||
|
|
||||||
aQuestion := dns.Question{
|
aQuestion := dns.Question{
|
||||||
Name: strings.ToLower(dns.Fqdn(d.PunycodeString())),
|
Name: dnsName,
|
||||||
Qtype: dns.TypeA,
|
Qtype: dns.TypeA,
|
||||||
Qclass: dns.ClassINET,
|
Qclass: dns.ClassINET,
|
||||||
}
|
}
|
||||||
delete(m.records, aQuestion)
|
delete(m.records, aQuestion)
|
||||||
|
|
||||||
aaaaQuestion := dns.Question{
|
aaaaQuestion := dns.Question{
|
||||||
Name: strings.ToLower(dns.Fqdn(d.PunycodeString())),
|
Name: dnsName,
|
||||||
Qtype: dns.TypeAAAA,
|
Qtype: dns.TypeAAAA,
|
||||||
Qclass: dns.ClassINET,
|
Qclass: dns.ClassINET,
|
||||||
}
|
}
|
||||||
@@ -267,7 +271,8 @@ func (m *Resolver) isDomainInList(domain domain.Domain, list domain.List) bool {
|
|||||||
func (m *Resolver) isManagementDomain(domain domain.Domain) bool {
|
func (m *Resolver) isManagementDomain(domain domain.Domain) bool {
|
||||||
m.mutex.RLock()
|
m.mutex.RLock()
|
||||||
defer m.mutex.RUnlock()
|
defer m.mutex.RUnlock()
|
||||||
return m.managementDomain != nil && domain.SafeString() == m.managementDomain.SafeString()
|
|
||||||
|
return m.mgmtDomain != nil && domain == *m.mgmtDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
// addNewDomains adds all new domains to the cache
|
// addNewDomains adds all new domains to the cache
|
||||||
|
@@ -904,7 +904,7 @@ func toZone(d domain.Domain) domain.Domain {
|
|||||||
|
|
||||||
// PopulateManagementDomain populates the DNS cache with management domain
|
// PopulateManagementDomain populates the DNS cache with management domain
|
||||||
func (s *DefaultServer) PopulateManagementDomain(mgmtURL *url.URL) error {
|
func (s *DefaultServer) PopulateManagementDomain(mgmtURL *url.URL) error {
|
||||||
if s.mgmtCacheResolver != nil && mgmtURL != nil {
|
if s.mgmtCacheResolver != nil {
|
||||||
return s.mgmtCacheResolver.PopulateFromConfig(s.ctx, mgmtURL)
|
return s.mgmtCacheResolver.PopulateFromConfig(s.ctx, mgmtURL)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user