Пример #1
0
        private static async Task InstallHwiAsync(string fullBaseDirectory, string hwiDir)
        {
            string hwiSoftwareDir = Path.Combine(fullBaseDirectory, "Hwi", "Software");

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                string hwiLinuxZip = Path.Combine(hwiSoftwareDir, "hwi-linux64.zip");
                await IoHelpers.BetterExtractZipToDirectoryAsync(hwiLinuxZip, hwiDir);

                Logger.LogInfo($"Extracted {hwiLinuxZip} to {hwiDir}.", nameof(HwiProcessManager));
            }
            else             // OSX
            {
                string hwiOsxZip = Path.Combine(hwiSoftwareDir, "hwi-osx64.zip");
                await IoHelpers.BetterExtractZipToDirectoryAsync(hwiOsxZip, hwiDir);

                Logger.LogInfo($"Extracted {hwiOsxZip} to {hwiDir}.", nameof(HwiProcessManager));
            }

            // Make sure there's sufficient permission.
            string chmodHwiDirCmd = $"chmod -R 750 {hwiDir}";

            EnvironmentHelpers.ShellExec(chmodHwiDirCmd);
            Logger.LogInfo($"Shell command executed: {chmodHwiDirCmd}.", nameof(HwiProcessManager));
        }
Пример #2
0
 public static void OpenFileInTextEditor(string filePath)
 {
     if (File.Exists(filePath))
     {
         Process process = null;
         try
         {
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             {
                 process = Process.Start(new ProcessStartInfo {
                     FileName = filePath, UseShellExecute = true
                 });
             }
             else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
             {
                 // If no associated application/json MimeType is found xdg-open opens retrun error
                 // but it tries to open it anyway using the console editor (nano, vim, other..)
                 EnvironmentHelpers.ShellExec($"gedit {filePath} || xdg-open {filePath}");
             }
             else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
             {
                 process = Process.Start(new ProcessStartInfo {
                     FileName = "open", Arguments = "-e " + filePath, CreateNoWindow = true
                 });
             }
         }
         finally
         {
             process?.Dispose();
         }
     }
 }
Пример #3
0
        public static void OpenFileInTextEditor(string filePath)
        {
            if (File.Exists(filePath))
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    // If no associated application/json MimeType is found xdg-open opens retrun error
                    // but it tries to open it anyway using the console editor (nano, vim, other..)
                    EnvironmentHelpers.ShellExec($"gedit {filePath} || xdg-open {filePath}", waitForExit: false);
                }
                else
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        bool openWithNotepad = true;                         // If there is an exception with the registry read we use notepad.

                        try
                        {
                            openWithNotepad = !EnvironmentHelpers.IsFileTypeAssociated("json");
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex);
                        }

                        if (openWithNotepad)
                        {
                            // Open file using Notepad.
                            using Process process = Process.Start(new ProcessStartInfo
                            {
                                FileName        = "notepad.exe",
                                Arguments       = filePath,
                                CreateNoWindow  = true,
                                UseShellExecute = false
                            });
                            return;                             // Opened with notepad, return.
                        }
                    }

                    // Open file wtih the default editor.
                    using (Process process = Process.Start(new ProcessStartInfo
                    {
                        FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? filePath : "open",
                        Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {filePath}" : "",
                        CreateNoWindow = true,
                        UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                    }))
                    { }
                }
            }
            else
            {
                NotificationHelpers.Error("File not found.");
            }
        }
Пример #4
0
 public static void OpenBrowser(string url)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
     {
         // If no associated application/json MimeType is found xdg-open opens retrun error
         // but it tries to open it anyway using the console editor (nano, vim, other..)
         EnvironmentHelpers.ShellExec($"xdg-open {url}", waitForExit: false);
     }
     else
     {
         using (Process process = Process.Start(new ProcessStartInfo {
             FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? url : "open",
             Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {url}" : "",
             CreateNoWindow = true,
             UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
         })) { }
     }
 }
Пример #5
0
 public static void OpenFileInTextEditor(string filePath)
 {
     if (File.Exists(filePath))
     {
         if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
         {
             // If no associated application/json MimeType is found xdg-open opens retrun error
             // but it tries to open it anyway using the console editor (nano, vim, other..)
             EnvironmentHelpers.ShellExec($"gedit {filePath} || xdg-open {filePath}");
         }
         else
         {
             using (Process process = Process.Start(new ProcessStartInfo
             {
                 FileName = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? filePath : "open",
                 Arguments = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? $"-e {filePath}" : "",
                 CreateNoWindow = true,
                 UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
             })) { }
         }
     }
 }
Пример #6
0
        private static void InstallTor(string fullBaseDirectory, string torDir)
        {
            string torDaemonsDir = Path.Combine(fullBaseDirectory, "TorDaemons");

            string dataZip = Path.Combine(torDaemonsDir, "data-folder.zip");

            IoHelpers.BetterExtractZipToDirectoryAsync(dataZip, torDir).GetAwaiter().GetResult();
            Logger.LogInfo <TorProcessManager>($"Extracted {dataZip} to {torDir}.");

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                string torWinZip = Path.Combine(torDaemonsDir, "tor-win32.zip");
                IoHelpers.BetterExtractZipToDirectoryAsync(torWinZip, torDir).GetAwaiter().GetResult();
                Logger.LogInfo <TorProcessManager>($"Extracted {torWinZip} to {torDir}.");
            }
            else             // Linux or OSX
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    string torLinuxZip = Path.Combine(torDaemonsDir, "tor-linux64.zip");
                    IoHelpers.BetterExtractZipToDirectoryAsync(torLinuxZip, torDir).GetAwaiter().GetResult();
                    Logger.LogInfo <TorProcessManager>($"Extracted {torLinuxZip} to {torDir}.");
                }
                else                 // OSX
                {
                    string torOsxZip = Path.Combine(torDaemonsDir, "tor-osx64.zip");
                    IoHelpers.BetterExtractZipToDirectoryAsync(torOsxZip, torDir).GetAwaiter().GetResult();
                    Logger.LogInfo <TorProcessManager>($"Extracted {torOsxZip} to {torDir}.");
                }

                // Make sure there's sufficient permission.
                string chmodTorDirCmd = $"chmod -R 750 {torDir}";
                EnvironmentHelpers.ShellExec(chmodTorDirCmd);
                Logger.LogInfo <TorProcessManager>($"Shell command executed: {chmodTorDirCmd}.");
            }
        }
Пример #7
0
        public void Start(bool ensureRunning, string dataDir)
        {
            if (TorSocks5EndPoint is null)
            {
                return;
            }

            new Thread(delegate()              // Don't ask. This is the only way it worked on Win10/Ubuntu18.04/Manjuro(1 processor VM)/Fedora(1 processor VM)
            {
                try
                {
                    // 1. Is it already running?
                    // 2. Can I simply run it from output directory?
                    // 3. Can I copy and unzip it from assets?
                    // 4. Throw exception.

                    try
                    {
                        if (IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
                        {
                            Logger.LogInfo <TorProcessManager>("Tor is already running.");
                            return;
                        }

                        var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);
                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            if (!fullBaseDirectory.StartsWith('/'))
                            {
                                fullBaseDirectory.Insert(0, "/");
                            }
                        }

                        var torDir = Path.Combine(dataDir, "tor");

                        var torPath = "";
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            torPath = $@"{torDir}\Tor\tor.exe";
                        }
                        else                         // Linux or OSX
                        {
                            torPath = $@"{torDir}/Tor/tor";
                        }

                        if (!File.Exists(torPath))
                        {
                            Logger.LogInfo <TorProcessManager>($"Tor instance NOT found at {torPath}. Attempting to acquire it...");
                            InstallTor(fullBaseDirectory, torDir);
                        }
                        else if (!IoHelpers.CheckExpectedHash(torPath, Path.Combine(fullBaseDirectory, "TorDaemons")))
                        {
                            Logger.LogInfo <TorProcessManager>($"Updating Tor...");

                            string backupTorDir = $"{torDir}_backup";
                            if (Directory.Exists(backupTorDir))
                            {
                                Directory.Delete(backupTorDir, true);
                            }
                            Directory.Move(torDir, backupTorDir);

                            InstallTor(fullBaseDirectory, torDir);
                        }
                        else
                        {
                            Logger.LogInfo <TorProcessManager>($"Tor instance found at {torPath}.");
                        }

                        string torArguments = $"--SOCKSPort {TorSocks5EndPoint}";
                        if (!string.IsNullOrEmpty(LogFile))
                        {
                            IoHelpers.EnsureContainingDirectoryExists(LogFile);
                            var logFileFullPath = Path.GetFullPath(LogFile);
                            torArguments       += $" --Log \"notice file {logFileFullPath}\"";
                        }

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            TorProcess = Process.Start(new ProcessStartInfo {
                                FileName               = torPath,
                                Arguments              = torArguments,
                                UseShellExecute        = false,
                                CreateNoWindow         = true,
                                RedirectStandardOutput = true
                            });
                            Logger.LogInfo <TorProcessManager>($"Starting Tor process with Process.Start.");
                        }
                        else                         // Linux and OSX
                        {
                            string runTorCmd = $"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:={torDir}/Tor && export LD_LIBRARY_PATH && cd {torDir}/Tor && ./tor {torArguments}";
                            EnvironmentHelpers.ShellExec(runTorCmd, false);
                            Logger.LogInfo <TorProcessManager>($"Started Tor process with shell command: {runTorCmd}.");
                        }

                        if (ensureRunning)
                        {
                            Task.Delay(3000).ConfigureAwait(false).GetAwaiter().GetResult();                             // dotnet brainfart, ConfigureAwait(false) IS NEEDED HERE otherwise (only on) Manjuro Linux fails, WTF?!!
                            if (!IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
                            {
                                throw new TorException("Attempted to start Tor, but it is not running.");
                            }
                            Logger.LogInfo <TorProcessManager>("Tor is running.");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new TorException("Could not automatically start Tor. Try running Tor manually.", ex);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError <TorProcessManager>(ex);
                }
            }).Start();
        }
Пример #8
0
        public void Start(bool ensureRunning, string dataDir)
        {
            new Thread(delegate()              // Don't ask. This is the only way it worked on Win10/Ubuntu18.04/Manjuro(1 processor VM)/Fedora(1 processor VM)
            {
                try
                {
                    // 1. Is it already running?
                    // 2. Can I simply run it from output directory?
                    // 3. Can I copy and unzip it from assets?
                    // 4. Throw exception.

                    try
                    {
                        if (IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
                        {
                            Logger.LogInfo <TorProcessManager>("Tor is already running.");
                            return;
                        }

                        var fullBaseDirectory = Path.GetFullPath(AppContext.BaseDirectory);
                        if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            if (!fullBaseDirectory.StartsWith('/'))
                            {
                                fullBaseDirectory.Insert(0, "/");
                            }
                        }

                        var torDir = Path.Combine(dataDir, "tor");

                        var torPath = "";
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            torPath = $@"{torDir}\Tor\tor.exe";
                        }
                        else                         // Linux or OSX
                        {
                            torPath = $@"{torDir}/Tor/tor";
                        }

                        if (!File.Exists(torPath))
                        {
                            Logger.LogInfo <TorProcessManager>($"Tor instance NOT found at {torPath}. Attempting to acquire it...");
                            string torDaemonsDir = Path.Combine(fullBaseDirectory, "TorDaemons");

                            string dataZip = Path.Combine(torDaemonsDir, "data-folder.zip");
                            IoHelpers.BetterExtractZipToDirectoryAsync(dataZip, torDir).GetAwaiter().GetResult();
                            Logger.LogInfo <TorProcessManager>($"Extracted {dataZip} to {torDir}.");

                            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                            {
                                string torWinZip = Path.Combine(torDaemonsDir, "tor-win32.zip");
                                IoHelpers.BetterExtractZipToDirectoryAsync(torWinZip, torDir).GetAwaiter().GetResult();
                                Logger.LogInfo <TorProcessManager>($"Extracted {torWinZip} to {torDir}.");
                            }
                            else                             // Linux or OSX
                            {
                                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                                {
                                    string torLinuxZip = torLinuxZip = Path.Combine(torDaemonsDir, "tor-linux64.zip");
                                    IoHelpers.BetterExtractZipToDirectoryAsync(torLinuxZip, torDir).GetAwaiter().GetResult();
                                    Logger.LogInfo <TorProcessManager>($"Extracted {torLinuxZip} to {torDir}.");
                                }
                                else                                 // OSX
                                {
                                    string torOsxZip = Path.Combine(torDaemonsDir, "tor-osx64.zip");
                                    IoHelpers.BetterExtractZipToDirectoryAsync(torOsxZip, torDir).GetAwaiter().GetResult();
                                    Logger.LogInfo <TorProcessManager>($"Extracted {torOsxZip} to {torDir}.");
                                }

                                // Make sure there's sufficient permission.
                                string chmodTorDirCmd = $"chmod -R 777 {torDir}";
                                EnvironmentHelpers.ShellExec(chmodTorDirCmd);
                                Logger.LogInfo <TorProcessManager>($"Shell command executed: {chmodTorDirCmd}.");
                            }
                        }
                        else
                        {
                            Logger.LogInfo <TorProcessManager>($"Tor instance found at {torPath}.");
                        }

                        string torArguments = $"--SOCKSPort {TorSocks5EndPoint.Port}";
                        if (!string.IsNullOrEmpty(LogFile))
                        {
                            IoHelpers.EnsureContainingDirectoryExists(LogFile);
                            var logFileFullPath = Path.GetFullPath(LogFile);
                            torArguments       += $" --Log \"notice file {logFileFullPath}\"";
                        }

                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            var torProcessStartInfo = new ProcessStartInfo(torPath)
                            {
                                Arguments              = torArguments,
                                UseShellExecute        = false,
                                CreateNoWindow         = true,
                                RedirectStandardOutput = true
                            };
                            Process.Start(torProcessStartInfo);
                            Logger.LogInfo <TorProcessManager>($"Starting Tor process with Process.Start.");
                        }
                        else                         // Linux and OSX
                        {
                            string runTorCmd = $"LD_LIBRARY_PATH=$LD_LIBRARY_PATH:={torDir}/Tor && export LD_LIBRARY_PATH && cd {torDir}/Tor && ./tor {torArguments}";
                            EnvironmentHelpers.ShellExec(runTorCmd, false);
                            Logger.LogInfo <TorProcessManager>($"Started Tor process with shell command: {runTorCmd}.");
                        }

                        if (ensureRunning)
                        {
                            Task.Delay(3000).ConfigureAwait(false).GetAwaiter().GetResult();                             // dotnet brainfart, ConfigureAwait(false) IS NEEDED HERE otherwise (only on) Manjuro Linux fails, WTF?!!
                            if (!IsTorRunningAsync(TorSocks5EndPoint).GetAwaiter().GetResult())
                            {
                                throw new TorException("Attempted to start Tor, but it is not running.");
                            }
                            Logger.LogInfo <TorProcessManager>("Tor is running.");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new TorException("Could not automatically start Tor. Try running Tor manually.", ex);
                    }
                }
                catch (Exception ex)
                {
                    Logger.LogError <TorProcessManager>(ex);
                }
            }).Start();
        }