private void RetrieveRessourceTable(int counter = 0)
        {
            if (!_keyRetrieved)
            {
                RetrieveKey();
            }

            if (counter == 3)
            {
                ClientContext.Logger.LogWarning($"Failed to reach API! ({counter} retries!)", "", "", 0);
                return;
            }

            try {
                using (WebClient webClient = new WebClient())
                    using (MemoryStream input = new MemoryStream(webClient.DownloadData($"http://{RandomProvider()}/0000000000000000-1")))
                        using (MemoryStream output = new MemoryStream()) {
                            SecurityExtension.DecryptAES(input, output, Unprotect(_token));
                            output.Position = 0;

                            Ressources           = JsonConvert.DeserializeObject <RessourceTableView>(Encoding.UTF8.GetString(output.ToArray()));
                            _ressourcesRetrieved = true;
                            return;
                        }
            } catch (Exception e) {
                ClientContext.Logger.LogError(e);
            }

            RetrieveRessourceTable(++counter);
        }
Esempio n. 2
0
        static void AppendEncrypt(string token, string source, string destination)
        {
            RessourceTableView tableView = null;

            using (Stream input = File.OpenRead(Path.Combine(destination, "0000000000000000-1")))
                using (MemoryStream output = new MemoryStream()) {
                    SecurityExtension.DecryptAES(input, output, token);
                    output.Position = 0;
                    tableView       = JsonConvert.DeserializeObject <RessourceTableView>(Encoding.UTF8.GetString(output.ToArray()));
                }

            int    counter   = tableView.Table.Values.Max(x => x.Container) - 1;
            Stream container = null;

            foreach (string path in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories))
            {
                string encryptedPath = ByteArrayToString(token.Hash(Path.GetRelativePath(source, path).Replace(@"\", "/")));
                if (tableView.Table.ContainsKey(encryptedPath))
                {
                    continue;
                }
                Console.WriteLine("{0}  --  {1}", Path.GetRelativePath(source, path).Replace(@"\", "/"), encryptedPath);

                using (MemoryStream output = new MemoryStream())
                    using (Stream input = FileCompressed(path)) { //File.Open(path, FileMode.Open, FileAccess.Read)) {
                        SecurityExtension.EncryptAES(input, output, token);

                        if (container == null || container.Length + output.Length > (50 * 1024 * 1024))
                        {
                            container?.Dispose();
                            container = File.Open(Path.Combine(destination, $"0000000000000000-{++counter}"), FileMode.OpenOrCreate);
                            container.Seek(0, SeekOrigin.End); // seek end

                            Console.WriteLine("Current Archive: {0}", counter);
                        }

                        output.Position = 0;
                        tableView.Table.Add(encryptedPath, new RessourceTableItemView {
                            Container = counter,
                            Length    = output.Length,
                            Offset    = container.Position
                        });

                        output.CopyTo(container);
                    }
            }

            container?.Dispose();

            using (Stream output = File.Create(Path.Combine(destination, "0000000000000000-1")))
                using (MemoryStream input = new MemoryStream(
                           Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(tableView)))) {
                    SecurityExtension.EncryptAES(input, output, token);
                }
        }
        public async Task Retrieve(string ressource, Stream target, int counter = 0)
        {
            if (!_keyRetrieved)
            {
                RetrieveKey();
            }

            if (!_ressourcesRetrieved)
            {
                RetrieveRessourceTable();
            }

            if (counter == 3)
            {
                ClientContext.Logger.LogWarning($"Failed to retrieve ressource '{ressource}'! ({counter} retries!)");
                return;
            }

            try {
                string res = ByteArrayToString(Unprotect(_token).Hash(ressource));

                if (!Ressources.Table.TryGetValue(res, out RessourceTableItemView itemView))
                {
                    ClientContext.Logger.LogInformation($"File {ressource} does not exsit in the ressource table!");
                    return;
                }

                using (WebClient webClient = new WebClientWithRange(itemView.Offset, itemView.Offset + (itemView.Length - 1)))
                    using (MemoryStream input = new MemoryStream(await webClient.DownloadDataTaskAsync($"http://{RandomProvider()}/0000000000000000-{itemView.Container}")))
                        using (MemoryStream output = new MemoryStream()) {
                            SecurityExtension.DecryptAES(input, output, Unprotect(_token));
                            output.Position = 0;

                            using (GZipStream decompressor = new GZipStream(output, CompressionMode.Decompress, true)) {
                                decompressor.CopyTo(target);
                            }
                        }
            } catch (Exception e) {
                ClientContext.Logger.LogError(e);
                await Retrieve(ressource, target, ++counter);
            }
        }
Esempio n. 4
0
        static bool CheckExists(string token, string destination)
        {
            if (!File.Exists(Path.Combine(destination, "0000000000000000-0")) ||
                !File.ReadAllBytes(Path.Combine(destination, "0000000000000000-0")).SequenceEqual(token.Hash()))
            {
                return(false);
            }

            try {
                if (File.Exists(Path.Combine(destination, "0000000000000000-1")))
                {
                    using (Stream input = File.OpenRead(Path.Combine(destination, "0000000000000000-1")))
                        using (MemoryStream output = new MemoryStream()) {
                            SecurityExtension.DecryptAES(input, output, token);
                            output.Position = 0;

                            var result = JsonConvert.DeserializeObject <RessourceTableView>(Encoding.UTF8.GetString(output.ToArray()));
                            return(result.TokenHash.SequenceEqual(token.Hash()));
                        }
                }
            } catch { }

            return(false);
        }