-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
342 lines (297 loc) · 10.4 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using iMobileDevice;
using iMobileDevice.iDevice;
using iMobileDevice.Lockdown;
using iMobileDevice.Plist;
class Program
{
private static readonly string DeveloperDiskImagesPath = "DeveloperDiskImages";
static void Main(string[] args)
{
NativeLibraries.Load();
bool exitProgram = false;
while (!exitProgram)
{
DisplayMainMenu();
string choice = Console.ReadLine();
switch (choice)
{
case "1":
DetectAndHandleDevice();
break;
case "2":
DisableSimulatedLocation();
break;
case "3":
exitProgram = true;
Console.WriteLine("Exiting program. Goodbye!");
break;
case "help":
DisplayHelp();
break;
default:
Console.WriteLine("Invalid choice. Please enter 1, 2 or 'help'.");
break;
}
}
}
static void DisableSimulatedLocation()
{
var udids = ListConnectedDevices();
if (udids.Count == 0)
{
Console.WriteLine("No devices connected. Please connect a device.");
PromptRetry();
}
else
{
string selectedUdid = udids.Count == 1 ? udids[0] : GetUserSelectedUdid(udids);
ResetLocation(selectedUdid);
}
}
static void ResetLocation(string udid)
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "idevicesetlocation",
Arguments = $"--udid {udid} reset",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
using (StreamReader reader = process.StandardError)
{
string error = reader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error: " + error);
}
}
}
Console.WriteLine("Simulated location has been disabled.");
Console.WriteLine("Press any key to return to the main menu.");
Console.ReadKey();
}
static void DisplayMainMenu()
{
Console.Clear();
Console.WriteLine("Welcome to IOSLocationSimulator");
Console.WriteLine("1. Connect a device");
Console.WriteLine("2. Reset Location");
Console.WriteLine("3. Exit");
Console.WriteLine("Type 'help' for instructions.");
Console.Write("Enter your choice: ");
}
static void DisplayHelp()
{
Console.Clear();
Console.WriteLine("Help Guide:");
Console.WriteLine("1 - Connect your iOS device to set a new location.");
Console.WriteLine("2 - Exit the program.");
Console.WriteLine("Follow the on-screen instructions for each option.");
Console.WriteLine("Press any key to return to the main menu.");
Console.ReadKey();
}
static void DetectAndHandleDevice()
{
var udids = ListConnectedDevices();
if (udids.Count == 0)
{
Console.WriteLine("No devices connected. Please connect a device.");
PromptRetry();
}
else
{
try
{
string selectedUdid = udids.Count == 1 ? udids[0] : GetUserSelectedUdid(udids);
string iosVersion = GetiOSVersion(selectedUdid);
if (!string.IsNullOrEmpty(iosVersion) && iosVersion.StartsWith("16"))
{
MountDeveloperDiskImage(selectedUdid, iosVersion);
}
SetLocationWithInput(selectedUdid);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
PromptRetry();
}
}
}
static void PromptRetry()
{
Console.WriteLine("Press any key to try again.");
Console.ReadKey();
Console.Clear();
}
static void SetLocationWithInput(string udid)
{
Console.WriteLine("Enter the Latitude (e.g., 37.7749):");
if (!double.TryParse(Console.ReadLine(), NumberStyles.Any, CultureInfo.InvariantCulture, out double latitude))
{
Console.WriteLine("Invalid latitude format. Please enter a valid number.");
return;
}
Console.WriteLine("Enter the Longitude (e.g., -122.4194):");
if (!double.TryParse(Console.ReadLine(), NumberStyles.Any, CultureInfo.InvariantCulture, out double longitude))
{
Console.WriteLine("Invalid longitude format. Please enter a valid number.");
return;
}
SetLocation(udid, latitude, longitude);
}
static List<string> ListConnectedDevices()
{
List<string> deviceUdids = new List<string>();
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var lockdown = LibiMobileDevice.Instance.Lockdown;
var ret = idevice.idevice_get_device_list(out udids, ref count);
if (ret == iDeviceError.NoDevice)
{
return deviceUdids;
}
ret.ThrowOnError();
foreach (var udid in udids)
{
deviceUdids.Add(udid);
}
return deviceUdids;
}
static string GetUserSelectedUdid(List<string> udids)
{
Console.WriteLine("Multiple devices detected. Please select a device:");
for (int i = 0; i < udids.Count; i++)
{
Console.WriteLine($"{i + 1}. {udids[i]}");
}
Console.Write("Enter the number of the desired device: ");
int choice = Convert.ToInt32(Console.ReadLine());
return udids[choice - 1];
}
static void SetLocation(string udid, double latitude, double longitude)
{
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "idevicesetlocation",
Arguments = $"--udid {udid} -- {latitude.ToString(CultureInfo.InvariantCulture)} {longitude.ToString(CultureInfo.InvariantCulture)}",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
using (StreamReader reader = process.StandardError)
{
string error = reader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error: " + error);
}
}
}
}
static void MountDeveloperDiskImage(string udid, string iosVersion)
{
// Normalize the iOS version (e.g., "16.0.1" to "16.0")
string normalizedVersion = NormalizeVersion(iosVersion);
// Construct the path to the disk image based on the normalized iOS version
string diskImageDirectory = Path.Combine(DeveloperDiskImagesPath, normalizedVersion);
string diskImagePath = Path.Combine(diskImageDirectory, "DeveloperDiskImage.dmg");
string diskImageSignaturePath = diskImagePath + ".signature";
if (!File.Exists(diskImagePath) || !File.Exists(diskImageSignaturePath))
{
Console.WriteLine($"Developer disk image for iOS {normalizedVersion} not found.");
return;
}
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "ideviceimagemounter",
Arguments = $"{diskImagePath} {diskImageSignaturePath}",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.WriteLine(result);
}
using (StreamReader reader = process.StandardError)
{
string error = reader.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Console.WriteLine("Error: " + error);
}
}
}
}
static string NormalizeVersion(string version)
{
// Example: Convert "16.0.1" to "16.0"
var versionParts = version.Split('.');
if (versionParts.Length >= 2)
{
return versionParts[0] + "." + versionParts[1];
}
return version;
}
static string GetiOSVersion(string udid)
{
iDeviceError ideviceError = iDeviceError.NoDevice;
LockdownError lockdownError = LockdownError.InvalidArg;
iDeviceHandle deviceHandle;
LockdownClientHandle lockdownHandle;
string productVersion = string.Empty;
ideviceError = LibiMobileDevice.Instance.iDevice.idevice_new(out deviceHandle, udid);
if (ideviceError != iDeviceError.Success)
{
Console.WriteLine($"Failed to connect to device with UDID: {udid}");
return productVersion;
}
lockdownError = LibiMobileDevice.Instance.Lockdown.lockdownd_client_new_with_handshake(deviceHandle, out lockdownHandle, "GetiOSVersion");
if (lockdownError != LockdownError.Success)
{
Console.WriteLine($"Failed to start lockdown session with device: {udid}");
return productVersion;
}
lockdownError = LibiMobileDevice.Instance.Lockdown.lockdownd_get_value(lockdownHandle, null, "ProductVersion", out PlistHandle versionPlist);
if (lockdownError == LockdownError.Success)
{
versionPlist.Api.Plist.plist_get_string_val(versionPlist, out productVersion);
}
else
{
Console.WriteLine($"Failed to retrieve iOS version from device: {udid}");
}
versionPlist.Dispose();
lockdownHandle.Dispose();
deviceHandle.Dispose();
return productVersion;
}
}