📄 connection.java
字号:
*
* @return A {@link ConnectionInfo} object.
* @throws IOException
* In case of any failure behind the scenes.
*/
public synchronized ConnectionInfo getConnectionInfo() throws IOException
{
if (tm == null)
throw new IllegalStateException(
"Cannot get details of connection, you need to establish a connection first.");
return tm.getConnectionInfo(1);
}
/**
* After a successful connect, one has to authenticate oneself. This method
* can be used to tell which authentication methods are supported by the
* server at a certain stage of the authentication process (for the given
* username).
* <p>
* Note 1: the username will only be used if no authentication step was done
* so far (it will be used to ask the server for a list of possible
* authentication methods). Otherwise, this method ignores the user name and
* returns a cached method list (which is based on the information contained
* in the last negative server response).
* <p>
* Note 2: the server may return method names that are not supported by this
* implementation.
* <p>
* After a successful authentication, this method must not be called
* anymore.
*
* @param user
* A <code>String</code> holding the username.
*
* @return a (possibly emtpy) array holding authentication method names.
* @throws IOException
*/
public synchronized String[] getRemainingAuthMethods(String user) throws IOException
{
if (user == null)
throw new IllegalArgumentException("user argument may not be NULL!");
if (tm == null)
throw new IllegalStateException("Connection is not established!");
if (authenticated)
throw new IllegalStateException("Connection is already authenticated!");
if (am == null)
am = new AuthenticationManager(tm);
if (cm == null)
cm = new ChannelManager(tm);
return am.getRemainingMethods(user);
}
/**
* Determines if the authentication phase is complete. Can be called at any
* time.
*
* @return <code>true</code> if no further authentication steps are
* needed.
*/
public synchronized boolean isAuthenticationComplete()
{
return authenticated;
}
/**
* Returns true if there was at least on failed authentication request and
* the last failed authentication request was marked with "partial success"
* by the server. This is only needed in the rare case of SSH-2 server setups
* that cannot be satisfied with a single successful authentication request
* (i.e., multiple authentication steps are needed.)
* <p>
* If you are interested in the details, then have a look at
* draft-ietf-secsh-userauth-XX.txt.
*
* @return if the there was a failed authentication step and the last one
* was marked as a "partial success".
*/
public synchronized boolean isAuthenticationPartialSuccess()
{
if (am == null)
return false;
return am.getPartialSuccess();
}
/**
* Checks if a specified authentication method is available. This method is
* actually just a wrapper for {@link #getRemainingAuthMethods(String)
* getRemainingAuthMethods()}.
*
* @param user
* A <code>String</code> holding the username.
* @param method
* An authentication method name (e.g., "publickey", "password",
* "keyboard-interactive") as specified by the SSH-2 standard.
* @return if the specified authentication method is currently available.
* @throws IOException
*/
public synchronized boolean isAuthMethodAvailable(String user, String method) throws IOException
{
if (method == null)
throw new IllegalArgumentException("method argument may not be NULL!");
String methods[] = getRemainingAuthMethods(user);
for (int i = 0; i < methods.length; i++)
{
if (methods[i].compareTo(method) == 0)
return true;
}
return false;
}
/**
* Open a new {@link Session} on this connection. Works only after one has passed
* successfully the authentication step. There is no limit on the number of
* concurrent sessions.
*
* @return A {@link Session} object.
* @throws IOException
*/
public synchronized Session openSession() throws IOException
{
if (tm == null)
throw new IllegalStateException("Cannot open session, you need to establish a connection first.");
if (!authenticated)
throw new IllegalStateException("Cannot open session, connection is not authenticated.");
return new Session(cm, rnd);
}
/**
* Removes duplicates from a String array, keeps only first occurence
* of each element. Does not destroy order of elements; can handle nulls.
* Uses a very efficient O(N^2) algorithm =)
*
* @param list a String array.
* @return a cleaned String array.
*/
private String[] removeDuplicates(String[] list)
{
if ((list == null) || (list.length < 2))
return list;
String[] list2 = new String[list.length];
int count = 0;
for (int i = 0; i < list.length; i++)
{
boolean duplicate = false;
String element = list[i];
for (int j = 0; j < count; j++)
{
if (((element == null) && (list2[j] == null)) || ((element != null) && (element.equals(list2[j]))))
{
duplicate = true;
break;
}
}
if (duplicate)
continue;
list2[count++] = list[i];
}
if (count == list2.length)
return list2;
String[] tmp = new String[count];
System.arraycopy(list2, 0, tmp, 0, count);
return tmp;
}
/**
* Unless you know what you are doing, you will never need this.
*
* @param ciphers
*/
public synchronized void setClient2ServerCiphers(String[] ciphers)
{
if ((ciphers == null) || (ciphers.length == 0))
throw new IllegalArgumentException();
ciphers = removeDuplicates(ciphers);
BlockCipherFactory.checkCipherList(ciphers);
cryptoWishList.c2s_enc_algos = ciphers;
}
/**
* Unless you know what you are doing, you will never need this.
*
* @param macs
*/
public synchronized void setClient2ServerMACs(String[] macs)
{
if ((macs == null) || (macs.length == 0))
throw new IllegalArgumentException();
macs = removeDuplicates(macs);
MAC.checkMacList(macs);
cryptoWishList.c2s_mac_algos = macs;
}
/**
* Sets the parameters for the diffie-hellman group exchange. Unless you
* know what you are doing, you will never need this. Default values are
* defined in the {@link DHGexParameters} class.
*
* @param dgp {@link DHGexParameters}, non null.
*
*/
public synchronized void setDHGexParameters(DHGexParameters dgp)
{
if (dgp == null)
throw new IllegalArgumentException();
dhgexpara = dgp;
}
/**
* Unless you know what you are doing, you will never need this.
*
* @param ciphers
*/
public synchronized void setServer2ClientCiphers(String[] ciphers)
{
if ((ciphers == null) || (ciphers.length == 0))
throw new IllegalArgumentException();
ciphers = removeDuplicates(ciphers);
BlockCipherFactory.checkCipherList(ciphers);
cryptoWishList.s2c_enc_algos = ciphers;
}
/**
* Unless you know what you are doing, you will never need this.
*
* @param macs
*/
public synchronized void setServer2ClientMACs(String[] macs)
{
if ((macs == null) || (macs.length == 0))
throw new IllegalArgumentException();
macs = removeDuplicates(macs);
MAC.checkMacList(macs);
cryptoWishList.s2c_mac_algos = macs;
}
/**
* Define the set of allowed server host key algorithms to be used for
* the following key exchange operations.
* <p>
* Unless you know what you are doing, you will never need this.
*
* @param algos An array of allowed server host key algorithms.
* SSH-2 defines <code>ssh-dss</code> and <code>ssh-rsa</code>.
* The entries of the array must be ordered after preference, i.e.,
* the entry at index 0 is the most preferred one. You must specify
* at least one entry.
*/
public synchronized void setServerHostKeyAlgorithms(String[] algos)
{
if ((algos == null) || (algos.length == 0))
throw new IllegalArgumentException();
algos = removeDuplicates(algos);
KexManager.checkServerHostkeyAlgorithmsList(algos);
cryptoWishList.serverHostKeyAlgorithms = algos;
}
/**
* Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm) on the underlying socket.
* <p>
* Can be called at any time. If the connection has not yet been established
* then the passed value will be stored and set after the socket has been set up.
* The default value that will be used is <code>false</code>.
*
* @param enable the argument passed to the <code>Socket.setTCPNoDelay()</code> method.
* @throws IOException
*/
public synchronized void setTCPNoDelay(boolean enable) throws IOException
{
tcpNoDelay = enable;
if (tm != null)
tm.setTcpNoDelay(enable);
}
/**
* Request a remote port forwarding.
* If successful, then forwarded connections will be redirected to the given target address.
* You can cancle a requested remote port forwarding by calling
* {@link #cancelRemotePortForwarding(int) cancelRemotePortForwarding()}.
* <p>
* A call of this method will block until the peer either agreed or disagreed to your request-
* <p>
* Note 1: this method typically fails if you
* <ul>
* <li>pass a port number for which the used remote user has not enough permissions (i.e., port
* < 1024)</li>
* <li>or pass a port number that is already in use on the remote server</li>
* <li>or if remote port forwarding is disabled on the server.</li>
* </ul>
* <p>
* Note 2: (from the openssh man page): By default, the listening socket on the server will be
* bound to the loopback interface only. This may be overriden by specifying a bind address.
* Specifying a remote bind address will only succeed if the server's <b>GatewayPorts</b> option
* is enabled (see sshd_config(5)).
*
* @param bindAddress address to bind to on the server:
* <ul>
* <li>"" means that connections are to be accepted on all protocol families
* supported by the SSH implementation</li>
* <li>"0.0.0.0" means to listen on all IPv4 addresses</li>
* <li>"::" means to listen on all IPv6 addresses</li>
* <li>"localhost" means to listen on all protocol families supported by the SSH
* implementation on loopback addresses only, [RFC3330] and RFC3513]</li>
* <li>"127.0.0.1" and "::1" indicate listening on the loopback interfaces for
* IPv4 and IPv6 respectively</li>
* </ul>
* @param bindPort port number to bind on the server (must be > 0)
* @param targetAddress the target address (IP or hostname)
* @param targetPort the target port
* @throws IOException
*/
public synchronized void requestRemotePortForwarding(String bindAddress, int bindPort, String targetAddress,
int targetPort) throws IOException
{
if (tm == null)
throw new IllegalStateException("You need to establish a connection first.");
if (!authenticated)
throw new IllegalStateException("The connection is not authenticated.");
if ((bindAddress == null) || (targetAddress == null) || (bindPort <= 0) || (targetPort <= 0))
throw new IllegalArgumentException();
cm.requestGlobalForward(bindAddress, bindPort, targetAddress, targetPort);
}
/**
* Cancel an earlier requested remote port forwarding.
* Currently active forwardings will not be affected (e.g., disrupted).
* Note that further connection forwarding requests may be received until
* this method has returned.
*
* @param bindPort the allocated port number on the server
* @throws IOException if the remote side refuses the cancel request or another low
* level error occurs (e.g., the underlying connection is closed)
*/
public synchronized void cancelRemotePortForwarding(int bindPort) throws IOException
{
if (tm == null)
throw new IllegalStateException("You need to establish a connection first.");
if (!authenticated)
throw new IllegalStateException("The connection is not authenticated.");
cm.requestCancelGlobalForward(bindPort);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -