2020-01-06 00:21:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Lib.Validation;
|
|
|
|
|
using File = System.IO.File;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Lib.Downloaders
|
|
|
|
|
{
|
|
|
|
|
// IPS4 is the site used by LoversLab, VectorPlexus, etc. the general mechanics of each site are the
|
|
|
|
|
// same, so we can fairly easily abstract the state.
|
|
|
|
|
// Pass in the state type via TState
|
|
|
|
|
public abstract class AbstractIPS4Downloader<TDownloader, TState> : AbstractNeedsLoginDownloader, IDownloader
|
|
|
|
|
where TState : AbstractIPS4Downloader<TDownloader, TState>.State<TDownloader>, new()
|
|
|
|
|
where TDownloader : IDownloader
|
|
|
|
|
{
|
|
|
|
|
public override string SiteName { get; }
|
|
|
|
|
public override Uri SiteURL { get; }
|
2020-01-22 19:57:48 +00:00
|
|
|
|
|
2020-01-06 00:21:05 +00:00
|
|
|
|
public async Task<AbstractDownloadState> GetDownloaderState(dynamic archiveINI)
|
|
|
|
|
{
|
|
|
|
|
Uri url = DownloaderUtils.GetDirectURL(archiveINI);
|
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var absolute = true;
|
|
|
|
|
if (url == null || url.Host != SiteURL.Host) return null;
|
|
|
|
|
if (!url.PathAndQuery.StartsWith("/files/file/"))
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(url.Query)) return null;
|
|
|
|
|
if (!url.Query.Substring(1).StartsWith("/files/file/")) return null;
|
|
|
|
|
absolute = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var id = HttpUtility.ParseQueryString(url.Query)["r"];
|
|
|
|
|
var file = absolute
|
|
|
|
|
? url.AbsolutePath.Split('/').Last(s => s != "")
|
|
|
|
|
: url.Query.Substring(1).Split('/').Last(s => s != "");
|
|
|
|
|
|
2020-01-06 00:21:05 +00:00
|
|
|
|
return new TState
|
|
|
|
|
{
|
|
|
|
|
FileID = id,
|
|
|
|
|
FileName = file
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class State<TDownloader> : AbstractDownloadState where TDownloader : IDownloader
|
|
|
|
|
{
|
|
|
|
|
public string FileID { get; set; }
|
|
|
|
|
public string FileName { get; set; }
|
|
|
|
|
|
2020-01-22 19:57:48 +00:00
|
|
|
|
private static bool IsHTTPS => Downloader.SiteURL.AbsolutePath.StartsWith("https://");
|
|
|
|
|
private static string URLPrefix => IsHTTPS ? "https://" : "http://";
|
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
private static string Site => string.IsNullOrWhiteSpace(Downloader.SiteURL.Query)
|
|
|
|
|
? $"{URLPrefix}{Downloader.SiteURL.Host}"
|
|
|
|
|
: Downloader.SiteURL.ToString();
|
|
|
|
|
|
2020-01-11 04:15:53 +00:00
|
|
|
|
public override object[] PrimaryKey
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-01-27 17:16:44 +00:00
|
|
|
|
return FileID == null
|
|
|
|
|
? new object[] {Downloader.SiteURL, FileName}
|
|
|
|
|
: new object[] {Downloader.SiteURL, FileName, FileID};
|
2020-01-11 04:15:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
|
|
|
|
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-18 19:57:26 +00:00
|
|
|
|
public override async Task<bool> Download(Archive a, string destination)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
|
|
|
|
var stream = await ResolveDownloadStream();
|
2020-01-18 20:52:09 +00:00
|
|
|
|
using (var file = File.Open(destination, FileMode.Create))
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
|
|
|
|
stream.CopyTo(file);
|
|
|
|
|
}
|
2020-01-18 19:57:26 +00:00
|
|
|
|
return true;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Stream> ResolveDownloadStream()
|
|
|
|
|
{
|
|
|
|
|
var downloader = (AbstractNeedsLoginDownloader)(object)DownloadDispatcher.GetInstance<TDownloader>();
|
|
|
|
|
|
|
|
|
|
TOP:
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var csrfurl = FileID == null
|
|
|
|
|
? $"{Site}/files/file/{FileName}/?do=download"
|
|
|
|
|
: $"{Site}/files/file/{FileName}/?do=download&r={FileID}";
|
2020-01-06 00:21:05 +00:00
|
|
|
|
var html = await downloader.AuthedClient.GetStringAsync(csrfurl);
|
|
|
|
|
|
|
|
|
|
var pattern = new Regex("(?<=csrfKey=).*(?=[&\"\'])|(?<=csrfKey: \").*(?=[&\"\'])");
|
|
|
|
|
var matches = pattern.Matches(html).Cast<Match>();
|
|
|
|
|
|
|
|
|
|
var csrfKey = matches.Where(m => m.Length == 32).Select(m => m.ToString()).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (csrfKey == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var url = FileID == null
|
|
|
|
|
? $"{Site}/files/file/{FileName}/?do=download&confirm=1&t=1&csrfKey={csrfKey}"
|
|
|
|
|
: $"{Site}/files/file/{FileName}/?do=download&r={FileID}&confirm=1&t=1&csrfKey={csrfKey}";
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var streamResult = await downloader.AuthedClient.GetAsync(url);
|
|
|
|
|
if (streamResult.StatusCode != HttpStatusCode.OK)
|
|
|
|
|
{
|
|
|
|
|
Utils.Error(new InvalidOperationException(), $"{downloader.SiteName} servers reported an error for file: {FileID}");
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
var contentType = streamResult.Content.Headers.ContentType;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
|
2020-01-27 17:16:44 +00:00
|
|
|
|
if (contentType.MediaType != "application/json")
|
|
|
|
|
return await streamResult.Content.ReadAsStreamAsync();
|
|
|
|
|
|
|
|
|
|
// Sometimes LL hands back a json object telling us to wait until a certain time
|
|
|
|
|
var times = (await streamResult.Content.ReadAsStringAsync()).FromJSONString<WaitResponse>();
|
|
|
|
|
var secs = times.Download - times.CurrentTime;
|
|
|
|
|
for (int x = 0; x < secs; x++)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
2020-01-27 17:16:44 +00:00
|
|
|
|
Utils.Status($"Waiting for {secs} at the request of {downloader.SiteName}", x * 100 / secs);
|
|
|
|
|
await Task.Delay(1000);
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
2020-01-27 17:16:44 +00:00
|
|
|
|
Utils.Status("Retrying download");
|
|
|
|
|
goto TOP;
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class WaitResponse
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("download")]
|
|
|
|
|
public int Download { get; set; }
|
|
|
|
|
[JsonProperty("currentTime")]
|
|
|
|
|
public int CurrentTime { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-13 22:55:55 +00:00
|
|
|
|
public override async Task<bool> Verify(Archive a)
|
2020-01-06 00:21:05 +00:00
|
|
|
|
{
|
|
|
|
|
var stream = await ResolveDownloadStream();
|
|
|
|
|
if (stream == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
stream.Close();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override IDownloader GetDownloader()
|
|
|
|
|
{
|
|
|
|
|
return DownloadDispatcher.GetInstance<TDownloader>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string GetReportEntry(Archive a)
|
|
|
|
|
{
|
2020-01-27 17:16:44 +00:00
|
|
|
|
return $"* {((INeedsLogin)GetDownloader()).SiteName} - [{a.Name}]({Site}/files/file/{FileName}/?do=download&r={FileID})";
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
2020-01-11 04:15:53 +00:00
|
|
|
|
|
|
|
|
|
public override string[] GetMetaIni()
|
|
|
|
|
{
|
|
|
|
|
if (FileID != null)
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
"[General]",
|
2020-01-27 17:16:44 +00:00
|
|
|
|
$"directURL={Site}/files/file/{FileName}/?do=download&r={FileID}&confirm=1&t=1"
|
2020-01-11 04:15:53 +00:00
|
|
|
|
};
|
|
|
|
|
return new[]
|
|
|
|
|
{
|
|
|
|
|
"[General]",
|
2020-01-27 17:16:44 +00:00
|
|
|
|
$"directURL={Site}/files/file/{FileName}"
|
2020-01-11 04:15:53 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static AbstractNeedsLoginDownloader Downloader => (AbstractNeedsLoginDownloader)(object)DownloadDispatcher.GetInstance<TDownloader>();
|
2020-01-06 00:21:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected AbstractIPS4Downloader(Uri loginUri, string encryptedKeyName, string cookieDomain) :
|
|
|
|
|
base(loginUri, encryptedKeyName, cookieDomain, "ips4_member_id")
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|