Skip to content

Commit

Permalink
Pull request: create aghnet package
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from mk-aghnet to master

Updates AdguardTeam#2829.

Squashed commit of the following:

commit 519806c
Merge: 92376c8 9736123
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 16 19:13:56 2021 +0300

    Merge branch 'master' into mk-aghnet

commit 92376c8
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 16 18:37:22 2021 +0300

    aghnet: fix linux

commit 7f36d19
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 16 18:08:30 2021 +0300

    aghnet: mv network utils from util

commit aa68c70
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 16 17:30:06 2021 +0300

    aghnet: mv ipDetector here

commit b033657
Author: Eugene Burkov <[email protected]>
Date:   Tue Mar 16 17:24:07 2021 +0300

    all: mk aghnet package, rename sysutil package
  • Loading branch information
EugeneOne1 authored and heyxkhoa committed Mar 17, 2023
1 parent 1cdfd83 commit fb00247
Show file tree
Hide file tree
Showing 26 changed files with 135 additions and 148 deletions.
16 changes: 8 additions & 8 deletions internal/home/ipdetector.go → internal/aghnet/ipdetector.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package home
package aghnet

import "net"

// ipDetector describes IP address properties.
type ipDetector struct {
// IPDetector describes IP address properties.
type IPDetector struct {
nets []*net.IPNet
}

// newIPDetector returns a new IP detector.
func newIPDetector() (ipd *ipDetector, err error) {
// NewIPDetector returns a new IP detector.
func NewIPDetector() (ipd *IPDetector, err error) {
specialNetworks := []string{
"0.0.0.0/8",
"10.0.0.0/8",
Expand Down Expand Up @@ -43,7 +43,7 @@ func newIPDetector() (ipd *ipDetector, err error) {
"fe80::/10",
}

ipd = &ipDetector{
ipd = &IPDetector{
nets: make([]*net.IPNet, len(specialNetworks)),
}
for i, ipnetStr := range specialNetworks {
Expand All @@ -59,10 +59,10 @@ func newIPDetector() (ipd *ipDetector, err error) {
return ipd, nil
}

// detectSpecialNetwork returns true if IP address is contained by any of
// DetectSpecialNetwork returns true if IP address is contained by any of
// special-purpose IP address registries according to RFC-6890
// (https://tools.ietf.org/html/rfc6890).
func (ipd *ipDetector) detectSpecialNetwork(ip net.IP) bool {
func (ipd *IPDetector) DetectSpecialNetwork(ip net.IP) bool {
for _, ipnet := range ipd.nets {
if ipnet.Contains(ip) {
return true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package home
package aghnet

import (
"net"
Expand All @@ -9,10 +9,10 @@ import (
)

func TestIPDetector_detectSpecialNetwork(t *testing.T) {
var ipd *ipDetector
var ipd *IPDetector
var err error

ipd, err = newIPDetector()
ipd, err = NewIPDetector()
require.Nil(t, err)

testCases := []struct {
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestIPDetector_detectSpecialNetwork(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, ipd.detectSpecialNetwork(tc.ip))
assert.Equal(t, tc.want, ipd.DetectSpecialNetwork(tc.ip))
})
}
}
59 changes: 58 additions & 1 deletion internal/util/network.go → internal/aghnet/net.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
package util
// Package aghnet contains some utilities for networking.
package aghnet

import (
"encoding/json"
"errors"
"fmt"
"net"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/AdguardTeam/golibs/log"
)

// ErrNoStaticIPInfo is returned by IfaceHasStaticIP when no information about
// the IP being static is available.
const ErrNoStaticIPInfo agherr.Error = "no information about static ip"

// IfaceHasStaticIP checks if interface is configured to have static IP address.
// If it can't give a definitive answer, it returns false and an error for which
// errors.Is(err, ErrNoStaticIPInfo) is true.
func IfaceHasStaticIP(ifaceName string) (has bool, err error) {
return ifaceHasStaticIP(ifaceName)
}

// IfaceSetStaticIP sets static IP address for network interface.
func IfaceSetStaticIP(ifaceName string) (err error) {
return ifaceSetStaticIP(ifaceName)
}

// GatewayIP returns IP address of interface's gateway.
func GatewayIP(ifaceName string) net.IP {
cmd := exec.Command("ip", "route", "show", "dev", ifaceName)
log.Tracef("executing %s %v", cmd.Path, cmd.Args)
d, err := cmd.Output()
if err != nil || cmd.ProcessState.ExitCode() != 0 {
return nil
}

fields := strings.Fields(string(d))
// The meaningful "ip route" command output should contain the word
// "default" at first field and default gateway IP address at third
// field.
if len(fields) < 3 || fields[0] != "default" {
return nil
}

return net.ParseIP(fields[2])
}

// CanBindPort checks if we can bind to the given port.
func CanBindPort(port int) (can bool, err error) {
var addr *net.TCPAddr
addr, err = net.ResolveTCPAddr("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return false, err
}

var listener *net.TCPListener
listener, err = net.ListenTCP("tcp", addr)
if err != nil {
return false, err
}
_ = listener.Close()
return true, nil
}

// NetInterface represents an entry of network interfaces map.
type NetInterface struct {
MTU int `json:"mtu"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build darwin

package sysutil
package aghnet

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux

package sysutil
package aghnet

import (
"bufio"
Expand All @@ -13,7 +13,6 @@ import (
"strings"

"github.com/AdguardTeam/AdGuardHome/internal/aghio"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/golibs/file"
)

Expand Down Expand Up @@ -132,7 +131,7 @@ func ifacesStaticConfig(r io.Reader, ifaceName string) (has bool, err error) {
}

func ifaceSetStaticIP(ifaceName string) (err error) {
ipNet := util.GetSubnet(ifaceName)
ipNet := GetSubnet(ifaceName)
if ipNet.IP == nil {
return errors.New("can't get IP address")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux

package sysutil
package aghnet

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !linux,!darwin

package sysutil
package aghnet

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package aghnet

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions internal/sysutil/os.go → internal/aghos/os.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package sysutil contains utilities for functions requiring system calls.
package sysutil
// Package aghos contains utilities for functions requiring system calls.
package aghos

import "syscall"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build freebsd

package sysutil
package aghos

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build linux

package sysutil
package aghos

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion internal/sysutil/os_unix.go → internal/aghos/os_unix.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build aix darwin dragonfly netbsd openbsd solaris

package sysutil
package aghos

import (
"os"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build windows

package sysutil
package aghos

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion internal/sysutil/syslog.go → internal/aghos/syslog.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sysutil
package aghos

// ConfigureSyslog reroutes standard logger output to syslog.
func ConfigureSyslog(serviceName string) error {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build !windows,!plan9

package sysutil
package aghos

import (
"log/syslog"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// +build windows plan9

package sysutil
package aghos

import (
"strings"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sysutil
package aghos

import (
"testing"
Expand Down
17 changes: 8 additions & 9 deletions internal/dhcpd/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
"os"
"strings"

"github.com/AdguardTeam/AdGuardHome/internal/sysutil"
"github.com/AdguardTeam/AdGuardHome/internal/util"
"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/golibs/log"
)

Expand Down Expand Up @@ -93,7 +92,7 @@ func (s *Server) handleDHCPStatus(w http.ResponseWriter, r *http.Request) {

func (s *Server) enableDHCP(ifaceName string) (code int, err error) {
var hasStaticIP bool
hasStaticIP, err = sysutil.IfaceHasStaticIP(ifaceName)
hasStaticIP, err = aghnet.IfaceHasStaticIP(ifaceName)
if err != nil {
if errors.Is(err, os.ErrPermission) {
// ErrPermission may happen here on Linux systems where
Expand All @@ -110,7 +109,7 @@ func (s *Server) enableDHCP(ifaceName string) (code int, err error) {
log.Info("error while checking static ip: %s; "+
"assuming machine has static ip and going on", err)
hasStaticIP = true
} else if errors.Is(err, sysutil.ErrNoStaticIPInfo) {
} else if errors.Is(err, aghnet.ErrNoStaticIPInfo) {
// Couldn't obtain a definitive answer. Assume static
// IP an go on.
log.Info("can't check for static ip; " +
Expand All @@ -124,7 +123,7 @@ func (s *Server) enableDHCP(ifaceName string) (code int, err error) {
}

if !hasStaticIP {
err = sysutil.IfaceSetStaticIP(ifaceName)
err = aghnet.IfaceSetStaticIP(ifaceName)
if err != nil {
err = fmt.Errorf("setting static ip: %w", err)

Expand Down Expand Up @@ -267,7 +266,7 @@ type netInterfaceJSON struct {
func (s *Server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
response := map[string]netInterfaceJSON{}

ifaces, err := util.GetValidNetInterfaces()
ifaces, err := aghnet.GetValidNetInterfaces()
if err != nil {
httpError(r, w, http.StatusInternalServerError, "Couldn't get interfaces: %s", err)
return
Expand Down Expand Up @@ -317,7 +316,7 @@ func (s *Server) handleDHCPInterfaces(w http.ResponseWriter, r *http.Request) {
}
}
if len(jsonIface.Addrs4)+len(jsonIface.Addrs6) != 0 {
jsonIface.GatewayIP = sysutil.GatewayIP(iface.Name)
jsonIface.GatewayIP = aghnet.GatewayIP(iface.Name)
response[iface.Name] = jsonIface
}
}
Expand Down Expand Up @@ -397,13 +396,13 @@ func (s *Server) handleDHCPFindActiveServer(w http.ResponseWriter, r *http.Reque

found4, err4 := CheckIfOtherDHCPServersPresentV4(interfaceName)

isStaticIP, err := sysutil.IfaceHasStaticIP(interfaceName)
isStaticIP, err := aghnet.IfaceHasStaticIP(interfaceName)
if err != nil {
result.V4.StaticIP.Static = "error"
result.V4.StaticIP.Error = err.Error()
} else if !isStaticIP {
result.V4.StaticIP.Static = "no"
result.V4.StaticIP.IP = util.GetSubnet(interfaceName).String()
result.V4.StaticIP.IP = aghnet.GetSubnet(interfaceName).String()
}

if found4 {
Expand Down
Loading

0 comments on commit fb00247

Please sign in to comment.