All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pastdev.jsch.tunnel.Tunnel Maven / Gradle / Ivy

Go to download

A set of extensions on top of JSch providing a full SCP protocol implementation, tunneling including multi-hop, a Java 7 FileSystem like implementation for Java 6 and remote command execution

The newest version!
package com.pastdev.jsch.tunnel;


/**
 * Tunnel stores all the information needed to define an ssh port-forwarding
 * tunnel.
 * 
 * @see rfc4254
 */
public class Tunnel {
    private String spec;
    private String destinationHostname;
    private int destinationPort;
    private String localAlias;
    private int localPort;
    private int assignedLocalPort;

    /**
     * Creates a Tunnel from a spec string. For details on this
     * string, see {@link #getSpec()}.
     * 

* Both localAlias and localPort are optional, in * which case they default to localhost and 0 * respectively. *

*

* Examples: * *

     * // Equivalaent to new Tunnel("localhost", 0, "foobar", 1234);
     * new Tunnel( "foobar:1234" );
     * // Equivalaent to new Tunnel("localhost", 1234, "foobar", 1234);
     * new Tunnel( "1234:foobar:1234" );
     * // Equivalaent to new Tunnel("local_foobar", 1234, "foobar", 1234);
     * new Tunnel( "local_foobar:1234:foobar:1234" );
     * 
* * @param spec A tunnel spec string * * @see #Tunnel(String, int, String, int) * @see rfc4254 */ public Tunnel( String spec ) { String[] parts = spec.split( ":" ); if ( parts.length == 4 ) { this.localAlias = parts[0]; this.localPort = Integer.parseInt( parts[1] ); this.destinationHostname = parts[2]; this.destinationPort = Integer.parseInt( parts[3] ); } else if ( parts.length == 3 ) { this.localPort = Integer.parseInt( parts[0] ); this.destinationHostname = parts[1]; this.destinationPort = Integer.parseInt( parts[2] ); } else { this.localPort = 0; // dynamically assigned port this.destinationHostname = parts[0]; this.destinationPort = Integer.parseInt( parts[1] ); } } /** * Creates a Tunnel to destinationPort on * destinationHostname from a dynamically assigned port on * localhost. Simply calls * * @param destinationHostname * The hostname to tunnel to * @param destinationPort * The port to tunnel to * * @see #Tunnel(int, String, int) * @see rfc4254 */ public Tunnel( String destinationHostname, int destinationPort ) { this( 0, destinationHostname, destinationPort ); } /** * Creates a Tunnel to destinationPort on * destinationHostname from localPort on * localhost. * * @param localPort * The local port to bind to * @param destinationHostname * The hostname to tunnel to * @param destinationPort * The port to tunnel to * * @see #Tunnel(String, int, String, int) * @see rfc4254 */ public Tunnel( int localPort, String destinationHostname, int destinationPort ) { this( null, localPort, destinationHostname, destinationPort ); } /** * Creates a Tunnel to destinationPort on * destinationHostname from localPort on * localAlias. *

* This is similar in behavior to the -L option in ssh, with * the exception that you can specify 0 for the local port in * which case the port will be dynamically allocated and you can * {@link #getAssignedLocalPort()} after the tunnel has been started. *

*

* A common use case for localAlias might be to link your * loopback interfaces to names via an entries in /etc/hosts * which would allow you to use the same port number for more than one * tunnel. For example: * *

     * 127.0.0.2 foo
     * 127.0.0.3 bar
     * 
* * Would allow you to have both of these open at the same time: * *
     * new Tunnel( "foo", 1234, "remote_foo", 1234 );
     * new Tunnel( "bar", 1234, "remote_bar", 1234 );
     * 
* * @param localAlias * The local interface to bind to * @param localPort * The local port to bind to * @param destinationHostname * The hostname to tunnel to * @param destinationPort * The port to tunnel to * * @see com.jcraft.jsch.Session#setPortForwardingL(String, int, String, int) * @see rfc4254 */ public Tunnel( String localAlias, int localPort, String destinationHostname, int destinationPort ) { this.localAlias = localAlias; this.localPort = localPort; this.destinationHostname = destinationHostname; this.destinationPort = destinationPort; } /** * Returns true if other is a Tunnel whose spec * (either specified or calculated) is equal to this tunnels * spec. * * @return True if both tunnels have equivalent spec's * * @see #getSpec() */ @Override public boolean equals( Object other ) { return (other instanceof Tunnel) && getSpec().equals( ((Tunnel) other).getSpec() ); } /** * Returns the local port currently bound to. If 0 was * specified as the port to bind to, this will return the dynamically * allocated port, otherwise it will return the port specified. * * @return The local port currently bound to */ public int getAssignedLocalPort() { return assignedLocalPort == 0 ? localPort : assignedLocalPort; } /** * Returns the hostname of the destination. * * @return The hostname of the destination */ public String getDestinationHostname() { return destinationHostname; } /** * Returns the port of the destination. * * @return The port of the destination */ public int getDestinationPort() { return destinationPort; } /** * Returns the local alias bound to. See rfc4254 for * details on acceptible values. * * @return The local alias bound to */ public String getLocalAlias() { return localAlias; } /** * Returns the port this tunnel was configured with. If you want to get the * runtime port, use {@link #getAssignedLocalPort()}. * * @return The port this tunnel was configured with */ public int getLocalPort() { return localPort; } /** * Returns the spec string (either calculated or specified) for this tunnel. *

* A spec string is composed of 4 parts separated by a colon (: * ): *

    *
  1. localAlias (optional)
  2. *
  3. localPort (optional)
  4. *
  5. destinationHostname
  6. *
  7. destinationPort
  8. *
* * @return The spec string */ public String getSpec() { if ( spec == null ) { spec = toString().toLowerCase(); } return spec; } @Override public int hashCode() { return getSpec().hashCode(); } void setAssignedLocalPort( int port ) { this.assignedLocalPort = port; } @Override public String toString() { return (localAlias == null ? "" : localAlias + ":") + (assignedLocalPort == 0 ? localPort : ("(0)" + assignedLocalPort)) + ":" + destinationHostname + ":" + destinationPort; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy