mirror of
https://github.com/weaveworks/scope.git
synced 2026-07-20 22:10:30 +00:00
refactor: put all network detection code in one place
This commit is contained in:
@@ -2,7 +2,6 @@ package host
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -86,19 +85,7 @@ func NewReporter(hostID, hostName, probeID, version string, pipes controls.PipeC
|
||||
func (*Reporter) Name() string { return "Host" }
|
||||
|
||||
// GetLocalNetworks is exported for mocking
|
||||
var GetLocalNetworks = func() ([]*net.IPNet, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
localNets := report.Networks{}
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
|
||||
localNets = append(localNets, ipnet)
|
||||
}
|
||||
}
|
||||
return localNets, nil
|
||||
}
|
||||
var GetLocalNetworks = report.GetLocalNetworks
|
||||
|
||||
// Report implements Reporter.
|
||||
func (r *Reporter) Report() (report.Report, error) {
|
||||
|
||||
@@ -82,3 +82,18 @@ func AddLocalBridge(name string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLocalNetworks returns all the local networks.
|
||||
func GetLocalNetworks() ([]*net.IPNet, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
localNets := Networks{}
|
||||
for _, addr := range addrs {
|
||||
if ipnet, ok := addr.(*net.IPNet); ok && ipnet.IP.To4() != nil {
|
||||
localNets = append(localNets, ipnet)
|
||||
}
|
||||
}
|
||||
return localNets, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user