Beispiel #1
0
        public static async Task <Common.Http.Client> GetAuthorizedClient(string?apiKey = null)
        {
            var client = new Common.Http.Client();

            client.Headers.Add(("X-API-KEY", await GetAPIKey(apiKey)));
            return(client);
        }
Beispiel #2
0
        public static async Task <Common.Http.Client> GetClient()
        {
            var client = new Common.Http.Client();

            client.Headers.Add((Consts.MetricsKeyHeader, await Metrics.GetMetricsKey()));
            return(client);
        }
Beispiel #3
0
        public static async Task <List <ModlistMetadata> > LoadFromGithub()
        {
            var client = new Common.Http.Client();

            Utils.Log("Loading ModLists from GitHub");
            var metadataResult = client.GetStringAsync(Consts.ModlistMetadataURL);
            var summaryResult  = client.GetStringAsync(Consts.ModlistSummaryURL);

            var metadata = (await metadataResult).FromJsonString <List <ModlistMetadata> >();

            try
            {
                var summaries = (await summaryResult).FromJsonString <List <ModListSummary> >().ToDictionary(d => d.MachineURL);

                foreach (var data in metadata)
                {
                    if (summaries.TryGetValue(data.Links.MachineURL, out var summary))
                    {
                        data.ValidationSummary = summary;
                    }
                }
            }
            catch (Exception)
            {
                // ignored
            }

            return(metadata.OrderBy(m => (m.ValidationSummary?.HasFailures ?? false ? 1 : 0, m.Title)).ToList());
        }
Beispiel #4
0
            public override async Task <bool> Verify(Archive archive)
            {
                var client = new Common.Http.Client();
                var result = await client.GetAsync(Url, errorsAsExceptions : false);

                return(result.IsSuccessStatusCode);
            }
Beispiel #5
0
            public override async Task <bool> Download(Archive a, AbsolutePath destination)
            {
                destination.Parent.CreateDirectory();
                var definition = await GetDefinition();

                await using var fs = await destination.Create();

                using var mmfile = MemoryMappedFile.CreateFromFile(fs, null, definition.Size, MemoryMappedFileAccess.ReadWrite, HandleInheritability.None, false);
                var client = new Common.Http.Client();

                using var queue = new WorkQueue();
                await definition.Parts.PMap(queue, async part =>
                {
                    Utils.Status($"Downloading {a.Name}", Percent.FactoryPutInRange(definition.Parts.Length - part.Index, definition.Parts.Length));
                    await using var ostream = mmfile.CreateViewStream(part.Offset, part.Size);
                    using var response      = await client.GetAsync($"{Url}/parts/{part.Index}");
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new HttpException((int)response.StatusCode, response.ReasonPhrase);
                    }
                    await response.Content.CopyToAsync(ostream);
                });

                return(true);
            }
Beispiel #6
0
        private static async Task <IEnumerable <UpdateRecord> > GetFeed(Uri uri)
        {
            var client = new Common.Http.Client();
            var data   = await client.GetStringAsync(uri);

            var reader  = XmlReader.Create(new StringReader(data));
            var results = SyndicationFeed.Load(reader);

            return(results.Items
                   .Select(itm =>
            {
                if (TryParseGameUrl(itm.Links.First(), out var game, out var modId))
                {
                    return new UpdateRecord
                    {
                        TimeStamp = itm.PublishDate.UtcDateTime,
                        Game = game,
                        ModId = modId
                    };
                }

                return null;
            })
                   .NotNull());
        }
Beispiel #7
0
        public static Common.Http.Client GetClient(IEnumerable <Cookie> cookies, string referer)
        {
            var client = new Common.Http.Client();

            client.Headers.Add(("Referrer", referer));
            client.Cookies.AddRange(cookies.Select(cookie => new System.Net.Cookie(cookie.Name, cookie.Value, cookie.Path, cookie.Domain)));
            return(client);
        }
Beispiel #8
0
            private async Task <CDNFileDefinition> GetDefinition()
            {
                var client = new Common.Http.Client();

                using var data = await client.GetAsync(Url + "/definition.json.gz");

                await using var gz = new GZipStream(await data.Content.ReadAsStreamAsync(), CompressionMode.Decompress);
                return(gz.FromJson <CDNFileDefinition>());
            }
            private async Task <HTTPDownloader.State?> Resolve()
            {
                var client = new Common.Http.Client();
                var body   = await client.GetHtmlAsync(Url);

                var node = body.DocumentNode.DescendantsAndSelf().First(d => d.HasClass("input") && d.HasClass("popsok") &&
                                                                        d.GetAttributeValue("aria-label", "") == "Download file");

                return(new HTTPDownloader.State(node.GetAttributeValue("href", "not-found")));
            }
Beispiel #10
0
        public static Common.Http.Client GetClient()
        {
            var client = new Common.Http.Client();

            if (Utils.HaveEncryptedJson(Consts.MetricsKeyHeader))
            {
                client.Headers.Add((Consts.MetricsKeyHeader, Utils.FromEncryptedJson <string>(Consts.MetricsKeyHeader)));
            }
            return(client);
        }
Beispiel #11
0
        public async Task LoadListsFromGithub()
        {
            var client = new Common.Http.Client();

            Utils.Log("Loading server whitelist");
            using (var response = await client.GetAsync(Consts.ServerWhitelistURL))
                using (var result = await response.Content.ReadAsStreamAsync())
                {
                    ServerWhitelist = result.FromYaml <ServerWhitelist>();
                    Utils.Log($"Loaded permissions for {ServerWhitelist.AllowedPrefixes?.Count ?? 0} servers and {ServerWhitelist.GoogleIDs?.Count ?? 0} Google Drive files");
                }
        }
Beispiel #12
0
        /// <summary>
        /// Given an archive hash, search the Wabbajack server for a matching .ini file
        /// </summary>
        /// <param name="hash"></param>
        /// <returns></returns>
        public static async Task <string?> GetModIni(Hash hash)
        {
            var client = new Common.Http.Client();

            try
            {
                return(await client.GetStringAsync(
                           $"{Consts.WabbajackBuildServerUri}indexed_files/{hash.ToHex()}/meta.ini"));
            }
            catch (HttpException)
            {
                return(null);
            }
        }
Beispiel #13
0
            public override async Task <bool> Download(Archive a, AbsolutePath destination)
            {
                destination.Parent.CreateDirectory();
                var definition = await GetDefinition();

                await using var fs = destination.Create();
                var client = new Common.Http.Client();
                await definition.Parts.DoProgress($"Downloading {a.Name}", async part =>
                {
                    fs.Position        = part.Offset;
                    using var response = await client.GetAsync($"{Url}/parts/{part.Index}");
                    if (!response.IsSuccessStatusCode)
                    {
                        throw new HttpException((int)response.StatusCode, response.ReasonPhrase);
                    }
                    await response.Content.CopyToAsync(fs);
                });

                return(true);
            }
            private async Task <HTTPDownloader.State> ToHttpState()
            {
                var initialURL = $"https://drive.google.com/uc?id={Id}&export=download";
                var client     = new Common.Http.Client();

                using var response = await client.GetAsync(initialURL);

                if (!response.IsSuccessStatusCode)
                {
                    throw new HttpException((int)response.StatusCode, response.ReasonPhrase);
                }
                var regex     = new Regex("(?<=/uc\\?export=download&amp;confirm=).*(?=;id=)");
                var confirm   = regex.Match(await response.Content.ReadAsStringAsync());
                var url       = $"https://drive.google.com/uc?export=download&confirm={confirm}&id={Id}";
                var httpState = new HTTPDownloader.State(url)
                {
                    Client = client
                };

                return(httpState);
            }
Beispiel #15
0
        public static async Task <bool> UploadPackagedInis(IEnumerable <Archive> archives)
        {
            archives = archives.ToArray(); // defensive copy
            Utils.Log($"Packaging {archives.Count()} inis");
            try
            {
                await using var ms = new MemoryStream();
                using (var z = new ZipArchive(ms, ZipArchiveMode.Create, true))
                {
                    foreach (var e in archives)
                    {
                        if (e.State == null)
                        {
                            continue;
                        }
                        var entry = z.CreateEntry(Path.GetFileName(e.Name));
                        await using var os = entry.Open();
                        await os.WriteAsync(Encoding.UTF8.GetBytes(string.Join("\n", e.State.GetMetaIni())));
                    }
                }

                var client   = new Common.Http.Client();
                var response = await client.PostAsync($"{Consts.WabbajackBuildServerUri}indexed_files/notify", new ByteArrayContent(ms.ToArray()));

                if (response.IsSuccessStatusCode)
                {
                    return(true);
                }

                Utils.Log("Error sending Inis");
                Utils.Log(await response.Content.ReadAsStringAsync());
                return(false);
            }
            catch (Exception ex)
            {
                Utils.Log(ex.ToString());
                return(false);
            }
        }
Beispiel #16
0
        public async Task Send(Channel channel, DiscordMessage message)
        {
            try
            {
                string url = channel switch
                {
                    Channel.Spam => _settings.SpamWebHook,
                    Channel.Ham => _settings.HamWebHook,
                    _ => null
                };
                if (url == null)
                {
                    return;
                }

                var client = new Common.Http.Client();
                await client.PostAsync(url, new StringContent(message.ToJson(true), Encoding.UTF8, "application/json"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.ToJson());
            }
        }
            private async Task <(Common.Http.Client, CDPTree, CollectedBNetInfo)> ResolveDownloadInfo()
            {
                var info = new CollectedBNetInfo();

                var login_info = await Utils.FromEncryptedJson <BethesdaNetData>(DataName);

                var client = new Common.Http.Client();

                client.Headers.Add(("User-Agent", "bnet"));
                foreach (var header in login_info.headers.Where(h => h.Key.ToLower().StartsWith("x-")))
                {
                    client.Headers.Add((header.Key, header.Value));
                }

                var posted = await client.PostAsync("https://api.bethesda.net/beam/accounts/external_login",
                                                    new StringContent(login_info.body, Encoding.UTF8, "application/json"));

                info.AccessToken = (await posted.Content.ReadAsStringAsync()).FromJsonString <BeamLoginResponse>().access_token;

                client.Headers.Add(("x-cdp-app", "UGC SDK"));
                client.Headers.Add(("x-cdp-app-ver", "0.9.11314/debug"));
                client.Headers.Add(("x-cdp-lib-ver", "0.9.11314/debug"));
                client.Headers.Add(("x-cdp-platform", "Win/32"));

                posted = await client.PostAsync("https://api.bethesda.net/cdp-user/auth",
                                                new StringContent("{\"access_token\": \"" + info.AccessToken + "\"}", Encoding.UTF8,
                                                                  "application/json"));

                info.CDPToken = (await posted.Content.ReadAsStringAsync()).FromJsonString <CDPLoginResponse>().token;

                client.Headers.Add(("X-Access-Token", info.AccessToken));
                var got = await client.GetAsync($"https://api.bethesda.net/mods/ugc-workshop/content/get?content_id={ContentId}");

                JObject data = JObject.Parse(await got.Content.ReadAsStringAsync());

                var content = data["platform"] !["response"] !["content"] !;
Beispiel #18
0
 public async Task DoesntReuseHttpMessages()
 {
     var client = new Common.Http.Client();
     // If we reuse the HTTP message this will throw a invalid operation exception
     await Assert.ThrowsAsync <HttpRequestException>(async() => await client.GetAsync("http://blerg.blaz.bloz.buz"));
 }
Beispiel #19
0
 public async Task Prepare()
 {
     AuthedClient = (await GetAuthedClient()) ?? throw new NotLoggedInError(this);
 }
Beispiel #20
0
 private Client(Common.Http.Client client)
 {
     _client = client;
 }