Uploaded image for project: 'MINA SSHD'
  1. MINA SSHD
  2. SSHD-1033

Unable to use DynamicPortForwarding + LocalPortForwarding

    XMLWordPrintableJSON

Details

    Description

      This is the error running LocalForwarder + DynamicForwarder:

      [sshd-SshClient[457e2f02]-nio2-thread-6] WARN org.apache.sshd.common.forward.DefaultForwardingFilter - sessionCreated(Nio2Session[local=/127.0.0.1:8000, remote=/127.0.0.1:57657])[local=/127.0.0.1:8000, remote=null] cannot locate original local entry for accepted=/127.0.0.1:8000
      [sshd-SshClient[457e2f02]-nio2-thread-2] WARN org.apache.sshd.client.channel.ClientChannelPendingMessagesQueue - operationComplete(ClientChannelPendingMessagesQueue[channel=TcpipClientChannel[id=1, recipient=-1]-ClientSessionImpl[testuser@/192.168.x.x:22], open=false]) SshChannelOpenException[open failed] signaled
      [sshd-SshClient[457e2f02]-nio2-thread-2] WARN org.apache.sshd.common.forward.DefaultForwardingFilter - Failed (SshChannelOpenException) to open channel for session=Nio2Session[local=/127.0.0.1:8000, remote=/127.0.0.1:57657]: open failed
      

      This is the code to reproduce.

      • When testLocal = true, testDynamic = true; dynamic fail.
      • if the order of test are inverted, fail the local instead of dynamic.
      • if one test is false, the other works well.
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.HttpURLConnection;
      import java.net.InetSocketAddress;
      import java.net.Proxy;
      import java.net.URL;
      
      import org.apache.sshd.client.SshClient;
      import org.apache.sshd.client.future.ConnectFuture;
      import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
      import org.apache.sshd.client.session.ClientSession;
      import org.apache.sshd.client.session.forward.DynamicPortForwardingTracker;
      import org.apache.sshd.client.session.forward.ExplicitPortForwardingTracker;
      import org.apache.sshd.common.util.net.SshdSocketAddress;
      
      public class SSHClient {
      	public static void test(final String username, final String password, final String host, final int port)
      			throws IOException {
      		boolean testLocal = true, testDynamic = true;
      		try (SshClient client = SshClient.setUpDefaultClient()) {
      			client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
      			client.start();
      			try {
      				ConnectFuture connect = client.connect(username, host, port);
      				connect.await(10000);
      				ClientSession session = connect.getClientSession();
      				session.addPasswordIdentity(password);
      				session.auth().verify(10000);
      
      				if (testLocal) {
      					System.out.println("================== Local ==================");
      					ExplicitPortForwardingTracker localTracker = session.createLocalPortForwardingTracker(
      							new SshdSocketAddress("localhost", 8082),
      							new SshdSocketAddress("test.javastack.org", 80));
      					sleep(1000);
      					System.out.println("LocalPortForwarding: " //
      							+ localTracker.getLocalAddress() + " -> " //
      							+ localTracker.getRemoteAddress());
      					SshdSocketAddress localSocketAddress = localTracker.getLocalAddress();
      					if (localSocketAddress != null) {
      						Proxy proxy = new Proxy(Proxy.Type.HTTP,
      								new InetSocketAddress(localSocketAddress.getHostName(), //
      										localSocketAddress.getPort()));
      						testRemoteURL(proxy);
      					}
      				}
      
      				if (testDynamic) {
      					System.out.println("================== Dynamic ==================");
      					DynamicPortForwardingTracker dynamicTracker = session
      							.createDynamicPortForwardingTracker(new SshdSocketAddress("localhost", 8000));
      					sleep(1000);
      					System.out.println("DynamicPortForwarding: " //
      							+ dynamicTracker.getLocalAddress());
      					SshdSocketAddress dynamicSocketAddress = dynamicTracker.getLocalAddress();
      					if (dynamicSocketAddress != null) {
      						Proxy proxy = new Proxy(Proxy.Type.SOCKS,
      								new InetSocketAddress(dynamicSocketAddress.getHostName(), //
      										dynamicSocketAddress.getPort()));
      						testRemoteURL(proxy);
      					}
      				}
      
      				// @see org.apache.sshd.common.forward.DefaultForwardingFilter#sessionCreated
      
      				sleep(1000);
      			} finally {
      				client.stop();
      			}
      		}
      
      	}
      
      	private static final void sleep(final long t) {
      		try {
      			Thread.sleep(t);
      		} catch (Exception ign) {
      		}
      	}
      
      	private static final void testRemoteURL(final Proxy proxy) throws IOException {
      		HttpURLConnection connection = (HttpURLConnection) new URL("http://test.javastack.org/")
      				.openConnection(proxy);
      		System.out.println("Get URL: " + connection.getURL());
      		try {
      			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      			System.out.println("Response from server:");
      			String inputLine;
      			while ((inputLine = in.readLine()) != null) {
      				System.out.println(inputLine);
      			}
      			in.close();
      		} catch (Exception e) {
      			System.out.println("Failed: " + e);
      		}
      	}
      }
      
      <!-- dependencies used --->
      <dependency>
        <groupId>org.apache.sshd</groupId>
        <artifactId>sshd-core</artifactId>
        <version>2.5.1</version>
      </dependency>
      <dependency>
        <groupId>org.apache.mina</groupId>
        <artifactId>mina-core</artifactId>
        <version>2.1.3</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.30</version>
      </dependency>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.30</version>
      </dependency>
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              technobcn Guillermo Grandes
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 5h 10m
                  5h 10m