From 2a126e84500239d6adcf4336bf9bfd815275f5fa Mon Sep 17 00:00:00 2001 From: giskard Date: Wed, 2 Oct 2024 01:07:41 +0800 Subject: [PATCH] udp2p: setup udp p2p to bypass nat --- src/meson.build | 2 ++ src/udp2p/manager.cc | 75 +++++++++++++++++++++++++++++++++++++++++++ src/udp2p/manager.h | 46 ++++++++++++++++++++++++++ src/udp2p/meson.build | 3 ++ 4 files changed, 126 insertions(+) create mode 100644 src/udp2p/manager.cc create mode 100644 src/udp2p/manager.h create mode 100644 src/udp2p/meson.build diff --git a/src/meson.build b/src/meson.build index 051c041..6893415 100644 --- a/src/meson.build +++ b/src/meson.build @@ -26,8 +26,10 @@ deps = [ ] subdir('utils/') +subdir('udp2p/') deps += utils_deps +deps += udp2p_deps exe = executable('mtrx', sources: src, diff --git a/src/udp2p/manager.cc b/src/udp2p/manager.cc new file mode 100644 index 0000000..5177685 --- /dev/null +++ b/src/udp2p/manager.cc @@ -0,0 +1,75 @@ +#include +#include + +#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 diff --git a/src/udp2p/manager.h b/src/udp2p/manager.h new file mode 100644 index 0000000..a0ebef2 --- /dev/null +++ b/src/udp2p/manager.h @@ -0,0 +1,46 @@ +#ifndef INCLUDE_UDP2P_MANAGER_H +#define INCLUDE_UDP2P_MANAGER_H + +#include +#include + +#include + +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 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 onlineMap_; +}; + +}; // namespace udp2p +}; // namespace mtrx + +#endif diff --git a/src/udp2p/meson.build b/src/udp2p/meson.build new file mode 100644 index 0000000..5505d2a --- /dev/null +++ b/src/udp2p/meson.build @@ -0,0 +1,3 @@ +udp2p_deps = [ + dependency('asio'), +]