mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-08-10 13:20:33 +02: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:
+17
-7
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -14,21 +15,30 @@ func patchNetIfaceName(iface *net.Interface) error {
|
|||||||
return err
|
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() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
if strings.Contains(line, "*") {
|
if strings.Contains(line, "*") {
|
||||||
// Network services is disabled.
|
// Network services is disabled.
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !strings.Contains(line, "Device: "+iface.Name) {
|
if !strings.Contains(line, "Device: "+ifaceName) {
|
||||||
|
prevLine = line
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
parts := strings.Split(line, ",")
|
parts := strings.SplitN(prevLine, " ", 2)
|
||||||
if _, networkServiceName, ok := strings.Cut(parts[0], "(Hardware Port: "); ok {
|
if len(parts) == 2 {
|
||||||
mainLog.Debug().Str("network_service", networkServiceName).Msg("found network service name for interface")
|
return strings.TrimSpace(parts[1])
|
||||||
iface.Name = networkServiceName
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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