mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-02-03 22:18:39 +00:00
Fix parsing network service name on darwin
The network service name appears on the previous line, not the same line with "Device" name. Updates #57
This commit is contained in:
committed by
Cuong Manh Le
parent
4c2d21a8f8
commit
64f2dcb25b
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
@@ -14,21 +15,30 @@ func patchNetIfaceName(iface *net.Interface) error {
|
||||
return err
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(bytes.NewReader(b))
|
||||
if name := networkServiceName(iface.Name, bytes.NewReader(b)); name != "" {
|
||||
iface.Name = name
|
||||
mainLog.Debug().Str("network_service", name).Msg("found network service name for interface")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func networkServiceName(ifaceName string, r io.Reader) string {
|
||||
scanner := bufio.NewScanner(r)
|
||||
prevLine := ""
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.Contains(line, "*") {
|
||||
// Network services is disabled.
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(line, "Device: "+iface.Name) {
|
||||
if !strings.Contains(line, "Device: "+ifaceName) {
|
||||
prevLine = line
|
||||
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
|
||||
parts := strings.SplitN(prevLine, " ", 2)
|
||||
if len(parts) == 2 {
|
||||
return strings.TrimSpace(parts[1])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return ""
|
||||
}
|
||||
|
||||
59
cmd/ctrld/net_darwin_test.go
Normal file
59
cmd/ctrld/net_darwin_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const listnetworkserviceorderOutput = `
|
||||
(1) USB 10/100/1000 LAN 2
|
||||
(Hardware Port: USB 10/100/1000 LAN, Device: en7)
|
||||
|
||||
(2) Ethernet
|
||||
(Hardware Port: Ethernet, Device: en0)
|
||||
|
||||
(3) Wi-Fi
|
||||
(Hardware Port: Wi-Fi, Device: en1)
|
||||
|
||||
(4) Bluetooth PAN
|
||||
(Hardware Port: Bluetooth PAN, Device: en4)
|
||||
|
||||
(5) Thunderbolt Bridge
|
||||
(Hardware Port: Thunderbolt Bridge, Device: bridge0)
|
||||
|
||||
(6) kernal
|
||||
(Hardware Port: com.wireguard.macos, Device: )
|
||||
|
||||
(7) WS BT
|
||||
(Hardware Port: com.wireguard.macos, Device: )
|
||||
|
||||
(8) ca-001-stg
|
||||
(Hardware Port: com.wireguard.macos, Device: )
|
||||
|
||||
(9) ca-001-stg-2
|
||||
(Hardware Port: com.wireguard.macos, Device: )
|
||||
|
||||
`
|
||||
|
||||
func Test_networkServiceName(t *testing.T) {
|
||||
tests := []struct {
|
||||
ifaceName string
|
||||
networkServiceName string
|
||||
}{
|
||||
{"en7", "USB 10/100/1000 LAN 2"},
|
||||
{"en0", "Ethernet"},
|
||||
{"en1", "Wi-Fi"},
|
||||
{"en4", "Bluetooth PAN"},
|
||||
{"bridge0", "Thunderbolt Bridge"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
tc := tc
|
||||
t.Run(tc.ifaceName, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
name := networkServiceName(tc.ifaceName, strings.NewReader(listnetworkserviceorderOutput))
|
||||
assert.Equal(t, tc.networkServiceName, name)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user