From c3d30e4424d445ac77e3498102035532be5335a7 Mon Sep 17 00:00:00 2001 From: hoijui Date: Mon, 3 Jan 2011 11:44:45 +0100 Subject: [PATCH] introduce RegisterCommandProcessor --- .../com/springrts/tasserver/TASServer.java | 77 ----------- .../tasserver/commands/CommandProcessors.java | 2 + .../impl/RegisterCommandProcessor.java | 130 ++++++++++++++++++ 3 files changed, 132 insertions(+), 77 deletions(-) create mode 100644 src/main/java/com/springrts/tasserver/commands/impl/RegisterCommandProcessor.java diff --git a/src/main/java/com/springrts/tasserver/TASServer.java b/src/main/java/com/springrts/tasserver/TASServer.java index dc48d77..b0487d2 100644 --- a/src/main/java/com/springrts/tasserver/TASServer.java +++ b/src/main/java/com/springrts/tasserver/TASServer.java @@ -577,83 +577,6 @@ public boolean tryToExecCommand(String command, Client client) { Misc.makeSentence(commands) + "\"", ex); return false; } - } else if (commands[0].equals("REGISTER")) { - if (commands.length != 3) { - client.sendLine("REGISTRATIONDENIED Bad command arguments"); - return false; - } - - if (!context.getAccountsService().isRegistrationEnabled()) { - client.sendLine("REGISTRATIONDENIED Sorry, account registration is currently disabled"); - return false; - } - - if (client.getAccount().getAccess() != Account.Access.NONE) { // only clients which aren't logged-in can register - client.sendLine("REGISTRATIONDENIED You are already logged-in, no need to register new account"); - return false; - } - - if (context.getServer().isLanMode()) { // no need to register account in LAN mode since it accepts any userName - client.sendLine("REGISTRATIONDENIED Can't register in LAN-mode. Login with any username and password to proceed"); - return false; - } - - // validate userName: - String valid = Account.isOldUsernameValid(commands[1]); - if (valid != null) { - client.sendLine(new StringBuilder("REGISTRATIONDENIED Invalid username (reason: ") - .append(valid).append(")").toString()); - return false; - } - - // validate password: - valid = Account.isPasswordValid(commands[2]); - if (valid != null) { - client.sendLine(new StringBuilder("REGISTRATIONDENIED Invalid password (reason: ") - .append(valid).append(")").toString()); - return false; - } - Account acc = context.getAccountsService().findAccountNoCase(commands[1]); - if (acc != null) { - client.sendLine("REGISTRATIONDENIED Account already exists"); - return false; - } - - // check for reserved names: - if (Account.RESERVED_NAMES.contains(commands[1])) { - client.sendLine("REGISTRATIONDENIED Invalid account name - you are trying to register a reserved account name"); - return false; - } - /*if (!whiteList.contains(client.getIp())) { - if (registrationTimes.containsKey(client.ip) - && (int)(registrationTimes.get(client.ip)) + 3600 > (System.currentTimeMillis()/1000)) { - client.sendLine("REGISTRATIONDENIED This ip has already registered an account recently"); - context.getClients().sendToAllAdministrators("SERVERMSG Client at " + client.ip + "'s registration of " + commands[1] + " was blocked due to register spam"); - return false; - } - registrationTimes.put(client.ip, (int)(System.currentTimeMillis()/1000));*/ - /*String proxyDNS = "dnsbl.dronebl.org"; //Bot checks this with the broadcast, no waiting for a response - String[] ipChunks = client.ip.split("\\."); - for (int i = 0; i < 4; i++) { - proxyDNS = ipChunks[i] + "." + proxyDNS; - } - try { - InetAddress.getByName(proxyDNS); - client.sendLine("REGISTRATIONDENIED Using a known proxy ip"); - context.getClients().sendToAllAdministrators("SERVERMSG Client at " + client.ip + "'s registration of " + commands[1] + " was blocked as it is a proxy ip"); - return false; - } catch (UnknownHostException e) { - } - }*/ - context.getClients().sendToAllAdministrators(new StringBuilder("SERVERMSG New registration of <") - .append(commands[1]).append("> at ") - .append(client.getIp()).toString()); - acc = new Account( - commands[1], - commands[2], client.getIp(), client.getCountry()); - context.getAccountsService().addAccount(acc); - context.getAccountsService().saveAccounts(false); // let's save new accounts info to disk - client.sendLine("REGISTRATIONACCEPTED"); } else if (commands[0].equals("UPTIME")) { if (client.getAccount().getAccess().compareTo(Account.Access.NORMAL) < 0) { return false; diff --git a/src/main/java/com/springrts/tasserver/commands/CommandProcessors.java b/src/main/java/com/springrts/tasserver/commands/CommandProcessors.java index 8fe0658..5e63721 100644 --- a/src/main/java/com/springrts/tasserver/commands/CommandProcessors.java +++ b/src/main/java/com/springrts/tasserver/commands/CommandProcessors.java @@ -23,6 +23,7 @@ import com.springrts.tasserver.commands.impl.CreateAccountCommandProcessor; import com.springrts.tasserver.commands.impl.OpenBattleCommandProcessor; import com.springrts.tasserver.commands.impl.PingCommandProcessor; +import com.springrts.tasserver.commands.impl.RegisterCommandProcessor; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.HashMap; @@ -92,6 +93,7 @@ public void init() { commandProcessorClasses.add(PingCommandProcessor.class); commandProcessorClasses.add(OpenBattleCommandProcessor.class); commandProcessorClasses.add(CreateAccountCommandProcessor.class); + commandProcessorClasses.add(RegisterCommandProcessor.class); try { load(commandProcessorClasses); diff --git a/src/main/java/com/springrts/tasserver/commands/impl/RegisterCommandProcessor.java b/src/main/java/com/springrts/tasserver/commands/impl/RegisterCommandProcessor.java new file mode 100644 index 0000000..af0437c --- /dev/null +++ b/src/main/java/com/springrts/tasserver/commands/impl/RegisterCommandProcessor.java @@ -0,0 +1,130 @@ +/* + Copyright (c) 2010 Robin Vobruba + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +package com.springrts.tasserver.commands.impl; + + +import com.springrts.tasserver.Account; +import com.springrts.tasserver.Client; +import com.springrts.tasserver.commands.AbstractCommandProcessor; +import com.springrts.tasserver.commands.CommandProcessingException; +import com.springrts.tasserver.commands.InvalidNumberOfArgumentsCommandProcessingException; +import com.springrts.tasserver.commands.SupportedCommand; +import java.util.List; + +/** + * @author hoijui + */ +@SupportedCommand("REGISTER") +public class RegisterCommandProcessor extends AbstractCommandProcessor { + + public RegisterCommandProcessor() { + super(2, 2, Account.Access.NONE); + } + + @Override + public boolean process(Client client, List args) + throws CommandProcessingException + { + boolean checksOk = false; + try { + checksOk = super.process(client, args); + } catch (InvalidNumberOfArgumentsCommandProcessingException ex) { + client.sendLine("REGISTRATIONDENIED Bad command arguments"); + throw ex; + } + if (!checksOk) { + return false; + } + + if (!getContext().getAccountsService().isRegistrationEnabled()) { + client.sendLine("REGISTRATIONDENIED Sorry, account registration is currently disabled"); + return false; + } + + if (client.getAccount().getAccess() != Account.Access.NONE) { // only clients which aren't logged-in can register + client.sendLine("REGISTRATIONDENIED You are already logged-in, no need to register new account"); + return false; + } + + if (getContext().getServer().isLanMode()) { // no need to register account in LAN mode since it accepts any userName + client.sendLine("REGISTRATIONDENIED Can't register in LAN-mode. Login with any username and password to proceed"); + return false; + } + + String username = args.get(0); + String password = args.get(1); + + // validate userName: + String valid = Account.isOldUsernameValid(username); + if (valid != null) { + client.sendLine(new StringBuilder("REGISTRATIONDENIED Invalid username (reason: ") + .append(valid).append(")").toString()); + return false; + } + + // validate password: + valid = Account.isPasswordValid(password); + if (valid != null) { + client.sendLine(new StringBuilder("REGISTRATIONDENIED Invalid password (reason: ") + .append(valid).append(")").toString()); + return false; + } + Account acc = getContext().getAccountsService().findAccountNoCase(username); + if (acc != null) { + client.sendLine("REGISTRATIONDENIED Account already exists"); + return false; + } + + // check for reserved names: + if (Account.RESERVED_NAMES.contains(username)) { + client.sendLine("REGISTRATIONDENIED Invalid account name - you are trying to register a reserved account name"); + return false; + } + /*if (!getContext().whiteList.contains(client.getIp())) { + if (registrationTimes.containsKey(client.ip) + && (int)(registrationTimes.get(client.ip)) + 3600 > (System.currentTimeMillis()/1000)) { + client.sendLine("REGISTRATIONDENIED This ip has already registered an account recently"); + context.getClients().sendToAllAdministrators("SERVERMSG Client at " + client.ip + "'s registration of " + username + " was blocked due to register spam"); + return false; + } + registrationTimes.put(client.ip, (int)(System.currentTimeMillis()/1000));*/ + /*String proxyDNS = "dnsbl.dronebl.org"; //Bot checks this with the broadcast, no waiting for a response + String[] ipChunks = client.ip.split("\\."); + for (int i = 0; i < 4; i++) { + proxyDNS = ipChunks[i] + "." + proxyDNS; + } + try { + InetAddress.getByName(proxyDNS); + client.sendLine("REGISTRATIONDENIED Using a known proxy ip"); + context.getClients().sendToAllAdministrators("SERVERMSG Client at " + client.ip + "'s registration of " + username + " was blocked as it is a proxy ip"); + return false; + } catch (UnknownHostException e) { + } + }*/ + getContext().getClients().sendToAllAdministrators(new StringBuilder("SERVERMSG New registration of <") + .append(username).append("> at ") + .append(client.getIp()).toString()); + acc = new Account( + username, + password, client.getIp(), client.getCountry()); + getContext().getAccountsService().addAccount(acc); + getContext().getAccountsService().saveAccounts(false); // let's save new accounts info to disk + client.sendLine("REGISTRATIONACCEPTED"); + return true; + } +}