RepoFullDownloader/RepoFullDownloader-Core/Program.cs

254 lines
10 KiB
C#
Raw Normal View History

2021-10-17 22:14:26 -07:00
using ICSharpCode.SharpZipLib.BZip2;
using ICSharpCode.SharpZipLib.GZip;
using RepoFullDownloader;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text.Json;
using System.Threading;
namespace RepoFullDownloader_Core
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("RepoFullDownloader by KawaiiZenbo");
// Initial Checks
if (!Directory.Exists("./output/"))
{
Directory.CreateDirectory("./output/");
}
if(args.Length != 0)
{
string url = args[0];
2021-10-18 18:12:12 -07:00
if(!args[0].StartsWith("https://") && !args[0].StartsWith("http://"))
2021-10-17 22:14:26 -07:00
{
url = "http://" + url;
}
if (!args[0].EndsWith("/"))
{
url += "/";
}
try
{
DownloadRepo(url, false);
}
catch(Exception)
{
Console.WriteLine("No APT repo was found, trying as Installer repo");
DownloadInstallerRepo(url);
}
}
else
{
if (!File.Exists("./options.json"))
{
Console.WriteLine("Could not find options.json");
Console.WriteLine("Generating example...");
// generate example options
Options exampleOptions = new Options();
exampleOptions.originalFilenames = false;
Repo repo1 = new Repo();
repo1.url = "http://cydia.invoxiplaygames.uk/";
repo1.isInstaller = false;
Repo repo2 = new Repo();
2021-10-18 18:12:12 -07:00
repo2.url = "http://apptapp.saurik.com/";
2021-10-17 22:14:26 -07:00
repo2.isInstaller = true;
exampleOptions.repos = new[] { repo1, repo2 };
string exampleOut = JsonSerializer.Serialize(exampleOptions);
File.WriteAllText("./options.json", exampleOut);
return;
}
// Load Options from 'options.json'
string optionsJson = File.ReadAllText("./options.json");
Options options = JsonSerializer.Deserialize<Options>(optionsJson);
foreach (Repo r in options.repos)
{
if (r.isInstaller)
{
DownloadInstallerRepo(r.url);
}
else
{
DownloadRepo(r.url, options.originalFilenames);
}
}
}
// this is the part where it is over
Console.WriteLine("done :)");
}
static void DownloadRepo(string link, bool keepOg)
{
2021-10-18 18:12:12 -07:00
if (!link.StartsWith("https://") && !link.StartsWith("http://"))
{
link = "http://" + link;
}
if (!link.EndsWith("/"))
{
link += "/";
}
2021-10-22 15:30:51 -07:00
string cleanLink = link.TrimEnd('/').Replace("http://", "").Replace("https://", "").Replace("/", "_").Replace(":", "_");
2021-10-18 18:12:12 -07:00
Directory.CreateDirectory($"./output/{cleanLink}");
2021-10-17 22:14:26 -07:00
WebClient webClient = new WebClient();
// headers because some repos are 'interesting'
webClient.Headers.Add("X-Machine", "iPod4,1");
webClient.Headers.Add("X-Unique-ID", "0000000000000000000000000000000000000000");
webClient.Headers.Add("X-Firmware", "6.1");
webClient.Headers.Add("User-Agent", "Telesphoreo APT-HTTP/1.0.999");
// Attempt to download packages file (try/catch hell)
try
{
Console.WriteLine("Attempting to download " + link + "Packages.bz2");
2021-10-18 18:12:12 -07:00
webClient.DownloadFile(new Uri(link + "Packages.bz2"), $"./output/{cleanLink}/Packages.bz2");
FileStream packagesBz2 = new FileInfo($"./output/{cleanLink}/Packages.bz2").OpenRead();
FileStream packagesBz2Decompressed = File.Create($"./output/{cleanLink}/Packages");
2021-10-17 22:14:26 -07:00
BZip2.Decompress(packagesBz2, packagesBz2Decompressed, true);
}
catch (Exception e)
{
try
{
Console.WriteLine("Could not download " + link + "Packages.bz2: " + e.Message);
Console.WriteLine("Attempting to download " + link + "Packages.gz");
2021-10-18 18:12:12 -07:00
webClient.DownloadFile(new Uri(link + "Packages.gz"), $"./output/{cleanLink}/Packages.gz");
FileStream packagesGz = new FileInfo($"./output/{cleanLink}/Packages.gz").OpenRead();
FileStream packagesGzDecompressed = File.Create($"./output/{cleanLink}/Packages");
2021-10-17 22:18:24 -07:00
GZip.Decompress(packagesGz, packagesGzDecompressed, true);
2021-10-17 22:14:26 -07:00
}
catch (Exception _e)
{
try
{
Console.WriteLine("Could not download " + link + "Packages.gz: " + _e.Message);
Console.WriteLine("Attempting to download " + link + "Packages");
2021-10-18 18:12:12 -07:00
webClient.DownloadFile(new Uri(link + "Packages"), $"./output/{cleanLink}/Packages");
2021-10-17 22:14:26 -07:00
}
catch (Exception __e)
{
Console.WriteLine("Could not download " + link + "Packages: " + __e.Message);
Console.WriteLine("Could not locate packages file in " + link);
Console.WriteLine(__e.Message);
throw;
}
}
}
Thread.Sleep(500);
// Clean list of package links, names, and versions
List<CydiaPackage> packages = new List<CydiaPackage>();
2021-10-18 18:12:12 -07:00
foreach (string s in File.ReadAllText($"./output/{cleanLink}/Packages").Split("\n\n"))
2021-10-17 22:14:26 -07:00
{
string name = "";
string version = "";
string _link = "";
foreach(string s2 in s.Split('\n'))
{
if (s2.StartsWith("Package: "))
{
name = s2.Remove(0, 8).Trim();
}
else if (s2.StartsWith("Version: "))
{
version = s2.Remove(0, 8).Trim();
}
else if (s2.StartsWith("Filename: "))
{
_link = s2.Remove(0, 9).Trim();
}
}
packages.Add(new CydiaPackage(_link, name, version));
}
2021-10-18 18:12:12 -07:00
// remove last one because ????
2021-10-17 22:14:26 -07:00
packages.RemoveAt(packages.Count - 1);
2021-10-22 15:30:51 -07:00
List<string> failed = new List<string>();
2021-10-17 22:14:26 -07:00
foreach(CydiaPackage p in packages)
{
// Download all packages on repo
2021-10-22 15:30:51 -07:00
Random r = new Random();
2021-10-17 22:14:26 -07:00
try
{
2021-10-22 15:30:51 -07:00
string[] choppedUp = p.link.Split('/');
string fileToDownload = keepOg ? $"./output/{cleanLink}/" + choppedUp[choppedUp.Length - 1] : $"./output/{cleanLink}/" + p.name + "-" + p.version + ".deb";
if (File.Exists(fileToDownload))
2021-10-17 22:14:26 -07:00
{
2021-10-22 15:30:51 -07:00
fileToDownload += "_" + r.Next(0000, 9999);
2021-10-17 22:14:26 -07:00
}
2021-10-22 15:30:51 -07:00
webClient.DownloadFile(new Uri(link + p.link), fileToDownload);
Console.WriteLine("Successfully downloaded " + link + p.link + " as " + fileToDownload);
2021-10-17 22:14:26 -07:00
}
2021-10-22 15:30:51 -07:00
catch (Exception e)
2021-10-17 22:14:26 -07:00
{
2021-10-22 15:30:51 -07:00
Console.WriteLine("Could not download " + link + p.link);
Console.WriteLine(e.Message);
failed.Add(link + p.link);
2021-10-17 22:14:26 -07:00
}
}
2021-10-22 15:30:51 -07:00
Console.WriteLine("Finished downloading " + link);
if(failed.Count != 0) File.WriteAllLines($"./output/{cleanLink}/failed.txt", failed);
2021-10-17 22:14:26 -07:00
}
static void DownloadInstallerRepo(string link)
{
2021-10-18 18:12:12 -07:00
if (!link.StartsWith("https://") && !link.StartsWith("http://"))
{
link = "http://" + link;
}
if (!link.EndsWith("/"))
{
link += "/";
}
string cleanLink = link.Replace("http://", "").Replace("/", "_");
2021-10-17 22:14:26 -07:00
WebClient webClient = new WebClient();
try
{
Console.WriteLine("Attempting to download installer repo " + link);
2021-10-18 18:12:12 -07:00
webClient.DownloadFile(new Uri(link), $"./output/{cleanLink}/packages.xml");
2021-10-17 22:14:26 -07:00
}
catch (Exception e)
{
Console.WriteLine("Could not download package list from " + link + ": " + e.Message);
return;
}
2021-10-18 18:12:12 -07:00
List<string> plist = new List<string>(File.ReadAllLines($"./output/{cleanLink}/packages.xml"));
2021-10-17 22:14:26 -07:00
List<string> packages = new List<string>();
int i = 1;
foreach (string s in plist)
{
if (s.Contains("ocation</key>"))
{
packages.Add(plist[i].Split('<')[1].Remove(0, 7));
}
i++;
}
foreach (string s in packages)
{
Random r = new Random();
try
{
string[] choppedUp = s.Split('/');
2021-10-18 18:12:12 -07:00
string fileToDownload = $"./output/{cleanLink}/" + choppedUp[choppedUp.Length - 1];
2021-10-17 22:14:26 -07:00
if (File.Exists(fileToDownload))
{
fileToDownload += "_" + r.Next(0000, 9999);
}
webClient.DownloadFile(new Uri(s), fileToDownload);
Console.WriteLine("Successfully downloaded " + s);
}
catch (Exception e)
{
Console.WriteLine("Could not download " + s);
Console.WriteLine(e.Message);
}
}
}
}
}