Skip to content

Commit

Permalink
introduce RegisterCommandProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
hoijui committed Jan 18, 2011
1 parent 3f1ea04 commit c3d30e4
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 77 deletions.
77 changes: 0 additions & 77 deletions src/main/java/com/springrts/tasserver/TASServer.java
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
@@ -0,0 +1,130 @@
/*
Copyright (c) 2010 Robin Vobruba <robin.vobruba@derisk.ch>
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 <http://www.gnu.org/licenses/>.
*/

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<String> 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;
}
}

0 comments on commit c3d30e4

Please sign in to comment.