/** * Starts a local 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 local The local address * @param remote The remote address * @return The tracker instance * @throws IOException If failed to set up the requested forwarding * @see #startLocalPortForwarding(SshdSocketAddress, SshdSocketAddress) */ default ExplicitPortForwardingTracker createLocalPortForwardingTracker(SshdSocketAddress local, SshdSocketAddress remote) throws IOException { return new ExplicitPortForwardingTracker(this, true, local, remote, startLocalPortForwarding(local, remote)); }
/** * Starts a local 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 local The local address * @param remote The remote address * @return The tracker instance * @throws IOException If failed to set up the requested forwarding * @see #startLocalPortForwarding(SshdSocketAddress, SshdSocketAddress) */ default ExplicitPortForwardingTracker createLocalPortForwardingTracker(SshdSocketAddress local, SshdSocketAddress remote) throws IOException { return new ExplicitPortForwardingTracker(this, true, local, remote, startLocalPortForwarding(local, remote)); }
@Test public void testLocalForwardingNativeReuse() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); session.stopLocalPortForwarding(bound); SshdSocketAddress bound2 = session.startLocalPortForwarding(local, remote); session.stopLocalPortForwarding(bound2); } }
@Test public void testLocalForwardingNativeReuse() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); session.stopLocalPortForwarding(bound); SshdSocketAddress bound2 = session.startLocalPortForwarding(local, remote); session.stopLocalPortForwarding(bound2); } }
@Test public void testLocalForwardingNativeReuse() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); session.stopLocalPortForwarding(bound); SshdSocketAddress bound2 = session.startLocalPortForwarding(local, remote); session.stopLocalPortForwarding(bound2); } }
@Test public void testLocalForwardingNativeBigPayload() throws Exception { try (ClientSession session = createNativeSession()) { String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); byte[] buf = new byte[bytes.length + Long.SIZE]; SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); 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.stopLocalPortForwarding(bound); } } }
@Test public void testLocalForwardingNative() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); 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.stopLocalPortForwarding(bound); } } }
@Test public void testLocalForwardingNative() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); 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.stopLocalPortForwarding(bound); } } }
@Test public void testLocalForwardingNativeBigPayload() throws Exception { try (ClientSession session = createNativeSession()) { String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); byte[] buf = new byte[bytes.length + Long.SIZE]; SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); 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.stopLocalPortForwarding(bound); } } }
@Test public void testLocalForwardingNative() throws Exception { try (ClientSession session = createNativeSession()) { SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); 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.stopLocalPortForwarding(bound); } } }
@Test public void testLocalForwardingNativeBigPayload() throws Exception { try (ClientSession session = createNativeSession()) { String expected = getCurrentTestName(); byte[] bytes = expected.getBytes(StandardCharsets.UTF_8); byte[] buf = new byte[bytes.length + Long.SIZE]; SshdSocketAddress local = new SshdSocketAddress("", 0); SshdSocketAddress remote = new SshdSocketAddress(TEST_LOCALHOST, echoPort); SshdSocketAddress bound = session.startLocalPortForwarding(local, remote); try (Socket s = new Socket(bound.getHostName(), bound.getPort()); OutputStream output = s.getOutputStream(); InputStream input = s.getInputStream()) { s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); 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.stopLocalPortForwarding(bound); } } }