0
|
1
|
|
2 package com.trilead.ssh2;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.io.InputStream;
|
|
6 import java.io.OutputStream;
|
|
7
|
|
8 import com.trilead.ssh2.channel.Channel;
|
|
9 import com.trilead.ssh2.channel.ChannelManager;
|
|
10 import com.trilead.ssh2.channel.LocalAcceptThread;
|
|
11
|
|
12
|
|
13 /**
|
|
14 * A <code>LocalStreamForwarder</code> forwards an Input- and Outputstream
|
|
15 * pair via the secure tunnel to another host (which may or may not be identical
|
|
16 * to the remote SSH-2 server).
|
|
17 *
|
|
18 * @author Christian Plattner, plattner@trilead.com
|
|
19 * @version $Id: LocalStreamForwarder.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
|
|
20 */
|
|
21 public class LocalStreamForwarder {
|
|
22 ChannelManager cm;
|
|
23
|
|
24 String host_to_connect;
|
|
25 int port_to_connect;
|
|
26 LocalAcceptThread lat;
|
|
27
|
|
28 Channel cn;
|
|
29
|
|
30 LocalStreamForwarder(ChannelManager cm, String host_to_connect, int port_to_connect) throws IOException {
|
|
31 this.cm = cm;
|
|
32 this.host_to_connect = host_to_connect;
|
|
33 this.port_to_connect = port_to_connect;
|
|
34 cn = cm.openDirectTCPIPChannel(host_to_connect, port_to_connect, "127.0.0.1", 0);
|
|
35 }
|
|
36
|
|
37 /**
|
|
38 * @return An <code>InputStream</code> object.
|
|
39 * @throws IOException
|
|
40 */
|
|
41 public InputStream getInputStream() throws IOException {
|
|
42 return cn.getStdoutStream();
|
|
43 }
|
|
44
|
|
45 /**
|
|
46 * Get the OutputStream. Please be aware that the implementation MAY use an
|
|
47 * internal buffer. To make sure that the buffered data is sent over the
|
|
48 * tunnel, you have to call the <code>flush</code> method of the
|
|
49 * <code>OutputStream</code>. To signal EOF, please use the
|
|
50 * <code>close</code> method of the <code>OutputStream</code>.
|
|
51 *
|
|
52 * @return An <code>OutputStream</code> object.
|
|
53 * @throws IOException
|
|
54 */
|
|
55 public OutputStream getOutputStream() throws IOException {
|
|
56 return cn.getStdinStream();
|
|
57 }
|
|
58
|
|
59 /**
|
|
60 * Close the underlying SSH forwarding channel and free up resources.
|
|
61 * You can also use this method to force the shutdown of the underlying
|
|
62 * forwarding channel. Pending output (OutputStream not flushed) will NOT
|
|
63 * be sent. Pending input (InputStream) can still be read. If the shutdown
|
|
64 * operation is already in progress (initiated from either side), then this
|
|
65 * call is a no-op.
|
|
66 *
|
|
67 * @throws IOException
|
|
68 */
|
|
69 public void close() throws IOException {
|
|
70 cm.closeChannel(cn, "Closed due to user request.", true);
|
|
71 }
|
|
72 }
|