Creating your Server

To build your server you need to extend the com.maverick.nio.Daemon class and implement its configure method. Within this method you create any number of SshContext instances which define the configuration for incoming connections. An SshContext is tied to a listening interface so therefore, if required, you can run different types of configuration within the same JVM. 

 

public class ExampleSSHD extends Daemon {

     static Logger log = LoggerFactory.getLogger(ExampleSSHD.clas

     public ExampleSSHD() {

     }

     protected void configure(DaemonContext context) throws SshException, IOException {

          if (log.isInfoEnabled())

               log.info("Configuring Example SSHD");

 

          SshContext sshContext = new SshContext(this);

 

          try {

               sshContext.loadOrGenerateHostKey(

                    new File("ssh_host_rsa_key"),

                    SshKeyPairGenerator.SSH2_RSA, 2048);

 

               sshContext.loadOrGenerateHostKey(

                    new File("ssh_host_dsa_key"),

                    SshKeyPairGenerator.SSH2_DSA, 1024);

 

               sshContext.loadOrGenerateHostKey(

                    new File("ssh_host_key_ecdsa_256"),

                    SshKeyPairGenerator.ECDSA, 256);

 

               sshContext.loadOrGenerateHostKey(

                    new File("ssh_host_key_ecdsa_384"),

                    SshKeyPairGenerator.ECDSA, 384);

 

               sshContext.loadOrGenerateHostKey(

                    new File("ssh_host_key_ecdsa_521"),

               SshKeyPairGenerator.ECDSA, 521);

          } catch (InvalidPassphraseException e) {

               throw new SshException(e);

          }

         

          sshContext.setSoftwareVersionComments("ExampleSSHD_1.2.3_Comments");

          

          context.addListeningInterface("0.0.0.0", 4000, sshContext);

          

          if (log.isInfoEnabled())

               log.info("Configuration complete.");

     }

 }

 

Within the configure method we first create a SshContext and the load a number of host keys into the configuration. Here we are loading a 2048 bit ssh-rsa key, 1024 bit ssh-dss key* and all the available sizes of a ecdsa key.

It is advisable to support as many host key types as possible so that you maintain the maximum compatibility with incoming clients.  

Next we set the software, version, comments field. This is used as part of the initial negotiation and identifies your server implementation. Use something clear and concise so that other vendors can determine the type of SSH server.

Finally in our example we add the SshContext to the Daemon using the addListeningInterface method. Here we tie the configuration to an IP/Port. You can use the same SshContext across multiple listening interfaces if required.

 

Starting the Server

 

To start the server create your main method ensuring you add your license code where indicated before calling the API.

public static void main(String[] args) throws Exception {

          org.apache.log4j.BasicConfigurator.configure();

          Thread.currentThread().setName("Main");

          // ADD YOUR LICENSE CODE HERE....

 

          // END OF LICENSE CODE

          ExampleSSHD sshd = new ExampleSSHD();

          sshd.startup();

}

 

The startup method starts the server. Before we can run this however we need to extend our configure method to support the various authentication, session and file systems you want to provide to incoming SSH clients.

Authentication Providers

Installing a File System for SFTP

Supporting SCP

 

* Depending on the installed JCE you may be restricted to creating only 1024 bit keys. If you require higher ssh-dss key sizes generate them externally and load them as above. We support reading and using larger key sizes, unfortunately the JCE restriction prevents us from generating them.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.