示例#1
0
        public async Task <SshKey> LoadOrCreateAsync()
        {
            var keyDir = Path.GetDirectoryName(keyPath);

            try
            {
                Directory.CreateDirectory(keyDir);
            }
            catch (Exception e)
            {
                Trace.WriteLine("Failed to create keys directory: " + e.ToString());
                throw new SshKeyException(ErrorStrings.FailedToCreateSshKeysDirectory(e.Message));
            }

            if (!File.Exists(keyPath))
            {
                var startInfo = new ProcessStartInfo
                {
                    FileName = Path.Combine(SDKUtil.GetSshPath(),
                                            YetiConstants.SshKeygenWinExecutable),
                    Arguments = "-f \"" + keyPath + "\" -t rsa -N \"\"",
                };
                using (var process = managedProcessFactory.Create(startInfo))
                {
                    try
                    {
                        int code = await process.RunToExitAsync();

                        if (code != 0)
                        {
                            Trace.WriteLine("Key generation returned code: " + code);
                            throw new SshKeyException(ErrorStrings.SshKeyGenerationFailed(
                                                          YetiConstants.SshKeygenWinExecutable, code));
                        }
                    }
                    catch (ProcessException e)
                    {
                        Trace.WriteLine("Key generation threw: " + e.ToString());
                        throw new SshKeyException(
                                  ErrorStrings.FailedToRunSshKeyGeneration(e.Message));
                    }
                }
            }
            try
            {
                return(new SshKey {
                    PublicKey = File.ReadAllText(keyPath + ".pub")
                });
            }
            catch (Exception e)
            {
                Trace.WriteLine("Failed to read key file: " + e.ToString());
                throw new SshKeyException(ErrorStrings.FailedToReadSshKeyFile(e.Message));
            }
        }
        /// <summary>
        /// If PlatformStadia is available we pass the gamelet's ip and port for an scp
        /// connection in pre-generated scp command (it includes the full path to scp executable
        /// and all other flags).
        /// </summary>
        /// <param name="debuggerUrl">Url for ConnectRemote.</param>
        /// <param name="targetIpAddress">Gamelet ip address.</param>
        /// <param name="targetPort">Gamelet port./</param>
        /// <returns>ConnectRemote url enriched with a
        /// configuration for scp.exe if applicable.</returns>
        string CreateConnectRemoteArgument(string debuggerUrl, string targetIpAddress,
                                           int targetPort)
        {
            if (!_stadiaPlatformAvailable)
            {
                return(debuggerUrl);
            }

            string executable           = Path.Combine(SDKUtil.GetSshPath(), YetiConstants.ScpWinExecutable);
            string sshKeyFilePath       = SDKUtil.GetSshKeyFilePath();
            string sshKnownHostFilePath = SDKUtil.GetSshKnownHostsFilePath();

            return
                ($"{debuggerUrl};\"{executable}\" -v -T -i \"{sshKeyFilePath}\" -F NUL -P " +
                 $"{targetPort} -oStrictHostKeyChecking=yes " +
                 $"-oUserKnownHostsFile=\"{sshKnownHostFilePath}\" cloudcast@{targetIpAddress}:");
        }
 string GetScpPath()
 {
     return(Path.Combine(SDKUtil.GetSshPath(), YetiConstants.ScpWinExecutable));
 }