예제 #1
0
        public Task StartAsync(Tool tool)
        {
            // start the desired process
            var arguments = string.Join(" ", tool.Settings.GetArguments(tool));
            var startInfo = new ProcessStartInfo
            {
                FileName = tool.ExecutablePath,
                Arguments = arguments,
                WorkingDirectory = tool.WorkingDirectory,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                WindowStyle = ProcessWindowStyle.Hidden,
            };

            Process process = Process.Start(startInfo);

            if (process == null)
            {
                throw new TorSharpException($"Unable to start the process '{tool.ExecutablePath}'.");
            }

            _processes.Add(process);

            return CompletedTask;
        }
예제 #2
0
        private async Task InitializeAsync()
        {
            _settings.ZippedToolsDirectory = GetAbsoluteCreate(_settings.ZippedToolsDirectory);
            _settings.ExtractedToolsDirectory = GetAbsoluteCreate(_settings.ExtractedToolsDirectory);

            _tor = Extract(TorToolSettings);
            _privoxy = Extract(PrivoxyToolSettings);

            if (_settings.TorControlPassword != null && _settings.HashedTorControlPassword == null)
            {
                _settings.HashedTorControlPassword = _torPasswordHasher.HashPassword(_settings.TorControlPassword);
            }

            await ConfigureAndStartAsync(_tor, new TorConfigurationDictionary(_tor.DirectoryPath)).ConfigureAwait(false);
            await ConfigureAndStartAsync(_privoxy, new PrivoxyConfigurationDictionary()).ConfigureAwait(false);
        }
        public Task StartAsync(Tool tool)
        {
            IntPtr desktopHandle = WindowsUtility.CreateDesktop(DesktopName);

            // embrace the madness -- it seems Windows always wants exactly one instance of "ctfmon.exe" in the new desktop
            var ctfmonStartInfo = new ProcessStartInfo {FileName = "ctfmon.exe", WorkingDirectory = "."};
            WindowsApi.PROCESS_INFORMATION ctfmonProcess = WindowsUtility.CreateProcess(ctfmonStartInfo, DesktopName);
            AssociateWithJob(ctfmonProcess, false);

            // start the desired process
            var arguments = string.Join(" ", tool.Settings.GetArguments(tool));
            var startInfo = new ProcessStartInfo
            {
                FileName = tool.ExecutablePath,
                Arguments = arguments,
                WorkingDirectory = tool.WorkingDirectory
            };
            WindowsApi.PROCESS_INFORMATION targetProcess = WindowsUtility.CreateProcess(startInfo, DesktopName);
            AssociateWithJob(targetProcess, true);

            WindowsApi.CloseDesktop(desktopHandle);
            return Task.FromResult((object) null);
        }
예제 #4
0
        /// <summary>Extracts the provided tool.</summary>
        /// <param name="tool">The tool to extract.</param>
        /// <param name="reloadTool">Whether or not to reload the tool to the disk whether or not it already exists.</param>
        /// <returns>True if the tool was extracted, false if the tool already existed.</returns>
        private bool ExtractTool(Tool tool, bool reloadTool)
        {
            if (!reloadTool && File.Exists(tool.ExecutablePath))
            {
                return false;
            }

            if (Directory.Exists(tool.DirectoryPath))
            {
                Directory.Delete(tool.DirectoryPath, true);
            }

            ZipFile.ExtractToDirectory(tool.ZipPath, tool.DirectoryPath);
            if (!tool.Settings.IsNested)
            {
                return true;
            }

            string[] entries = Directory.EnumerateFileSystemEntries(tool.DirectoryPath).ToArray();
            int fileCount = entries.Count(e => !File.GetAttributes(e).HasFlag(FileAttributes.Directory));
            if (fileCount > 0 || entries.Length != 1)
            {
                throw new TorSharpException($"The tool directory for {tool.Name} was expected to only contain exactly one directory.");
            }

            // move each nested file to its parent directory, accounting for a file or directory with the same name as its directory
            string nestedDirectory = entries.First();
            string nestedDirectoryName = Path.GetFileName(nestedDirectory);
            string[] nestedEntries = Directory.EnumerateFileSystemEntries(nestedDirectory).ToArray();

            string duplicate = null;
            foreach (string nestedEntry in nestedEntries)
            {
                string fileName = Path.GetFileName(nestedEntry);
                if (fileName == nestedDirectoryName)
                {
                    duplicate = nestedEntry;
                    continue;
                }

                string newLocation = Path.Combine(tool.DirectoryPath, fileName);
                Directory.Move(nestedEntry, newLocation);
            }

            if (duplicate != null)
            {
                string temporaryLocation = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                Directory.Move(duplicate, temporaryLocation);
                Directory.Delete(nestedDirectory, false);
                Directory.Move(temporaryLocation, nestedDirectory);
            }
            else
            {
                Directory.Delete(nestedDirectory, false);
            }

            return true;
        }
예제 #5
0
 private async Task<Tool> ConfigureAndStartAsync(Tool tool, IConfigurationDictionary configurationDictionary)
 {
     var configurer = new LineByLineConfigurer(configurationDictionary, new ConfigurationFormat());
     await configurer.ApplySettings(tool.ConfigurationPath, _settings).ConfigureAwait(false);
     await _toolRunner.StartAsync(tool).ConfigureAwait(false);
     return tool;
 }