public void ReplaceHostFile(string[] args)
        {
            string newPath = "";

            foreach (var argument in args)
            {
                if (argument.Contains("-newPath"))
                {
                    newPath = argument.Substring(10, (argument.Length - 11)).Replace("%20", " ");
                    if (!FileModification.FileExists(newPath))
                    {
                        newPath = "";
                    }
                }
            }

            if (newPath == "")
            {
                Console.WriteLine("No valid path for new system file was given.");
                if (!_unitTestSetup)
                {
                    Console.ReadKey();
                    Environment.Exit(1);
                }
            }

            string hostContent = FileModification.ReadFile(newPath);

            FileModification.WriteFile(GetHostFileLocation(), hostContent);
            if (!_unitTestSetup)
            {
                Environment.Exit(0);
            }
        }
示例#2
0
        private void AddAllLinesFromHostFileIntoEntryList()
        {
            string fileContent = FileModification.ReadFile(FileModification.GetHostFileLocation());

            using (StringReader reader = new StringReader(fileContent))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    HostEntry newEntry = new HostEntry()
                    {
                        Content = line, LineNumber = (CurrentLineNumber + 1), IsEnabled = false
                    };
                    HostEntries.Add(newEntry);
                    CurrentLineNumber++;
                }
            }
        }
示例#3
0
        public void CheckHostFileWasReplaced()
        {
            //Arrange
            string newHostFileLocation = $"{TempFolder.Name}{@"\hosts"}";

            _systemLevelModification = new SystemLevelModification(true, newHostFileLocation);
            string newHostFileTwoLocation = $"{TempFolder.Name}{@"\hosts2"}";

            FileModification.WriteFile(newHostFileTwoLocation, "#Test");

            //Act
            _systemLevelModification.CreateNewHostFile();
            string hostFileContentBefore = FileModification.ReadFile(newHostFileLocation);

            _systemLevelModification.ReplaceHostFile(new string[] { $"-newPath={newHostFileTwoLocation.Replace(" ", "%20")}" });
            string hostFileContentAfter = FileModification.ReadFile(newHostFileLocation);

            //Assert
            hostFileContentAfter.Should().NotBe(hostFileContentBefore);
        }
        public void SetCurrentConfigurationFromConfig()
        {
            if (FileModification.FileExists(ConfigurationPath))
            {
                string configurationContent = FileModification.ReadFile(ConfigurationPath);
                if (configurationContent == "")
                {
                    CreateNewConfiguration();
                }
                else
                {
                    Configuration = JsonConvert.DeserializeObject <CurrentProgramConfiguration>(configurationContent);
                }
            }
            else
            {
                FileModification.CreateFolderIfNotExists(ConfigurationPath.Replace("\\config.settings", ""));
                CreateNewConfiguration();
            }

            CopyCurrentConfigurationToTempConfig();
        }
        public void LoadCurrentUserConfiguration()
        {
            if (!FileModification.FileExists(GetProxyUserConfigurationPath()))
            {
                throw new WebProxyNoProfilesFileException($"WebProxy file \"{GetProxyUserConfigurationPath()}\" does not exist.");
            }
            else
            {
                string encryptedConfigurationContent = FileModification.ReadFile(GetProxyUserConfigurationPath());
                bool   fileIsBroken = false;
                if (encryptedConfigurationContent != "")
                {
                    try
                    {
                        string configurationContent =
                            EncryptingHelper.Decrypt(encryptedConfigurationContent, _configurationPassword);
                        var configuration =
                            JsonConvert.DeserializeObject <CurrentUserConfiguration>(configurationContent);
                        ApplyUserConfiguration(configuration, false);
                    }
                    catch (Exception)
                    {
                        fileIsBroken = true;
                    }
                }
                else
                {
                    fileIsBroken = true;
                }

                if (fileIsBroken)
                {
                    throw new WebProxyBrokenProfileConfigurationException($"WebProxy file \"{GetProxyUserConfigurationPath()}\" is broken.");
                }
            }
        }
 private string GetHostFileContent()
 {
     return(FileModification.ReadFile(FileModification.GetHostFileLocation()));
 }
示例#7
0
        public CurrentUserConfiguration OpenConfiguration(string password)
        {
            string encryptedConfigurationContent = FileModification.ReadFile(ConfigurationPath);

            return(DecryptConfiguration(encryptedConfigurationContent, password));
        }