This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
MainView.swift
1205 lines (1047 loc) · 53.8 KB
/
MainView.swift
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ViewController.swift
// AppSigner
//
// Created by Daniel Radtke on 11/2/15.
// Copyright © 2015 Daniel Radtke. All rights reserved.
//
import Cocoa
import Foundation
class MainView: NSView, URLSessionDataDelegate, URLSessionDelegate, URLSessionDownloadDelegate {
//MARK: IBOutlets
@IBOutlet var ProvisioningProfilesPopup: NSPopUpButton!
@IBOutlet var CodesigningCertsPopup: NSPopUpButton!
@IBOutlet var StatusLabel: NSTextField!
@IBOutlet var InputFileText: NSTextField!
@IBOutlet var InputText: NSTextField!
@IBOutlet var YololibInputText: NSTextField!
@IBOutlet var BrowseButton: NSButton!
@IBOutlet var BrowButton: NSButton!
@IBOutlet var YololibButton: NSButton!
@IBOutlet var StartButton: NSButton!
@IBOutlet var NewApplicationIDTextField: NSTextField!
@IBOutlet var downloadProgress: NSProgressIndicator!
@IBOutlet var appDisplayName: NSTextField!
@IBOutlet var appShortVersion: NSTextField!
@IBOutlet var appVersion: NSTextField!
//MARK: Variables
var provisioningProfiles:[ProvisioningProfile] = []
var codesigningCerts: [String] = []
var profileFilename: String?
var ReEnableNewApplicationID = false
var PreviousNewApplicationID = ""
var outputFile: String?
var startSize: CGFloat?
var NibLoaded = false
//MARK: Constants
let defaults = UserDefaults()
let fileManager = FileManager.default
let bundleID = Bundle.main.bundleIdentifier
let arPath = "/usr/bin/ar"
let mktempPath = "/usr/bin/mktemp"
let tarPath = "/usr/bin/tar"
let unzipPath = "/usr/bin/unzip"
let zipPath = "/usr/bin/zip"
let defaultsPath = "/usr/bin/defaults"
let codesignPath = "/usr/bin/codesign"
let securityPath = "/usr/bin/security"
let chmodPath = "/bin/chmod"
let sc = XSystemCommand();
//MARK: Drag / Drop
var fileTypes: [String] = ["ipa","deb","app","xcarchive","mobileprovision"]
var urlFileTypes: [String] = ["ipa","deb"]
var fileTypeIsOk = false
func fileDropped(_ filename: String){
switch(filename.pathExtension.lowercased()){
case "ipa", "deb", "app", "xcarchive":
InputFileText.stringValue = filename
break
case "mobileprovision":
ProvisioningProfilesPopup.selectItem(at: 1)
checkProfileID(ProvisioningProfile(filename: filename))
break
default: break
}
}
func urlDropped(_ url: NSURL){
if let urlString = url.absoluteString {
InputFileText.stringValue = urlString
}
}
override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
if checkExtension(sender) == true {
self.fileTypeIsOk = true
return .copy
} else {
self.fileTypeIsOk = false
return NSDragOperation()
}
}
override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
if self.fileTypeIsOk {
return .copy
} else {
return NSDragOperation()
}
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
let pasteboard = sender.draggingPasteboard()
if let board = pasteboard.propertyList(forType: "NSFilenamesPboardType") as? NSArray {
if let filePath = board[0] as? String {
fileDropped(filePath)
return true
}
}
if let types = pasteboard.types {
if types.contains(NSURLPboardType) {
if let url = NSURL(from: pasteboard) {
urlDropped(url)
}
}
}
return false
}
func checkExtension(_ drag: NSDraggingInfo) -> Bool {
if let board = drag.draggingPasteboard().propertyList(forType: "NSFilenamesPboardType") as? NSArray,
let path = board[0] as? String {
return self.fileTypes.contains(path.pathExtension.lowercased())
}
if let types = drag.draggingPasteboard().types {
if types.contains(NSURLPboardType) {
if let url = NSURL(from: drag.draggingPasteboard()),
let suffix = url.pathExtension {
return self.urlFileTypes.contains(suffix.lowercased())
}
}
}
return false
}
//MARK: Functions
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
register(forDraggedTypes: [NSFilenamesPboardType, NSURLPboardType])
}
required init?(coder: NSCoder) {
super.init(coder: coder)
register(forDraggedTypes: [NSFilenamesPboardType, NSURLPboardType])
}
override func awakeFromNib() {
super.awakeFromNib()
if NibLoaded == false {
NibLoaded = true
// Do any additional setup after loading the view.
populateProvisioningProfiles()
populateCodesigningCerts()
if let defaultCert = defaults.string(forKey: "signingCertificate") {
if codesigningCerts.contains(defaultCert) {
Log.write("Loaded Codesigning Certificate from Defaults: \(defaultCert)")
CodesigningCertsPopup.selectItem(withTitle: defaultCert)
}
}
setStatus("Ready")
if checkXcodeCLI() == false {
if #available(OSX 10.10, *) {
let _ = installXcodeCLI()
} else {
let alert = NSAlert()
alert.messageText = "Please install the Xcode command line tools and re-launch this application."
alert.runModal()
}
NSApplication.shared().terminate(self)
}
UpdatesController.checkForUpdate()
}
}
func installXcodeCLI() -> AppSignerTaskOutput {
return Process().execute("/usr/bin/xcode-select", workingDirectory: nil, arguments: ["--install"])
}
func checkXcodeCLI() -> Bool {
if #available(OSX 10.10, *) {
if Process().execute("/usr/bin/xcode-select", workingDirectory: nil, arguments: ["-p"]).status != 0 {
return false
}
} else {
if Process().execute("/usr/sbin/pkgutil", workingDirectory: nil, arguments: ["--pkg-info=com.apple.pkg.DeveloperToolsCLI"]).status != 0 {
// Command line tools not available
return false
}
}
return true
}
func makeTempFolder()->String?{
let tempTask = Process().execute(mktempPath, workingDirectory: nil, arguments: ["-d","-t",bundleID!])
if tempTask.status != 0 {
return nil
}
return tempTask.output.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}
func setStatus(_ status: String){
Log.write(status)
StatusLabel.stringValue = status
}
func populateProvisioningProfiles(){
let zeroWidthSpace = ""
self.provisioningProfiles = ProvisioningProfile.getProfiles().sorted {
($0.name == $1.name && $0.created.timeIntervalSince1970 > $1.created.timeIntervalSince1970) || $0.name < $1.name
}
setStatus("Found \(provisioningProfiles.count) Provisioning Profile\(provisioningProfiles.count>1 || provisioningProfiles.count<1 ? "s":"")")
ProvisioningProfilesPopup.removeAllItems()
ProvisioningProfilesPopup.addItems(withTitles: [
"Re-Sign Only",
"Choose Custom File",
"––––––––––––––––––––––"
])
let formatter = DateFormatter()
formatter.dateStyle = .short
formatter.timeStyle = .medium
var newProfiles: [ProvisioningProfile] = []
var zeroWidthPadding: String = ""
for profile in provisioningProfiles {
zeroWidthPadding = "\(zeroWidthPadding)\(zeroWidthSpace)"
if profile.expires.timeIntervalSince1970 > Date().timeIntervalSince1970 {
newProfiles.append(profile)
ProvisioningProfilesPopup.addItem(withTitle: "\(profile.name)\(zeroWidthPadding) (\(profile.teamID))")
let toolTipItems = [
"\(profile.name)",
"",
"Team ID: \(profile.teamID)",
"Created: \(formatter.string(from: profile.created as Date))",
"Expires: \(formatter.string(from: profile.expires as Date))"
]
ProvisioningProfilesPopup.lastItem!.toolTip = toolTipItems.joined(separator: "\n")
setStatus("Added profile \(profile.appID), expires (\(formatter.string(from: profile.expires as Date)))")
} else {
setStatus("Skipped profile \(profile.appID), expired (\(formatter.string(from: profile.expires as Date)))")
}
}
self.provisioningProfiles = newProfiles
chooseProvisioningProfile(ProvisioningProfilesPopup)
}
func getCodesigningCerts() -> [String] {
var output: [String] = []
let securityResult = Process().execute(securityPath, workingDirectory: nil, arguments: ["find-identity","-v","-p","codesigning"])
if securityResult.output.characters.count < 1 {
return output
}
let rawResult = securityResult.output.components(separatedBy: "\"")
var index: Int
for index in stride(from: 0, through: rawResult.count - 2, by: 2) {
if !(rawResult.count - 1 < index + 1) {
output.append(rawResult[index+1])
}
}
return output
}
func showCodesignCertsErrorAlert(){
let alert = NSAlert()
alert.messageText = "No codesigning certificates found"
alert.informativeText = "I can attempt to fix this automatically, would you like me to try?"
alert.addButton(withTitle: "Yes")
alert.addButton(withTitle: "No")
if alert.runModal() == NSAlertFirstButtonReturn {
if let tempFolder = makeTempFolder() {
iASShared.fixSigning(tempFolder)
try? fileManager.removeItem(atPath: tempFolder)
populateCodesigningCerts()
}
}
}
func populateCodesigningCerts() {
CodesigningCertsPopup.removeAllItems()
self.codesigningCerts = getCodesigningCerts()
setStatus("Found \(self.codesigningCerts.count) Codesigning Certificate\(self.codesigningCerts.count>1 || self.codesigningCerts.count<1 ? "s":"")")
if self.codesigningCerts.count > 0 {
for cert in self.codesigningCerts {
CodesigningCertsPopup.addItem(withTitle: cert)
setStatus("Added signing certificate \"\(cert)\"")
}
} else {
showCodesignCertsErrorAlert()
}
}
func checkProfileID(_ profile: ProvisioningProfile?){
if let profile = profile {
self.profileFilename = profile.filename
setStatus("Selected provisioning profile \(profile.appID)")
if profile.expires.timeIntervalSince1970 < Date().timeIntervalSince1970 {
ProvisioningProfilesPopup.selectItem(at: 0)
setStatus("Provisioning profile expired")
chooseProvisioningProfile(ProvisioningProfilesPopup)
}
if profile.appID.characters.index(of: "*") == nil {
// Not a wildcard profile
NewApplicationIDTextField.stringValue = profile.appID
NewApplicationIDTextField.isEnabled = false
} else {
// Wildcard profile
if NewApplicationIDTextField.isEnabled == false {
NewApplicationIDTextField.stringValue = ""
NewApplicationIDTextField.isEnabled = true
}
}
} else {
ProvisioningProfilesPopup.selectItem(at: 0)
setStatus("Invalid provisioning profile")
chooseProvisioningProfile(ProvisioningProfilesPopup)
}
}
func controlsEnabled(_ enabled: Bool){
if(enabled){
InputFileText.isEnabled = true
BrowseButton.isEnabled = true
ProvisioningProfilesPopup.isEnabled = true
CodesigningCertsPopup.isEnabled = true
NewApplicationIDTextField.isEnabled = ReEnableNewApplicationID
NewApplicationIDTextField.stringValue = PreviousNewApplicationID
StartButton.isEnabled = true
appDisplayName.isEnabled = true
} else {
// Backup previous values
PreviousNewApplicationID = NewApplicationIDTextField.stringValue
ReEnableNewApplicationID = NewApplicationIDTextField.isEnabled
InputFileText.isEnabled = false
BrowseButton.isEnabled = false
ProvisioningProfilesPopup.isEnabled = false
CodesigningCertsPopup.isEnabled = false
NewApplicationIDTextField.isEnabled = false
StartButton.isEnabled = false
appDisplayName.isEnabled = false
}
}
func recursiveDirectorySearch(_ path: String, extensions: [String], found: ((_ file: String) -> Void)){
if let files = try? fileManager.contentsOfDirectory(atPath: path) {
var isDirectory: ObjCBool = true
for file in files {
let currentFile = path.stringByAppendingPathComponent(file)
fileManager.fileExists(atPath: currentFile, isDirectory: &isDirectory)
if isDirectory.boolValue {
recursiveDirectorySearch(currentFile, extensions: extensions, found: found)
}
if extensions.contains(file.pathExtension) {
found(currentFile)
}
}
}
}
func unzip(_ inputFile: String, outputPath: String)->AppSignerTaskOutput {
return Process().execute(unzipPath, workingDirectory: nil, arguments: ["-q",inputFile,"-d",outputPath])
}
func zip(_ inputPath: String, outputFile: String)->AppSignerTaskOutput {
return Process().execute(zipPath, workingDirectory: inputPath, arguments: ["-qry", outputFile, "."])
}
func cleanup(_ tempFolder: String){
do {
Log.write("Deleting: \(tempFolder)")
try fileManager.removeItem(atPath: tempFolder)
} catch let error as NSError {
setStatus("Unable to delete temp folder")
Log.write(error.localizedDescription)
}
controlsEnabled(true)
}
func bytesToSmallestSi(_ size: Double) -> String {
let prefixes = ["","K","M","G","T","P","E","Z","Y"]
for i in 1...6 {
let nextUnit = pow(1024.00, Double(i+1))
let unitMax = pow(1024.00, Double(i))
if size < nextUnit {
return "\(round((size / unitMax)*100)/100)\(prefixes[i])B"
}
}
return "\(size)B"
}
func getPlistKey(_ plist: String, keyName: String)->String? {
let currTask = Process().execute(defaultsPath, workingDirectory: nil, arguments: ["read", plist, keyName])
if currTask.status == 0 {
return String(currTask.output.characters.dropLast())
} else {
return nil
}
}
func setPlistKey(_ plist: String, keyName: String, value: String)->AppSignerTaskOutput {
return Process().execute(defaultsPath, workingDirectory: nil, arguments: ["write", plist, keyName, value])
}
//MARK: NSURL Delegate
var downloading = false
var downloadError: NSError?
var downloadPath: String!
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
downloadError = downloadTask.error as NSError?
if downloadError == nil {
do {
try fileManager.moveItem(at: location, to: URL(fileURLWithPath: downloadPath))
} catch let error as NSError {
setStatus("Unable to move downloaded file")
Log.write(error.localizedDescription)
}
}
downloading = false
downloadProgress.doubleValue = 0.0
downloadProgress.stopAnimation(nil)
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
//StatusLabel.stringValue = "Downloading file: \(bytesToSmallestSi(Double(totalBytesWritten))) / \(bytesToSmallestSi(Double(totalBytesExpectedToWrite)))"
let percentDownloaded = (Double(totalBytesWritten) / Double(totalBytesExpectedToWrite)) * 100
downloadProgress.doubleValue = percentDownloaded
}
//MARK: Codesigning
func codeSign(_ file: String, certificate: String, entitlements: String?,before:((_ file: String, _ certificate: String, _ entitlements: String?)->Void)?, after: ((_ file: String, _ certificate: String, _ entitlements: String?, _ codesignTask: AppSignerTaskOutput)->Void)?)->AppSignerTaskOutput{
Log.write("###################file: \(file)")
let useEntitlements: Bool = ({
if entitlements == nil {
return false
} else {
if fileManager.fileExists(atPath: entitlements!) {
return true
} else {
return false
}
}
})()
if let beforeFunc = before {
beforeFunc(file, certificate, entitlements)
}
var arguments = ["-vvv","-fs",certificate,"--no-strict"]
if useEntitlements {
arguments.append("--entitlements=\(entitlements!)")
}
arguments.append(file)
let codesignTask = Process().execute(codesignPath, workingDirectory: nil, arguments: arguments)
Log.write("##############codesignTask: \(codesignTask)")
if let afterFunc = after {
afterFunc(file, certificate, entitlements, codesignTask)
}
return codesignTask
}
func testSigning(_ certificate: String, tempFolder: String )->Bool? {
let codesignTempFile = tempFolder.stringByAppendingPathComponent("test-sign")
// Copy our binary to the temp folder to use for testing.
let path = ProcessInfo.processInfo.arguments[0]
if (try? fileManager.copyItem(atPath: path, toPath: codesignTempFile)) != nil {
codeSign(codesignTempFile, certificate: certificate, entitlements: nil, before: nil, after: nil)
let verificationTask = Process().execute(codesignPath, workingDirectory: nil, arguments: ["-v",codesignTempFile])
try? fileManager.removeItem(atPath: codesignTempFile)
if verificationTask.status == 0 {
return true
} else {
return false
}
} else {
setStatus("Error testing codesign")
}
return nil
}
func startSigning() {
controlsEnabled(false)
//MARK: Get output filename
let saveDialog = NSSavePanel()
saveDialog.allowedFileTypes = ["ipa"]
saveDialog.nameFieldStringValue = InputFileText.stringValue.lastPathComponent.stringByDeletingPathExtension
if saveDialog.runModal() == NSFileHandlingPanelOKButton {
outputFile = saveDialog.url!.path
Thread.detachNewThreadSelector(#selector(self.signingThread), toTarget: self, with: nil)
} else {
outputFile = nil
controlsEnabled(true)
}
}
func signingThread(){
//MARK: Set up variables
var warnings = 0
var binaryName=""
var inputFile = InputFileText.stringValue
var inputDylib = InputText.stringValue;
let dylibComp = inputDylib.lastPathComponent
var yololibFile = YololibInputText.stringValue
var yoloComp = yololibFile.lastPathComponent
var provisioningFile = self.profileFilename
let signingCertificate = self.CodesigningCertsPopup.selectedItem?.title
let newApplicationID = self.NewApplicationIDTextField.stringValue.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let newDisplayName = self.appDisplayName.stringValue.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let newShortVersion = self.appShortVersion.stringValue.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let newVersion = self.appVersion.stringValue.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
let inputStartsWithHTTP = inputFile.lowercased().substring(to: inputFile.characters.index(inputFile.startIndex, offsetBy: 4)) == "http"
var eggCount: Int = 0
var continueSigning: Bool? = nil
//MARK: Sanity checks
// Check signing certificate selection
if signingCertificate == nil {
setStatus("No signing certificate selected")
return
}
// Check if input file exists
var inputIsDirectory: ObjCBool = false
if !inputStartsWithHTTP && !fileManager.fileExists(atPath: inputFile, isDirectory: &inputIsDirectory){
DispatchQueue.main.async(execute: {
let alert = NSAlert()
alert.messageText = "Input file not found"
alert.addButton(withTitle: "OK")
alert.informativeText = "The file \(inputFile) could not be found"
alert.runModal()
self.controlsEnabled(true)
})
return
}
//MARK: Create working temp folder
var tempFolder: String! = nil
if let tmpFolder = makeTempFolder() {
tempFolder = tmpFolder
} else {
setStatus("Error creating temp folder")
return
}
let workingDirectory = tempFolder.stringByAppendingPathComponent("out")
let eggDirectory = tempFolder.stringByAppendingPathComponent("eggs")
let payloadDirectory = workingDirectory.stringByAppendingPathComponent("Payload/")
let entitlementsPlist = tempFolder.stringByAppendingPathComponent("entitlements.plist")
Log.write("Temp folder: \(tempFolder)")
Log.write("Working directory: \(workingDirectory)")
Log.write("Payload directory: \(payloadDirectory)")
//MARK: Codesign Test
DispatchQueue.main.async(execute: {
if let codesignResult = self.testSigning(signingCertificate!, tempFolder: tempFolder) {
if codesignResult == false {
let alert = NSAlert()
alert.messageText = "Codesigning error"
alert.addButton(withTitle: "Yes")
alert.addButton(withTitle: "No")
alert.informativeText = "You appear to have a error with your codesigning certificate, do you want me to try and fix the problem?"
let response = alert.runModal()
if response == NSAlertFirstButtonReturn {
iASShared.fixSigning(tempFolder)
if self.testSigning(signingCertificate!, tempFolder: tempFolder) == false {
let errorAlert = NSAlert()
errorAlert.messageText = "Unable to Fix"
errorAlert.addButton(withTitle: "OK")
errorAlert.informativeText = "I was unable to automatically resolve your codesigning issue ☹\n\nIf you have previously trusted your certificate using Keychain, please set the Trust setting back to the system default."
errorAlert.runModal()
continueSigning = false
return
}
} else {
continueSigning = false
return
}
}
}
continueSigning = true
})
while true {
if continueSigning != nil {
if continueSigning! == false {
continueSigning = nil
cleanup(tempFolder); return
}
break
}
usleep(100)
}
//MARK: Create Egg Temp Directory
do {
try fileManager.createDirectory(atPath: eggDirectory, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
setStatus("Error creating egg temp directory")
Log.write(error.localizedDescription)
cleanup(tempFolder); return
}
//MARK: Download file
downloading = false
downloadError = nil
downloadPath = tempFolder.stringByAppendingPathComponent("download.\(inputFile.pathExtension)")
if inputStartsWithHTTP {
let defaultConfigObject = URLSessionConfiguration.default
let defaultSession = Foundation.URLSession(configuration: defaultConfigObject, delegate: self, delegateQueue: OperationQueue.main)
if let url = URL(string: inputFile) {
downloading = true
let downloadTask = defaultSession.downloadTask(with: url)
setStatus("Downloading file")
downloadProgress.startAnimation(nil)
downloadTask.resume()
defaultSession.finishTasksAndInvalidate()
}
while downloading {
usleep(100000)
}
if downloadError != nil {
setStatus("Error downloading file, \(downloadError!.localizedDescription.lowercased())")
cleanup(tempFolder); return
} else {
inputFile = downloadPath
}
}
Log.write("inputFile.pathExtension.lowercased()=\(inputFile.pathExtension)")
//MARK: Process input file
switch(inputFile.pathExtension.lowercased()){
case "deb":
//MARK: --Unpack deb
let debPath = tempFolder.stringByAppendingPathComponent("deb")
do {
try fileManager.createDirectory(atPath: debPath, withIntermediateDirectories: true, attributes: nil)
try fileManager.createDirectory(atPath: workingDirectory, withIntermediateDirectories: true, attributes: nil)
setStatus("Extracting deb file")
let debTask = Process().execute(arPath, workingDirectory: debPath, arguments: ["-x", inputFile])
Log.write(debTask.output)
if debTask.status != 0 {
setStatus("Error processing deb file")
cleanup(tempFolder); return
}
var tarUnpacked = false
for tarFormat in ["tar","tar.gz","tar.bz2","tar.lzma","tar.xz"]{
let dataPath = debPath.stringByAppendingPathComponent("data.\(tarFormat)")
if fileManager.fileExists(atPath: dataPath){
setStatus("Unpacking data.\(tarFormat)")
let tarTask = Process().execute(tarPath, workingDirectory: debPath, arguments: ["-xf",dataPath])
Log.write(tarTask.output)
if tarTask.status == 0 {
tarUnpacked = true
}
break
}
}
if !tarUnpacked {
setStatus("Error unpacking data.tar")
cleanup(tempFolder); return
}
try fileManager.moveItem(atPath: debPath.stringByAppendingPathComponent("Applications"), toPath: payloadDirectory)
} catch {
setStatus("Error processing deb file")
cleanup(tempFolder); return
}
break
case "ipa":
//MARK: --Unzip ipa
do {
try fileManager.createDirectory(atPath: workingDirectory, withIntermediateDirectories: true, attributes: nil)
setStatus("Extracting ipa file")
let unzipTask = self.unzip(inputFile, outputPath: workingDirectory)
if unzipTask.status != 0 {
setStatus("Error extracting ipa file")
cleanup(tempFolder); return
}
} catch {
setStatus("Error extracting ipa file")
cleanup(tempFolder); return
}
break
case "app":
//MARK: --Copy app bundle
if !inputIsDirectory.boolValue {
setStatus("Unsupported input file")
cleanup(tempFolder); return
}
do {
try fileManager.createDirectory(atPath: payloadDirectory, withIntermediateDirectories: true, attributes: nil)
setStatus("Copying app to payload directory")
try fileManager.copyItem(atPath: inputFile, toPath: payloadDirectory.stringByAppendingPathComponent(inputFile.lastPathComponent))
} catch {
setStatus("Error copying app to payload directory")
cleanup(tempFolder); return
}
break
case "xcarchive":
//MARK: --Copy app bundle from xcarchive
if !inputIsDirectory.boolValue {
setStatus("Unsupported input file")
cleanup(tempFolder); return
}
do {
try fileManager.createDirectory(atPath: workingDirectory, withIntermediateDirectories: true, attributes: nil)
setStatus("Copying app to payload directory")
try fileManager.copyItem(atPath: inputFile.stringByAppendingPathComponent("Products/Applications/"), toPath: payloadDirectory)
} catch {
setStatus("Error copying app to payload directory")
cleanup(tempFolder); return
}
break
default:
setStatus("Unsupported input file")
cleanup(tempFolder); return
}
if !fileManager.fileExists(atPath: payloadDirectory){
setStatus("Payload directory doesn't exist")
cleanup(tempFolder); return
}
do {
var isDirectory: ObjCBool = true
let files = try fileManager.contentsOfDirectory(atPath: payloadDirectory)
for file in files {
fileManager.fileExists(atPath: payloadDirectory.stringByAppendingPathComponent(file), isDirectory: &isDirectory)
if !isDirectory.boolValue { continue }
let appBundlePath = payloadDirectory.stringByAppendingPathComponent(file)
if appBundlePath.hasSuffix("app") {
let stringC = String().appendingFormat("scp %@ %@",inputDylib,appBundlePath)
Log.write("####################stringC\(stringC)")
sc.rystemCommand(stringC)
let filesAsiic = try fileManager.contentsOfDirectory(atPath: appBundlePath)
for fileAsi in filesAsiic {
let lastCPath = fileAsi.pathExtension
var isDirect: ObjCBool = false
fileManager.fileExists(atPath: fileAsi, isDirectory: &isDirect)
let fileP = appBundlePath.appendingFormat("/%@",fileAsi)
let typec = XSystemCommand().contentDataPath(fileP)
if (lastCPath.isEmpty||lastCPath.characters.count==0)&&202254==typec
{
binaryName=fileAsi;
// Log.write("####################numBytes:%d",pathData.numBytes);
Log.write("####################3fileAsi:\(fileAsi,fileAsi.pathExtension,fileAsi.stringByDeletingPathExtension,fileAsi.stringByDeletingLastPathComponent,typec)")
}
}
}
}
} catch {
setStatus("Error copying app to payload directory")
cleanup(tempFolder); return
}
// Loop through app bundles in payload directory
do {
let files = try fileManager.contentsOfDirectory(atPath: payloadDirectory)
var isDirectory: ObjCBool = true
for file in files {
fileManager.fileExists(atPath: payloadDirectory.stringByAppendingPathComponent(file), isDirectory: &isDirectory)
if !isDirectory.boolValue { continue }
//MARK: Bundle variables setup
let appBundlePath = payloadDirectory.stringByAppendingPathComponent(file)
let appBundleInfoPlist = appBundlePath.stringByAppendingPathComponent("Info.plist")
let appBundleProvisioningFilePath = appBundlePath.stringByAppendingPathComponent("embedded.mobileprovision")
let useAppBundleProfile = (provisioningFile == nil && fileManager.fileExists(atPath: appBundleProvisioningFilePath))
//MARK: Delete CFBundleResourceSpecification from Info.plist
Log.write(Process().execute(defaultsPath, workingDirectory: nil, arguments: ["delete",appBundleInfoPlist,"CFBundleResourceSpecification"]).output)
//MARK: Copy Provisioning Profile
if provisioningFile != nil {
if fileManager.fileExists(atPath: appBundleProvisioningFilePath) {
setStatus("Deleting embedded.mobileprovision")
do {
try fileManager.removeItem(atPath: appBundleProvisioningFilePath)
} catch let error as NSError {
setStatus("Error deleting embedded.mobileprovision")
Log.write(error.localizedDescription)
cleanup(tempFolder); return
}
}
setStatus("Copying provisioning profile to app bundle")
do {
try fileManager.copyItem(atPath: provisioningFile!, toPath: appBundleProvisioningFilePath)
} catch let error as NSError {
setStatus("Error copying provisioning profile")
Log.write(error.localizedDescription)
cleanup(tempFolder); return
}
}
//MARK: Generate entitlements.plist
if provisioningFile != nil || useAppBundleProfile {
setStatus("Parsing entitlements")
if let profile = ProvisioningProfile(filename: useAppBundleProfile ? appBundleProvisioningFilePath : provisioningFile!){
if let entitlements = profile.getEntitlementsPlist(tempFolder) {
Log.write("–––––––––––––––––––––––\n\(entitlements)")
Log.write("–––––––––––––––––––––––")
do {
try entitlements.write(toFile: entitlementsPlist, atomically: false, encoding: String.Encoding.utf8.rawValue)
setStatus("Saved entitlements to \(entitlementsPlist)")
} catch let error as NSError {
setStatus("Error writing entitlements.plist, \(error.localizedDescription)")
}
} else {
setStatus("Unable to read entitlements from provisioning profile")
warnings += 1
}
if profile.appID != "*" && (newApplicationID != "" && newApplicationID != profile.appID) {
setStatus("Unable to change App ID to \(newApplicationID), provisioning profile won't allow it")
cleanup(tempFolder); return
}
} else {
setStatus("Unable to parse provisioning profile, it may be corrupt")
warnings += 1
}
}
//MARK: Make sure that the executable is well... executable.
if let bundleExecutable = getPlistKey(appBundleInfoPlist, keyName: "CFBundleExecutable"){
Process().execute(chmodPath, workingDirectory: nil, arguments: ["755", appBundlePath.stringByAppendingPathComponent(bundleExecutable)])
}
//MARK: Change Application ID
if newApplicationID != "" {
if let oldAppID = getPlistKey(appBundleInfoPlist, keyName: "CFBundleIdentifier") {
func changeAppexID(_ appexFile: String){
let appexPlist = appexFile.stringByAppendingPathComponent("Info.plist")
if let appexBundleID = getPlistKey(appexPlist, keyName: "CFBundleIdentifier"){
let newAppexID = "\(newApplicationID)\(appexBundleID.substring(from: oldAppID.endIndex))"
setStatus("Changing \(appexFile) id to \(newAppexID)")
setPlistKey(appexPlist, keyName: "CFBundleIdentifier", value: newAppexID)
}
if Process().execute(defaultsPath, workingDirectory: nil, arguments: ["read", appexPlist,"WKCompanionAppBundleIdentifier"]).status == 0 {
setPlistKey(appexPlist, keyName: "WKCompanionAppBundleIdentifier", value: newApplicationID)
}
recursiveDirectorySearch(appexFile, extensions: ["app"], found: changeAppexID)
}
recursiveDirectorySearch(appBundlePath, extensions: ["appex"], found: changeAppexID)
}
setStatus("Changing App ID to \(newApplicationID)")
let IDChangeTask = setPlistKey(appBundleInfoPlist, keyName: "CFBundleIdentifier", value: newApplicationID)
if IDChangeTask.status != 0 {
setStatus("Error changing App ID")
Log.write(IDChangeTask.output)
cleanup(tempFolder); return
}
}
//MARK: Change Display Name
if newDisplayName != "" {
let appDirectory = payloadDirectory.stringByAppendingPathComponent(file)
let appFiles = try fileManager.contentsOfDirectory(atPath: appDirectory)
var isDirectory: ObjCBool = true
for appFile in appFiles {
fileManager.fileExists(atPath: payloadDirectory.stringByAppendingPathComponent(appFile), isDirectory: &isDirectory)
if !isDirectory.boolValue { continue }
if appFile.hasSuffix(".lproj")
{
let lprojPath = appDirectory.stringByAppendingPathComponent(appFile)
let stringFiles = try fileManager.contentsOfDirectory(atPath: lprojPath)
var isDirectory: ObjCBool = true
for stringFile in stringFiles
{
if stringFile.hasSuffix(".strings")
{
let stringPath = lprojPath.stringByAppendingPathComponent(stringFile)
sc.fileString(stringPath, newDisplayName: newDisplayName)
//sc.fileString(stringPath);
Log.write("##########stringFile:##########\(stringPath)");
let displayNameChangeTask = Process().execute(defaultsPath, workingDirectory: nil, arguments: ["write",stringPath,"CFBundleDisplayName", newDisplayName])
if displayNameChangeTask.status != 0 {
setStatus("Error changing display name")
Log.write(displayNameChangeTask.output)
cleanup(tempFolder); return
}
}
}
}
}
setStatus("Changing Display Name to \(newDisplayName))")
let displayNameChangeTask = Process().execute(defaultsPath, workingDirectory: nil, arguments: ["write",appBundleInfoPlist,"CFBundleDisplayName", newDisplayName])
if displayNameChangeTask.status != 0 {
setStatus("Error changing display name")
Log.write(displayNameChangeTask.output)
cleanup(tempFolder); return
}
}
//MARK: Change Version
if newVersion != "" {
setStatus("Changing Version to \(newVersion)")
let versionChangeTask = Process().execute(defaultsPath, workingDirectory: nil, arguments: ["write",appBundleInfoPlist,"CFBundleVersion", newVersion])
if versionChangeTask.status != 0 {
setStatus("Error changing version")
Log.write(versionChangeTask.output)
cleanup(tempFolder); return
}
}
//MARK: Change Short Version
if newShortVersion != "" {
setStatus("Changing Short Version to \(newShortVersion)")
let shortVersionChangeTask = Process().execute(defaultsPath, workingDirectory: nil, arguments: ["write",appBundleInfoPlist,"CFBundleShortVersionString", newShortVersion])
if shortVersionChangeTask.status != 0 {
setStatus("Error changing short version")
Log.write(shortVersionChangeTask.output)
cleanup(tempFolder); return
}
}
func generateFileSignFunc(_ payloadDirectory:String, entitlementsPath: String, signingCertificate: String)->((_ file:String)->Void){
let useEntitlements: Bool = ({
if fileManager.fileExists(atPath: entitlementsPath) {
return true
}
return false
})()
func shortName(_ file: String, payloadDirectory: String)->String{
return file.substring(from: payloadDirectory.endIndex)
}
func beforeFunc(_ file: String, certificate: String, entitlements: String?){
setStatus("Codesigning \(shortName(file, payloadDirectory: payloadDirectory))\(useEntitlements ? " with entitlements":"")")
}
func afterFunc(_ file: String, certificate: String, entitlements: String?, codesignOutput: AppSignerTaskOutput){
if codesignOutput.status != 0 {
setStatus("Error codesigning \(shortName(file, payloadDirectory: payloadDirectory))")
Log.write(codesignOutput.output)
warnings += 1
}
}
func output(_ file:String){
Log.write("##############: \(file,signingCertificate,entitlementsPath,beforeFunc,afterFunc)")
codeSign(file, certificate: signingCertificate, entitlements: entitlementsPath, before: beforeFunc, after: afterFunc)
}
return output
}
//MARK: Codesigning - General
let signableExtensions = ["dylib","so","0","vis","pvr","framework","appex","app"]
//MARK: Codesigning - Eggs
let eggSigningFunction = generateFileSignFunc(eggDirectory, entitlementsPath: entitlementsPlist, signingCertificate: signingCertificate!)
func signEgg(_ eggFile: String){