-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwinform.cs
More file actions
593 lines (419 loc) · 20.2 KB
/
mainwinform.cs
File metadata and controls
593 lines (419 loc) · 20.2 KB
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
using iMobileDevice;
using iMobileDevice.DiagnosticsRelay;
using iMobileDevice.iDevice;
using iMobileDevice.Lockdown;
using iMobileDevice.Recovery;
using PurpleBuddy;
using PurpleBuddy.Foundation.Core.Constants.MainWinfrom;
using PurpleBuddy.Primitive.Commands;
using PurpleBuddy.Primitive.Devices;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Constants = PurpleBuddy.Foundation.Core.Constants.MainWinfrom.Constants;
namespace Prizrak
{
public partial class main_form : Form
{
public main_form()
{
InitializeComponent();
NativeLibraries.Load();
}
IDeviceBootCommands ShutdownDeviceCommand = new ShutdownDevice();
IDeviceBootCommands RebootDeviceCommand = new RebootDevice();
IDeviceActivationCommands DeactivateDeviceCommand = new DeactivateDevice();
IDeviceActivationCommands ActivateDeviceCommand = new ActivateDevice();
iMobileNormalCommands BasicNormalModeCommands = new iMobileNormalCommands();
AppleDevices AppleDevice = new AppleDevices();
ModelRegions DeviceRegions = new ModelRegions();
Constants constants = new Constants();
/// <summary>
/// Holds primitive information for each device on connection. <see cref="updateNewDeviceInformation(iDeviceHandle)">
/// </summary>
private struct NormalDeviceInfo
{
public string model;
public ulong capacity;
public string color;
public string udid;
public string imei;
public string iccid;
public string region;
public ListViewItem item;
public string[] data;
}
/// <summary>
/// Updates UI lable with currently connected device count. <see cref="toolstrip_connected_devices_lable"/>
/// </summary>
private async void getDeviceCount()
{
ReadOnlyCollection<string> udids;
int count;
while (true)
{
count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var lockdown = LibiMobileDevice.Instance.Lockdown;
var ret = idevice.idevice_get_device_list(out udids, ref count);
change_via_thread.ControlInvoke(connected_normal_mode_devices_listview, () => toolstrip_connected_devices_lable.Text = "NMM: " + count.ToString());
udids = null;
await Task.Delay(constants.SleepBetweenConnectedDevicesUpdate);
}
}
/// <summary>
/// Fills listview with primitive information of newly connected iOS device which handle is passed by - > <see cref="toolstrip_connected_devices_lable_TextChanged_1(object, EventArgs)"/>
/// </summary>
/// <param name="deviceHandle">handle of the device passed.</param>
private void updateNewDeviceInformation(iDeviceHandle deviceHandle)
{
NormalDeviceInfo DeviceNormalInformation = new NormalDeviceInfo();
DeviceNormalInformation.data = new string[7];
var lockdown = LibiMobileDevice.Instance.Lockdown;
LockdownClientHandle lockdowndevice;
lockdown.lockdownd_client_new_with_handshake(deviceHandle, out lockdowndevice, "Ghost");
try
{
lockdown.lockdownd_get_value(lockdowndevice, null, "ProductType", out var model);
LibiMobileDevice.Instance.Plist.plist_get_string_val(model, out DeviceNormalInformation.model);
}
catch (Exception)
{
DeviceNormalInformation.model = "";
}
if (string.IsNullOrWhiteSpace(DeviceNormalInformation.model))
DeviceNormalInformation.data[0] = "N/A";
else
{
foreach (KeyValuePair<string, string> iphonemake in AppleDevice.AppleDevicesIphones)
{
if (DeviceNormalInformation.model == iphonemake.Key)
{
DeviceNormalInformation.data[0] = iphonemake.Value;
break;
}
}
}
try
{
lockdown.lockdownd_get_value(lockdowndevice, "com.apple.disk_usage.factory", "TotalDiskCapacity", out var storage);
LibiMobileDevice.Instance.Plist.plist_get_uint_val(storage, ref DeviceNormalInformation.capacity);
}
catch (Exception)
{
DeviceNormalInformation.capacity = 0;
}
if (DeviceNormalInformation.capacity == 0)
{
DeviceNormalInformation.data[1] = "N/A";
}
else
DeviceNormalInformation.data[1] = DeviceNormalInformation.capacity.ToString().Split('0')[0];
try
{
lockdown.lockdownd_get_value(lockdowndevice, null, "DeviceEnclosureColor", out var color);
LibiMobileDevice.Instance.Plist.plist_get_string_val(color, out DeviceNormalInformation.color);
}
catch(Exception)
{
DeviceNormalInformation.color = "N/A";
}
if (string.IsNullOrWhiteSpace(DeviceNormalInformation.color))
DeviceNormalInformation.data[2] = "N/A";
else
{
foreach (KeyValuePair<string, string> iphonecolor in AppleDevice.IphoneColors)
{
if (DeviceNormalInformation.color == iphonecolor.Key)
{
DeviceNormalInformation.data[2] = iphonecolor.Value;
break;
}
}
}
try
{
lockdown.lockdownd_get_device_udid(lockdowndevice, out DeviceNormalInformation.udid);
}
catch (Exception)
{
DeviceNormalInformation.udid = "";
}
if (string.IsNullOrWhiteSpace(DeviceNormalInformation.udid))
DeviceNormalInformation.data[3] = "N/A";
else
DeviceNormalInformation.data[3] = DeviceNormalInformation.udid; //.Split('-')[1] = ECID
try
{
lockdown.lockdownd_get_value(lockdowndevice, null, "InternationalMobileEquipmentIdentity", out var d_imei);
LibiMobileDevice.Instance.Plist.plist_get_string_val(d_imei, out DeviceNormalInformation.imei);
}
catch (Exception)
{
DeviceNormalInformation.imei = "";
}
if (string.IsNullOrWhiteSpace(DeviceNormalInformation.imei))
DeviceNormalInformation.data[4] = "N/A";
else
DeviceNormalInformation.data[4] = DeviceNormalInformation.imei;
try
{
lockdown.lockdownd_get_value(lockdowndevice, null, "IntegratedCircuitCardIdentity", out var iccid).ThrowOnError();
LibiMobileDevice.Instance.Plist.plist_get_string_val(iccid, out DeviceNormalInformation.iccid);
}
catch (Exception)
{
DeviceNormalInformation.iccid = "";
}
if (string.IsNullOrWhiteSpace(DeviceNormalInformation.iccid))
DeviceNormalInformation.data[5] = "N/A";
else
DeviceNormalInformation.data[5] = DeviceNormalInformation.iccid;
try
{
lockdown.lockdownd_get_value(lockdowndevice, null, "RegionInfo", out var region);
LibiMobileDevice.Instance.Plist.plist_get_string_val(region, out DeviceNormalInformation.region);
}
catch (Exception)
{
DeviceNormalInformation.region = "";
}
if (string.IsNullOrWhiteSpace(DeviceNormalInformation.imei))
DeviceNormalInformation.data[6] = "N/A";
else
{
foreach (KeyValuePair<string, string> regions in DeviceRegions.DeviceModelRegions)
{
if (DeviceNormalInformation.region.Contains(regions.Key))
{
DeviceNormalInformation.data[6] = regions.Value;
break;
}
}
}
DeviceNormalInformation.item = new ListViewItem(DeviceNormalInformation.data);
try
{
change_via_thread.ControlInvoke(connected_normal_mode_devices_listview, () => connected_normal_mode_devices_listview.BeginUpdate());
change_via_thread.ControlInvoke(connected_normal_mode_devices_listview, () => connected_normal_mode_devices_listview.Items.Add(DeviceNormalInformation.item));
change_via_thread.ControlInvoke(connected_normal_mode_devices_listview, () => connected_normal_mode_devices_listview.EndUpdate());
}
catch (Exception)
{
return;
}
}
private struct MAIN_FORM
{
public Task[] form_load_tasks;
}
private void main_form_Load(object sender, EventArgs e)
{
MAIN_FORM essentials = new MAIN_FORM();
connected_normal_mode_devices_listview.View = View.Details;
connected_normal_mode_devices_listview.ListViewItemSorter = null;
connected_normal_mode_devices_listview.Columns.Add("MODEL", 125);
connected_normal_mode_devices_listview.Columns.Add("CAPACITY", 70);
connected_normal_mode_devices_listview.Columns.Add("COLOR", 70);
connected_normal_mode_devices_listview.Columns.Add("UDID", 180);
connected_normal_mode_devices_listview.Columns.Add("IMEI", 150);
connected_normal_mode_devices_listview.Columns.Add("ICCID", 70);
connected_normal_mode_devices_listview.Columns.Add("REGION", 150);
essentials.form_load_tasks = new Task[1];
essentials.form_load_tasks[0] = Task.Run(() => { getDeviceCount(); });
}
private void main_form_FormClosing(object sender, FormClosingEventArgs e)
{
using (Process process = Process.GetCurrentProcess())
{
process.Kill();
}
}
private void toolstrip_shutdown_devices_button_Click_1(object sender, EventArgs e)
{
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (var device_id in udids)
{
iDeviceHandle device_handle = null;
idevice.idevice_new(out device_handle, device_id);
Thread t_shutdown_iosdevices = new Thread(() => ShutdownDeviceCommand.executeCommand(udid: device_id));
t_shutdown_iosdevices.Start();
}
}
private void toolstrip_reboot_devices_button_Click(object sender, EventArgs e)
{
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (var device_id in udids)
{
iDeviceHandle device_handle = null;
idevice.idevice_new(out device_handle, device_id);
Thread t_shutdown_iosdevices = new Thread(() => RebootDeviceCommand.executeCommand(udid: device_id));
t_shutdown_iosdevices.Start();
}
}
private void toolstrip_enter_recovery_devices_button_Click_1(object sender, EventArgs e)
{
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (var device_id in udids)
{
iDeviceHandle device_handle = null;
idevice.idevice_new(out device_handle, device_id);
Thread t_enter_recovery_iosdevices = new Thread(() => BasicNormalModeCommands.enterRecoveryMode(idevice: device_handle));
t_enter_recovery_iosdevices.Start();
}
}
private void toolstrip_connected_devices_lable_TextChanged_1(object sender, EventArgs e)
{
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (ListViewItem item in connected_normal_mode_devices_listview.Items)
{
bool matched = false;
foreach (var device_id in udids)
{
if(item.SubItems[3].Text == device_id)
matched = true;
}
if (!matched)
item.Remove();
}
foreach (var device_id in udids)
{
bool matched = false;
foreach (ListViewItem item in connected_normal_mode_devices_listview.Items)
{
if (device_id == item.SubItems[3].Text)
matched = true;
}
if (!matched)
{
iDeviceHandle DeviceHandle = null;
idevice.idevice_new(out DeviceHandle, device_id);
Task updateListViewWithNewDevice = new Task(() => updateNewDeviceInformation(deviceHandle: DeviceHandle));
updateListViewWithNewDevice.Start();
}
}
}
private void toolstrip_activate_devices_Click(object sender, EventArgs e)
{
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (var deviceudid in udids)
{
iDeviceHandle device_handle = null;
idevice.idevice_new(out device_handle, deviceudid);
Thread t_activation_devices = new Thread(() => ActivateDeviceCommand.executeCommand(udid: deviceudid));
t_activation_devices.Start();
}
}
private void toolstrip_deactivate_devices_Click(object sender, EventArgs e)
{
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (var device_id in udids)
{
iDeviceHandle device_handle = null;
idevice.idevice_new(out device_handle, device_id);
Thread t_deactivate_devices = new Thread(() => DeactivateDeviceCommand.executeCommand(udid: device_id));
t_deactivate_devices.Start();
}
}
private void toolstripTest_Click(object sender, EventArgs e)
{
}
private void connected_normal_mode_devices_listview_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var focusedItem = connected_normal_mode_devices_listview.FocusedItem;
if (focusedItem != null && focusedItem.Bounds.Contains(e.Location))
{
normal_mode_listview_rightclick_menu.Show(Cursor.Position);
}
}
}
private void rcm_enterrecovery_Click(object sender, EventArgs e)
{
string deviceudid = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[3].Text;
ReadOnlyCollection<string> udids;
int count = 0;
var idevice = LibiMobileDevice.Instance.iDevice;
var result = idevice.idevice_get_device_list(out udids, ref count);
foreach (var device_id in udids)
{
if (device_id == deviceudid)
{
iDeviceHandle device_handle = null;
idevice.idevice_new(out device_handle, device_id);
Thread t_enter_recovery_iosdevices = new Thread(() => BasicNormalModeCommands.enterRecoveryMode(idevice: device_handle));
t_enter_recovery_iosdevices.Start();
return;
}
}
}
private void rcm_rebootdevice_Click(object sender, EventArgs e)
{
string deviceudid = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[3].Text;
Thread t_shutdown_iosdevices = new Thread(() => RebootDeviceCommand.executeCommand(udid: deviceudid));
t_shutdown_iosdevices.Start();
}
private void rcm_shutdowndevice_Click(object sender, EventArgs e)
{
string deviceudid = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[3].Text;
Thread t_shutdown_iosdevices = new Thread(() => ShutdownDeviceCommand.executeCommand(udid: deviceudid));
t_shutdown_iosdevices.Start();
}
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
var idevice = LibiMobileDevice.Instance.iDevice;
string deviceudid = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[3].Text;
iDeviceHandle deviceHandle = null;
idevice.idevice_new(out deviceHandle, deviceudid);
DeviceProperties deviceProperties = new DeviceProperties();
deviceProperties.DeviceHandle = deviceHandle;
deviceProperties.DeviceUDID = deviceudid;
deviceProperties.DeviceMarketingName = connected_normal_mode_devices_listview.SelectedItems[0].Text;
deviceProperties.DeviceCapacity = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[1].Text;
deviceProperties.DeviceColor = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[2].Text;
deviceProperties.DeviceICCID = connected_normal_mode_devices_listview.SelectedItems[0].SubItems[5].Text;
deviceProperties.Show();
}
private void toolstrip_connected_devices_lable_Click(object sender, EventArgs e)
{
}
}
class change_via_thread
{
delegate void UniversalVoidDelegate();
public static void ControlInvoke(Control control, Action function)
{
if (control.IsDisposed || control.Disposing)
return;
if (control.InvokeRequired)
{
control.Invoke(new UniversalVoidDelegate(() => ControlInvoke(control, function)));
return;
}
function();
}
}
}