Repository: mina-sshd Updated Branches: refs/heads/master 1a983a40f -> ace6c9fed
[SSHD-831] Expose tunneling information from port forwarding channel classes Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/e1acf19c Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/e1acf19c Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/e1acf19c Branch: refs/heads/master Commit: e1acf19c62e6cbf8740323c73acbb55bcfe2dee4 Parents: 1a983a4 Author: roberto.deandrea <[email protected]> Authored: Fri Jul 6 06:00:10 2018 +0300 Committer: Lyor Goldstein <[email protected]> Committed: Fri Jul 6 10:20:44 2018 +0300 ---------------------------------------------------------------------- .../ForwardingTunnelEndpointsProvider.java | 31 ++++++++++++++ .../sshd/common/forward/TcpipClientChannel.java | 22 +++++++++- .../sshd/server/forward/TcpipServerChannel.java | 44 ++++++++++++++++---- 3 files changed, 86 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e1acf19c/sshd-core/src/main/java/org/apache/sshd/common/forward/ForwardingTunnelEndpointsProvider.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/ForwardingTunnelEndpointsProvider.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/ForwardingTunnelEndpointsProvider.java new file mode 100644 index 0000000..da5f77b --- /dev/null +++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/ForwardingTunnelEndpointsProvider.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.sshd.common.forward; + +import org.apache.sshd.common.util.net.SshdSocketAddress; + +/** + * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> + */ +public interface ForwardingTunnelEndpointsProvider { + SshdSocketAddress getTunnelEntrance(); + + SshdSocketAddress getTunnelExit(); +} http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e1acf19c/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java index 9c19a9d..498d61f 100644 --- a/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java +++ b/sshd-core/src/main/java/org/apache/sshd/common/forward/TcpipClientChannel.java @@ -47,7 +47,7 @@ import org.apache.sshd.common.util.net.SshdSocketAddress; * * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ -public class TcpipClientChannel extends AbstractClientChannel { +public class TcpipClientChannel extends AbstractClientChannel implements ForwardingTunnelEndpointsProvider { /** * Type of channel being created. The type's {@link #getName()} * method returns the SSH request type @@ -74,6 +74,8 @@ public class TcpipClientChannel extends AbstractClientChannel { private final Type typeEnum; private final IoSession serverSession; private final SshdSocketAddress remote; + private SshdSocketAddress tunnelEntrance; + private SshdSocketAddress tunnelExit; public TcpipClientChannel(Type type, IoSession serverSession, SshdSocketAddress remote) { super(Objects.requireNonNull(type, "No type specified").getName()); @@ -96,13 +98,19 @@ public class TcpipClientChannel extends AbstractClientChannel { InetSocketAddress dst; Type openType = getTcpipChannelType(); switch (openType) { - case Direct: + case Direct: { src = (InetSocketAddress) serverSession.getRemoteAddress(); dst = this.remote.toInetSocketAddress(); + InetSocketAddress loc = (InetSocketAddress) serverSession.getLocalAddress(); + tunnelEntrance = new SshdSocketAddress(loc.getHostString(), loc.getPort()); + tunnelExit = new SshdSocketAddress(dst.getHostString(), dst.getPort()); break; + } case Forwarded: src = (InetSocketAddress) serverSession.getRemoteAddress(); dst = (InetSocketAddress) serverSession.getLocalAddress(); + tunnelEntrance = new SshdSocketAddress(src.getHostString(), src.getPort()); + tunnelExit = new SshdSocketAddress(dst.getHostString(), dst.getPort()); break; default: throw new SshException("Unknown client channel type: " + openType); @@ -166,4 +174,14 @@ public class TcpipClientChannel extends AbstractClientChannel { protected void doWriteExtendedData(byte[] data, int off, long len) throws IOException { throw new UnsupportedOperationException(getChannelType() + "Tcpip channel does not support extended data"); } + + @Override + public SshdSocketAddress getTunnelEntrance() { + return tunnelEntrance; + } + + @Override + public SshdSocketAddress getTunnelExit() { + return tunnelExit; + } } http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/e1acf19c/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java ---------------------------------------------------------------------- diff --git a/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java b/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java index 258d47d..a16a0de 100644 --- a/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java +++ b/sshd-core/src/main/java/org/apache/sshd/server/forward/TcpipServerChannel.java @@ -35,6 +35,7 @@ import org.apache.sshd.common.channel.ChannelFactory; import org.apache.sshd.common.channel.ChannelOutputStream; import org.apache.sshd.common.channel.Window; import org.apache.sshd.common.channel.exception.SshChannelOpenException; +import org.apache.sshd.common.forward.ForwardingTunnelEndpointsProvider; import org.apache.sshd.common.future.CloseFuture; import org.apache.sshd.common.io.IoConnectFuture; import org.apache.sshd.common.io.IoConnector; @@ -58,7 +59,7 @@ import org.apache.sshd.server.forward.TcpForwardingFilter.Type; * * @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a> */ -public class TcpipServerChannel extends AbstractServerChannel { +public class TcpipServerChannel extends AbstractServerChannel implements ForwardingTunnelEndpointsProvider { public abstract static class TcpipFactory implements ChannelFactory, ExecutorServiceCarrier { private final ForwardingFilter.Type type; @@ -98,6 +99,9 @@ public class TcpipServerChannel extends AbstractServerChannel { private IoConnector connector; private IoSession ioSession; private OutputStream out; + private SshdSocketAddress tunnelEntrance; + private SshdSocketAddress tunnelExit; + private SshdSocketAddress originatorAddress; public TcpipServerChannel(ForwardingFilter.Type type) { this.type = Objects.requireNonNull(type, "No channel type specified"); @@ -108,6 +112,24 @@ public class TcpipServerChannel extends AbstractServerChannel { } @Override + public SshdSocketAddress getTunnelEntrance() { + return tunnelEntrance; + } + + @Override + public SshdSocketAddress getTunnelExit() { + return tunnelExit; + } + + public SshdSocketAddress getOriginatorAddress() { + return originatorAddress; + } + + public IoSession getIoSession() { + return ioSession; + } + + @Override protected OpenFuture doInit(Buffer buffer) { String hostToConnect = buffer.getString(); int portToConnect = buffer.getInt(); @@ -134,6 +156,10 @@ public class TcpipServerChannel extends AbstractServerChannel { throw new IllegalStateException("Unknown server channel type: " + channelType); } + originatorAddress = new SshdSocketAddress(originatorIpAddress, originatorPort); + tunnelEntrance = new SshdSocketAddress(hostToConnect, portToConnect); + tunnelExit = address; + Session session = getSession(); FactoryManager manager = Objects.requireNonNull(session.getFactoryManager(), "No factory manager"); TcpForwardingFilter filter = manager.getTcpForwardingFilter(); @@ -152,7 +178,7 @@ public class TcpipServerChannel extends AbstractServerChannel { } } catch (Error e) { log.warn("doInit({})[{}] failed ({}) to consult forwarding filter: {}", - session, channelType, e.getClass().getSimpleName(), e.getMessage()); + session, channelType, e.getClass().getSimpleName(), e.getMessage()); if (debugEnabled) { log.debug("doInit(" + this + ")[" + type + "] filter consultation failure details", e); } @@ -193,7 +219,7 @@ public class TcpipServerChannel extends AbstractServerChannel { boolean immediately = !session.isOpen(); if (debugEnabled) { log.debug("exceptionCaught({}) signal close immediately={} due to {}[{}]", - TcpipServerChannel.this, immediately, cause.getClass().getSimpleName(), cause.getMessage()); + TcpipServerChannel.this, immediately, cause.getClass().getSimpleName(), cause.getMessage()); } close(immediately); } @@ -293,8 +319,8 @@ public class TcpipServerChannel extends AbstractServerChannel { // allocate a temporary executor service if none provided ExecutorService executors = (service == null) - ? ThreadUtils.newSingleThreadExecutor("TcpIpServerChannel-ConnectorCleanup[" + getSession() + "]") - : service; + ? ThreadUtils.newSingleThreadExecutor("TcpIpServerChannel-ConnectorCleanup[" + getSession() + "]") + : service; // shutdown the temporary executor service if had to create it boolean shutdown = (executors != service) || isShutdownOnExit(); @@ -345,8 +371,8 @@ public class TcpipServerChannel extends AbstractServerChannel { } catch (Throwable e) { if (log.isDebugEnabled()) { log.debug("handleWriteDataSuccess({})[{}] failed ({}) to consume len={}: {}", - this, SshConstants.getCommandMessageName(cmd & 0xFF), - e.getClass().getSimpleName(), len, e.getMessage()); + this, SshConstants.getCommandMessageName(cmd & 0xFF), + e.getClass().getSimpleName(), len, e.getMessage()); } session.exceptionCaught(e); } @@ -356,8 +382,8 @@ public class TcpipServerChannel extends AbstractServerChannel { boolean debugEnabled = log.isDebugEnabled(); if (debugEnabled) { log.debug("handleWriteDataFailure({})[{}] failed ({}) to write len={}: {}", - this, SshConstants.getCommandMessageName(cmd & 0xFF), - t.getClass().getSimpleName(), len, t.getMessage()); + this, SshConstants.getCommandMessageName(cmd & 0xFF), + t.getClass().getSimpleName(), len, t.getMessage()); } if (log.isTraceEnabled()) {
