cmd/ctrld: use network service on darwin

This commit is contained in:
Cuong Manh Le
2023-01-19 12:24:21 +07:00
committed by Cuong Manh Le
parent 05cfb9b661
commit 47c280cf1d
3 changed files with 44 additions and 0 deletions

View File

@@ -613,5 +613,8 @@ func netIfaceFromName(ifaceName string) (*net.Interface, error) {
if iface == nil {
return nil, errors.New("interface not found")
}
if err := patchNetIfaceName(iface); err != nil {
return nil, err
}
return iface, err
}

34
cmd/ctrld/net_darwin.go Normal file
View File

@@ -0,0 +1,34 @@
package main
import (
"bufio"
"bytes"
"net"
"os/exec"
"strings"
)
func patchNetIfaceName(iface *net.Interface) error {
b, err := exec.Command("networksetup", "-listnetworkserviceorder").Output()
if err != nil {
return err
}
scanner := bufio.NewScanner(bytes.NewReader(b))
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "*") {
// Network services is disabled.
continue
}
if !strings.Contains(line, "Device: "+iface.Name) {
continue
}
parts := strings.Split(line, ",")
if _, networkServiceName, ok := strings.Cut(parts[0], "(Hardware Port: "); ok {
mainLog.Debug().Str("network_service", networkServiceName).Msg("found network service name for interface")
iface.Name = networkServiceName
}
}
return nil
}

7
cmd/ctrld/net_others.go Normal file
View File

@@ -0,0 +1,7 @@
//go:build !darwin
package main
import "net"
func patchNetIfaceName(iface *net.Interface) error { return nil }