[object Object]

← back to Exo

macos interface type

02ee8b7d9a7797a827772e2a521d2273a1fe266f · 2024-12-06 23:30:20 +0000 · Alex Cheema

Files touched

Diff

commit 02ee8b7d9a7797a827772e2a521d2273a1fe266f
Author: Alex Cheema <alexcheema123@gmail.com>
Date:   Fri Dec 6 23:30:20 2024 +0000

    macos interface type
---
 exo/helpers.py                      | 59 +++++++++++++++++++++++++------------
 exo/networking/udp/udp_discovery.py |  2 +-
 2 files changed, 41 insertions(+), 20 deletions(-)

diff --git a/exo/helpers.py b/exo/helpers.py
index da253b21..6dd61daa 100644
--- a/exo/helpers.py
+++ b/exo/helpers.py
@@ -236,7 +236,43 @@ def get_all_ip_addresses_and_interfaces():
     if DEBUG >= 1: print("Failed to get all IP addresses. Defaulting to localhost.")
     return [("localhost", "lo")]
 
-def get_interface_priority_and_type(ifname: str) -> Tuple[int, str]:
+async def get_macos_interface_type(ifname: str) -> Optional[Tuple[int, str]]:
+  try:
+    proc = await asyncio.create_subprocess_exec('networksetup', '-listallhardwareports',
+      stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE)
+    output, _ = await proc.communicate()
+    output = output.decode('utf-8')
+
+    # Parse the output into blocks
+    blocks = output.split('\n\n')
+    for block in blocks:
+      lines = block.strip().split('\n')
+      if len(lines) < 2:
+        continue
+
+      # Get the Hardware Port and Device lines
+      hw_port = lines[0].split(': ', 1)[1] if ': ' in lines[0] else ''
+      device = lines[1].split(': ', 1)[1] if ': ' in lines[1] else ''
+
+      if device == ifname:
+        # Thunderbolt interfaces
+        if 'Thunderbolt' in hw_port:
+          return (5, "Thunderbolt/10GbE")
+
+        # Ethernet adapters
+        if 'Ethernet Adapter' in hw_port:
+          return (4, "Ethernet")
+
+        # WiFi
+        if hw_port == 'Wi-Fi':
+          return (3, "WiFi")
+
+  except Exception as e:
+    if DEBUG >= 2: print(f"Error detecting macOS interface type: {e}")
+
+  return None
+
+async def get_interface_priority_and_type(ifname: str) -> Tuple[int, str]:
   # Local container/virtual interfaces
   if (ifname.startswith(('docker', 'br-', 'veth', 'cni', 'flannel', 'calico', 'weave')) or
     'bridge' in ifname):
@@ -246,25 +282,10 @@ def get_interface_priority_and_type(ifname: str) -> Tuple[int, str]:
   if ifname.startswith('lo'):
     return (6, "Loopback")
 
-  # On macOS, use networksetup to accurately identify interface types
+  # On macOS, try to get interface type using networksetup
   if psutil.MACOS:
-    try:
-      import subprocess
-      result = subprocess.run(['networksetup', '-listallhardwareports'], capture_output=True, text=True)
-      output = result.stdout
-
-      # Find the hardware port info for this interface
-      for block in output.split('\n\n'):
-        if f'Device: {ifname}' in block:
-          if 'Ethernet' in block or 'LAN' in block:
-            return (4, "Ethernet")
-          elif 'Wi-Fi' in block or 'Airport' in block:
-            return (3, "WiFi")
-          elif 'Thunderbolt' in block:
-            return (5, "Thunderbolt/10GbE")
-    except Exception as e:
-      if DEBUG >= 2:
-        print(f"Error detecting macOS interface type: {e}")
+    macos_type = await get_macos_interface_type(ifname)
+    if macos_type is not None: return macos_type
 
   # Traditional detection for non-macOS systems or fallback
   if ifname.startswith(('tb', 'nx', 'ten')):
diff --git a/exo/networking/udp/udp_discovery.py b/exo/networking/udp/udp_discovery.py
index 624933d2..1909b1f4 100644
--- a/exo/networking/udp/udp_discovery.py
+++ b/exo/networking/udp/udp_discovery.py
@@ -88,7 +88,7 @@ class UDPDiscovery(Discovery):
       # Explicitly broadcasting on all assigned ips since broadcasting on `0.0.0.0` on MacOS does not broadcast over
       # the Thunderbolt bridge when other connection modalities exist such as WiFi or Ethernet
       for addr, interface_name in get_all_ip_addresses_and_interfaces():
-        interface_priority, interface_type = get_interface_priority_and_type(interface_name)
+        interface_priority, interface_type = await get_interface_priority_and_type(interface_name)
         message = json.dumps({
           "type": "discovery",
           "node_id": self.node_id,

← f4619d46 use psutil for mac detection  ·  back to Exo  ·  viz positioning of inteface description 5f6625a7 →