Java Code Examples for com.jcraft.jsch.Session#setPortForwardingR()

The following examples show how to use com.jcraft.jsch.Session#setPortForwardingR() . 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 5 votes vote down vote up
@Test
public void testRemoteForwarding() throws Exception {
    Session session = createSession();
    try {
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort);
             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, StandardCharsets.UTF_8);
            assertEquals("Mismatched data", expected, res);
        } finally {
            session.delPortForwardingR(forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 2
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoteForwardingSecondTimeInSameSession() throws Exception {
    Session session = createSession();
    try {
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        session.delPortForwardingR(TEST_LOCALHOST, forwardedPort);
        waitForForwardingRequest(CancelTcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort);
             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, StandardCharsets.UTF_8);
            assertEquals("Mismatched data", expected, res);
        } finally {
            session.delPortForwardingR(TEST_LOCALHOST, forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 3
Source File: JSchChannelsSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void setPortForwardingR(String bind_address, int rport, String host, int lport) throws JSchException {
    portForwarding.addPortForwardingInfoR(bind_address, rport, host, lport);
    for (Session s : sessions.keySet()) {
        if (s.isConnected()) {
            s.setPortForwardingR(bind_address, rport, host, lport);
        }
    }
}
 
Example 4
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoteForwarding() throws Exception {
    Session session = createSession();
    try {
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort);
             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, StandardCharsets.UTF_8);
            assertEquals("Mismatched data", expected, res);
        } finally {
            session.delPortForwardingR(forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 5
Source File: PortForwardingTest.java    From termd with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoteForwardingSecondTimeInSameSession() throws Exception {
    Session session = createSession();
    try {
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        session.delPortForwardingR(TEST_LOCALHOST, forwardedPort);
        waitForForwardingRequest(CancelTcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort);
             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, StandardCharsets.UTF_8);
            assertEquals("Mismatched data", expected, res);
        } finally {
            session.delPortForwardingR(TEST_LOCALHOST, forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 6
Source File: PortForwardingTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 45000)
public void testRemoteForwardingWithDisconnect() throws Exception {
    Session session = createSession();
    try {
        // 1. Create a Port Forward
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        // 2. Establish a connection through it
        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort)) {
            s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));

            // 3. Simulate the client going away
            rudelyDisconnectJschSession(session);

            // 4. Make sure the NIOprocessor is not stuck
            Thread.sleep(TimeUnit.SECONDS.toMillis(1L));
            // from here, we need to check all the threads running and find a
            // "NioProcessor-"
            // that is stuck on a PortForward.dispose
            ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
            while (root.getParent() != null) {
                root = root.getParent();
            }

            for (int index = 0;; index++) {
                Collection<Thread> pending = findThreads(root, "NioProcessor-");
                if (GenericUtils.size(pending) <= 0) {
                    log.info("Finished after " + index + " iterations");
                    break;
                }
                try {
                    Thread.sleep(TimeUnit.SECONDS.toMillis(1L));
                } catch (InterruptedException e) {
                    // ignored
                }
            }

            session.delPortForwardingR(forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}
 
Example 7
Source File: PortForwardingTest.java    From termd with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 45000)
public void testRemoteForwardingWithDisconnect() throws Exception {
    Session session = createSession();
    try {
        // 1. Create a Port Forward
        int forwardedPort = Utils.getFreePort();
        session.setPortForwardingR(forwardedPort, TEST_LOCALHOST, echoPort);
        waitForForwardingRequest(TcpipForwardHandler.REQUEST, TimeUnit.SECONDS.toMillis(5L));

        // 2. Establish a connection through it
        try (Socket s = new Socket(TEST_LOCALHOST, forwardedPort)) {
            s.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));

            // 3. Simulate the client going away
            rudelyDisconnectJschSession(session);

            // 4. Make sure the NIOprocessor is not stuck
            Thread.sleep(TimeUnit.SECONDS.toMillis(1L));
            // from here, we need to check all the threads running and find a
            // "NioProcessor-"
            // that is stuck on a PortForward.dispose
            ThreadGroup root = Thread.currentThread().getThreadGroup().getParent();
            while (root.getParent() != null) {
                root = root.getParent();
            }

            for (int index = 0;; index++) {
                Collection<Thread> pending = findThreads(root, "NioProcessor-");
                if (GenericUtils.size(pending) <= 0) {
                    log.info("Finished after " + index + " iterations");
                    break;
                }
                try {
                    Thread.sleep(TimeUnit.SECONDS.toMillis(1L));
                } catch (InterruptedException e) {
                    // ignored
                }
            }

            session.delPortForwardingR(forwardedPort);
        }
    } finally {
        session.disconnect();
    }
}