Skip to content

Commit

Permalink
fruity: Handle devices without tunnel support on non-macOS
Browse files Browse the repository at this point in the history
In the portable CoreDevice backend.
  • Loading branch information
oleavr committed Jul 22, 2024
1 parent 6b56c88 commit e34a533
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/fruity/device-monitor-macos.vala
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ namespace Frida.Fruity {
}
}

public async Tunnel? find_tunnel (Cancellable? cancellable) throws Error, IOError {
public async Tunnel? find_tunnel (UsbmuxDevice? device, Cancellable? cancellable) throws Error, IOError {
while (tunnel_request != null) {
try {
return yield tunnel_request.future.wait_async (cancellable);
Expand Down
38 changes: 30 additions & 8 deletions src/fruity/device-monitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,9 @@ namespace Frida.Fruity {
}

public async Tunnel? find_tunnel (Cancellable? cancellable) throws Error, IOError {
var usbmux_device = find_usbmux_device ();
foreach (var transport in transports) {
Tunnel? tunnel = yield transport.find_tunnel (cancellable);
Tunnel? tunnel = yield transport.find_tunnel (usbmux_device, cancellable);
if (tunnel != null)
return tunnel;
}
Expand Down Expand Up @@ -438,7 +439,7 @@ namespace Frida.Fruity {
get;
}

public abstract async Tunnel? find_tunnel (Cancellable? cancellable) throws Error, IOError;
public abstract async Tunnel? find_tunnel (UsbmuxDevice? device, Cancellable? cancellable) throws Error, IOError;
}

public enum ConnectionType {
Expand Down Expand Up @@ -699,7 +700,7 @@ namespace Frida.Fruity {
Object (device: device);
}

public async Tunnel? find_tunnel (Cancellable? cancellable) throws Error, IOError {
public async Tunnel? find_tunnel (UsbmuxDevice? device, Cancellable? cancellable) throws Error, IOError {
return null;
}
}
Expand Down Expand Up @@ -1066,7 +1067,8 @@ namespace Frida.Fruity {
if (tunnel_request != null) {
try {
var tunnel = yield tunnel_request.future.wait_async (cancellable);
yield tunnel.close (cancellable);
if (tunnel != null)
yield tunnel.close (cancellable);
} catch (Error e) {
} finally {
tunnel_request = null;
Expand All @@ -1076,7 +1078,7 @@ namespace Frida.Fruity {
usb_device.close ();
}

public async Tunnel? find_tunnel (Cancellable? cancellable) throws Error, IOError {
public async Tunnel? find_tunnel (UsbmuxDevice? device, Cancellable? cancellable) throws Error, IOError {
while (tunnel_request != null) {
try {
return yield tunnel_request.future.wait_async (cancellable);
Expand All @@ -1089,8 +1091,28 @@ namespace Frida.Fruity {
tunnel_request = new Promise<Tunnel> ();

try {
var tunnel = new PortableUsbTunnel (usb_device, pairing_store);
yield tunnel.open (cancellable);
bool supported_by_os = true;
if (device != null) {
var lockdown = yield LockdownClient.open (device, cancellable);
yield lockdown.start_session (cancellable);
var response = yield lockdown.get_value (null, null, cancellable);
Fruity.PlistDict properties = response.get_dict ("Value");
if (properties.get_string ("ProductName") == "iPhone OS") {
uint ios_major_version = uint.parse (properties.get_string ("ProductVersion").split (".")[0]);
supported_by_os = ios_major_version >= 17;
}
}

PortableUsbTunnel? tunnel = null;
if (supported_by_os) {
tunnel = new PortableUsbTunnel (usb_device, pairing_store);
try {
yield tunnel.open (cancellable);
} catch (Error e) {
if (!(e is Error.NOT_SUPPORTED))
throw e;
}
}

tunnel_request.resolve (tunnel);

Expand Down Expand Up @@ -1460,7 +1482,7 @@ namespace Frida.Fruity {
}
}

public async Tunnel? find_tunnel (Cancellable? cancellable) throws Error, IOError {
public async Tunnel? find_tunnel (UsbmuxDevice? device, Cancellable? cancellable) throws Error, IOError {
while (tunnel_request != null) {
try {
return yield tunnel_request.future.wait_async (cancellable);
Expand Down
14 changes: 7 additions & 7 deletions src/fruity/ncm.vala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace Frida.Fruity {
}
}
if (!found_cdc_header || !found_data_interface)
throw_user_error ("No USB CDC-NCM interface found");
throw new Error.NOT_SUPPORTED ("%s", make_user_error_message ("No USB CDC-NCM interface found"));

uint8 mac_address[6];
string mac_address_str = yield device.read_string_descriptor (mac_address_index, device.default_language_id,
Expand All @@ -149,7 +149,8 @@ namespace Frida.Fruity {
try {
Usb.check (handle.claim_interface (data_iface), "Failed to claim USB interface");
} catch (Error e) {
throw_user_error (@"Unable to claim USB CDC-NCM interface ($(e.message))");
throw new Error.PERMISSION_DENIED ("%s",
make_user_error_message (@"Unable to claim USB CDC-NCM interface ($(e.message))"));
}
Usb.check (handle.set_interface_alt_setting (data_iface, data_altsetting),
"Failed to set USB interface alt setting");
Expand All @@ -162,13 +163,12 @@ namespace Frida.Fruity {
return true;
}

[NoReturn]
private void throw_user_error (string message) throws Error {
private string make_user_error_message (string message) {
#if WINDOWS
throw new Error.NOT_SUPPORTED ("%s; use https://zadig.akeo.ie to switch from Apple's official driver onto " +
"Microsoft's WinUSB driver, so libusb can access it", message);
return message + "; use https://zadig.akeo.ie to switch from Apple's official driver onto Microsoft's WinUSB " +
"driver, so libusb can access it";
#else
throw new Error.NOT_SUPPORTED ("%s", message);
return message;
#endif
}

Expand Down

0 comments on commit e34a533

Please sign in to comment.