Archived Forum Post

Index of archived forum posts

Question:

C# SshTunnel faces to listening, but realy don't listen.

Apr 27 '15 at 14:20

Hi, there is my code for port forwarding. When i send correct credentils, it's work, all is fine. Forwarded port really listening. But when i send bad password in credentials, sshTunnel.BeginAccepting and sshTunnel.IsAccpting return TRUE! Port faces to be listening (see this log:

sshTunnel.ConnectLog:
ChilkatLog:
  ListenThread:
    listenPort: 1352
  --ListenThread
--ChilkatLog

sshTunnel.LastErrorText:
ChilkatLog:
  BeginAccepting:
    DllDate: Mar  6 2015
    ChilkatVersion: 9.5.0.48
    UnlockPrefix: Testing ssh library
    Username: MUSASHI:odraska
    Architecture: Little Endian; 32-bit
    Language: .NET 4.0
    VerboseLogging: 0
    listenPort: 1352
    destPort: 1352
    destHostname: notes.dcit.cz
    Listen thread started.
    Success.
  --BeginAccepting
--ChilkatLog

), but really dont listening (i cant connect on localport with application). Where is a problem? (I'm testing this after 30 days - i am thinking about buying this licence, but i want this to properly test). When i send correct credentials and not use sshTunnel.UnlockComponent(), it's still working..

    private bool forwardPort(string host, int remotePort, int localPort)
    {
        bool isListening = false;

        SshTunnel sshTunnel = new SshTunnel()
        {
            SshHostname = ConnectionInfo.Host,
            SshPort = ConnectionInfo.PortNumber,
            SshLogin = ConnectionInfo.User,
            SshPassword = ConnectionInfo.Password,
            DestHostname = host,
            DestPort = remotePort,
            ListenBindIpAddress = LOCALHOST_IPV4
        };
        sshTunnel.UnlockComponent("Testing ssh library");

        if (sshTunnel.BeginAccepting(localPort))
        {
            if (!forwardedPorts.Contains(sshTunnel) && sshTunnel.IsAccepting)
            {
                forwardedPorts.Add(sshTunnel);
                SendMessage(string.Format("{0}:{1} listened on {2}\n", sshTunnel.DestHostname, sshTunnel.DestPort, sshTunnel.ListenPort),
                    EventLogEntryType.Information);
            }
            else
            {
                SendMessage(string.Format("{0}:{1} already listened on {2}\n", sshTunnel.DestHostname, sshTunnel.DestPort, sshTunnel.ListenPort), EventLogEntryType.Warning);
            }
            SendMessage(sshTunnel.ConnectLog, EventLogEntryType.Information);
        }
        else
        {
            PortsStatus.Add(string.Format("{0}:{1} forward to {2} ERROR.\n", sshTunnel.DestHostname, sshTunnel.DestPort, sshTunnel.ListenPort));
            SendMessage(sshTunnel.ConnectLog, EventLogEntryType.Error);
            isListening = false;
        }
        return isListening;
    }

Answer

Thanks! I'll look into this problem..


Answer

The BeginAccepting method will return true even if invalid login credentials are used. This is because it is simply starting the background thread to begin accepting connections. When an incoming connection arrives, it is at that point that a connection to the SSH server is established and the login credentials used.


Answer

Ok, but why is not any CallBack function/event/delegate, in which I could evaluate whether logged onto the ssh server was successful?

In case I wrote an application that would work as follows:

The user enter a username / password to log on ssh server and then app forwards the application ports, according from a list. Subsequent user wants to use programs that connect through the port. For this case, if my application should give users report that they have applied the wrong name password before it redirects than ports. Otherwise my application shape that everything is fine. But at the moment when a user runs a program to communicate via forwarded port, receives messages about bad Log in. This is considerably impractical.

In this moment is more practical this solution: User enter credentials -> click "Connect" -> if succesfull connect to ssh server, than forward ports. If connect was not successfull (bad login, problems with network, etc..), show message to user "Fix the problem." It would be possible, make event/callback, which would returned true / false for bad establish connection to a ssh server? Or it's possible to use object Ssh()? When i use the object Ssh(), so i don't know which method use to send the command "ssh -L <portnumber>: localhostOrAddress: <portnumber> <sshhost>".

Thanks for your patience.


Answer

An event callback can only be on the same thread.

An event callback works like this (on a thread of execution)

  1. Application calls a library method.
  2. The library method is executing..
  3. The library method makes a callback into the application's event callback handler.
  4. The app's event callback handler method is executing.
  5. The apps' event callback handler method returns.
  6. The library method continues running.
  7. The library method returns.
  8. The application continues running.

How would you propose that code running in one thread of execution somehow make a "callback" into another thread of execution?