⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authenticationmanager.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on May 30, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.GTADS.usermanager;

import java.net.*;
import java.util.Hashtable;
import java.io.*;

import org.GTADS.server.*;
import org.GTADS.messenger.*;
import org.GTADS.protocol.*;
import org.GTADS.client.ClientConnectionHandler;
import org.GTADS.debug.*;
import org.GTADS.helper.*;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class AuthenticationManager {
	public static String AUTHENTICATION_FLAG = "AUTHENTICATE";
	public static int LOGIN_FAILED = 0;
	public static int LOGIN_SUCCESS = 1;
	public static int LOGIN_NEW = 2;
	public static int MAXED_OUT = 3;
	public static int NEW_USER_SUCCESS = 4;
	public static int NEW_USER_DISABLED = 5;
	public static int NEW_USER_FAIL = 6;
	public static int USER_BANNED = 7;
	
	public static int requestAuthentication(Socket clientLogin, DSChatServer thisServer) throws IOException, EOFException{
		try {
		DataOutputStream dout = (DataOutputStream)ServerCacheHandler.socketStreamCache.get(clientLogin);
		DataInputStream din = new DataInputStream(clientLogin.getInputStream());
		MessageAdapter.sendData(null, clientLogin, 
				new MetaData(MetaData.FROM_SERVER, MetaData.GENERIC,MetaData.LOGIN_SESSION), AUTHENTICATION_FLAG);
		
		String incomingAuth = din.readUTF();
		return validateClientAuth(incomingAuth, clientLogin, thisServer);
		} catch (Exception E) {
			return LOGIN_FAILED;
		}
	}
	
	public static void readInPasswords(){
		try {
			FileReader authFile = new FileReader(ServerConfig.getInstance().getPasswdFileName());
			BufferedReader inputBuffer = new BufferedReader(authFile);
			boolean endOfFile = false;
			String acctUserPass[] = new String[2];
			String acctLine = new String();
			Hashtable temporaryAccountHash = new Hashtable();
			
			
			while (!endOfFile) {
				acctLine = inputBuffer.readLine();
				if (acctLine == null)
					endOfFile = true;
				else {
					acctUserPass = acctLine.split(",");
					if (acctUserPass.length > 1){
						temporaryAccountHash.put(acctUserPass[0], acctUserPass[1]);
						ServerCacheHandler.userPasswordCache.put(acctUserPass[0], acctUserPass[1]);
					}
				}
			}
			
			ServerCacheHandler.userPasswordCache = (Hashtable)temporaryAccountHash.clone();
			
		} catch (FileNotFoundException F) {
			ServerLogger.sendConsoleOutput("Accounts file " + ServerConfig.getInstance().getPasswdFileName() +
					" Not Found...", AuthenticationManager.class);
			ServerLogger.sendConsoleOutput("!!!Halt server immediately and address problem!!!", AuthenticationManager.class);
		} catch (IOException e) {
			ServerLogger.sendConsoleOutput("!!!Error reading accounts file!!!", AuthenticationManager.class);
		}
	}
	
	public static int validateClientAuth(String authLine, Socket clientLogin, DSChatServer thisServer) {
		String messageLine[] = new String[4];
		String userName;
		String passWord;
		int amountOfFields;
		
		if (BanManagement.getInstance().checkIP(clientLogin.getInetAddress().getHostAddress())){
			return USER_BANNED;
		}
		
		if (authLine != null){
			authLine = Helper.clipCarriageReturn(authLine);
			messageLine = authLine.split(",");
			amountOfFields = messageLine.length;
			
			if (amountOfFields == 4) {
				userName = messageLine[0];
				passWord = messageLine[3];
			} else
				return LOGIN_FAILED;
			
			if (BanManagement.getInstance().checkUser(userName)){
				return USER_BANNED;
			}
			
			if (userName != null && passWord != null){
				if (userName.equalsIgnoreCase(MetaData.NEW_USER_ACCT)){
					try {
						if (ServerConfig.getInstance().clientSignupEnabled()){
							return createNewUserAccount(clientLogin);
						}
						else {
							return NEW_USER_DISABLED;
						}
					} catch (IOException ioe){
						return NEW_USER_FAIL;
					}
				}
				if (((String)ServerCacheHandler.userPasswordCache.get(userName)) != null && 
						((String)ServerCacheHandler.userPasswordCache.get(userName)).equals(passWord)){
					// Check if login goes over the max allowed clients
					// Also check if the user will bump a dup login off
					// or if user is super user.
					if (ServerConfig.getInstance().getMaxClients() != 0) {
						if (ServerCacheHandler.userSocketCache.size() >= ServerConfig.getInstance().getMaxClients()){
							if (!userName.equalsIgnoreCase(ServerConfig.getInstance().getAdminAccount()) &&
									ServerCacheHandler.userSocketCache.get(userName) == null){
								return MAXED_OUT;
							}
						}
					}
					// If cache exists this means the user is already logged in
					// or the socket is still alive
					// We should check if it exists and kill it if it exists.
					
					if (ServerCacheHandler.userSocketCache.get(userName) != null){
						// TODO: Add a kill connection function outside of DSChatServer
						DSChatServer.getInstance().killConnection((Socket)ServerCacheHandler.userSocketCache.get(userName)
								, thisServer);
						DSChatServer.getInstance().addConnected();
					}
					ServerCacheHandler.setUserSocketCache(userName,clientLogin);
					return LOGIN_SUCCESS;
				}
			}
		}
		return LOGIN_FAILED;
	}
	
	public static int loginClient(Socket clientSocket, String username, String password) throws IOException{
		try {
		DataInputStream din = new DataInputStream(clientSocket.getInputStream());
		String incomingString = new String();
		String authenticationString = new String("SERVER,0,LOGIN,AUTHENTICATE" + ProtocolHandler.carriageReturn);
		String newUserOkString = new String("SERVER,0," + MetaData.NEW_USER_ACCT + ",PROCEED" + ProtocolHandler.carriageReturn);
		String loginStatus = new String("SERVER,0,LOGIN,");
		String newUserDisabledString = new String ("SERVER,0," + MetaData.NEW_USER_ACCT + ",DISABLED");
		
			incomingString = din.readUTF();
			if (incomingString != null && incomingString.equalsIgnoreCase(authenticationString)
					&& !ClientConnectionHandler.isNewAcct){
				MessageAdapter.sendData(null, clientSocket, new MetaData(username,MetaData.GENERIC, MetaData.LOGIN_SESSION), password);
				
				Thread.sleep(0);
				incomingString = din.readUTF();
				if (incomingString != null && incomingString.equalsIgnoreCase(loginStatus + ProtocolHandler.LOGIN_SUCCESS + ProtocolHandler.carriageReturn))
					return LOGIN_SUCCESS;
				else if (incomingString != null && incomingString.equalsIgnoreCase(loginStatus + ProtocolHandler.MAXED_USERS + ProtocolHandler.carriageReturn))
					return MAXED_OUT;
				else if (incomingString != null && incomingString.equalsIgnoreCase(loginStatus + MetaData.USER_BANNED + ProtocolHandler.carriageReturn)){
					return USER_BANNED;
				}
			}
			else if (incomingString != null && incomingString.equalsIgnoreCase(authenticationString)){
				ClientConnectionHandler.isNewAcct = false;
				MetaData newUserHeader = new MetaData(MetaData.NEW_USER_ACCT,MetaData.GENERIC, MetaData.LOGIN_SESSION);
				MessageAdapter.sendData(null, clientSocket, newUserHeader, password);
				incomingString = din.readUTF();
				
				if (incomingString != null && incomingString.equalsIgnoreCase(newUserOkString)){
					newUserHeader = new MetaData(username,MetaData.GENERIC, MetaData.LOGIN_SESSION);
					MessageAdapter.sendData(null, clientSocket, newUserHeader, password);
					
					incomingString = din.readUTF();
					if (incomingString != null && incomingString.equalsIgnoreCase(loginStatus + ProtocolHandler.LOGIN_SUCCESS + ProtocolHandler.carriageReturn)){
						return NEW_USER_SUCCESS;
					}
				}
				else if (incomingString != null && incomingString.equalsIgnoreCase(newUserDisabledString + ProtocolHandler.carriageReturn)){
					return NEW_USER_DISABLED;
				}
			}
			return LOGIN_FAILED;
		} catch (InterruptedException o) {
			return LOGIN_FAILED;
		}
	}
	
	private static int createNewUserAccount(Socket clientLogin) throws IOException {
		int success = NEW_USER_FAIL;
		DataInputStream din = new DataInputStream(clientLogin.getInputStream());
		MetaData newUserHeader = new MetaData(MetaData.FROM_SERVER, MetaData.GENERIC,
				MetaData.NEW_USER_ACCT);
		MessageAdapter.sendData(null, clientLogin, newUserHeader, "PROCEED");
		// Gives client the go ahead to write new user
		
		String incomingUserLine = din.readUTF();
		if (incomingUserLine != null)
			success = writeUserAccount(incomingUserLine);
		
		return success;
	}
	
	private static int writeUserAccount(String incomingUserLine){
		int success = NEW_USER_FAIL;
		String messageLine[] = new String[4];
		String userName;
		String password;
		
		incomingUserLine = Helper.clipCarriageReturn(incomingUserLine);
		
		messageLine = incomingUserLine.split(",");
		if (messageLine.length == 4){
			userName = messageLine[0];
			password = messageLine[3];
			
			if (!ServerCacheHandler.userPasswordCache.containsKey(userName)){
				// Validate Username
				if (validateCleanText(userName) && validateCleanText(password)){
					if (writeUserPassToFile(userName, password)){
						success = NEW_USER_SUCCESS;
					}
				}
			}
		}
		return success;
	}
	
	public static boolean isAdminAccount(String username){
		boolean isAdmin = false;
		if (username.equals(ServerConfig.getInstance().getAdminAccount())){
			isAdmin = true;
		}
		return isAdmin;
	}
	
	private static boolean writeUserPassToFile(String user, String pass){
		boolean success = false;
		
		String fileName = ServerConfig.getInstance().getPasswdFileName();
		try {
			File accountsFile = new File(fileName);
			
			if (accountsFile.exists()){
				FileWriter accountWriter = new FileWriter(accountsFile, true);
				BufferedWriter buffer = new BufferedWriter(accountWriter);
				
				buffer.newLine();
				buffer.write(user + "," + pass);
				//buffer.newLine();
				buffer.close();
				ServerCacheHandler.userPasswordCache.put(user, pass);
				success = true;
			}
			return success;
		} catch (IOException ioe){
			return success;
		}
	}
	
	private static boolean validateCleanText(String text){
		for (int i = 1; i < text.length(); i++){
			if (!(text.charAt(i) >= 'a' && text.charAt(i) <= 'z' ||
					text.charAt(i) >= 'A' && text.charAt(i) <= 'Z' ||
					text.charAt(i) >= '0' && text.charAt(i) <= '9' ||
					text.charAt(i) == '_')){
						return false;
					}
		}
		return true;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -