public ChannelDirectTcpip() {
}
protected OpenFuture doInit(Buffer buffer) {
final OpenFuture f = new DefaultOpenFuture(this);
String hostToConnect = buffer.getString();
int portToConnect = buffer.getInt();
InetSocketAddress address;
try {
address = new InetSocketAddress(hostToConnect, portToConnect);
} catch (RuntimeException e) {
address = null;
}
final ServerSession serverSession = (ServerSession)getSession();
final ForwardingFilter filter = serverSession.getServerFactoryManager().getForwardingFilter();
if (address == null || filter == null || !filter.canConnect(address, serverSession)) {
super.close(true);
f.setException(new OpenChannelException(SshConstants.SSH_OPEN_ADMINISTRATIVELY_PROHIBITED, "connect denied"));
return f;
}
String originatorIpAddress = buffer.getString();
int originatorPort = buffer.getInt();
log.info("Receiving request for direct tcpip: hostToConnect={}, portToConnect={}, originatorIpAddress={}, originatorPort={}",
new Object[] { hostToConnect, portToConnect, originatorIpAddress, originatorPort });
connector = new NioSocketConnector();
out = new ChannelOutputStream(this, remoteWindow, log, SshConstants.Message.SSH_MSG_CHANNEL_DATA);
IoHandler handler = new IoHandlerAdapter() {
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
IoBuffer ioBuffer = (IoBuffer) message;
int r = ioBuffer.remaining();
byte[] b = new byte[r];
ioBuffer.get(b, 0, r);
out.write(b, 0, r);
out.flush();
}
@Override
public void sessionClosed(IoSession session) throws Exception {
sendEof();
}
};
connector.setHandler(handler);
ConnectFuture future = connector.connect(address);
future.addListener(new IoFutureListener<ConnectFuture>() {
public void operationComplete(ConnectFuture future) {
if (future.isConnected()) {
ioSession = future.getSession();
f.setOpened();
} else if (future.getException() != null) {
closeImmediately0();
if (future.getException() instanceof ConnectException) {
f.setException(new OpenChannelException(
SshConstants.SSH_OPEN_CONNECT_FAILED,
future.getException().getMessage(),
future.getException()));
} else {
f.setException(future.getException());
}
}
}
});
return f;