public void init() {
try {
ShellFactory factory = context.getPlugin(ShellFactory.class);
//
SshServer server = SshServer.setUpDefaultServer();
server.setPort(port);
if (this.idleTimeout > 0) {
server.getProperties().put(ServerFactoryManager.IDLE_TIMEOUT, String.valueOf(this.idleTimeout));
}
if (this.authTimeout > 0) {
server.getProperties().put(ServerFactoryManager.AUTH_TIMEOUT, String.valueOf(this.authTimeout));
}
server.setShellFactory(new CRaSHCommandFactory(factory, encoding));
server.setCommandFactory(new SCPCommandFactory(context));
server.setKeyPairProvider(keyPairProvider);
//
ArrayList<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>(0);
for (SubsystemFactoryPlugin plugin : context.getPlugins(SubsystemFactoryPlugin.class)) {
namedFactoryList.add(plugin.getFactory());
}
server.setSubsystemFactories(namedFactoryList);
//
for (AuthenticationPlugin authenticationPlugin : authenticationPlugins) {
if (server.getPasswordAuthenticator() == null && authenticationPlugin.getCredentialType().equals(String.class)) {
server.setPasswordAuthenticator(new PasswordAuthenticator() {
public boolean authenticate(String _username, String _password, ServerSession session) {
if (genericAuthenticate(String.class, _username, _password)) {
// We store username and password in session for later reuse
session.setAttribute(USERNAME, _username);
session.setAttribute(PASSWORD, _password);
return true;
} else {
return false;
}
}
});
}
if (server.getPublickeyAuthenticator() == null && authenticationPlugin.getCredentialType().equals(PublicKey.class)) {
server.setPublickeyAuthenticator(new PublickeyAuthenticator() {
public boolean authenticate(String username, PublicKey key, ServerSession session) {
return genericAuthenticate(PublicKey.class, username, key);
}
});
}
}
//
log.log(Level.INFO, "About to start CRaSSHD");
server.start();
localPort = server.getPort();
log.log(Level.INFO, "CRaSSHD started on port " + localPort);
//
this.server = server;
}