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