This commit is contained in:
Shikhar Bhushan
2010-03-21 01:21:27 +01:00
parent e327e2e8ad
commit bdb1fe73e3

View File

@@ -54,25 +54,25 @@ public class LocalPortForwarder {
} }
}); });
new StreamCopier("chan2soc", getInputStream(), sock.getOutputStream()) // new StreamCopier("chan2soc", getInputStream(), sock.getOutputStream())
.bufSize(getLocalMaxPacketSize()) // .bufSize(getLocalMaxPacketSize())
.errorCallback(closer) // .errorCallback(closer)
.daemon(true) // .daemon(true)
.start(); .start();
new StreamCopier("soc2chan", sock.getInputStream(), getOutputStream()) // new StreamCopier("soc2chan", sock.getInputStream(), getOutputStream())
.bufSize(getRemoteMaxPacketSize()) // .bufSize(getRemoteMaxPacketSize())
.errorCallback(closer) // .errorCallback(closer)
.daemon(true) // .daemon(true)
.start(); .start();
} }
@Override @Override
protected SSHPacket buildOpenReq() { protected SSHPacket buildOpenReq() {
return super.buildOpenReq() // return super.buildOpenReq()
.putString(host) // .putString(host)
.putInt(port) // .putInt(port)
.putString(ss.getInetAddress().getHostAddress()) // .putString(ss.getInetAddress().getHostAddress())
.putInt(ss.getLocalPort()); .putInt(ss.getLocalPort());
} }
@@ -85,6 +85,19 @@ public class LocalPortForwarder {
private final String host; private final String host;
private final int port; private final int port;
/**
* Create a local port forwarder with specified binding ({@code listeningAddr}. It does not, however, start
* listening unless {@link #listen() explicitly told to}. The {@link javax.net.ServerSocketFactory#getDefault()
* default} server socket factory is used.
*
* @param conn {@link Connection} implementation
* @param listeningAddr {@link SocketAddress} this forwarder will listen on, if {@code null} then an ephemeral port
* and valid local address will be picked to bind the server socket
* @param host what host the SSH server will further forward to
* @param port port on {@code toHost}
*
* @throws IOException if there is an error binding on specified {@code listeningAddr}
*/
public LocalPortForwarder(Connection conn, SocketAddress listeningAddr, String host, int port) public LocalPortForwarder(Connection conn, SocketAddress listeningAddr, String host, int port)
throws IOException { throws IOException {
this(ServerSocketFactory.getDefault(), conn, listeningAddr, host, port); this(ServerSocketFactory.getDefault(), conn, listeningAddr, host, port);
@@ -94,6 +107,7 @@ public class LocalPortForwarder {
* Create a local port forwarder with specified binding ({@code listeningAddr}. It does not, however, start * Create a local port forwarder with specified binding ({@code listeningAddr}. It does not, however, start
* listening unless {@link #listen() explicitly told to}. * listening unless {@link #listen() explicitly told to}.
* *
* @param ssf factory to use for creating the server socket
* @param conn {@link Connection} implementation * @param conn {@link Connection} implementation
* @param listeningAddr {@link SocketAddress} this forwarder will listen on, if {@code null} then an ephemeral port * @param listeningAddr {@link SocketAddress} this forwarder will listen on, if {@code null} then an ephemeral port
* and valid local address will be picked to bind the server socket * and valid local address will be picked to bind the server socket
@@ -112,16 +126,21 @@ public class LocalPortForwarder {
ss.bind(listeningAddr); ss.bind(listeningAddr);
} }
/** @return the address to which this forwarder is bound for listening */
public SocketAddress getListeningAddress() { public SocketAddress getListeningAddress() {
return ss.getLocalSocketAddress(); return ss.getLocalSocketAddress();
} }
/** Start listening for incoming connections and forward to remote host as a channel. */ /**
* Start listening for incoming connections and forward to remote host as a channel.
*
* @throws IOException
*/
public void listen() public void listen()
throws IOException { throws IOException {
log.info("Listening on {}", ss.getLocalSocketAddress()); log.info("Listening on {}", ss.getLocalSocketAddress());
Socket sock; Socket sock;
while (true) { while (!Thread.currentThread().isInterrupted()) {
sock = ss.accept(); sock = ss.accept();
log.info("Got connection from {}", sock.getRemoteSocketAddress()); log.info("Got connection from {}", sock.getRemoteSocketAddress());
DirectTCPIPChannel chan = new DirectTCPIPChannel(conn, sock); DirectTCPIPChannel chan = new DirectTCPIPChannel(conn, sock);