SocketServerpublic final class SocketServer extends Object implements Runnablethis class is used to receive incoming connections for the telnet UI and
then authenticate and create a console session for the connection |
Fields Summary |
---|
private final ServerSocket | serverSocket | private final Set | allowedHosts | private final int | maxLoginAttempts | private final org.gudy.azureus2.ui.console.multiuser.UserManager | userManager | private final UI | ui |
Constructors Summary |
---|
public SocketServer(UI ui, int port, Set allowedHosts, org.gudy.azureus2.ui.console.multiuser.UserManager userManager, int maxLoginAttempts)
this.ui = ui;
this.allowedHosts = allowedHosts;
this.userManager = userManager;
serverSocket = new ServerSocket(port);
this.maxLoginAttempts = maxLoginAttempts;
|
Methods Summary |
---|
private boolean | checkHost(java.lang.String hostName)compare the specified host (might be a hostname or an IP - dont really care)
and see if it is a match against one of the allowed hosts
if( hostName == null )
return false;
hostName = hostName.toLowerCase();
// System.out.println("checking host: " + hostName);
for (Iterator iter = allowedHosts.iterator(); iter.hasNext();) {
String allowedHost = (String) iter.next();
if( hostName.equals(allowedHost) )
return true;
}
return false;
| private boolean | isAllowed(java.net.InetSocketAddress addr)check that the specified host/ip is allowed
InetAddress address = addr.getAddress();
if( checkHost(address.getHostAddress()) )
return true;
else if( checkHost(address.getHostName()))
return true;
else
return false;
| private org.gudy.azureus2.ui.console.UserProfile | login(java.io.InputStream in, java.io.OutputStream out)if usermanager is null (ie: multi user is not enabled), returns the default user profile
otherwise, requests username and password and authenticates user before returning
the user profile for this user
if( userManager == null )
return UserProfile.DEFAULT_USER_PROFILE;
PrintStream ps = new PrintStream(out);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
ps.print("Username: ");
String username = br.readLine();
ps.print("Password: ");
String password = br.readLine();
UserProfile userProfile = userManager.authenticate(username, password);
if( userProfile != null )
{
ps.println("Login successful");
return userProfile;
}
ps.println("Login failed");
return null;
| public void | run()start up the server socket and when a new connection is received, check that
the source address is in our permitted list and if so, start a new console input
on that socket.
int threadNum = 1;
System.out.println("Telnet server started. Listening on port: " + serverSocket.getLocalPort());
while( true ) {
try {
Socket socket = serverSocket.accept();
InetSocketAddress addr = (InetSocketAddress) socket.getRemoteSocketAddress();
if( addr.isUnresolved() || ! isAllowed(addr) ) {
System.out.println("TelnetUI: rejecting connection from: " + addr + " as address is not allowed");
socket.close();
}
else {
System.out.println("TelnetUI: accepting connection from: " + addr);
int loginAttempts = 0;
while( true ) {
// TODO: might want to put this in another thread so the port doesnt block while the user logs in
//System.out.println("TelnetUI: starting login" );
UserProfile profile = login( socket.getInputStream(), socket.getOutputStream() );
//System.out.println("TelnetUI: login profile obtained" );
if( profile != null ) {
//System.out.println("TelnetUI: creating console input" );
ui.createNewConsoleInput("Telnet Console " + threadNum++, socket.getInputStream(), new PrintStream(socket.getOutputStream()), profile);
break;
}
//System.out.println("TelnetUI: failed to obtain login profile" );
loginAttempts++;
if( loginAttempts >= maxLoginAttempts ) {
System.out.println("TelnetUI: rejecting connection from: " + addr + " as number of failed connections > max login attempts (" + maxLoginAttempts + ")");
socket.close();
break;
}
}
}
}
catch (Throwable t) {
t.printStackTrace();
break;
}
}
|
|