Telnet Server library User Guide

Quick Start Guide
VERY quick start

Very little is required to get this TelnetServer up and running in the simplest mode.

  1. Include libtsj.jar in your project/classpath
  2. Add the following code to your main():
    
    TelnetServer telnet = new TelnetServer(port);
    telnet.start();
    					
Done!

This will launch a telnet server on a specified port. It will display a default prompt to the user - "telnet", and will allow a default number of concurrent connections - 100.

Overloaded constructors are available that will allow setting prompt and maximum number of concurrent connections.

A deprecated static singleton interface is available, but it's use is discouraged.

Now let's spend a little more time setting up the server, so that it would actually be useful.

Adding a command

Adding a command is done by simply adding @TelnetCommand annotation to the method you wish to make into a telnet command. The only requirements are that the method is static and the argument list should contains only one of the accepted classes:

Here's some sample code:


public class SomeModule {
	@TelnetCommand(command="cmd", usage="cmd", help="this command does something useful", admin=false)
	public static void someCommand(TelnetClient cli) {
		cli.getOut().write("Some useful info\n");
		cli.getOut().flush();
	}
}
Security
Administrative commands

Currently TelnetServer library has two levels of authentication: administrative commands and login authentication. Administrative commands are commands that are not allowed to be executed by a regular users. In order to perform such command, user must first authenticate using admin command.

Admin commands are labeled at such by setting admin flag of the TelnetCommand annotation to true. The flag is set to false by default.

For example:


@TelnetCommand(command="delete", usage="delete <id>", help="Delete an item by id", admin=true)
public static void delete(TelnetClient cli, String id) {
	if (id == null) {
		cli.getOut().write("Missing required argument <id>\n");
		cli.getOut().flush();
	}
	SomeClass.instance().deleteById(id);
}

If the user has not already authenticated, they will receive an error message saying that the command is for privileged users only.

Server Authentication

In addition to authenticated command TelnetServer library provides a way to lock down the server itself, by requiring users to authenticate before letting them in.

This is done by setting an authenticator for the TelnetServer. You can either use one of the provided authenticators or create your own.

Simple password authentication can be done using SimpleAuthenticator:


TelnetServer server = new TelnetServer(port, prompt);	
server.setAuthenticator(new SimpleAuthenticator("f00b4r"));
server.start();

This will present a user who attempts to log into the server with a prompt asking for a password. In case if they do not enter the specified password the connection will be terminated.