Run a virtual iPhone on an Apple Silicon Mac.
Swift
window: Show guest IP in subtitle once vphoned connects
zqxwce77d4a04c554e
diff --git a/scripts/vphoned/vphoned.m b/scripts/vphoned/vphoned.mindex 30166096..500cb124 100644--- a/scripts/vphoned/vphoned.m+++ b/scripts/vphoned/vphoned.m@@ -15,7 +15,11 @@#include <CommonCrypto/CommonDigest.h>#import <Foundation/Foundation.h>+#include <arpa/inet.h>+#include <ifaddrs.h>#include <mach-o/dyld.h>+#include <net/if.h>+#include <netinet/in.h>#include <sys/socket.h>#include <sys/stat.h>#include <unistd.h>@@ -94,6 +98,44 @@ static const char *self_executable_path(void) {return path;}+// MARK: - Network++/// Returns the first non-loopback IPv4 address, preferring en* interfaces+/// (Wi-Fi/cellular over virtual). Returns nil if no usable address is found.+static NSString *primary_ipv4_address(void) {+ struct ifaddrs *ifap = NULL;+ if (getifaddrs(&ifap) != 0 || ifap == NULL)+ return nil;++ NSString *preferred = nil;+ NSString *fallback = nil;++ for (struct ifaddrs *cur = ifap; cur != NULL; cur = cur->ifa_next) {+ if (cur->ifa_addr == NULL || cur->ifa_addr->sa_family != AF_INET)+ continue;+ if ((cur->ifa_flags & IFF_UP) == 0 ||+ (cur->ifa_flags & IFF_LOOPBACK) != 0)+ continue;++ char buf[INET_ADDRSTRLEN] = {0};+ struct sockaddr_in *sin = (struct sockaddr_in *)cur->ifa_addr;+ if (!inet_ntop(AF_INET, &sin->sin_addr, buf, sizeof(buf)))+ continue;++ NSString *addr = [NSString stringWithUTF8String:buf];+ NSString *name = [NSString stringWithUTF8String:cur->ifa_name];+ if ([name hasPrefix:@"en"] || [name hasPrefix:@"pdp_ip"]) {+ preferred = addr;+ break;+ }+ if (!fallback)+ fallback = addr;+ }++ freeifaddrs(ifap);+ return preferred ?: fallback;+}+// MARK: - Auto-update/// Receive raw binary from host, write to CACHE_PATH, chmod +x.@@ -295,6 +337,9 @@ static BOOL handle_client(int fd) {@"name" : @"vphoned",@"caps" : caps,} mutableCopy];+ NSString *ip = primary_ipv4_address();+ if (ip)+ helloResp[@"ip"] = ip;if (needUpdate)helloResp[@"need_update"] = @YES;diff --git a/sources/vphone-cli/VPhoneControl.swift b/sources/vphone-cli/VPhoneControl.swiftindex c9844156..49c0a17d 100644--- a/sources/vphone-cli/VPhoneControl.swift+++ b/sources/vphone-cli/VPhoneControl.swift@@ -27,6 +27,7 @@ class VPhoneControl {private(set) var isConnected = falseprivate(set) var guestName = ""private(set) var guestCaps: [String] = []+ private(set) var guestIP: String?/// Path to the signed vphoned binary. When set, enables auto-update.var guestBinaryURL: URL?@@ -199,6 +200,7 @@ class VPhoneControl {let type = resp["t"] as? String ?? ""let name = resp["name"] as? String ?? "unknown"let caps = resp["caps"] as? [String] ?? []+ let ip = resp["ip"] as? Stringlet needUpdate = resp["need_update"] as? Bool ?? falseTask { @MainActor in@@ -213,8 +215,10 @@ class VPhoneControl {}self.guestName = nameself.guestCaps = caps+ self.guestIP = ipself.isConnected = true- print("[control] connected to \(name) v\(version), caps: \(caps)")+ let ipSuffix = ip.map { " (\($0))" } ?? ""+ print("[control] connected to \(name) v\(version)\(ipSuffix), caps: \(caps)")if needUpdate && self.variant != .less {self.pushUpdate(fd: fd)@@ -728,6 +732,7 @@ class VPhoneControl {isConnected = falseguestName = ""guestCaps = []+ guestIP = nil// Fail all pending requestsfailAllPending()diff --git a/sources/vphone-cli/VPhoneWindowController.swift b/sources/vphone-cli/VPhoneWindowController.swiftindex 56df54fd..d5ec7cf6 100644--- a/sources/vphone-cli/VPhoneWindowController.swift+++ b/sources/vphone-cli/VPhoneWindowController.swift@@ -9,6 +9,7 @@ class VPhoneWindowController: NSObject, NSToolbarDelegate {private weak var control: VPhoneControl?private weak var virtualMachineView: VPhoneVirtualMachineView?private(set) var touchIDMonitor: VPhoneTouchIDMonitor?+ private var ecid: String?private nonisolated static let homeItemID = NSToolbarItem.Identifier("home")@@ -21,6 +22,7 @@ class VPhoneWindowController: NSObject, NSToolbarDelegate {keyHelper: VPhoneKeyHelper, control: VPhoneControl, ecid: String?) {self.control = control+ self.ecid = ecidlet view = VPhoneVirtualMachineView()view.virtualMachine = vm@@ -45,7 +47,7 @@ class VPhoneWindowController: NSObject, NSToolbarDelegate {window.isReleasedWhenClosed = falsewindow.contentAspectRatio = windowSizewindow.title = "VPHONE [loading]"- window.subtitle = ecid ?? ""+ window.subtitle = makeSubtitle(ip: nil)window.contentView = vmViewif let ecid {if !window.setFrameAutosaveName("vphone-\(ecid)") {@@ -81,10 +83,20 @@ class VPhoneWindowController: NSObject, NSToolbarDelegate {Task { @MainActor inguard let self, let window, let control = self.control else { return }window.title = control.isConnected ? "VPHONE [connected]" : "VPHONE [disconnected]"+ window.subtitle = self.makeSubtitle(ip: control.isConnected ? control.guestIP : nil)}}}+ private func makeSubtitle(ip: String?) -> String {+ switch (ecid, ip) {+ case let (ecid?, ip?): "\(ecid) — \(ip)"+ case let (ecid?, nil): ecid+ case let (nil, ip?): ip+ case (nil, nil): ""+ }+ }+// MARK: - NSToolbarDelegatenonisolated func toolbar(