change
This commit is contained in:
parent
d873dc5b35
commit
4fda3714f1
3 changed files with 32 additions and 15 deletions
9
pkg/config/config.go
Normal file
9
pkg/config/config.go
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import "net"
|
||||||
|
|
||||||
|
// the whole `FEC0::/10` prefix is deprecated.
|
||||||
|
// [RFC 3879]: https://tools.ietf.org/html/rfc3879
|
||||||
|
func isUnicastLinkLocal(ip net.IP) bool {
|
||||||
|
return len(ip) == net.IPv6len && ip[0] == 0xfe && ip[1] == 0xc0
|
||||||
|
}
|
|
@ -3,17 +3,28 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
|
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultResolvConfPath specifies path to default resolv config file on UNIX.
|
// DefaultResolvConfPath specifies path to default resolv config file on UNIX.
|
||||||
const DefaultResolvConfPath = "/etc/resolv.conf"
|
const DefaultResolvConfPath = "/etc/resolv.conf"
|
||||||
|
|
||||||
|
// GetDefaultServers get system default nameserver
|
||||||
func GetDefaultServers() ([]string, int, []string, error) {
|
func GetDefaultServers() ([]string, int, []string, error) {
|
||||||
// if no nameserver is provided, take it from `resolv.conf`
|
// if no nameserver is provided, take it from `resolv.conf`
|
||||||
cfg, err := dns.ClientConfigFromFile(DefaultResolvConfPath)
|
cfg, err := dns.ClientConfigFromFile(DefaultResolvConfPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, nil, err
|
return nil, 0, nil, err
|
||||||
}
|
}
|
||||||
return cfg.Servers, cfg.Ndots, cfg.Search, nil
|
servers := make([]string, 0)
|
||||||
|
for _, server := range cfg.Servers {
|
||||||
|
ip := net.ParseIP(server)
|
||||||
|
if isUnicastLinkLocal(ip) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
servers = append(servers, server)
|
||||||
|
}
|
||||||
|
return servers, cfg.Ndots, cfg.Search, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -9,8 +8,11 @@ import (
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GAA_FLAG_INCLUDE_GATEWAYS Return the addresses of default gateways.
|
||||||
|
// This flag is supported on Windows Vista and later.
|
||||||
const GAA_FLAG_INCLUDE_GATEWAYS = 0x00000080
|
const GAA_FLAG_INCLUDE_GATEWAYS = 0x00000080
|
||||||
|
|
||||||
|
// IpAdapterWinsServerAddress structure in a linked list of Windows Internet Name Service (WINS) server addresses for the adapter.
|
||||||
type IpAdapterWinsServerAddress struct {
|
type IpAdapterWinsServerAddress struct {
|
||||||
Length uint32
|
Length uint32
|
||||||
_ uint32
|
_ uint32
|
||||||
|
@ -18,6 +20,7 @@ type IpAdapterWinsServerAddress struct {
|
||||||
Address windows.SocketAddress
|
Address windows.SocketAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IpAdapterGatewayAddress structure in a linked list of gateways for the adapter.
|
||||||
type IpAdapterGatewayAddress struct {
|
type IpAdapterGatewayAddress struct {
|
||||||
Length uint32
|
Length uint32
|
||||||
_ uint32
|
_ uint32
|
||||||
|
@ -25,6 +28,8 @@ type IpAdapterGatewayAddress struct {
|
||||||
Address windows.SocketAddress
|
Address windows.SocketAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IpAdapterAddresses structure is the header node for a linked list of addresses for a particular adapter.
|
||||||
|
// This structure can simultaneously be used as part of a linked list of IP_ADAPTER_ADDRESSES structures.
|
||||||
type IpAdapterAddresses struct {
|
type IpAdapterAddresses struct {
|
||||||
Length uint32
|
Length uint32
|
||||||
IfIndex uint32
|
IfIndex uint32
|
||||||
|
@ -49,7 +54,7 @@ type IpAdapterAddresses struct {
|
||||||
/* more fields might be present here. */
|
/* more fields might be present here. */
|
||||||
TransmitLinkSpeed uint64
|
TransmitLinkSpeed uint64
|
||||||
ReceiveLinkSpeed uint64
|
ReceiveLinkSpeed uint64
|
||||||
FirstWINSServerAddress *IpAdapterWinsServerAddress
|
FirstWinsServerAddress *IpAdapterWinsServerAddress
|
||||||
FirstGatewayAddress *IpAdapterGatewayAddress
|
FirstGatewayAddress *IpAdapterGatewayAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,16 +86,7 @@ func adapterAddresses() ([]*IpAdapterAddresses, error) {
|
||||||
return aas, nil
|
return aas, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
/// As per [RFC 3879], the whole `FEC0::/10` prefix is
|
func getDefaultDNSServers() ([]string, error) {
|
||||||
/// deprecated. New software must not support site-local
|
|
||||||
/// addresses.
|
|
||||||
///
|
|
||||||
/// [RFC 3879]: https://tools.ietf.org/html/rfc3879
|
|
||||||
func isUnicastLinkLocal(ip net.IP) bool {
|
|
||||||
return len(ip) == net.IPv6len && ip[0] == 0xfe && ip[1] == 0xc0
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDefaultDnsServers() ([]string, error) {
|
|
||||||
ifs, err := adapterAddresses()
|
ifs, err := adapterAddresses()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -116,8 +112,9 @@ func GetDefaultDnsServers() ([]string, error) {
|
||||||
return dnsServers, nil
|
return dnsServers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDefaultServers get system default nameserver
|
||||||
func GetDefaultServers() ([]string, int, []string, error) {
|
func GetDefaultServers() ([]string, int, []string, error) {
|
||||||
// TODO: DNS Suffix
|
// TODO: DNS Suffix
|
||||||
servers, err := GetDefaultDnsServers()
|
servers, err := getDefaultDNSServers()
|
||||||
return servers, 0, nil, err
|
return servers, 0, nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue