From 49d6d1c58f6b098852f7f9eb9f324f61cfd3fb98 Mon Sep 17 00:00:00 2001 From: badaix Date: Thu, 6 Feb 2025 21:11:38 +0100 Subject: [PATCH] Add username/password to Hello message --- client/client_settings.hpp | 4 ++++ client/controller.cpp | 2 +- client/snapclient.cpp | 4 ++++ common/message/hello.hpp | 24 +++++++++++++++++++++++- doc/binary_protocol.md | 4 ++++ server/server.cpp | 6 ++++-- 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/client/client_settings.hpp b/client/client_settings.hpp index 9140ea5d..72ec60bf 100644 --- a/client/client_settings.hpp +++ b/client/client_settings.hpp @@ -66,6 +66,10 @@ struct ClientSettings std::string protocol{"tcp"}; /// server port size_t port{1704}; + /// username + std::optional username; + /// password + std::optional password; /// server certificate std::optional server_certificate; /// Certificate file diff --git a/client/controller.cpp b/client/controller.cpp index 11968beb..fd8f28f5 100644 --- a/client/controller.cpp +++ b/client/controller.cpp @@ -454,7 +454,7 @@ void Controller::worker() settings_.host_id = ::getHostId(macAddress); // Say hello to the server - auto hello = std::make_shared(macAddress, settings_.host_id, settings_.instance); + auto hello = std::make_shared(macAddress, settings_.host_id, settings_.instance, settings_.server.username, settings_.server.password); clientConnection_->sendRequest( hello, 2s, [this](const boost::system::error_code& ec, std::unique_ptr response) mutable { diff --git a/client/snapclient.cpp b/client/snapclient.cpp index 3297dfe3..95f91d2d 100644 --- a/client/snapclient.cpp +++ b/client/snapclient.cpp @@ -368,6 +368,10 @@ int main(int argc, char** argv) throw SnapException("Snapclient is built without wss support"); #endif } + if (!uri.user.empty()) + settings.server.username = uri.user; + if (!uri.password.empty()) + settings.server.password = uri.password; } if (server_cert_opt->is_set()) diff --git a/common/message/hello.hpp b/common/message/hello.hpp index 5e768319..791e9974 100644 --- a/common/message/hello.hpp +++ b/common/message/hello.hpp @@ -24,6 +24,7 @@ #include "json_message.hpp" // standard headers +#include #include @@ -41,7 +42,8 @@ public: } /// c'tor taking @p macAddress, @p id and @p instance - Hello(const std::string& mac_address, const std::string& id, size_t instance) : JsonMessage(message_type::kHello) + Hello(const std::string& mac_address, const std::string& id, size_t instance, std::optional username, std::optional password) + : JsonMessage(message_type::kHello) { msg["MAC"] = mac_address; msg["HostName"] = ::getHostName(); @@ -51,6 +53,10 @@ public: msg["Arch"] = ::getArch(); msg["Instance"] = instance; msg["ID"] = id; + if (username.has_value()) + msg["Username"] = username.value(); + if (password.has_value()) + msg["Password"] = password.value(); msg["SnapStreamProtocolVersion"] = 2; } @@ -122,6 +128,22 @@ public: } return id; } + + /// @return the username + std::optional getUsername() const + { + if (!msg.contains("Username")) + return std::nullopt; + return msg["Username"]; + } + + /// @return the password + std::optional getPassword() const + { + if (!msg.contains("Password")) + return std::nullopt; + return msg["Password"]; + } }; } // namespace msg diff --git a/doc/binary_protocol.md b/doc/binary_protocol.md index ffc0a6f0..82026042 100644 --- a/doc/binary_protocol.md +++ b/doc/binary_protocol.md @@ -118,11 +118,15 @@ Sample JSON payload (whitespace added for readability): "Instance": 1, "MAC": "00:11:22:33:44:55", "OS": "Arch Linux", + "Username": "Badaix", + "Password": "$ecret", "SnapStreamProtocolVersion": 2, "Version": "0.17.1" } ``` +The fields `Username` and `Password` are optional and only used if authentication and authorization is enabled on the server. + ### Client Info | Field | Type | Description | diff --git a/server/server.cpp b/server/server.cpp index 7dbcfd2e..3c0bf963 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -310,8 +310,10 @@ void Server::onMessageReceived(StreamSession* streamSession, const msg::BaseMess streamSession->clientId = helloMsg.getUniqueId(); LOG(INFO, LOG_TAG) << "Hello from " << streamSession->clientId << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion() << ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch() - << ", Protocol version: " << helloMsg.getProtocolVersion() << "\n"; - + << ", Protocol version: " << helloMsg.getProtocolVersion() << ", Userrname: " << helloMsg.getUsername().value_or("") + << ", Password: " << (helloMsg.getPassword().has_value() ? "" : "") << "\n"; + streamSession->stop(); + return; bool newGroup(false); GroupPtr group = Config::instance().getGroupFromClient(streamSession->clientId); if (group == nullptr)