/// <summary> /// Start attemt to establish link once. /// </summary> /// <returns>Link was successfully started.</returns> private bool startOnce() { // fill results dic lock (_forwardingResultsLock) { _forwardingResults = Host.Tunnels.ToDictionary(t => t, t => ForwardingResult.CreateSuccess()); } // Процесс _process = new Process { StartInfo = { FileName = ConsoleTools.PLinkLocation, CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, RedirectStandardInput = true, Arguments = ConsoleTools.PuttyArguments(Host, _profile, Host.AuthType) } }; _process.ErrorDataReceived += errorDataHandler; _process.Start(); _process.BeginErrorReadLine(); _process.StandardInput.AutoFlush = true; var buffer = new StringBuilder(); bool passwordProvided = false; bool passphraseForKeyProvided = false; while (!_process.HasExited) { while (_process.StandardOutput.Peek() >= 0) { char c = (char)_process.StandardOutput.Read(); buffer.Append(c); } _process.StandardOutput.DiscardBufferedData(); string data = buffer.ToString().ToLower(); buffer.Clear(); if (data.Contains(@"login as:")) { // invalid username provided stop(); // _process.StandardInput.WriteLine(username); LastStartError = Resources.PuttyLink_Error_InvalidUsername; } else if (data.Contains(@"password:"******"passphrase for key") && !passphraseForKeyProvided) { writeLineStdIn(Host.Password); passphraseForKeyProvided = true; } else { foreach (var line in data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) { Logger.Log.Debug(line); } } } PrivateKeysStorage.RemovePrivateKey(Host); return(Status == ELinkStatus.Started || Status == ELinkStatus.StartedWithWarnings); }
/// <summary> /// Start attemt to establish link once. /// </summary> /// <returns>Link was successfully started.</returns> private bool startOnce(TimeSpan?connectionTimeout) { // fill results dic lock (_forwardingResultsLock) { _forwardingResults = Host.Tunnels.ToDictionary(t => t, t => ForwardingResult.CreateSuccess()); } // Процесс _process = new Process { StartInfo = { FileName = ConsoleTools.PLinkLocation, CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardOutput = true, RedirectStandardInput = true, Arguments = ConsoleTools.PuttyArguments(Host, _profile, Host.AuthType, true) } }; _process.ErrorDataReceived += errorDataHandler; _process.Start(); _process.BeginErrorReadLine(); _process.StandardInput.AutoFlush = true; var p = _process; var processAutoKillTimer = connectionTimeout == null ? null : new Timer(delegate { ThreadContext.Properties[@"Host"] = Host; var connectionStarted = Status == ELinkStatus.Started || Status == ELinkStatus.StartedWithWarnings; if (!connectionStarted && p == _process) { try { p.Kill(); } catch { /**/ } Logger.Log.WarnFormat("[{0}] {1}", Host.Name, $"Connection timeout ({connectionTimeout})..."); } ThreadContext.Properties[@"Host"] = null; }, null, (long)connectionTimeout.Value.TotalMilliseconds, Timeout.Infinite); var buffer = new StringBuilder(); bool passwordProvided = false; bool passphraseForKeyProvided = false; while (!_process.HasExited) { while (_process.StandardOutput.Peek() >= 0) { char c = (char)_process.StandardOutput.Read(); buffer.Append(c); } _process.StandardOutput.DiscardBufferedData(); string data = buffer.ToString().ToLower(); buffer.Clear(); if (data.Contains(@"login as:")) { // invalid username provided stop(); // _process.StandardInput.WriteLine(username); LastStartError = Resources.PuttyLink_Error_InvalidUsername; } else if (data.Contains(@"password:"******"passphrase for key") && !passphraseForKeyProvided) { writeLineStdIn(Host.Password); passphraseForKeyProvided = true; } else { foreach (var line in data.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) { Logger.Log.Debug(line); } } } processAutoKillTimer?.Dispose(); PrivateKeysStorage.RemovePrivateKey(Host); return(Status == ELinkStatus.Started || Status == ELinkStatus.StartedWithWarnings); }