diff options
-rw-r--r-- | src/account-server/accountclient.cpp | 6 | ||||
-rw-r--r-- | src/account-server/accountclient.hpp | 12 | ||||
-rw-r--r-- | src/account-server/accounthandler.cpp | 16 | ||||
-rw-r--r-- | src/defines.h | 1 | ||||
-rw-r--r-- | src/net/netcomputer.cpp | 5 | ||||
-rw-r--r-- | src/net/netcomputer.hpp | 5 |
6 files changed, 45 insertions, 0 deletions
diff --git a/src/account-server/accountclient.cpp b/src/account-server/accountclient.cpp index 3b9f35e8..18eab583 100644 --- a/src/account-server/accountclient.cpp +++ b/src/account-server/accountclient.cpp @@ -26,6 +26,7 @@ AccountClient::AccountClient(ENetPeer *peer): status(CLIENT_LOGIN), mAccount(NULL) { + time(&lastLoginAttempt); } AccountClient::~AccountClient() @@ -44,3 +45,8 @@ void AccountClient::unsetAccount() delete mAccount; mAccount = NULL; } + +void AccountClient::updateLoginAttempt() +{ + time(&lastLoginAttempt); +} diff --git a/src/account-server/accountclient.hpp b/src/account-server/accountclient.hpp index b0bb0c3f..600ec2db 100644 --- a/src/account-server/accountclient.hpp +++ b/src/account-server/accountclient.hpp @@ -69,11 +69,23 @@ class AccountClient : public NetComputer Account *getAccount() const { return mAccount; } + /** + * Update lastLoginAttempt + */ + void updateLoginAttempt(); + + /** + * Returns the time of the last login attempt. + */ + int getLastLoginAttempt() const + { return lastLoginAttempt; } + int status; private: /** Account associated with connection */ Account *mAccount; + time_t lastLoginAttempt; }; #endif diff --git a/src/account-server/accounthandler.cpp b/src/account-server/accounthandler.cpp index 0b81787b..506d80f8 100644 --- a/src/account-server/accounthandler.cpp +++ b/src/account-server/accounthandler.cpp @@ -169,6 +169,22 @@ static void handleLoginMessage(AccountClient &computer, MessageIn &msg) return; } + // get the IP address + int address = computer.getIP(); + + // TODO: Check IP against blacklist + + time_t lastAttempt = computer.getLastLoginAttempt(); + if ((time(NULL) - lastAttempt) < 1) + { + reply.writeByte(LOGIN_INVALID_TIME); + computer.send(reply); + return; + } + + // updates the time last attempted to login + computer.updateLoginAttempt(); + std::string username = msg.readString(); std::string password = msg.readString(); diff --git a/src/defines.h b/src/defines.h index ea3aab57..b722e0d1 100644 --- a/src/defines.h +++ b/src/defines.h @@ -320,6 +320,7 @@ enum { // Login specific return values enum { LOGIN_INVALID_VERSION = 0x40, // the user is using an incompatible protocol + LOGIN_INVALID_TIME = 0x50, // the user tried logging in too fast LOGIN_BANNED // the user is currently banned }; diff --git a/src/net/netcomputer.cpp b/src/net/netcomputer.cpp index ff2bbb71..2ce30c02 100644 --- a/src/net/netcomputer.cpp +++ b/src/net/netcomputer.cpp @@ -103,3 +103,8 @@ operator <<(std::ostream &os, const NetComputer &comp) return os; } + +int NetComputer::getIP() +{ + return mPeer->address.host; +} diff --git a/src/net/netcomputer.hpp b/src/net/netcomputer.hpp index b85456a6..2c4d706a 100644 --- a/src/net/netcomputer.hpp +++ b/src/net/netcomputer.hpp @@ -81,6 +81,11 @@ class NetComputer send(const MessageOut &msg, bool reliable = true, unsigned int channel = 0); + /** + * Returns IP address of computer in 32bit int form + */ + int getIP(); + private: ENetPeer *mPeer; /**< Client peer */ |