github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/dns/sytemcfg_windows.go (about)

     1  /*
     2   * Copyright (C) 2022 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package dns
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/miekg/dns"
    24  	"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
    25  )
    26  
    27  // configuration returns the system DNS configuration.
    28  func configuration() (*dns.ClientConfig, error) {
    29  	addrs, err := winipcfg.GetAdaptersAddresses(2, 0x0010)
    30  	if err != nil {
    31  		return nil, fmt.Errorf("error getting adapters addresses: %w", err)
    32  	}
    33  
    34  	resolvers := map[string]bool{}
    35  	for _, addr := range addrs {
    36  		for dnsServer := addr.FirstDNSServerAddress; dnsServer != nil; dnsServer = dnsServer.Next {
    37  			ip := dnsServer.Address.IP()
    38  			resolvers[ip.String()] = true
    39  		}
    40  	}
    41  
    42  	servers := []string{}
    43  	for server := range resolvers {
    44  		servers = append(servers, server)
    45  	}
    46  
    47  	return &dns.ClientConfig{
    48  		Servers:  servers,
    49  		Port:     "53",
    50  		Ndots:    1,
    51  		Timeout:  5,
    52  		Attempts: 2,
    53  	}, nil
    54  }
    55  
    56  // ConfiguredServers returns DNS server IPs from the system DNS configuration.
    57  func ConfiguredServers() ([]string, error) {
    58  	config, err := configuration()
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return config.Servers, nil
    63  }