Pull request: 2509 type-safety vol.2

Merge in DNS/adguard-home from 2509-type-safety-vol2 to master

Updates #2509.

Squashed commit of the following:

commit c944e4e0a9949fc894c90b4bc1f739148a67fd9d
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Thu Jan 21 19:36:20 2021 +0300

    all: imp docs

commit e8ac1815c492b0a9434596e35a48755cac2b9f3b
Author: Eugene Burkov <e.burkov@adguard.com>
Date:   Wed Jan 20 12:38:48 2021 +0300

    all: imp JSON encoding, decoding
This commit is contained in:
Eugene Burkov
2021-01-21 19:55:41 +03:00
parent 7fab31beae
commit d9482b7588
10 changed files with 226 additions and 189 deletions

View File

@@ -1,6 +1,7 @@
package util
import (
"encoding/json"
"errors"
"fmt"
"net"
@@ -13,14 +14,30 @@ import (
"github.com/AdguardTeam/golibs/log"
)
// NetInterface represents a list of network interfaces
// NetInterface represents an entry of network interfaces map.
type NetInterface struct {
Name string // Network interface name
MTU int // MTU
HardwareAddr string // Hardware address
Addresses []net.IP // Array with the network interface addresses
Subnets []*net.IPNet // Array with CIDR addresses of this network interface
Flags string // Network interface flags (up, broadcast, etc)
MTU int `json:"mtu"`
Name string `json:"name"`
HardwareAddr net.HardwareAddr `json:"hardware_address"`
Flags net.Flags `json:"flags"`
// Array with the network interface addresses.
Addresses []net.IP `json:"ip_addresses,omitempty"`
// Array with IP networks for this network interface.
Subnets []*net.IPNet `json:"-"`
}
// MarshalJSON implements the json.Marshaler interface for *NetInterface.
func (iface *NetInterface) MarshalJSON() ([]byte, error) {
type netInterface NetInterface
return json.Marshal(&struct {
HardwareAddr string `json:"hardware_address"`
Flags string `json:"flags"`
*netInterface
}{
HardwareAddr: iface.HardwareAddr.String(),
Flags: iface.Flags.String(),
netInterface: (*netInterface)(iface),
})
}
// GetValidNetInterfaces returns interfaces that are eligible for DNS and/or DHCP
@@ -40,7 +57,7 @@ func GetValidNetInterfaces() ([]net.Interface, error) {
// GetValidNetInterfacesForWeb returns interfaces that are eligible for DNS and WEB only
// we do not return link-local addresses here
func GetValidNetInterfacesForWeb() ([]NetInterface, error) {
func GetValidNetInterfacesForWeb() ([]*NetInterface, error) {
ifaces, err := GetValidNetInterfaces()
if err != nil {
return nil, fmt.Errorf("couldn't get interfaces: %w", err)
@@ -49,7 +66,7 @@ func GetValidNetInterfacesForWeb() ([]NetInterface, error) {
return nil, errors.New("couldn't find any legible interface")
}
var netInterfaces []NetInterface
var netInterfaces []*NetInterface
for _, iface := range ifaces {
addrs, err := iface.Addrs()
@@ -57,24 +74,21 @@ func GetValidNetInterfacesForWeb() ([]NetInterface, error) {
return nil, fmt.Errorf("failed to get addresses for interface %s: %w", iface.Name, err)
}
netIface := NetInterface{
Name: iface.Name,
netIface := &NetInterface{
MTU: iface.MTU,
HardwareAddr: iface.HardwareAddr.String(),
Name: iface.Name,
HardwareAddr: iface.HardwareAddr,
Flags: iface.Flags,
}
if iface.Flags != 0 {
netIface.Flags = iface.Flags.String()
}
// Collect network interface addresses
// Collect network interface addresses.
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
// not an IPNet, should not happen
// Should be net.IPNet, this is weird.
return nil, fmt.Errorf("got iface.Addrs() element %s that is not net.IPNet, it is %T", addr, addr)
}
// ignore link-local
// Ignore link-local.
if ipNet.IP.IsLinkLocalUnicast() {
continue
}
@@ -82,7 +96,7 @@ func GetValidNetInterfacesForWeb() ([]NetInterface, error) {
netIface.Subnets = append(netIface.Subnets, ipNet)
}
// Discard interfaces with no addresses
// Discard interfaces with no addresses.
if len(netIface.Addresses) != 0 {
netInterfaces = append(netInterfaces, netIface)
}