-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
262 lines (233 loc) · 11.9 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
using LibHac;
using LibHac.IO;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using static Utils;
using static WebUtils;
namespace Harvest
{
internal class Program
{
private static void Main(string[] args)
{
var keyFile = Environment.ExpandEnvironmentVariables("%USERPROFILE%/.switch/prod.keys");
var keysInUserDir = true;
var getDeltasOnly = false;
#region Pre-execution checks
if (!File.Exists(keyFile))
{
if (!File.Exists("prod.keys"))
{
Console.Error.WriteLine("Error: prod.keys is missing!");
return;
}
else keysInUserDir = false;
}
if (!File.Exists("edge.token"))
{
Console.Error.WriteLine("Error: Please add an edge token to a file named \"edge.token\".");
return;
}
if (!File.Exists("device.id"))
{
Console.Error.WriteLine("Error: Please add your device ID in hexadecimal text form to a file named \"device.id\".");
return;
}
if (!File.Exists("nx_tls_client_cert.pfx"))
{
Console.Error.WriteLine("Error: Please add your Switch certificate to a file named \"nx_tls_client_cert.pfx\".");
return;
}
#endregion
var deviceID = Convert.ToInt64(File.ReadAllText("device.id"), 16);
var keys = ExternalKeys.ReadKeyFile(keysInUserDir ? keyFile : "prod.keys");
var sysID = "0100000000000816";
// To-do: Clean up
if (args[0] == "-s" || args[0] == sysID)
{
var JSON = (string)GET(deviceID, SunUrl, true);
var Version = args.Length > 1 ? args[1] : (string)JObject.Parse(JSON)["system_update_metas"][0]["title_version"];
HEAD(deviceID, nMetaURL('s', sysID, Version, deviceID));
var contentID = HEAD(deviceID, nMetaURL('s', sysID, Version, deviceID));
using (var meta = (HttpWebResponse)GET(deviceID, nContentURL('s', contentID), false))
using (var rd = new BinaryReader(meta.GetResponseStream()))
using (var strm = new MemoryStorage(rd.ReadBytes((int)meta.ContentLength)))
{
Directory.CreateDirectory(sysID);
strm.WriteAllBytes($"{sysID}/{contentID}.cnmt.nca");
using (var nca = new Nca(keys, strm, false).OpenSection(0, false, IntegrityCheckLevel.None, false))
{
var pfs = new Pfs(nca);
var cnmt = new Cnmt(pfs.OpenFile(pfs.Files[0]).AsStream());
foreach (var entry in cnmt.MetaEntries)
{
var tid = $"{entry.TitleId:x16}";
HEAD(deviceID, nMetaURL('a', tid, $"{entry.Version.Version}", deviceID));
var cID = HEAD(deviceID, nMetaURL('a', tid, $"{entry.Version.Version}", deviceID));
using (var met2 = (HttpWebResponse)GET(deviceID, nContentURL('a', cID), false))
using (var rdr = new BinaryReader(met2.GetResponseStream()))
using (var strm2 = new MemoryStorage(rdr.ReadBytes((int)meta.ContentLength)))
{
Directory.CreateDirectory($"{sysID}/{tid}");
strm2.WriteAllBytes($"{sysID}/{tid}/{cID}.cnmt.nca");
using (var nca2 = new Nca(keys, strm2, false).OpenSection(0, false, IntegrityCheckLevel.None, false))
{
var pfs2 = new Pfs(nca2);
var cnmt2 = new Cnmt(pfs2.OpenFile(pfs2.Files[0]).AsStream());
foreach (var entry2 in cnmt2.ContentEntries)
{
string NCAID = entry2.NcaId.ToHexString().ToLower();
using (var Req = (HttpWebResponse)GET(deviceID, nContentURL('c', NCAID), false))
using (var Output = File.OpenWrite($"{sysID}/{tid}/{NCAID}.nca"))
Req.GetResponseStream().CopyTo(Output);
}
}
}
}
}
}
return;
}
string id = null,
idFirstReq = null,
idSecondReq = null;
List<string> tidTargets = new List<string>(),
verTargets = new List<string>(),
entryTargets = new List<string>();
var numOfListings = 0;
if (args.Length == 2)
{
if (args[1] == "-d")
getDeltasOnly = true;
else if (args[0] == "-t")
{
var rightsID = args[1];
using (var tikReq = (HttpWebResponse)GET(deviceID, CETKURL(rightsID), false))
using (var strm = tikReq.GetResponseStream())
using (var rd = new BinaryReader(strm))
{
File.WriteAllBytes($"{rightsID}.tik", rd.ReadBytes(0x2C0));
File.WriteAllBytes($"{rightsID}.cert", rd.ReadBytes(0x700));
}
return;
}
}
GET(deviceID, Aqua(deviceID), false);
if (args.Length == 3)
{
numOfListings = 2;
if (args[0].Substring(13, 3) != "000" && args[0].Substring(13, 3) != "800")
{
tidTargets.Add(null);
tidTargets.Add(args[0]);
}
else
{
tidTargets.Add(null);
tidTargets.Add($"{args[0].Substring(0, 13)}800");
}
verTargets.Add(null);
verTargets.Add(args[2]);
}
else
{
var Streq = (string)GET(deviceID, Superfly(GetBaseTID(args[0])), true);
if (args[0].Substring(13, 3) == "000")
{
foreach (JToken Listing in JArray.Parse(Streq))
if ((string)Listing["title_type"] != "AddOnContent")
{
numOfListings++;
tidTargets.Add(Listing["title_id"].ToString());
verTargets.Add(Listing["version"].ToString());
}
}
else
{
foreach (JToken Listing in JArray.Parse(Streq))
if ((string)Listing["title_id"] == args[0])
{
numOfListings++;
tidTargets.Add(Listing["title_id"].ToString());
verTargets.Add(Listing["version"].ToString());
}
}
}
if (args.Length == 1)
id = HEAD(deviceID, MetaURL('a', tidTargets[0], verTargets[0], deviceID));
if (args.Length == 1 && id != null)
GET(deviceID, ContentURL('a', id), true);
if (numOfListings >= 2)
idFirstReq = HEAD(deviceID, MetaURL('a', tidTargets[1], verTargets[1], deviceID));
if (args.Length == 1 && numOfListings >= 2)
GET(deviceID, ContentURL('a', idFirstReq), true);
if (numOfListings >= 2)
idSecondReq = HEAD(deviceID, MetaURL('a', tidTargets[1], verTargets[1], deviceID));
if (numOfListings >= 2)
GET(deviceID, ContentURL('a', idSecondReq), true);
if (args.Length == 1)
{
using (var meta = (HttpWebResponse)GET(deviceID, ContentURL('a', id), false))
using (var rd = new BinaryReader(meta.GetResponseStream()))
using (var strm = new MemoryStorage(rd.ReadBytes((int)meta.ContentLength)))
{
Directory.CreateDirectory(tidTargets[0]);
strm.WriteAllBytes($"{tidTargets[0]}/{id}.cnmt.nca");
using (var nca = new Nca(keys, strm, false).OpenSection(0, false, IntegrityCheckLevel.None, false))
{
var pfs = new Pfs(nca);
var cnmt = new Cnmt(pfs.OpenFile(pfs.Files[0]).AsStream());
foreach (var entry in cnmt.ContentEntries)
{
string NCAID = entry.NcaId.ToHexString().ToLower();
using (var Req = (HttpWebResponse)GET(deviceID, ContentURL('c', NCAID), false))
using (var Output = File.OpenWrite($"{tidTargets[0]}/{NCAID}.nca"))
Req.GetResponseStream().CopyTo(Output);
}
}
}
}
if (numOfListings >= 2)
{
using (var meta = (HttpWebResponse)GET(deviceID, ContentURL('a', idSecondReq), false))
using (var read = new BinaryReader(meta.GetResponseStream()))
using (var metastrm = new MemoryStorage(read.ReadBytes((int)meta.ContentLength)))
{
Directory.CreateDirectory(tidTargets[1]);
metastrm.WriteAllBytes($"{tidTargets[1]}/{idSecondReq}.cnmt.nca");
using (var nca = new Nca(keys, metastrm, false).OpenSection(0, false, IntegrityCheckLevel.None, false))
{
var pfs = new Pfs(nca);
var cnmt = new Cnmt(pfs.OpenFile(pfs.Files[0]).AsStream());
foreach (var entry in getDeltasOnly ?
cnmt.ContentEntries.Where(e => e.Type == CnmtContentType.DeltaFragment) :
cnmt.ContentEntries)
{
var ncaID = entry.NcaId.ToHexString().ToLower();
using (var request = (HttpWebResponse)GET(deviceID, ContentURL('c', ncaID), false))
using (var output = File.OpenWrite($"{tidTargets[1]}/{ncaID}.nca"))
request.GetResponseStream().CopyTo(output);
if (entry.Type == CnmtContentType.Program)
{
var rightsID = new Nca(keys, new StreamStorage
(File.OpenRead($"{tidTargets[1]}/{ncaID}.nca"), false), false)
.Header.RightsId.ToHexString().ToLower();
using (var tikReq = (HttpWebResponse)GET(deviceID, CETKURL(rightsID), false))
using (var strm = tikReq.GetResponseStream())
using (var rd = new BinaryReader(strm))
{
File.WriteAllBytes($"{tidTargets[1]}/{rightsID}.tik", rd.ReadBytes(0x2C0));
File.WriteAllBytes($"{tidTargets[1]}/{rightsID}.cert", rd.ReadBytes(0x700));
}
}
}
}
}
}
}
}
}