-
Notifications
You must be signed in to change notification settings - Fork 12
/
AppHelper.cs
218 lines (196 loc) · 7.53 KB
/
AppHelper.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
using System;
using System.IO;
using Ionic.Zip;
using PlistCS;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace Brake
{
public class AppHelper
{
public AppHelper()
{
}
public static void Debug(String debugString)
{
Console.WriteLine("Debug: " + debugString);
}
public static void getIPAs(String location)
{
DirectoryInfo di = new DirectoryInfo(location);
Container container = Container.getContainer();
container.IPAItems = new List<IPAInfo>();
FileInfo[] files;
//byte[] data;
if (!di.Exists)
{
Console.WriteLine("Error: Directory not found?");
return;
}
try
{
files = di.GetFiles("*.ipa", SearchOption.TopDirectoryOnly);
}
catch (Exception e)
{
Console.WriteLine("Error: Exception occured, perhaps no permissions for directory?");
Console.WriteLine(e.ToString());
return;
}
foreach (FileInfo file in files)
{
try {
Debug("IPA found: " + file.Name);
using (ZipFile ipa = ZipFile.Read(file.FullName))
{
foreach (ZipEntry e in ipa)
{
Debug("ZipEntry: " + e.FileName);
if (e.FileName.Contains(".app/Info.plist"))
{
Debug("INFO.PLIST FOUND!!!!! " + GetTemporaryDirectory());
e.Extract(GetTemporaryDirectory(), ExtractExistingFileAction.OverwriteSilently);
string plistLocation = Path.Combine(GetTemporaryDirectory(), e.FileName);
//unpackDirectory + "/" + e.FileName;
Debug("yo yo yo");
Dictionary<string, object> plist = (Dictionary<string, object>)Plist.readPlist(plistLocation);
Debug("plist data: " + plist.ToString());
Debug("Bundle Name: " + plist["CFBundleExecutable"]);
IPAInfo info = new IPAInfo();
info.AppBundle = (string)plist["CFBundleExecutable"];
if (plist.ContainsKey("CFBundleDisplayName"))
{
info.AppName = (string)plist["CFBundleDisplayName"];
}
else
{
info.AppName = (string)plist["CFBundleName"];
}
info.AppVersion = (string)plist["CFBundleVersion"];
info.Location = file.FullName;
info.BinaryLocation = e.FileName.Replace("Info.plist", "") + info.AppBundle;
container.IPAItems.Add(info);
}
}
}
}
catch (Exception ex) {
Debug ("Error: could not analyze " + file.Name);
Debug ("Error: " + ex.ToString());
}
}
/*using (StreamWriter writer = new StreamWriter("Brake.xml")) {
XmlSerializer serializer = new XmlSerializer (typeof(Container));
serializer.Serialize (writer, container);
Debug ("serializing data");
}*/
container.SaveXML();
DeleteDirectory(GetTemporaryDirectory());
}
public static String extractIPA(IPAInfo info)
{
using (ZipFile ipa = ZipFile.Read(info.Location))
{
Debug("IPALocation: " + info.Location);
Debug("Payload location: " + info.BinaryLocation);
foreach (ZipEntry e in ipa)
{
//Debug ("file:" + e.FileName);
if (e.FileName.Contains (".sinf") || e.FileName.Contains (".supp")) {
//extract it
Debug (GetTemporaryDirectory ());
e.Extract (GetTemporaryDirectory (), ExtractExistingFileAction.OverwriteSilently);
} else if (e.FileName == info.BinaryLocation) {
Debug ("found binary!!");
e.Extract (GetTemporaryDirectory (), ExtractExistingFileAction.OverwriteSilently);
} else if (e.FileName == "iTunesMetadata") {
e.Extract (GetWorkingDirectory (), ExtractExistingFileAction.OverwriteSilently);
string plistLocation = Path.Combine(GetTemporaryDirectory(), e.FileName);
info.iTunesMetadata = (Dictionary<string, object>)Plist.readPlist (plistLocation);
}
}
}
//zip!
String AppDirectory = Path.Combine(GetTemporaryDirectory(), "Payload");
Debug("AppDirectory: " + AppDirectory);
//return null;
String ZipPath = Path.Combine(GetWorkingDirectory(), info.AppBundle + ".ipa");
using (ZipFile zip = new ZipFile())
{
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.None;
zip.AddDirectory(AppDirectory);
zip.Save(ZipPath);
}
DeleteDirectory(GetTemporaryDirectory());
return ZipPath;
}
public static String repack(IPAInfo info, String binaryLocation)
{
String finalLocation = info.AppName + " " + info.AppVersion + ".ipa";
File.Copy(info.Location, finalLocation, true);
using (ZipFile ipa = ZipFile.Read(finalLocation))
{
Debug("found binary!!");
try
{
var binary = ipa.AddFile(binaryLocation).FileName = info.BinaryLocation;
}
catch (System.ArgumentException)
{
//remove the existing binary and insert again :/
foreach (ZipEntry e in ipa)
{
if (e.FileName == info.BinaryLocation)
{
//Console.WriteLine ("binarylocation: " +);
Debug("found binary!!!!");
ipa.RemoveEntry(e);
var binary = ipa.AddFile(binaryLocation).FileName = info.BinaryLocation;
break;
}
}
}
Debug("binarylocation: " + binaryLocation + " " + info.BinaryLocation);
//binary.FileName = info.BinaryLocation;
ipa.Save();
}
return finalLocation;
}
private static string tempDir;
private static string workingDir;
public static string GetTemporaryDirectory()
{
if (tempDir == null)
{
tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(tempDir);
}
return tempDir;
}
public static string GetWorkingDirectory()
{
if (workingDir == null)
{
workingDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(workingDir);
}
return workingDir;
}
public static void DeleteDirectory(string target_dir)
{
tempDir = null;
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
}
}