public AccountSettingsMessage SetAccountSettings() { string apiKey = string.Empty; string accountId = string.Empty; while (true) { Console.WriteLine("Input account details here. Letter case and spacing needs to be accurate."); Console.Write("Api Key (Empty for no change): "); var apiInput = Console.ReadLine(); if (apiInput != string.Empty) { apiKey = apiInput; _logger.AddLogEntry("User updated API key."); } Console.Write("Account ID (Empty for no change): "); var accountInput = Console.ReadLine(); if (accountInput != string.Empty) { accountId = accountInput; _logger.AddLogEntry("User updated Account ID."); } if ((accountId != string.Empty || accountId != null) && (apiKey != string.Empty || apiKey != null)) { SaveSettings(apiKey, accountId); Console.Clear(); return(new AccountSettingsMessage(apiKey, accountId)); } Console.WriteLine("Not all required details are supplied"); } }
public void Runner() { _apiRequests = new ApiRequests(); while (true) { if (!_settingsQueue.IsEmpty) { while (true) { _settingsQueue.TryDequeue(out var newSettings); // check if valid if (AssertValidSettings(newSettings)) { _apiRequests = new ApiRequests(newSettings.ApiKey, newSettings.AccountId); break; } else { // check if old settings are still valid _logger.AddLogEntry($"Could not apply new settings. Trying to revert back to old settings."); if (!_apiRequests.HasValidSettings()) { _logger.AddLogEntry($"Could not use old settings. Waiting for new settings to arrive."); SpinWait.SpinUntil(() => !_settingsQueue.IsEmpty); } else { break; } } } } else { var res = CollectCurrentRates(); if (res != null) { var content = JObject.Parse(res.ReadAsStringAsync().Result); _collectorQueue.Enqueue(content); } Thread.Sleep(TimeSpan.FromMilliseconds(_frequency)); } } }
public void DeleteFile(string filePath) { logger.AddLogEntry(string.Format("Deleting file '{0}'...", filePath)); try { if (File.Exists(filePath)) { File.Delete(filePath); logger.AddLogEntry(string.Format("File '{0}' deleted.", filePath)); } } catch (Exception ex) { logger.AddLogEntry(string.Format("ERROR: {0}", ex.Message)); } }
private byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV) { // Check arguments. if (plainText == null || plainText.Length <= 0) { throw new ArgumentNullException("plainText"); } if (Key == null || Key.Length <= 0) { throw new ArgumentNullException("Key"); } if (IV == null || IV.Length <= 0) { throw new ArgumentNullException("IV"); } byte[] encrypted; string ivString; // Create an AesManaged object // with the specified key and IV. using (AesManaged aesAlg = new AesManaged()) { aesAlg.Key = Key; aesAlg.IV = IV; ivString = EncodingTools.Base64EncodeByteArrayToString(aesAlg.IV); // Create an encryptor to perform the stream transform. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); // Create the streams used for encryption. try { using (MemoryStream msEncrypt = new MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) { //Write all data to the stream. swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } catch (Exception e) { encrypted = new byte[0]; _logger.AddLogEntry($"Failed to encrypt settings: {e.Message}"); } } // Return the encrypted bytes from the memory stream. return(encrypted); }
public bool IsUnix() { int platform = (int)Environment.OSVersion.Platform; if (platform == 5) { _logger.AddLogEntry("Operating system found to be unable to run this program."); Environment.Exit(1); } if (platform == 4 || platform == 6) { _logger.AddLogEntry("Operating system found to be Unix based."); return(true); } _logger.AddLogEntry("Operating system found to be Windows based."); return(false); }
public string DownloadFile(string fileUrl) { logger.AddLogEntry(string.Format("Downloading data from '{0}'...", fileUrl)); try { string fileAddress = Path.GetTempFileName(); using (var client = new WebClient()) { client.DownloadFile(fileUrl, fileAddress); } logger.AddLogEntry(string.Format("File saved to '{0}'...", fileAddress)); return(fileAddress); } catch (Exception ex) { logger.AddLogEntry(string.Format("ERROR: {0}", ex.Message)); return(null); } }
public void Process(DateTime date) { logger.AddLogEntry(string.Format("Starting data retrieval for {0}...", date.ToString())); SiteParser siteParser = new SiteParser(logger); string fileUrl = siteParser.GetDownoadableFileUrl(date); if (string.IsNullOrEmpty(fileUrl)) { return; } FileDownloader fileDownloader = new FileDownloader(logger); string filePath = fileDownloader.DownloadFile(fileUrl); if (string.IsNullOrEmpty(filePath)) { return; } FileManager fileManager = new FileManager(logger); try { fileManager.CreateTempDirectory(); List <string> extractedFiles = fileManager.ExtractFilesToTempDirectory(filePath); ProcessXmlFiles(extractedFiles); } catch (Exception ex) { logger.AddLogEntry(string.Format("ERROR: {0}", ex.Message)); } finally { fileManager.DeleteTempDirectory(); fileManager.DeleteFile(filePath); } }
public string AesDecrypt(string input, string pass, byte[] iv = null) { string decrpyted; byte[] encryptedMessage = EncodingTools.StringToByteArray(input); if (iv == null) { var inputIvTuple = FindIv(input); iv = inputIvTuple.Item1; encryptedMessage = inputIvTuple.Item2; // if still null :( if (iv == null || encryptedMessage == null) { _logger.AddLogEntry($"Failed to gather required information. iv: {iv}, encryptedMessage: {encryptedMessage}"); return(string.Empty); } } try { using (AesManaged aes = new AesManaged()) { aes.Key = Hashing.ComputeSha256(pass); aes.IV = iv; // Encrypt the string to an array of bytes. decrpyted = DecryptStringFromBytes_Aes(encryptedMessage, aes.Key, aes.IV); } } catch (Exception e) { _logger.AddLogEntry($"Failed to decrypt message: {e}"); return(string.Empty); } return(decrpyted); }
public string GetDownoadableFileUrl(DateTime date) { string url = ConfigManager.FileAddressUrl.Replace("@DATE", date.ToString("dd.MM.yyyy")); logger.AddLogEntry(string.Format("Getting data from '{0}'...", url)); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); logger.AddLogEntry("Reading retrieved data..."); Stream resStream = response.GetResponseStream(); StreamReader reader = new StreamReader(resStream); string resultingPage = reader.ReadToEnd(); logger.AddLogEntry("Parsing page for file download address..."); int indexOfBikForm = resultingPage.IndexOf(@"<a name=""BikFormData"">"); int indexOfLink = indexOfBikForm == -1 ? -1 : resultingPage.IndexOf(@"<a href=", indexOfBikForm); int indexOfAddressStart = indexOfLink == -1 ? -1 : resultingPage.IndexOf(@"""", indexOfLink + 7); int indexOfAddressEnd = indexOfAddressStart == -1 ? -1 : resultingPage.IndexOf(@"""", indexOfAddressStart + 1); string fileAddress = indexOfAddressEnd == -1 ? "" : resultingPage.Substring(indexOfAddressStart + 1, indexOfAddressEnd - indexOfAddressStart - 1); if (fileAddress != "" && fileAddress[0] == '/') { fileAddress = response.ResponseUri.Scheme + "://" + response.ResponseUri.Host + fileAddress; } return(fileAddress); } catch (Exception ex) { logger.AddLogEntry(string.Format("ERROR: {0}", ex.Message)); return(null); } }