From 535b5a1c8cd19b5132124ec6a2ef48db80528994 Mon Sep 17 00:00:00 2001 From: Tim Perry Date: Wed, 5 Aug 2026 11:38:59 +0200 Subject: [PATCH] Wait for libnetwork on iOS in case to make connect script more reliable This notably applied in simulator testing, but presumably could also be required on devices in some real scenarios too. --- ios/ios-connect-hook.js | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/ios/ios-connect-hook.js b/ios/ios-connect-hook.js index 8349b2e..748ffb7 100644 --- a/ios/ios-connect-hook.js +++ b/ios/ios-connect-hook.js @@ -18,24 +18,27 @@ * SPDX-FileCopyrightText: Tim Perry */ -// This is the method we're going to patch: -// https://developer.apple.com/documentation/network/2976677-nw_connection_create (iOS 12+) -const libnetwork = Process.getModuleByName('libnetwork.dylib'); -const nw_connection_create = libnetwork.getExportByName('nw_connection_create'); +waitForModule('libnetwork.dylib', (libnetwork) => { + // This is the method we're going to patch: + // https://developer.apple.com/documentation/network/2976677-nw_connection_create (iOS 12+) + const nw_connection_create = libnetwork.getExportByName('nw_connection_create'); -// This is the method to make a new endpoint to connect to: -// https://developer.apple.com/documentation/network/2976720-nw_endpoint_create_host (iOS 12+) -const nw_endpoint_create_host = new NativeFunction( - libnetwork.findExportByName('nw_endpoint_create_host'), - 'pointer', ['pointer', 'pointer'] -); + // This is the method to make a new endpoint to connect to: + // https://developer.apple.com/documentation/network/2976720-nw_endpoint_create_host (iOS 12+) + const nw_endpoint_create_host = new NativeFunction( + libnetwork.findExportByName('nw_endpoint_create_host'), + 'pointer', ['pointer', 'pointer'] + ); -const newHostStr = Memory.allocUtf8String(PROXY_HOST); -const newPortStr = Memory.allocUtf8String(PROXY_PORT.toString()); + const newHostStr = Memory.allocUtf8String(PROXY_HOST); + const newPortStr = Memory.allocUtf8String(PROXY_PORT.toString()); -Interceptor.attach(nw_connection_create, { - onEnter: function (args) { - // Replace the endpoint argument entirely with our own: - args[0] = nw_endpoint_create_host(newHostStr, newPortStr); - } + Interceptor.attach(nw_connection_create, { + onEnter: function (args) { + // Replace the endpoint argument entirely with our own: + args[0] = nw_endpoint_create_host(newHostStr, newPortStr); + } + }); + + console.log(`== Redirecting Network framework connections to ${PROXY_HOST}:${PROXY_PORT} ==`); }); \ No newline at end of file