Home | History | Annotate | Download | only in examples
      1 /*
      2  * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
      3  * Please refer to the LICENSE.txt for licensing details.
      4  */
      5 import java.io.File;
      6 import java.io.IOException;
      7 
      8 import ch.ethz.ssh2.Connection;
      9 import ch.ethz.ssh2.LocalPortForwarder;
     10 
     11 public class PortForwarding
     12 {
     13 	public static void sleepSomeTime(long milliSeconds)
     14 	{
     15 		try
     16 		{
     17 			Thread.sleep(milliSeconds);
     18 		}
     19 		catch (InterruptedException e)
     20 		{
     21 		}
     22 	}
     23 
     24 	public static void main(String[] args)
     25 	{
     26 		String hostname = "127.0.0.1";
     27 		String username = "joe";
     28 
     29 		File keyfile = new File("~/.ssh/id_rsa"); // or "~/.ssh/id_dsa"
     30 		String keyfilePass = "joespass"; // will be ignored if not needed
     31 
     32 		try
     33 		{
     34 			/* Create a connection instance */
     35 
     36 			Connection conn = new Connection(hostname);
     37 
     38 			/* Now connect */
     39 
     40 			conn.connect();
     41 
     42 			/* Authenticate */
     43 
     44 			boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);
     45 
     46 			if (isAuthenticated == false)
     47 				throw new IOException("Authentication failed.");
     48 
     49 			/* ===== OK, now let's establish some local port forwardings ===== */
     50 
     51 			/* Example Port Forwarding: -L 8080:www.ethz.ch:80 (OpenSSH notation)
     52 			 *
     53 			 * This works by allocating a socket to listen on 8080 on the local interface (127.0.0.1).
     54 			 * Whenever a connection is made to this port (127.0.0.1:8080), the connection is forwarded
     55 			 * over the secure channel, and a connection is made to www.ethz.ch:80 from the remote
     56 			 * machine (i.e., the ssh server).
     57 			 *
     58 			 * (the above text is based partially on the OpenSSH man page)
     59 			 */
     60 
     61 			/* You can create as many of them as you want */
     62 
     63 			LocalPortForwarder lpf1 = conn.createLocalPortForwarder(8080, "www.ethz.ch", 80);
     64 
     65 			/* Now simply point your webbrowser to 127.0.0.1:8080 */
     66 			/* (on the host where you execute this program)                         */
     67 
     68 			/* ===== OK, now let's establish some remote port forwardings ===== */
     69 
     70 			/* Example Port Forwarding: -R 127.0.0.1:8080:www.ganymed.ethz.ch:80 (OpenSSH notation)
     71 			 *
     72 			 * Specifies that the port 127.0.0.1:8080 on the remote server is to be forwarded to the
     73 			 * given host and port on the local side.  This works by allocating a socket to listen to port
     74 			 * 8080 on the remote side (the ssh server), and whenever a connection is made to this port, the
     75 			 * connection is forwarded over the secure channel, and a connection is made to
     76 			 * www.ganymed.ethz.ch:80 by the Ganymed SSH-2 library.
     77 			 *
     78 			 * (the above text is based partially on the OpenSSH man page)
     79 			 */
     80 
     81 			/* You can create as many of them as you want */
     82 
     83 			conn.requestRemotePortForwarding("127.0.0.1", 8080, "www.ganymed.ethz.ch", 80);
     84 
     85 			/* Now, on the ssh server, if you connect to 127.0.0.1:8080, then the connection is forwarded
     86 			 * through the secure tunnel to the library, which in turn will forward the connection
     87 			 * to www.ganymed.ethz.ch:80. */
     88 
     89 			/* Sleep a bit... (30 seconds) */
     90 			sleepSomeTime(30000);
     91 
     92 			/* Stop accepting remote connections that are being forwarded to www.ganymed.ethz.ch:80 */
     93 
     94 			conn.cancelRemotePortForwarding(8080);
     95 
     96 			/* Sleep a bit... (20 seconds) */
     97 			sleepSomeTime(20000);
     98 
     99 			/* Stop accepting connections on 127.0.0.1:8080 that are being forwarded to www.ethz.ch:80 */
    100 
    101 			lpf1.close();
    102 
    103 			/* Close the connection */
    104 
    105 			conn.close();
    106 
    107 		}
    108 		catch (IOException e)
    109 		{
    110 			e.printStackTrace(System.err);
    111 			System.exit(2);
    112 		}
    113 	}
    114 }
    115