diff --git a/apps/master/MasterServer.cpp b/apps/master/MasterServer.cpp index ff8047c82..21b4742ec 100644 --- a/apps/master/MasterServer.cpp +++ b/apps/master/MasterServer.cpp @@ -57,6 +57,7 @@ MasterServer::MasterServer(const std::string &luaScript) peer = RakPeerInterface::GetInstance(); sockdescr = SocketDescriptor(port.as(), nullptr); peer->Startup(maxConnections.as(), &sockdescr, 1, 1000); + peer->SetLimitIPConnectionFrequency(true); peer->SetMaximumIncomingConnections(maxConnections.as()); peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW)); @@ -86,6 +87,13 @@ void MasterServer::Thread() PacketMasterAnnounce pma(peer); pma.SetSendStream(&send); + luaStuff([](sol::state &state) { + sol::protected_function func = state["OnInit"]; + sol::protected_function_result result = func.call(); + if (!result.valid()) + cerr << "Error: " << result.get() << endl; + }); + while (run) { Packet *packet = peer->Receive(); @@ -147,7 +155,7 @@ void MasterServer::Thread() SystemAddress addr; data.Read(addr); // update 1 server - ServerIter it = servers.find(addr); + auto it = servers.find(addr); if (it != servers.end()) { pair pairPtr(it->first, static_cast(it->second)); @@ -161,7 +169,7 @@ void MasterServer::Thread() } case ID_MASTER_ANNOUNCE: { - ServerIter iter = servers.find(packet->systemAddress); + auto iter = servers.find(packet->systemAddress); pma.SetReadStream(&data); SServer server; @@ -181,11 +189,8 @@ void MasterServer::Thread() lock_guard lock(banMutex); - if (find(banned.begin(), banned.end(), addr) != banned.end()) // check if address is banned - { - peer->AddToBanList(addr); + if (peer->IsBanned(addr)) // check if address is banned return false; - } luaStuff([&ret, &packet, &sserver, &addr](sol::state &state) { sol::protected_function func = state["OnServerAnnounce"]; @@ -220,7 +225,7 @@ void MasterServer::Thread() { cout << "Update rejected"; servers.erase(iter); - pendingACKs[packet->guid] = steady_clock::now(); + pendingACKs.erase(packet->guid); } } else @@ -258,7 +263,8 @@ void MasterServer::Thread() peer->CloseConnection(packet->systemAddress, true); break; default: - cout << "Wrong packet. id " << (unsigned) packet->data[0] << " packet length " << packet->length << " from " << packet->systemAddress.ToString() << endl; + cout << "Wrong packet. id " << (unsigned) packet->data[0] << " packet length " + << packet->length << " from " << packet->systemAddress.ToString() << endl; peer->CloseConnection(packet->systemAddress, true); } } @@ -317,17 +323,11 @@ void MasterServer::luaStuff(std::function f) void MasterServer::ban(const std::string &addr) { lock_guard lock(banMutex); - banned.push_back(addr); + peer->AddToBanList(addr.c_str()); } void MasterServer::unban(const std::string &addr) { lock_guard lock(banMutex); - auto it = find(banned.begin(), banned.end(), addr); - if (it != banned.end()) - { - banned.erase(it); - if(peer) - peer->RemoveFromBanList(addr.c_str()); - } + peer->RemoveFromBanList(addr.c_str()); } diff --git a/apps/master/MasterServer.hpp b/apps/master/MasterServer.hpp index 83ad09732..0d219e3cc 100644 --- a/apps/master/MasterServer.hpp +++ b/apps/master/MasterServer.hpp @@ -50,7 +50,6 @@ private: sol::state state; std::mutex luaMutex; std::mutex banMutex; - std::vector banned; // does not save on restart }; diff --git a/files/tes3mp/master/master.lua b/files/tes3mp/master/master.lua index f3cb05ec0..73ccd5d85 100644 --- a/files/tes3mp/master/master.lua +++ b/files/tes3mp/master/master.lua @@ -156,8 +156,6 @@ function OnCommand(command, v, address) return false end -loadBans() -- load on start - -------------------------------------[callbacks]------------------------------------- function OnAdminRequest(address, request) -- callback @@ -194,6 +192,10 @@ function OnServerAnnounce(address, sserver) -- callback return true end +function OnInit() -- called when master server thread was started + loadBans() +end + function OnExit() -- callback saveBans() end