mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 23:30:55 +03:00
- rename *Protocol to *Impl
- not use Buffer in an interface (ConnectionImpl), use byte[]
This commit is contained in:
@@ -42,7 +42,7 @@ import net.schmizz.sshj.common.SSHException;
|
|||||||
import net.schmizz.sshj.common.SecurityUtils;
|
import net.schmizz.sshj.common.SecurityUtils;
|
||||||
import net.schmizz.sshj.connection.Connection;
|
import net.schmizz.sshj.connection.Connection;
|
||||||
import net.schmizz.sshj.connection.ConnectionException;
|
import net.schmizz.sshj.connection.ConnectionException;
|
||||||
import net.schmizz.sshj.connection.ConnectionProtocol;
|
import net.schmizz.sshj.connection.ConnectionImpl;
|
||||||
import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder;
|
import net.schmizz.sshj.connection.channel.direct.LocalPortForwarder;
|
||||||
import net.schmizz.sshj.connection.channel.direct.Session;
|
import net.schmizz.sshj.connection.channel.direct.Session;
|
||||||
import net.schmizz.sshj.connection.channel.direct.SessionChannel;
|
import net.schmizz.sshj.connection.channel.direct.SessionChannel;
|
||||||
@@ -56,7 +56,7 @@ import net.schmizz.sshj.sftp.SFTPClient;
|
|||||||
import net.schmizz.sshj.sftp.StatefulSFTPClient;
|
import net.schmizz.sshj.sftp.StatefulSFTPClient;
|
||||||
import net.schmizz.sshj.transport.Transport;
|
import net.schmizz.sshj.transport.Transport;
|
||||||
import net.schmizz.sshj.transport.TransportException;
|
import net.schmizz.sshj.transport.TransportException;
|
||||||
import net.schmizz.sshj.transport.TransportProtocol;
|
import net.schmizz.sshj.transport.TransportImpl;
|
||||||
import net.schmizz.sshj.transport.compression.DelayedZlibCompression;
|
import net.schmizz.sshj.transport.compression.DelayedZlibCompression;
|
||||||
import net.schmizz.sshj.transport.compression.NoneCompression;
|
import net.schmizz.sshj.transport.compression.NoneCompression;
|
||||||
import net.schmizz.sshj.transport.compression.ZlibCompression;
|
import net.schmizz.sshj.transport.compression.ZlibCompression;
|
||||||
@@ -64,7 +64,7 @@ import net.schmizz.sshj.transport.verification.HostKeyVerifier;
|
|||||||
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
|
import net.schmizz.sshj.transport.verification.OpenSSHKnownHosts;
|
||||||
import net.schmizz.sshj.userauth.UserAuth;
|
import net.schmizz.sshj.userauth.UserAuth;
|
||||||
import net.schmizz.sshj.userauth.UserAuthException;
|
import net.schmizz.sshj.userauth.UserAuthException;
|
||||||
import net.schmizz.sshj.userauth.UserAuthProtocol;
|
import net.schmizz.sshj.userauth.UserAuthImpl;
|
||||||
import net.schmizz.sshj.userauth.keyprovider.FileKeyProvider;
|
import net.schmizz.sshj.userauth.keyprovider.FileKeyProvider;
|
||||||
import net.schmizz.sshj.userauth.keyprovider.KeyPairWrapper;
|
import net.schmizz.sshj.userauth.keyprovider.KeyPairWrapper;
|
||||||
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
|
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
|
||||||
@@ -143,7 +143,7 @@ public class SSHClient extends SocketClient implements SessionFactory {
|
|||||||
protected final UserAuth auth;
|
protected final UserAuth auth;
|
||||||
|
|
||||||
/** {@code ssh-connection} service */
|
/** {@code ssh-connection} service */
|
||||||
protected final ConnectionProtocol conn;
|
protected final ConnectionImpl conn;
|
||||||
|
|
||||||
/** Default constructor. Initializes this object using {@link DefaultConfig}. */
|
/** Default constructor. Initializes this object using {@link DefaultConfig}. */
|
||||||
public SSHClient() {
|
public SSHClient() {
|
||||||
@@ -157,9 +157,9 @@ public class SSHClient extends SocketClient implements SessionFactory {
|
|||||||
*/
|
*/
|
||||||
public SSHClient(Config config) {
|
public SSHClient(Config config) {
|
||||||
super(DEFAULT_PORT);
|
super(DEFAULT_PORT);
|
||||||
this.trans = new TransportProtocol(config);
|
this.trans = new TransportImpl(config);
|
||||||
this.auth = new UserAuthProtocol(trans);
|
this.auth = new UserAuthImpl(trans);
|
||||||
this.conn = new ConnectionProtocol(trans);
|
this.conn = new ConnectionImpl(trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -36,7 +36,6 @@
|
|||||||
package net.schmizz.sshj.connection;
|
package net.schmizz.sshj.connection;
|
||||||
|
|
||||||
import net.schmizz.concurrent.Future;
|
import net.schmizz.concurrent.Future;
|
||||||
import net.schmizz.sshj.common.Buffer;
|
|
||||||
import net.schmizz.sshj.common.SSHPacket;
|
import net.schmizz.sshj.common.SSHPacket;
|
||||||
import net.schmizz.sshj.connection.channel.Channel;
|
import net.schmizz.sshj.connection.channel.Channel;
|
||||||
import net.schmizz.sshj.connection.channel.OpenFailException;
|
import net.schmizz.sshj.connection.channel.OpenFailException;
|
||||||
@@ -98,7 +97,7 @@ public interface Connection {
|
|||||||
* @throws TransportException if there is an error sending the request
|
* @throws TransportException if there is an error sending the request
|
||||||
*/
|
*/
|
||||||
public Future<SSHPacket, ConnectionException> sendGlobalRequest(String name, boolean wantReply,
|
public Future<SSHPacket, ConnectionException> sendGlobalRequest(String name, boolean wantReply,
|
||||||
Buffer.PlainBuffer specifics) throws TransportException;
|
byte[] specifics) throws TransportException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a {@code SSH_MSG_OPEN_FAILURE} for specified {@code Reason} and {@code message}.
|
* Send a {@code SSH_MSG_OPEN_FAILURE} for specified {@code Reason} and {@code message}.
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ package net.schmizz.sshj.connection;
|
|||||||
import net.schmizz.concurrent.Future;
|
import net.schmizz.concurrent.Future;
|
||||||
import net.schmizz.concurrent.FutureUtils;
|
import net.schmizz.concurrent.FutureUtils;
|
||||||
import net.schmizz.sshj.AbstractService;
|
import net.schmizz.sshj.AbstractService;
|
||||||
import net.schmizz.sshj.common.Buffer;
|
|
||||||
import net.schmizz.sshj.common.DisconnectReason;
|
import net.schmizz.sshj.common.DisconnectReason;
|
||||||
import net.schmizz.sshj.common.ErrorNotifiable;
|
import net.schmizz.sshj.common.ErrorNotifiable;
|
||||||
import net.schmizz.sshj.common.Message;
|
import net.schmizz.sshj.common.Message;
|
||||||
@@ -38,7 +37,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/** {@link Connection} implementation. */
|
/** {@link Connection} implementation. */
|
||||||
public class ConnectionProtocol extends AbstractService implements Connection {
|
public class ConnectionImpl extends AbstractService implements Connection {
|
||||||
|
|
||||||
private final Object internalSynchronizer = new Object();
|
private final Object internalSynchronizer = new Object();
|
||||||
|
|
||||||
@@ -58,7 +57,7 @@ public class ConnectionProtocol extends AbstractService implements Connection {
|
|||||||
*
|
*
|
||||||
* @param trans transport layer
|
* @param trans transport layer
|
||||||
*/
|
*/
|
||||||
public ConnectionProtocol(Transport trans) {
|
public ConnectionImpl(Transport trans) {
|
||||||
super("ssh-connection", trans);
|
super("ssh-connection", trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,13 +174,10 @@ public class ConnectionProtocol extends AbstractService implements Connection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public Future<SSHPacket, ConnectionException> sendGlobalRequest(String name, boolean wantReply,
|
public Future<SSHPacket, ConnectionException> sendGlobalRequest(String name, boolean wantReply,
|
||||||
Buffer.PlainBuffer specifics) throws TransportException {
|
byte[] specifics) throws TransportException {
|
||||||
synchronized (globalReqFutures) {
|
synchronized (globalReqFutures) {
|
||||||
log.info("Making global request for `{}`", name);
|
log.info("Making global request for `{}`", name);
|
||||||
trans.write(new SSHPacket(Message.GLOBAL_REQUEST) //
|
trans.write(new SSHPacket(Message.GLOBAL_REQUEST).putString(name).putBoolean(wantReply).putRawBytes(specifics));
|
||||||
.putString(name) //
|
|
||||||
.putBoolean(wantReply) //
|
|
||||||
.putBuffer(specifics)); //
|
|
||||||
|
|
||||||
Future<SSHPacket, ConnectionException> future = null;
|
Future<SSHPacket, ConnectionException> future = null;
|
||||||
if (wantReply) {
|
if (wantReply) {
|
||||||
@@ -19,6 +19,7 @@ import net.schmizz.sshj.common.SSHException;
|
|||||||
|
|
||||||
/** A factory interface for creating SSH {@link Session session channels}. */
|
/** A factory interface for creating SSH {@link Session session channels}. */
|
||||||
public interface SessionFactory {
|
public interface SessionFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Opens a {@code session} channel. The returned {@link Session} instance allows {@link Session#exec(String)
|
* Opens a {@code session} channel. The returned {@link Session} instance allows {@link Session#exec(String)
|
||||||
* executing a remote command}, {@link Session#startSubsystem(String) starting a subsystem}, or {@link
|
* executing a remote command}, {@link Session#startSubsystem(String) starting a subsystem}, or {@link
|
||||||
|
|||||||
@@ -116,12 +116,12 @@ public class RemotePortForwarder extends AbstractForwardedChannelOpener {
|
|||||||
return address.equals(other.address) && port == other.port;
|
return address.equals(other.address) && port == other.port;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the address represented by this forward. */
|
/** @return the address represented by this forward. */
|
||||||
public String getAddress() {
|
public String getAddress() {
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the port represented by this forward. */
|
/** @return the port represented by this forward. */
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
@@ -179,8 +179,8 @@ public class RemotePortForwarder extends AbstractForwardedChannelOpener {
|
|||||||
*
|
*
|
||||||
* @return the {@link Forward} which was put into place on the remote host
|
* @return the {@link Forward} which was put into place on the remote host
|
||||||
*
|
*
|
||||||
* @throws net.schmizz.sshj.connection.ConnectionException
|
* @throws ConnectionException if there is an error requesting the forwarding
|
||||||
* if there is an error requesting the forwarding
|
* @throws TransportException
|
||||||
*/
|
*/
|
||||||
public Forward bind(Forward forward, ConnectListener listener) throws ConnectionException, TransportException {
|
public Forward bind(Forward forward, ConnectListener listener) throws ConnectionException, TransportException {
|
||||||
SSHPacket reply = req(PF_REQ, forward);
|
SSHPacket reply = req(PF_REQ, forward);
|
||||||
@@ -197,6 +197,7 @@ public class RemotePortForwarder extends AbstractForwardedChannelOpener {
|
|||||||
* @param forward the forward which is being cancelled
|
* @param forward the forward which is being cancelled
|
||||||
*
|
*
|
||||||
* @throws ConnectionException if there is an error with the cancellation request
|
* @throws ConnectionException if there is an error with the cancellation request
|
||||||
|
* @throws TransportException
|
||||||
*/
|
*/
|
||||||
public void cancel(Forward forward) throws ConnectionException, TransportException {
|
public void cancel(Forward forward) throws ConnectionException, TransportException {
|
||||||
try {
|
try {
|
||||||
@@ -207,13 +208,12 @@ public class RemotePortForwarder extends AbstractForwardedChannelOpener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected SSHPacket req(String reqName, Forward forward) throws ConnectionException, TransportException {
|
protected SSHPacket req(String reqName, Forward forward) throws ConnectionException, TransportException {
|
||||||
return conn.sendGlobalRequest(PF_REQ, true, new Buffer.PlainBuffer() //
|
final byte[] specifics = new Buffer.PlainBuffer().putString(forward.address).putInt(forward.port).getCompactData();
|
||||||
.putString(forward.address) //
|
return conn.sendGlobalRequest(reqName, true, specifics)
|
||||||
.putInt(forward.port)) //
|
|
||||||
.get(conn.getTimeout(), TimeUnit.SECONDS);
|
.get(conn.getTimeout(), TimeUnit.SECONDS);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the active forwards. */
|
/** @return the active forwards. */
|
||||||
public Set<Forward> getActiveForwards() {
|
public Set<Forward> getActiveForwards() {
|
||||||
return listeners.keySet();
|
return listeners.keySet();
|
||||||
}
|
}
|
||||||
@@ -223,8 +223,8 @@ public class RemotePortForwarder extends AbstractForwardedChannelOpener {
|
|||||||
* {@code ConnectListener} for that forward in a separate thread.
|
* {@code ConnectListener} for that forward in a separate thread.
|
||||||
*/
|
*/
|
||||||
public void handleOpen(SSHPacket buf) throws ConnectionException, TransportException {
|
public void handleOpen(SSHPacket buf) throws ConnectionException, TransportException {
|
||||||
ForwardedTCPIPChannel chan = new ForwardedTCPIPChannel(conn, buf.readInt(), buf.readInt(), buf.readInt(), //
|
final ForwardedTCPIPChannel chan = new ForwardedTCPIPChannel(conn, buf.readInt(), buf.readInt(), buf.readInt(),
|
||||||
new Forward(buf.readString(), buf.readInt()), //
|
new Forward(buf.readString(), buf.readInt()),
|
||||||
buf.readString(), buf.readInt());
|
buf.readString(), buf.readInt());
|
||||||
if (listeners.containsKey(chan.getParentForward()))
|
if (listeners.containsKey(chan.getParentForward()))
|
||||||
callListener(listeners.get(chan.getParentForward()), chan);
|
callListener(listeners.get(chan.getParentForward()), chan);
|
||||||
|
|||||||
@@ -45,13 +45,13 @@ final class Heartbeater extends Thread {
|
|||||||
|
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private final TransportProtocol trans;
|
private final TransportImpl trans;
|
||||||
|
|
||||||
private int interval;
|
private int interval;
|
||||||
|
|
||||||
private boolean started;
|
private boolean started;
|
||||||
|
|
||||||
Heartbeater(TransportProtocol trans) {
|
Heartbeater(TransportImpl trans) {
|
||||||
this.trans = trans;
|
this.trans = trans;
|
||||||
setName("heartbeater");
|
setName("heartbeater");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ final class KeyExchanger implements SSHPacketHandler, ErrorNotifiable {
|
|||||||
|
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private final TransportProtocol transport;
|
private final TransportImpl transport;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link HostKeyVerifier#verify(String, int, java.security.PublicKey)} is invoked by {@link #verifyHost(PublicKey)}
|
* {@link HostKeyVerifier#verify(String, int, java.security.PublicKey)} is invoked by {@link #verifyHost(PublicKey)}
|
||||||
@@ -95,10 +95,10 @@ final class KeyExchanger implements SSHPacketHandler, ErrorNotifiable {
|
|||||||
|
|
||||||
private final Event<TransportException> done;
|
private final Event<TransportException> done;
|
||||||
|
|
||||||
KeyExchanger(TransportProtocol trans) {
|
KeyExchanger(TransportImpl trans) {
|
||||||
this.transport = trans;
|
this.transport = trans;
|
||||||
/*
|
/*
|
||||||
* Use TransportProtocol's writeLock, since TransportProtocol.write() may wait on this event and the lock should
|
* Use TransportImpl's writeLock, since TransportImpl.write() may wait on this event and the lock should
|
||||||
* be released while waiting.
|
* be released while waiting.
|
||||||
*/
|
*/
|
||||||
this.done = new Event<TransportException>("kex done", TransportException.chainer, trans.getWriteLock());
|
this.done = new Event<TransportException>("kex done", TransportException.chainer, trans.getWriteLock());
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ final class Reader extends Thread {
|
|||||||
|
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
private final TransportProtocol trans;
|
private final TransportImpl trans;
|
||||||
|
|
||||||
Reader(TransportProtocol trans) {
|
Reader(TransportImpl trans) {
|
||||||
this.trans = trans;
|
this.trans = trans;
|
||||||
setName("reader");
|
setName("reader");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
/** A thread-safe {@link Transport} implementation. */
|
/** A thread-safe {@link Transport} implementation. */
|
||||||
public final class TransportProtocol implements Transport {
|
public final class TransportImpl implements Transport {
|
||||||
|
|
||||||
private static final class NullService extends AbstractService {
|
private static final class NullService extends AbstractService {
|
||||||
NullService(Transport trans) {
|
NullService(Transport trans) {
|
||||||
@@ -121,7 +121,7 @@ public final class TransportProtocol implements Transport {
|
|||||||
|
|
||||||
private final ReentrantLock writeLock = new ReentrantLock();
|
private final ReentrantLock writeLock = new ReentrantLock();
|
||||||
|
|
||||||
public TransportProtocol(Config config) {
|
public TransportImpl(Config config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.reader = new Reader(this);
|
this.reader = new Reader(this);
|
||||||
this.heartbeater = new Heartbeater(this);
|
this.heartbeater = new Heartbeater(this);
|
||||||
@@ -216,27 +216,26 @@ public class OpenSSHKnownHosts implements HostKeyVerifier {
|
|||||||
*
|
*
|
||||||
* @return {@code true} on successful verification or {@code false} on failure
|
* @return {@code true} on successful verification or {@code false} on failure
|
||||||
*/
|
*/
|
||||||
public boolean verify(String hostname, int port, PublicKey key) {
|
public boolean verify(final String hostname, final int port, final PublicKey key) {
|
||||||
KeyType type = KeyType.fromKey(key);
|
KeyType type = KeyType.fromKey(key);
|
||||||
if (type == KeyType.UNKNOWN)
|
if (type == KeyType.UNKNOWN)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (port != 22)
|
final String adjustedHostname = (port != 22) ? "[" + hostname + "]:" + port : hostname;
|
||||||
hostname = "[" + hostname + "]:" + port;
|
|
||||||
|
|
||||||
for (Entry e : entries)
|
for (Entry e : entries)
|
||||||
try {
|
try {
|
||||||
if (e.getType() == type && e.appliesTo(hostname))
|
if (e.getType() == type && e.appliesTo(adjustedHostname))
|
||||||
if (key.equals(e.getKey()))
|
if (key.equals(e.getKey()))
|
||||||
return true;
|
return true;
|
||||||
else {
|
else {
|
||||||
return hostKeyChangedAction(e, hostname, key);
|
return hostKeyChangedAction(e, adjustedHostname, key);
|
||||||
}
|
}
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
log.error("Error with {}: {}", e, ioe);
|
log.error("Error with {}: {}", e, ioe);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return hostKeyUnverifiableAction(hostname, key);
|
return hostKeyUnverifiableAction(adjustedHostname, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean hostKeyUnverifiableAction(String hostname, PublicKey key) {
|
protected boolean hostKeyUnverifiableAction(String hostname, PublicKey key) {
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ import java.util.Set;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/** {@link UserAuth} implementation. */
|
/** {@link UserAuth} implementation. */
|
||||||
public class UserAuthProtocol extends AbstractService implements UserAuth, AuthParams {
|
public class UserAuthImpl extends AbstractService implements UserAuth, AuthParams {
|
||||||
|
|
||||||
private final Set<String> allowed = new HashSet<String>();
|
private final Set<String> allowed = new HashSet<String>();
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ public class UserAuthProtocol extends AbstractService implements UserAuth, AuthP
|
|||||||
private volatile String banner;
|
private volatile String banner;
|
||||||
private volatile boolean partialSuccess;
|
private volatile boolean partialSuccess;
|
||||||
|
|
||||||
public UserAuthProtocol(Transport trans) {
|
public UserAuthImpl(Transport trans) {
|
||||||
super("ssh-userauth", trans);
|
super("ssh-userauth", trans);
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user