Skip to content

Commit

Permalink
darwin: network: add power on/off for wifi
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Feb 15, 2022
1 parent a931f7e commit 1521774
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/ents.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
<true/>
<key>com.apple.wlan.authentication</key>
<true/>
<key>com.apple.wifi.manager-access</key>
<true/>
</dict>
</plist>
5 changes: 4 additions & 1 deletion src/rpcclient/rpcclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ def call(self, address: int, argv: typing.List[int] = None) -> Symbol:
for arg in argv:
tmp = arg

if isinstance(arg, str):
if isinstance(arg, bool):
tmp = int(arg)

elif isinstance(arg, str):
tmp = self.symbols.malloc(len(arg) + 1)
tmp.poke(arg.encode() + b'\0')
free_list.append(tmp)
Expand Down
66 changes: 61 additions & 5 deletions src/rpcclient/rpcclient/darwin/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
from typing import List

from rpcclient.exceptions import BadReturnValueError
from rpcclient.exceptions import BadReturnValueError, RpcClientException
from rpcclient.network import Network

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -47,9 +47,11 @@ def __repr__(self):


class WifiInterface:
def __init__(self, client, interface):
def __init__(self, client, interface, wifi_manager_client, device):
self._client = client
self._interface = interface
self._wifi_manager_client = wifi_manager_client
self._device = device

def __enter__(self):
return self
Expand All @@ -73,6 +75,27 @@ def disconnect(self):
""" disconnect from current wifi network """
self._client.symbols.Apple80211Disassociate(self._interface)

def _set(self, is_on: bool):
if not is_on:
if self._client.symbols.WiFiManagerClientDisable(self._wifi_manager_client):
raise BadReturnValueError(f'WiFiManagerClientDisable failed ({self._client.last_error})')

if self._client.symbols.WiFiDeviceClientSetPower(self._device, is_on):
raise BadReturnValueError(f'WiFiDeviceClientSetPower failed ({self._client.last_error})')

if is_on:
if self._client.symbols.WiFiManagerClientEnable(self._wifi_manager_client):
raise BadReturnValueError(f'WiFiManagerClientEnable failed ({self._client.last_error})')

def turn_on(self):
self._set(True)

def turn_off(self):
self._set(False)

def is_on(self) -> bool:
return self._client.symbols.WiFiDeviceClientGetPower(self._device) == 1

def close(self):
self._client.symbols.Apple80211Close(self._interface)

Expand All @@ -89,16 +112,49 @@ def _load_wifi_library(self):
'/System/Library/Frameworks/CoreWLAN.framework/CoreWLAN',
# iOS
'/System/Library/PrivateFrameworks/WiFiKit.framework/WiFiKit'
]
]
for option in options:
if self._client.dlopen(option, 2):
return
logger.warning('WiFi library isn\'t available')

def get_wifi_interface(self, interface_name: str) -> WifiInterface:
@property
def wifi_interfaces(self) -> List[str]:
""" get a list of all available wifi interfaces """
with self._client.safe_malloc(8) as p_interface:
if self._client.symbols.Apple80211Open(p_interface):
raise BadReturnValueError(f'Apple80211Open failed ({self._client.last_error})')

with self._client.safe_malloc(8) as p_interface_names:
if self._client.symbols.Apple80211GetIfListCopy(p_interface[0], p_interface_names):
raise BadReturnValueError(f'Apple80211GetIfListCopy failed ({self._client.last_error})')

return p_interface_names[0].py

def get_wifi_interface(self, interface_name: str = None) -> WifiInterface:
""" get a specific wifi interface object for remote controlling """
with self._client.safe_malloc(8) as p_interface:
if self._client.symbols.Apple80211Open(p_interface):
raise BadReturnValueError(f'Apple80211Open failed ({self._client.last_error})')

if interface_name is None:
wifi_interfaces = self.wifi_interfaces

if not wifi_interfaces:
raise RpcClientException('no available wifi interfaces were found')

interface_name = wifi_interfaces[0]

if self._client.symbols.Apple80211BindToInterface(p_interface[0], self._client.cf(interface_name)):
raise BadReturnValueError(f'Apple80211BindToInterface failed ({self._client.last_error})')
return WifiInterface(self._client, p_interface[0])

wifi_manager_client = self._client.symbols.WiFiManagerClientCreate(0, 0)
if not wifi_manager_client:
raise BadReturnValueError('WiFiManagerClientCreate failed')

device = self._client.symbols.WiFiManagerClientGetDevice(wifi_manager_client,
self._client.cf(interface_name))
if not device:
raise BadReturnValueError('WiFiManagerClientGetDevice failed')

return WifiInterface(self._client, p_interface[0], wifi_manager_client, device)

0 comments on commit 1521774

Please sign in to comment.