Con este codigo puedo hacer una conexión a un servidor SSH y abrir un PORT FORWARDING DYNAMIC asi navegar por ese Tunnel, todo eso trabaja perfecto lo único que necesito implementar es un metodo para que reconnecte en caso de que falle la conexión, hasta ahora logré manejar el evento OnDisconnect y activar un Timer cada 10 segundos pero tengo mis dudas para llamar al Main del Modulo pues cuando genero un error de conexión el se queda esperando en la consola a que se presione ESC para entonces ejecutar
Código
client.Disconnect(New ProgressHandler)
y
terminar con
Código
SshNet.Shutdown()
supongo que primero tengo que cerrar bien la conexion osea ejecutar esas lineas antes de volver a llamar al Main quizas declarandolas como global y ejecutarlas en el manejador seria la solución?
Saludos y gracias cualquier ayuda...
Código
Imports Bitvise.FlowSshNet Public Enum ExitCodes Success = 0 UsageError = 1 SessionError = 2 FatalError = 3 End Enum Public Enum DisconnectReason Exception = 0 FlowError = 1 ConnectionError = 2 ConnectionLost = 3 ByServer = 4 ByClient = 5 End Enum Public Class MyClient Inherits Client Dim aTimer As New System.Timers.Timer Private Sub tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) aTimer.Stop() FlowSshNet_VbTnl.Main() End Sub Public Sub New() AddHandler OnHostKey, AddressOf OnMyHostKey AddHandler OnForwardingLog, AddressOf OnMyForwardingLog AddHandler OnDisconnect, AddressOf OnMyDisconnect AddHandler aTimer.Elapsed, AddressOf tick End Sub Sub Main() aTimer.AutoReset = True aTimer.Interval = 10000 '10 seconds End Sub Public Sub OnMyDisconnect(ByVal sender As Object, ByVal reason As DisconnectReason, ByVal desc As String) aTimer.Start() End Sub Public Function OnMyHostKey(ByVal sender As Object, ByVal publicKey As PublicKey) As Boolean Console.WriteLine("Received the following host key: ") Console.WriteLine(" MD5 Fingerprint: {0}", publicKey.GetMd5()) Console.WriteLine(" Bubble-Babble: {0}", publicKey.GetBubbleBabble()) Console.WriteLine(" SHA-256: {0}", publicKey.GetSha256()) Return True End Function Public Sub OnMyForwardingLog(ByVal sender As Object, ByVal log As ForwardingLog) Console.WriteLine(log.Desc) End Sub End Class Module FlowSshNet_VbTnl Sub OnUncaughtExceptionInEvent(ByVal sender As Object, ByVal fatal As Boolean, ByVal e As System.Exception) Console.WriteLine("Error: {0}", e.ToString()) Environment.Exit(ExitCodes.FatalError) End Sub Function Main() As Integer Try AddHandler SshNet.OnExceptionInEvent, AddressOf OnUncaughtExceptionInEvent Dim client As MyClient = New MyClient client.SetAppName("FlowSshNet_VbTnl") client.SetHost("1.1.1.1") client.SetPort(22) client.SetUserName("user") client.SetPassword("password") client.SetProxyType(ProxyType.HttpConnect) client.SetProxyHost("1.1.1.5") client.SetProxyPort("8080") client.SetProxyUserName("userproxy") client.SetProxyPassword("userpass") Dim progress As ProgressHandler = New ProgressHandler client.Connect(progress) progress.WaitDone() If Not progress.Success() Then Dim connectStep As UInt32 = progress.GetTaskSpecificStep() Dim auxInfo As String = progress.GetAuxInfo() Dim connErr As String = ProgressHandler.DescribeConnectError(connectStep, auxInfo) Console.WriteLine("{0}", connErr) Else Dim proxy As ProxyForwarding = New ProxyForwarding proxy.ListInterface = "127.0.0.1" proxy.ListPort = 1080 Dim enableProxyHandler As ForwardingHandler = New ForwardingHandler client.EnableProxyForwarding(proxy, enableProxyHandler) enableProxyHandler.WaitDone() If (enableProxyHandler.Success()) Then Console.WriteLine("Proxy forwarding enabled, listening port: {0}", enableProxyHandler.GetListPort().ToString()) Else Console.WriteLine("Error enabling proxy forwarding: {0}", enableProxyHandler.GetError().Desc) End If Console.WriteLine("Press Esc to stop forwarding") While Console.ReadKey(True).Key <> ConsoleKey.Escape End While If (enableProxyHandler.Success()) Then Dim handler As ForwardingHandler = New ForwardingHandler client.DisableProxyForwarding(handler) handler.WaitDone() If (handler.Success()) Then Console.WriteLine("Proxy forwarding disabled") Else Console.WriteLine("Error disabling proxy forwarding: {0}", handler.GetError().Desc) End If End If client.Disconnect(New ProgressHandler) Console.WriteLine("Press Esc to exit") While Console.ReadKey(True).Key <> ConsoleKey.Escape End While End If Catch e As System.Exception Console.WriteLine(e.Message) Return ExitCodes.FatalError Finally SshNet.Shutdown() End Try Return ExitCodes.Success End Function End Module