[Node] Check if ports match to get rid of unforwarded nodes

This commit is contained in:
momo5502 2017-05-31 18:03:05 +02:00
parent dbc4ac170b
commit 7af0f7368c
5 changed files with 24 additions and 6 deletions

View File

@ -712,7 +712,7 @@ namespace Components
Network::OnStart([] () Network::OnStart([] ()
{ {
mg_connection* nc = mg_bind(&Download::Mgr, Utils::String::VA("%hu", (Dvar::Var("net_port").get<int>() & 0xFFFF)), Download::EventHandler); mg_connection* nc = mg_bind(&Download::Mgr, Utils::String::VA("%hu", Network::GetPort()), Download::EventHandler);
if (nc) if (nc)
{ {

View File

@ -98,7 +98,7 @@ namespace Components
bool Network::Address::isSelf() bool Network::Address::isSelf()
{ {
if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback if (Game::NET_IsLocalAddress(this->address)) return true; // Loopback
if (this->getPort() != (Dvar::Var("net_port").get<int>() & 0xFFFF)) return false; // Port not equal if (this->getPort() != Network::GetPort()) return false; // Port not equal
for (int i = 0; i < *Game::numIP; ++i) for (int i = 0; i < *Game::numIP; ++i)
{ {
@ -287,6 +287,11 @@ namespace Components
Network::StartupSignal(); Network::StartupSignal();
} }
unsigned short Network::GetPort()
{
return static_cast<unsigned short>(Dvar::Var(0x64A3004).get<unsigned int>());
}
__declspec(naked) void Network::NetworkStartStub() __declspec(naked) void Network::NetworkStartStub()
{ {
__asm __asm

View File

@ -58,6 +58,8 @@ namespace Components
Network(); Network();
~Network(); ~Network();
static unsigned short GetPort();
static void Handle(std::string packet, Utils::Slot<Callback> callback); static void Handle(std::string packet, Utils::Slot<Callback> callback);
static void OnStart(Utils::Slot<CallbackRaw> callback); static void OnStart(Utils::Slot<CallbackRaw> callback);

View File

@ -261,6 +261,7 @@ namespace Components
entry->challenge = Utils::Cryptography::Rand::GenerateChallenge(); entry->challenge = Utils::Cryptography::Rand::GenerateChallenge();
Proto::Node::Packet packet; Proto::Node::Packet packet;
packet.set_port(Network::GetPort());
packet.set_challenge(entry->challenge); packet.set_challenge(entry->challenge);
#if defined(DEBUG) && !defined(DISABLE_NODE_LOG) #if defined(DEBUG) && !defined(DISABLE_NODE_LOG)
@ -412,6 +413,7 @@ namespace Components
std::string challenge = Utils::Cryptography::Rand::GenerateChallenge(); std::string challenge = Utils::Cryptography::Rand::GenerateChallenge();
Proto::Node::Packet packet; Proto::Node::Packet packet;
packet.set_port(Network::GetPort());
packet.set_challenge(challenge); packet.set_challenge(challenge);
packet.set_signature(Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, challenge)); packet.set_signature(Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, challenge));
@ -428,6 +430,11 @@ namespace Components
{ {
if (Dvar::Var("sv_lanOnly").get<bool>()) return; if (Dvar::Var("sv_lanOnly").get<bool>()) return;
Proto::Node::Packet packet;
if (!packet.ParseFromString(data)) return;
if (packet.challenge().empty()) return;
if (packet.port() && packet.port() != address.getPort()) return;
// Create a new entry, if we don't already know it // Create a new entry, if we don't already know it
if (!Node::FindNode(address)) if (!Node::FindNode(address))
{ {
@ -442,10 +449,6 @@ namespace Components
Logger::Print("Received registration request from %s\n", address.getCString()); Logger::Print("Received registration request from %s\n", address.getCString());
#endif #endif
Proto::Node::Packet packet;
if (!packet.ParseFromString(data)) return;
if (packet.challenge().empty()) return;
std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, packet.challenge()); std::string signature = Utils::Cryptography::ECC::SignMessage(Node::SignatureKey, packet.challenge());
std::string challenge = Utils::Cryptography::Rand::GenerateChallenge(); std::string challenge = Utils::Cryptography::Rand::GenerateChallenge();
@ -464,6 +467,7 @@ namespace Components
packet.set_challenge(challenge); packet.set_challenge(challenge);
packet.set_signature(signature); packet.set_signature(signature);
packet.set_publickey(Node::SignatureKey.getPublicKey()); packet.set_publickey(Node::SignatureKey.getPublicKey());
packet.set_port(Network::GetPort());
entry->lastTime = Game::Sys_Milliseconds(); entry->lastTime = Game::Sys_Milliseconds();
entry->challenge = challenge; entry->challenge = challenge;
@ -489,6 +493,7 @@ namespace Components
if (packet.challenge().empty()) return; if (packet.challenge().empty()) return;
if (packet.publickey().empty()) return; if (packet.publickey().empty()) return;
if (packet.signature().empty()) return; if (packet.signature().empty()) return;
if (packet.port() && packet.port() != address.getPort()) return;
std::string challenge = packet.challenge(); std::string challenge = packet.challenge();
std::string publicKey = packet.publickey(); std::string publicKey = packet.publickey();
@ -549,6 +554,7 @@ namespace Components
if (!packet.ParseFromString(data)) return; if (!packet.ParseFromString(data)) return;
if (packet.signature().empty()) return; if (packet.signature().empty()) return;
if (packet.publickey().empty()) return; if (packet.publickey().empty()) return;
if (packet.port() && packet.port() != address.getPort()) return;
std::string publicKey = packet.publickey(); std::string publicKey = packet.publickey();
std::string signature = packet.signature(); std::string signature = packet.signature();
@ -627,6 +633,7 @@ namespace Components
if (!packet.ParseFromString(data)) return; if (!packet.ParseFromString(data)) return;
if (packet.challenge().empty()) return; if (packet.challenge().empty()) return;
if (packet.signature().empty()) return; if (packet.signature().empty()) return;
if (packet.port() && packet.port() != address.getPort()) return;
std::string challenge = packet.challenge(); std::string challenge = packet.challenge();
std::string signature = packet.signature(); std::string signature = packet.signature();

View File

@ -8,6 +8,10 @@ message Packet
bytes challenge = 1; bytes challenge = 1;
bytes signature = 2; bytes signature = 2;
bytes publickey = 3; bytes publickey = 3;
// The port is used to check if a dedi sends data through a redirected port.
// This usually means the port is not forwarded
uint32 port = 4;
} }
message List message List