Search in sources :

Example 1 with ForwardingChannelInfo

use of com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo in project agileway by fangjinuo.

the class Ssh2ForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    if (!localForwarderMap.containsKey(ForwardingChannelInfo.id(channel))) {
        Connection delegate = this.connection.getDelegate();
        try {
            LocalPortForwarder localPortForwarder = delegate.createLocalPortForwarder(new InetSocketAddress(bindToHost, bindToPort), destHost, destPort);
            localForwarderMap.put(ForwardingChannelInfo.id(channel), localPortForwarder);
        } catch (Throwable ex) {
            throw new SshException(ex);
        }
    }
    return channel;
}
Also used : LocalPortForwarder(com.trilead.ssh2.LocalPortForwarder) InetSocketAddress(java.net.InetSocketAddress) Connection(com.trilead.ssh2.Connection) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Example 2 with ForwardingChannelInfo

use of com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo in project agileway by fangjinuo.

the class SshjForwardingClient method startRemoteForwarding.

@Override
public ForwardingChannelInfo startRemoteForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    SSHClient delegate = connection.getDelegate();
    RemotePortForwarder forwarder = delegate.getRemotePortForwarder();
    RemotePortForwarder.Forward forward = new RemotePortForwarder.Forward(bindToHost, bindToPort);
    try {
        forwarder.bind(forward, new SocketForwardingConnectListener(new InetSocketAddress(bindToHost, bindToPort)));
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return new ForwardingChannelInfo(ForwardingChannelInfo.REMOTE_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
}
Also used : SocketForwardingConnectListener(net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener) InetSocketAddress(java.net.InetSocketAddress) SSHClient(net.schmizz.sshj.SSHClient) SshException(com.jn.agileway.ssh.client.SshException) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) RemotePortForwarder(net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder)

Example 3 with ForwardingChannelInfo

use of com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo in project agileway by fangjinuo.

the class SshjForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    SSHClient delegate = connection.getDelegate();
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    if (!localForwardingMap.containsKey(ForwardingChannelInfo.id(channel))) {
        SocketAddress address = new InetSocketAddress(bindToHost, bindToPort);
        try {
            ServerSocket serverSocket = new ServerSocket();
            serverSocket.bind(address);
            Object parameters = Reflects.newInstance(LOCAL_PORT_FORWARDER_PARAMETERS, new Class[] { String.class, int.class, String.class, int.class }, bindToHost, bindToPort, destHost, destPort);
            final LocalPortForwarder forwarder = Reflects.invoke(NEW_LOCAL_PORT_FORWARDER, delegate, new Object[] { parameters, serverSocket }, true, true);
            Thread t = new Thread() {

                @Override
                public void run() {
                    try {
                        forwarder.listen();
                    } catch (Throwable ex) {
                    // ignore it , interrupted
                    }
                }
            };
            t.start();
            LocalForwardingChannelCtx ctx = new LocalForwardingChannelCtx();
            ctx.thread = t;
            ctx.channel = channel;
            ctx.forwarder = forwarder;
            localForwardingMap.put(ForwardingChannelInfo.id(channel), ctx);
        } catch (Throwable ex) {
            throw new SshException(ex);
        }
    }
    return channel;
}
Also used : LocalPortForwarder(net.schmizz.sshj.connection.channel.direct.LocalPortForwarder) InetSocketAddress(java.net.InetSocketAddress) SSHClient(net.schmizz.sshj.SSHClient) ServerSocket(java.net.ServerSocket) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 4 with ForwardingChannelInfo

use of com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo in project agileway by fangjinuo.

the class Ssh2ForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    LocalPortForwarder forwarder = localForwarderMap.get(channel);
    if (forwarder == null) {
        try {
            ch.ethz.ssh2.Connection delegate = this.connection.getDelegate();
            forwarder = delegate.createLocalPortForwarder(bindToPort, destHost, destPort);
            localForwarderMap.put(ForwardingChannelInfo.id(channel), forwarder);
        } catch (Throwable ex) {
            throw new SshException(ex);
        }
    }
    return channel;
}
Also used : LocalPortForwarder(ch.ethz.ssh2.LocalPortForwarder) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Example 5 with ForwardingChannelInfo

use of com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo in project agileway by fangjinuo.

the class J2sshForwardingClient method startLocalForwarding.

@Override
public ForwardingChannelInfo startLocalForwarding(String bindToHost, int bindToPort, String destHost, int destPort) throws SshException {
    ForwardingChannelInfo channel = new ForwardingChannelInfo(ForwardingChannelInfo.LOCAL_FORWARDING_CHANNEL, bindToHost, bindToPort, destHost, destPort);
    SshClient sshClient = this.connection.getDelegate();
    com.sshtools.j2ssh.forwarding.ForwardingClient delegate = sshClient.getForwardingClient();
    try {
        if (!delegate.getLocalForwardings().containsKey(ForwardingChannelInfo.id(channel))) {
            delegate.addLocalForwarding(ForwardingChannelInfo.id(channel), bindToHost, bindToPort, destHost, destPort);
        }
        delegate.startLocalForwarding(ForwardingChannelInfo.id(channel));
    } catch (Throwable ex) {
        throw new SshException(ex);
    }
    return channel;
}
Also used : SshClient(com.sshtools.j2ssh.SshClient) ForwardingChannelInfo(com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo) SshException(com.jn.agileway.ssh.client.SshException)

Aggregations

SshException (com.jn.agileway.ssh.client.SshException)7 ForwardingChannelInfo (com.jn.agileway.ssh.client.channel.forwarding.ForwardingChannelInfo)7 InetSocketAddress (java.net.InetSocketAddress)3 SshClient (com.sshtools.j2ssh.SshClient)2 Connection (com.trilead.ssh2.Connection)2 SSHClient (net.schmizz.sshj.SSHClient)2 LocalPortForwarder (ch.ethz.ssh2.LocalPortForwarder)1 LocalPortForwarder (com.trilead.ssh2.LocalPortForwarder)1 ServerSocket (java.net.ServerSocket)1 SocketAddress (java.net.SocketAddress)1 LocalPortForwarder (net.schmizz.sshj.connection.channel.direct.LocalPortForwarder)1 RemotePortForwarder (net.schmizz.sshj.connection.channel.forwarded.RemotePortForwarder)1 SocketForwardingConnectListener (net.schmizz.sshj.connection.channel.forwarded.SocketForwardingConnectListener)1