/** * Starts a remote port forwarding and returns a tracker that stops the * forwarding when the {@code close()} method is called. This tracker can * be used in a {@code try-with-resource} block to ensure cleanup of the * set up forwarding. * * @param remote The remote address * @param local The local address * @return The tracker instance * @throws IOException If failed to set up the requested forwarding * @see #startRemotePortForwarding(SshdSocketAddress, SshdSocketAddress) */ default ExplicitPortForwardingTracker createRemotePortForwardingTracker(SshdSocketAddress remote, SshdSocketAddress local) throws IOException { return new ExplicitPortForwardingTracker(this, false, local, remote, startRemotePortForwarding(remote, local)); }
/** * Starts a remote port forwarding and returns a tracker that stops the * forwarding when the {@code close()} method is called. This tracker can * be used in a {@code try-with-resource} block to ensure cleanup of the * set up forwarding. * * @param remote The remote address * @param local The local address * @return The tracker instance * @throws IOException If failed to set up the requested forwarding * @see #startRemotePortForwarding(SshdSocketAddress, SshdSocketAddress) */ default ExplicitPortForwardingTracker createRemotePortForwardingTracker(SshdSocketAddress remote, SshdSocketAddress local) throws IOException { return new ExplicitPortForwardingTracker(this, false, local, remote, startRemotePortForwarding(remote, local)); }
@Test public void testRemoteForwardingNative() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress remote = new SshdSocketAddress("", 0); SshdSocketAddress local = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startRemotePortForwarding(remote, local); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); 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); } finally { session.stopRemotePortForwarding(remote); } } }
@Test public void testRemoteForwardingNative() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress remote = new SshdSocketAddress("", 0); SshdSocketAddress local = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startRemotePortForwarding(remote, local); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); 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); } finally { session.stopRemotePortForwarding(remote); } } }
@Test public void testRemoteForwardingNativeBigPayload() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress remote = new SshdSocketAddress("", 0); SshdSocketAddress local = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startRemotePortForwarding(remote, local); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); byte[] buf = new byte[bytes.length + Long.SIZE]; for (int i = 0; i < 1000; i++) { output.write(bytes); output.flush(); int n = input.read(buf); String res = new String(buf, 0, n); assertEquals("Mismatched data at iteration #" + i, expected, res); } } finally { session.stopRemotePortForwarding(remote); } } }
@Test public void testRemoteForwardingNativeBigPayload() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress remote = new SshdSocketAddress("", 0); SshdSocketAddress local = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startRemotePortForwarding(remote, local); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); byte[] buf = new byte[bytes.length + Long.SIZE]; for (int i = 0; i < 1000; i++) { output.write(bytes); output.flush(); int n = input.read(buf); String res = new String(buf, 0, n); assertEquals("Mismatched data at iteration #" + i, expected, res); } } finally { session.stopRemotePortForwarding(remote); } } }
@Test public void testRemoteForwardingNative() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress remote = new SshdSocketAddress("", 0); SshdSocketAddress local = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startRemotePortForwarding(remote, local); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); 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); } finally { session.stopRemotePortForwarding(remote); } } }
@Test public void testRemoteForwardingNativeBigPayload() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress remote = new SshdSocketAddress("", 0); SshdSocketAddress local = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startRemotePortForwarding(remote, local); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); byte[] buf = new byte[bytes.length + Long.SIZE]; for (int i = 0; i < 1000; i++) { output.write(bytes); output.flush(); int n = input.read(buf); String res = new String(buf, 0, n); assertEquals("Mismatched data at iteration #" + i, expected, res); } } finally { session.stopRemotePortForwarding(remote); } } }