org.apache.sshd.client.channel.ChannelDirectTcpip Java Examples

The following examples show how to use org.apache.sshd.client.channel.ChannelDirectTcpip. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PortForwardingTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testForwardingChannel() throws Exception {
    try (ClientSession session = createNativeSession()) {
        SshdSocketAddress local = new SshdSocketAddress("", 0);
        SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort);

        try (ChannelDirectTcpip channel = session.createDirectTcpipChannel(local, remote)) {
            channel.open().verify(9L, TimeUnit.SECONDS);

            String expected = getCurrentTestName();
            byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);

            try (OutputStream output = channel.getInvertedIn();
                 InputStream input = channel.getInvertedOut()) {
                output.write(bytes);
                output.flush();

                byte[] buf = new byte[bytes.length + Long.SIZE];
                int n = input.read(buf);
                String res = new String(buf, 0, n);
                assertEquals("Mismatched data", expected, res);
            }
            channel.close(false);
        }
    }
}
 
Example #2
Source File: PortForwardingTest.java    From termd with Apache License 2.0 6 votes vote down vote up
@Test
public void testForwardingChannel() throws Exception {
    try (ClientSession session = createNativeSession()) {
        SshdSocketAddress local = new SshdSocketAddress("", 0);
        SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort);

        try (ChannelDirectTcpip channel = session.createDirectTcpipChannel(local, remote)) {
            channel.open().verify(9L, TimeUnit.SECONDS);

            String expected = getCurrentTestName();
            byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);

            try (OutputStream output = channel.getInvertedIn();
                 InputStream input = channel.getInvertedOut()) {
                output.write(bytes);
                output.flush();

                byte[] buf = new byte[bytes.length + Long.SIZE];
                int n = input.read(buf);
                String res = new String(buf, 0, n);
                assertEquals("Mismatched data", expected, res);
            }
            channel.close(false);
        }
    }
}
 
Example #3
Source File: SSHUtil.java    From xenon with Apache License 2.0 4 votes vote down vote up
Tunnel(ServerSocket ss, ChannelDirectTcpip tmp, int bufferSize) {
    this.server = ss;
    this.channel = tmp;
    this.bufferSize = bufferSize;
}
 
Example #4
Source File: SSHUtil.java    From xenon with Apache License 2.0 4 votes vote down vote up
/**
 * Connect an existing {@link SshClient} to the server at <code>location</code> and authenticate using the given <code>credential</code>.
 *
 * @param adaptorName
 *            the adaptor where this method was called from.
 * @param client
 *            the client to connect.
 * @param location
 *            the server to connect to
 * @param credential
 *            the credential to authenticate with.
 * @param bufferSize
 *            the buffer size used for the (optional) SSH tunnels.
 * @param timeout
 *            the timeout to use in connection setup (in milliseconds).
 * @return the connected {@link ClientSession}
 * @throws XenonException
 *             if the connection setup or authentication failed.
 */
public static SSHConnection connect(String adaptorName, SshClient client, String location, Credential credential, int bufferSize, long timeout)
        throws XenonException {

    if (credential == null) {
        throw new IllegalArgumentException("Credential may not be null");
    }

    if (timeout <= 0) {
        throw new IllegalArgumentException("Invalid timeout: " + timeout);
    }

    if (location == null) {
        throw new IllegalArgumentException("Location may not be null");
    }

    SshdSocketAddress[] locations = extractLocations(adaptorName, location);
    UserCredential[] creds = extractCredentials(adaptorName, locations, credential);

    SSHConnection connection = new SSHConnection(client, locations.length - 1);

    // Connect to the last location. This is either the destination (without tunneling) or the first hop.
    ClientSession session = connectAndAuthenticate(adaptorName, client, locations[0].getHostName(), locations[0].getPort(), creds[0], timeout);

    try {
        // If we have more that one location we need to tunnel via another location.
        for (int i = 1; i < locations.length; i++) {
            ChannelDirectTcpip channel = session.createDirectTcpipChannel(null, locations[i]);
            channel.open().await(timeout);

            ServerSocket server = new ServerSocket(0);
            int port = server.getLocalPort();

            Tunnel tunnel = new Tunnel(server, channel, bufferSize);
            tunnel.start();

            connection.addHop(i - 1, session, tunnel);

            session = connectAndAuthenticate(adaptorName, client, "localhost", port, creds[i], timeout);
        }

    } catch (IOException e) {
        // Attempt to cleanup the mess
        connection.close();

        try {
            session.close();
        } catch (IOException e1) {
            // ignored
        }

        throw new XenonException(adaptorName, "Failed to set up SSH forwarding", e);

    } catch (XenonException xe) {
        // Attempt to cleanup the mess
        connection.close();
        throw xe;
    }

    connection.setSession(session);
    return connection;
}
 
Example #5
Source File: MockClientSession.java    From xenon with Apache License 2.0 4 votes vote down vote up
@Override
public ChannelDirectTcpip createDirectTcpipChannel(SshdSocketAddress local, SshdSocketAddress remote) throws IOException {
    throw new RuntimeException("Not implemented");
}