Skip to content

Instantly share code, notes, and snippets.

@darinkes
Created March 31, 2014 10:15
Show Gist options
  • Save darinkes/9889304 to your computer and use it in GitHub Desktop.
Save darinkes/9889304 to your computer and use it in GitHub Desktop.
add the ability to catch SSH-Debug Log in your app, don't crash if Channel-Open failed, stricter privatekey regex + validation-method, various null exception fixes
diff --git a/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj b/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj
index 4204d96..8e160f3 100644
--- a/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj
+++ b/Renci.SshNet.NET35/Renci.SshNet.NET35.csproj
@@ -144,6 +144,9 @@
<Compile Include="..\Renci.SshNet\Common\HostKeyEventArgs.cs">
<Link>Common\HostKeyEventArgs.cs</Link>
</Compile>
+ <Compile Include="..\Renci.SshNet\Common\LogEventArgs.cs">
+ <Link>Common\HostKeyEventArgs.cs</Link>
+ </Compile>
<Compile Include="..\Renci.SshNet\Common\NetConfServerException.cs">
<Link>Common\NetConfServerException.cs</Link>
</Compile>
diff --git a/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj b/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj
index c5b41f4..68e8b77 100644
--- a/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj
+++ b/Renci.SshNet.Silverlight/Renci.SshNet.Silverlight.csproj
@@ -156,6 +156,9 @@
<Compile Include="..\Renci.SshNet\Common\HostKeyEventArgs.cs">
<Link>Common\HostKeyEventArgs.cs</Link>
</Compile>
+ <Compile Include="..\Renci.SshNet\Common\LogEventArgs.cs">
+ <Link>Common\HostKeyEventArgs.cs</Link>
+ </Compile>
<Compile Include="..\Renci.SshNet\Common\ObjectIdentifier.cs">
<Link>Common\ObjectIdentifier.cs</Link>
</Compile>
diff --git a/Renci.SshNet.Silverlight5/Renci.SshNet.Silverlight5.csproj b/Renci.SshNet.Silverlight5/Renci.SshNet.Silverlight5.csproj
index bf548de..e4ffd5f 100644
--- a/Renci.SshNet.Silverlight5/Renci.SshNet.Silverlight5.csproj
+++ b/Renci.SshNet.Silverlight5/Renci.SshNet.Silverlight5.csproj
@@ -165,6 +165,9 @@
<Compile Include="..\Renci.SshNet\Common\HostKeyEventArgs.cs">
<Link>Common\HostKeyEventArgs.cs</Link>
</Compile>
+ <Compile Include="..\Renci.SshNet\Common\LogEventArgs.cs">
+ <Link>Common\HostKeyEventArgs.cs</Link>
+ </Compile>
<Compile Include="..\Renci.SshNet\Common\ObjectIdentifier.cs">
<Link>Common\ObjectIdentifier.cs</Link>
</Compile>
diff --git a/Renci.SshNet.WindowsPhone/Renci.SshNet.WindowsPhone.csproj b/Renci.SshNet.WindowsPhone/Renci.SshNet.WindowsPhone.csproj
index 64b6f1f..a79fcbb 100644
--- a/Renci.SshNet.WindowsPhone/Renci.SshNet.WindowsPhone.csproj
+++ b/Renci.SshNet.WindowsPhone/Renci.SshNet.WindowsPhone.csproj
@@ -142,6 +142,9 @@
<Compile Include="..\Renci.SshNet\Common\HostKeyEventArgs.cs">
<Link>Common\HostKeyEventArgs.cs</Link>
</Compile>
+ <Compile Include="..\Renci.SshNet\Common\LogEventArgs.cs">
+ <Link>Common\HostKeyEventArgs.cs</Link>
+ </Compile>
<Compile Include="..\Renci.SshNet\Common\ObjectIdentifier.cs">
<Link>Common\ObjectIdentifier.cs</Link>
</Compile>
diff --git a/Renci.SshNet.WindowsPhone8/Renci.SshNet.WindowsPhone8.csproj b/Renci.SshNet.WindowsPhone8/Renci.SshNet.WindowsPhone8.csproj
index 4ba730c..533930c 100644
--- a/Renci.SshNet.WindowsPhone8/Renci.SshNet.WindowsPhone8.csproj
+++ b/Renci.SshNet.WindowsPhone8/Renci.SshNet.WindowsPhone8.csproj
@@ -189,6 +189,9 @@
<Compile Include="..\Renci.SshNet\Common\HostKeyEventArgs.cs">
<Link>Common\HostKeyEventArgs.cs</Link>
</Compile>
+ <Compile Include="..\Renci.SshNet\Common\LogEventArgs.cs">
+ <Link>Common\HostKeyEventArgs.cs</Link>
+ </Compile>
<Compile Include="..\Renci.SshNet\Common\ObjectIdentifier.cs">
<Link>Common\ObjectIdentifier.cs</Link>
</Compile>
diff --git a/Renci.SshNet/BaseClient.cs b/Renci.SshNet/BaseClient.cs
index c6aa337..0a8fb41 100644
--- a/Renci.SshNet/BaseClient.cs
+++ b/Renci.SshNet/BaseClient.cs
@@ -117,6 +117,8 @@ namespace Renci.SshNet
/// </example>
public event EventHandler<HostKeyEventArgs> HostKeyReceived;
+ public event EventHandler<LogEventArgs> LogAdded;
+
/// <summary>
/// Initializes a new instance of the <see cref="BaseClient"/> class.
/// </summary>
@@ -153,6 +155,7 @@ namespace Renci.SshNet
Session = new Session(ConnectionInfo);
Session.HostKeyReceived += Session_HostKeyReceived;
Session.ErrorOccured += Session_ErrorOccured;
+ Session.LogAdded += Session_LogAdded;
Session.Connect();
StartKeepAliveTimer();
OnConnected();
@@ -233,6 +236,15 @@ namespace Renci.SshNet
}
}
+ private void Session_LogAdded(object sender, LogEventArgs e)
+ {
+ var handler = this.LogAdded;
+ if (handler != null)
+ {
+ handler(this, e);
+ }
+ }
+
#region IDisposable Members
private bool _isDisposed;
@@ -267,6 +279,7 @@ namespace Renci.SshNet
{
this.Session.ErrorOccured -= Session_ErrorOccured;
this.Session.HostKeyReceived -= Session_HostKeyReceived;
+ this.Session.LogAdded -= Session_LogAdded;
this.Session.Dispose();
this.Session = null;
}
diff --git a/Renci.SshNet/ForwardedPortDynamic.NET.cs b/Renci.SshNet/ForwardedPortDynamic.NET.cs
index 3e44e6e..f6f8b22 100644
--- a/Renci.SshNet/ForwardedPortDynamic.NET.cs
+++ b/Renci.SshNet/ForwardedPortDynamic.NET.cs
@@ -69,6 +69,12 @@ namespace Renci.SshNet
throw new NotSupportedException(string.Format("SOCKS version {0} is not supported.", version));
}
+ if (!channel.IsOpen)
+ {
+ socket.Close();
+ throw new SshException("Unable to open Channel for DynamicForwarding.");
+ }
+
channel.Bind();
channel.Close();
diff --git a/Renci.SshNet/ForwardedPortDynamic.cs b/Renci.SshNet/ForwardedPortDynamic.cs
index a8038b0..132cc87 100644
--- a/Renci.SshNet/ForwardedPortDynamic.cs
+++ b/Renci.SshNet/ForwardedPortDynamic.cs
@@ -38,6 +38,7 @@ namespace Renci.SshNet
{
this.BoundHost = host;
this.BoundPort = port;
+ this.IsStarted = false;
}
/// <summary>
diff --git a/Renci.SshNet/ForwardedPortLocal.NET.cs b/Renci.SshNet/ForwardedPortLocal.NET.cs
index db709f1..6847c3c 100644
--- a/Renci.SshNet/ForwardedPortLocal.NET.cs
+++ b/Renci.SshNet/ForwardedPortLocal.NET.cs
@@ -3,6 +3,7 @@ using System.Net.Sockets;
using System.Net;
using System.Threading;
using Renci.SshNet.Channels;
+using Renci.SshNet.Common;
namespace Renci.SshNet
{
@@ -57,6 +58,12 @@ namespace Renci.SshNet
using (var channel = this.Session.CreateClientChannel<ChannelDirectTcpip>())
{
channel.Open(this.Host, this.Port, socket);
+
+ if (!channel.IsOpen)
+ {
+ socket.Close();
+ throw new SshException("Unable to open Channel for LocalForwarding");
+ }
channel.Bind();
diff --git a/Renci.SshNet/ForwardedPortLocal.cs b/Renci.SshNet/ForwardedPortLocal.cs
index 3a7c51d..1856d04 100644
--- a/Renci.SshNet/ForwardedPortLocal.cs
+++ b/Renci.SshNet/ForwardedPortLocal.cs
@@ -86,6 +86,7 @@ namespace Renci.SshNet
this.BoundPort = boundPort;
this.Host = host;
this.Port = port;
+ this.IsStarted = false;
}
/// <summary>
diff --git a/Renci.SshNet/PrivateKeyFile.cs b/Renci.SshNet/PrivateKeyFile.cs
index cc88020..1416b28 100644
--- a/Renci.SshNet/PrivateKeyFile.cs
+++ b/Renci.SshNet/PrivateKeyFile.cs
@@ -24,9 +24,9 @@ namespace Renci.SshNet
public class PrivateKeyFile : IDisposable
{
#if SILVERLIGHT
- private static readonly Regex _privateKeyRegex = new Regex(@"^-+ *BEGIN (?<keyName>\w+( \w+)*) PRIVATE KEY *-+\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,80}\r?\n)+)-+ *END \k<keyName> PRIVATE KEY *-+", RegexOptions.Multiline);
+ private static readonly Regex _privateKeyRegex = new Regex(@"^-+ *BEGIN (?<keyName>\w+( \w+)*) PRIVATE KEY *-+\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,80}\r?\n)+)-+ *END \k<keyName> PRIVATE KEY *-+$", RegexOptions.Multiline);
#else
- private static readonly Regex _privateKeyRegex = new Regex(@"^-+ *BEGIN (?<keyName>\w+( \w+)*) PRIVATE KEY *-+\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,80}\r?\n)+)-+ *END \k<keyName> PRIVATE KEY *-+", RegexOptions.Compiled | RegexOptions.Multiline);
+ private static readonly Regex _privateKeyRegex = new Regex(@"^-+ *BEGIN (?<keyName>\w+( \w+)*) PRIVATE KEY *-+\r?\n(Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: (?<cipherName>[A-Z0-9-]+),(?<salt>[A-F0-9]+)\r?\n\r?\n)?(?<data>([a-zA-Z0-9/+=]{1,80}\r?\n)+)-+ *END \k<keyName> PRIVATE KEY *-+$", RegexOptions.Compiled | RegexOptions.Multiline);
#endif
private Key _key;
@@ -243,6 +243,11 @@ namespace Renci.SshNet
}
}
+ public static bool PrivateKeyIsValid(string key)
+ {
+ return _privateKeyRegex.Match(key).Success;
+ }
+
private static byte[] GetCipherKey(string passphrase, int length)
{
List<byte> cipherKey = new List<byte>();
diff --git a/Renci.SshNet/Renci.SshNet.csproj b/Renci.SshNet/Renci.SshNet.csproj
index 6d8da3f..33a9042 100644
--- a/Renci.SshNet/Renci.SshNet.csproj
+++ b/Renci.SshNet/Renci.SshNet.csproj
@@ -84,6 +84,7 @@
<Compile Include="Common\ChannelOpenFailedEventArgs.cs" />
<Compile Include="Common\ChannelRequestEventArgs.cs" />
<Compile Include="Common\Extensions.NET.cs" />
+ <Compile Include="Common\LogEventArgs.cs" />
<Compile Include="Common\ProxyException.cs">
<SubType>Code</SubType>
</Compile>
diff --git a/Renci.SshNet/Session.NET.cs b/Renci.SshNet/Session.NET.cs
index 6432a5d..63048d2 100644
--- a/Renci.SshNet/Session.NET.cs
+++ b/Renci.SshNet/Session.NET.cs
@@ -19,6 +19,8 @@ namespace Renci.SshNet
new TraceSource("SshNet.Logging");
#endif
+ public event EventHandler<LogEventArgs> LogAdded;
+
/// <summary>
/// Gets a value indicating whether the socket is connected.
/// </summary>
@@ -191,6 +193,9 @@ namespace Renci.SshNet
partial void Log(string text)
{
this._log.TraceEvent(TraceEventType.Verbose, 1, text);
+ var handler = this.LogAdded;
+ if (handler != null)
+ handler(this, new LogEventArgs("Debug", text));
}
}
}
diff --git a/Renci.SshNet/ShellStream.cs b/Renci.SshNet/ShellStream.cs
index 23003ae..7b43811 100644
--- a/Renci.SshNet/ShellStream.cs
+++ b/Renci.SshNet/ShellStream.cs
@@ -733,7 +733,8 @@ namespace Renci.SshNet
}
}
- this._dataReceived.Set();
+ if (this._dataReceived != null)
+ this._dataReceived.Set();
this.OnDataReceived(e.Data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment