KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PortForwarding


1 import java.io.File JavaDoc;
2 import java.io.IOException JavaDoc;
3
4 import ch.ethz.ssh2.Connection;
5 import ch.ethz.ssh2.LocalPortForwarder;
6
7 public class PortForwarding
8 {
9     public static void sleepSomeTime(long milliSeconds)
10     {
11         try
12         {
13             Thread.sleep(milliSeconds);
14         }
15         catch (InterruptedException JavaDoc e)
16         {
17         }
18     }
19
20     public static void main(String JavaDoc[] args)
21     {
22         String JavaDoc hostname = "127.0.0.1";
23         String JavaDoc username = "joe";
24
25         File JavaDoc keyfile = new File JavaDoc("~/.ssh/id_rsa"); // or "~/.ssh/id_dsa"
26
String JavaDoc keyfilePass = "joespass"; // will be ignored if not needed
27

28         try
29         {
30             /* Create a connection instance */
31
32             Connection conn = new Connection(hostname);
33
34             /* Now connect */
35
36             conn.connect();
37
38             /* Authenticate */
39
40             boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);
41
42             if (isAuthenticated == false)
43                 throw new IOException JavaDoc("Authentication failed.");
44
45             /* ===== OK, now let's establish some local port forwardings ===== */
46             
47             /* Example Port Forwarding: -L 8080:www.ethz.ch:80 (OpenSSH notation)
48              *
49              * This works by allocating a socket to listen on 8080 on the local interface (127.0.0.1).
50              * Whenever a connection is made to this port (127.0.0.1:8080), the connection is forwarded
51              * over the secure channel, and a connection is made to www.ethz.ch:80 from the remote
52              * machine (i.e., the ssh server).
53              *
54              * (the above text is based partially on the OpenSSH man page)
55              */

56
57             /* You can create as many of them as you want */
58             
59             LocalPortForwarder lpf1 = conn.createLocalPortForwarder(8080, "www.ethz.ch", 80);
60             
61             /* Now simply point your webbrowser to 127.0.0.1:8080 */
62             /* (on the host where you execute this program) */
63
64             /* ===== OK, now let's establish some remote port forwardings ===== */
65             
66             /* Example Port Forwarding: -R 127.0.0.1:8080:www.ganymed.ethz.ch:80 (OpenSSH notation)
67              *
68              * Specifies that the port 127.0.0.1:8080 on the remote server is to be forwarded to the
69              * given host and port on the local side. This works by allocating a socket to listen to port
70              * 8080 on the remote side (the ssh server), and whenever a connection is made to this port, the
71              * connection is forwarded over the secure channel, and a connection is made to
72              * www.ganymed.ethz.ch:80 by the Ganymed SSH-2 library.
73              *
74              * (the above text is based partially on the OpenSSH man page)
75              */

76
77             /* You can create as many of them as you want */
78             
79             conn.requestRemotePortForwarding("127.0.0.1", 8080, "www.ganymed.ethz.ch", 80);
80
81             /* Now, on the ssh server, if you connect to 127.0.0.1:8080, then the connection is forwarded
82              * through the secure tunnel to the library, which in turn will forward the connection
83              * to www.ganymed.ethz.ch:80. */

84
85             /* Sleep a bit... (30 seconds) */
86             sleepSomeTime(30000);
87
88             /* Stop accepting remote connections that are being forwarded to www.ganymed.ethz.ch:80 */
89
90             conn.cancelRemotePortForwarding(8080);
91
92             /* Sleep a bit... (20 seconds) */
93             sleepSomeTime(20000);
94
95             /* Stop accepting connections on 127.0.0.1:8080 that are being forwarded to www.ethz.ch:80 */
96
97             lpf1.close();
98             
99             /* Close the connection */
100
101             conn.close();
102
103         }
104         catch (IOException JavaDoc e)
105         {
106             e.printStackTrace(System.err);
107             System.exit(2);
108         }
109     }
110 }
111
Popular Tags