Skip to content

Commit

Permalink
udp2p: setup udp p2p to bypass nat
Browse files Browse the repository at this point in the history
  • Loading branch information
rtgiskard committed Oct 1, 2024
1 parent 4b6ed9e commit 2a126e8
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ deps = [
]

subdir('utils/')
subdir('udp2p/')

deps += utils_deps
deps += udp2p_deps

exe = executable('mtrx',
sources: src,
Expand Down
75 changes: 75 additions & 0 deletions src/udp2p/manager.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <spdlog/spdlog.h>
#include <string>

#include "manager.h"

namespace mtrx {
namespace udp2p {

auto logger = spdlog::default_logger()->clone("udp2p");

bool AuthManager::updateUser(const std::string & id, const std::string & hash) {
if (userdb_.contains(id))
logger->info("update user: {}", id);
else
logger->info("add user: {}", id);

userdb_[id] = hash;
return true;
}

bool AuthManager::rmUser(const std::string & id) {
auto it = userdb_.find(id);
if (it == userdb_.end()) {
logger->error("no such user: {}", id);
return false;
}

userdb_.erase(it);
return true;
}

bool AuthManager::authUser(const std::string & id, const std::string & hash) {
auto it = userdb_.find(id);
if (it == userdb_.end()) {
logger->error("no such user: {}", id);
return false;
}
return it->second == hash;
}

bool EndpointManager::updateEndpoint(const std::string & id, const EndPoint & endpoint) {
if (onlineMap_.contains(id))
logger->info("update endpoint: {}", id);
else
logger->info("add endpoint: {}", id);

onlineMap_[id] = endpoint;
return true;
}

bool EndpointManager::rmEndpoint(const std::string & id) {
auto it = onlineMap_.find(id);
if (it == onlineMap_.end()) {
logger->error("no such endpoint: {}", id);
return false;
}

onlineMap_.erase(it);
return true;
};

const EndPoint & EndpointManager::getEndpointByID(const std::string & id) {

auto it = onlineMap_.find(id);
if (it == onlineMap_.end()) {
logger->warn("no such endpoint: {}", id);
static const EndPoint endpoint_zero{};
return endpoint_zero;
}

return onlineMap_[id];
}

} // namespace udp2p
} // namespace mtrx
46 changes: 46 additions & 0 deletions src/udp2p/manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef INCLUDE_UDP2P_MANAGER_H
#define INCLUDE_UDP2P_MANAGER_H

#include <map>
#include <string>

#include <asio.hpp>

namespace mtrx {
namespace udp2p {

struct EndPoint {
asio::ip::address_v4 ip4;
int port;
};

class AuthManager {
public:
AuthManager(){};
~AuthManager(){};

bool authUser(const std::string & id, const std::string & hash);
bool updateUser(const std::string & id, const std::string & hash);
bool rmUser(const std::string & id);

private:
std::map<std::string, std::string> userdb_;
};

class EndpointManager {
public:
EndpointManager(){};
~EndpointManager(){};

bool updateEndpoint(const std::string & id, const EndPoint & endpoint);
bool rmEndpoint(const std::string & id);
const EndPoint & getEndpointByID(const std::string & id);

private:
std::map<std::string, EndPoint> onlineMap_;
};

}; // namespace udp2p
}; // namespace mtrx

#endif
3 changes: 3 additions & 0 deletions src/udp2p/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
udp2p_deps = [
dependency('asio'),
]

0 comments on commit 2a126e8

Please sign in to comment.