Search in sources :

Example 1 with UserProfile

use of com.biglybt.ui.console.UserProfile in project BiglyBT by BiglySoftware.

the class SocketServer method login.

/**
 * 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
 * @param in input stream to read from
 * @param out stream to write messages to
 * @return username if login was successful, null otherwise
 * @throws IOException
 */
private UserProfile login(InputStream in, OutputStream out) throws IOException {
    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;
}
Also used : PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) UserProfile(com.biglybt.ui.console.UserProfile) BufferedReader(java.io.BufferedReader)

Example 2 with UserProfile

use of com.biglybt.ui.console.UserProfile in project BiglyBT by BiglySoftware.

the class SocketServer method 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.
 */
@Override
public void run() {
    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());
                    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;
        }
    }
}
Also used : PrintStream(java.io.PrintStream) UserProfile(com.biglybt.ui.console.UserProfile) InetSocketAddress(java.net.InetSocketAddress) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 3 with UserProfile

use of com.biglybt.ui.console.UserProfile in project BiglyBT by BiglySoftware.

the class UserManagerXMLPersist method doLoad.

@Override
public void doLoad(InputStream in, Map usersMap) {
    XMLDecoder decoder = new XMLDecoder(in);
    UserManagerConfig managerConfig = (UserManagerConfig) decoder.readObject();
    for (Iterator iter = managerConfig.getUsers().iterator(); iter.hasNext(); ) {
        UserProfile user = (UserProfile) iter.next();
        usersMap.put(user.getUsername().toLowerCase(), user);
    }
    System.out.println("UserManager: registered " + usersMap.size() + " users");
    decoder.close();
}
Also used : UserProfile(com.biglybt.ui.console.UserProfile) XMLDecoder(java.beans.XMLDecoder) Iterator(java.util.Iterator) UserManagerConfig(com.biglybt.ui.console.multiuser.UserManager.UserManagerConfig)

Aggregations

UserProfile (com.biglybt.ui.console.UserProfile)3 PrintStream (java.io.PrintStream)2 UserManagerConfig (com.biglybt.ui.console.multiuser.UserManager.UserManagerConfig)1 XMLDecoder (java.beans.XMLDecoder)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 InetSocketAddress (java.net.InetSocketAddress)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 Iterator (java.util.Iterator)1