[Master] Use RakNet ban system instead homebrew

pull/276/head
Koncord 7 years ago
parent 841a4f90c1
commit 846f83e3e4

@ -57,6 +57,7 @@ MasterServer::MasterServer(const std::string &luaScript)
peer = RakPeerInterface::GetInstance(); peer = RakPeerInterface::GetInstance();
sockdescr = SocketDescriptor(port.as<unsigned short>(), nullptr); sockdescr = SocketDescriptor(port.as<unsigned short>(), nullptr);
peer->Startup(maxConnections.as<unsigned short>(), &sockdescr, 1, 1000); peer->Startup(maxConnections.as<unsigned short>(), &sockdescr, 1, 1000);
peer->SetLimitIPConnectionFrequency(true);
peer->SetMaximumIncomingConnections(maxConnections.as<unsigned short>()); peer->SetMaximumIncomingConnections(maxConnections.as<unsigned short>());
peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW)); peer->SetIncomingPassword(TES3MP_MASTERSERVER_PASSW, (int) strlen(TES3MP_MASTERSERVER_PASSW));
@ -86,6 +87,13 @@ void MasterServer::Thread()
PacketMasterAnnounce pma(peer); PacketMasterAnnounce pma(peer);
pma.SetSendStream(&send); 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<string>() << endl;
});
while (run) while (run)
{ {
Packet *packet = peer->Receive(); Packet *packet = peer->Receive();
@ -147,7 +155,7 @@ void MasterServer::Thread()
SystemAddress addr; SystemAddress addr;
data.Read(addr); // update 1 server data.Read(addr); // update 1 server
ServerIter it = servers.find(addr); auto it = servers.find(addr);
if (it != servers.end()) if (it != servers.end())
{ {
pair<SystemAddress, QueryData> pairPtr(it->first, static_cast<QueryData>(it->second)); pair<SystemAddress, QueryData> pairPtr(it->first, static_cast<QueryData>(it->second));
@ -161,7 +169,7 @@ void MasterServer::Thread()
} }
case ID_MASTER_ANNOUNCE: case ID_MASTER_ANNOUNCE:
{ {
ServerIter iter = servers.find(packet->systemAddress); auto iter = servers.find(packet->systemAddress);
pma.SetReadStream(&data); pma.SetReadStream(&data);
SServer server; SServer server;
@ -181,11 +189,8 @@ void MasterServer::Thread()
lock_guard<mutex> lock(banMutex); lock_guard<mutex> lock(banMutex);
if (find(banned.begin(), banned.end(), addr) != banned.end()) // check if address is banned if (peer->IsBanned(addr)) // check if address is banned
{
peer->AddToBanList(addr);
return false; return false;
}
luaStuff([&ret, &packet, &sserver, &addr](sol::state &state) { luaStuff([&ret, &packet, &sserver, &addr](sol::state &state) {
sol::protected_function func = state["OnServerAnnounce"]; sol::protected_function func = state["OnServerAnnounce"];
@ -220,7 +225,7 @@ void MasterServer::Thread()
{ {
cout << "Update rejected"; cout << "Update rejected";
servers.erase(iter); servers.erase(iter);
pendingACKs[packet->guid] = steady_clock::now(); pendingACKs.erase(packet->guid);
} }
} }
else else
@ -258,7 +263,8 @@ void MasterServer::Thread()
peer->CloseConnection(packet->systemAddress, true); peer->CloseConnection(packet->systemAddress, true);
break; break;
default: 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); peer->CloseConnection(packet->systemAddress, true);
} }
} }
@ -317,17 +323,11 @@ void MasterServer::luaStuff(std::function<void(sol::state &)> f)
void MasterServer::ban(const std::string &addr) void MasterServer::ban(const std::string &addr)
{ {
lock_guard<mutex> lock(banMutex); lock_guard<mutex> lock(banMutex);
banned.push_back(addr); peer->AddToBanList(addr.c_str());
} }
void MasterServer::unban(const std::string &addr) void MasterServer::unban(const std::string &addr)
{ {
lock_guard<mutex> lock(banMutex); lock_guard<mutex> lock(banMutex);
auto it = find(banned.begin(), banned.end(), addr); peer->RemoveFromBanList(addr.c_str());
if (it != banned.end())
{
banned.erase(it);
if(peer)
peer->RemoveFromBanList(addr.c_str());
}
} }

@ -50,7 +50,6 @@ private:
sol::state state; sol::state state;
std::mutex luaMutex; std::mutex luaMutex;
std::mutex banMutex; std::mutex banMutex;
std::vector<std::string> banned; // does not save on restart
}; };

@ -156,8 +156,6 @@ function OnCommand(command, v, address)
return false return false
end end
loadBans() -- load on start
-------------------------------------[callbacks]------------------------------------- -------------------------------------[callbacks]-------------------------------------
function OnAdminRequest(address, request) -- callback function OnAdminRequest(address, request) -- callback
@ -194,6 +192,10 @@ function OnServerAnnounce(address, sserver) -- callback
return true return true
end end
function OnInit() -- called when master server thread was started
loadBans()
end
function OnExit() -- callback function OnExit() -- callback
saveBans() saveBans()
end end

Loading…
Cancel
Save