mirror of
https://github.com/badaix/snapcast.git
synced 2025-04-29 10:17:16 +02:00
Add username/password to Hello message
This commit is contained in:
parent
3cb2b4dd93
commit
49d6d1c58f
6 changed files with 40 additions and 4 deletions
|
@ -66,6 +66,10 @@ struct ClientSettings
|
||||||
std::string protocol{"tcp"};
|
std::string protocol{"tcp"};
|
||||||
/// server port
|
/// server port
|
||||||
size_t port{1704};
|
size_t port{1704};
|
||||||
|
/// username
|
||||||
|
std::optional<std::string> username;
|
||||||
|
/// password
|
||||||
|
std::optional<std::string> password;
|
||||||
/// server certificate
|
/// server certificate
|
||||||
std::optional<std::filesystem::path> server_certificate;
|
std::optional<std::filesystem::path> server_certificate;
|
||||||
/// Certificate file
|
/// Certificate file
|
||||||
|
|
|
@ -454,7 +454,7 @@ void Controller::worker()
|
||||||
settings_.host_id = ::getHostId(macAddress);
|
settings_.host_id = ::getHostId(macAddress);
|
||||||
|
|
||||||
// Say hello to the server
|
// Say hello to the server
|
||||||
auto hello = std::make_shared<msg::Hello>(macAddress, settings_.host_id, settings_.instance);
|
auto hello = std::make_shared<msg::Hello>(macAddress, settings_.host_id, settings_.instance, settings_.server.username, settings_.server.password);
|
||||||
clientConnection_->sendRequest<msg::ServerSettings>(
|
clientConnection_->sendRequest<msg::ServerSettings>(
|
||||||
hello, 2s, [this](const boost::system::error_code& ec, std::unique_ptr<msg::ServerSettings> response) mutable
|
hello, 2s, [this](const boost::system::error_code& ec, std::unique_ptr<msg::ServerSettings> response) mutable
|
||||||
{
|
{
|
||||||
|
|
|
@ -368,6 +368,10 @@ int main(int argc, char** argv)
|
||||||
throw SnapException("Snapclient is built without wss support");
|
throw SnapException("Snapclient is built without wss support");
|
||||||
#endif
|
#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())
|
if (server_cert_opt->is_set())
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
#include "json_message.hpp"
|
#include "json_message.hpp"
|
||||||
|
|
||||||
// standard headers
|
// standard headers
|
||||||
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,7 +42,8 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/// c'tor taking @p macAddress, @p id and @p instance
|
/// 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<std::string> username, std::optional<std::string> password)
|
||||||
|
: JsonMessage(message_type::kHello)
|
||||||
{
|
{
|
||||||
msg["MAC"] = mac_address;
|
msg["MAC"] = mac_address;
|
||||||
msg["HostName"] = ::getHostName();
|
msg["HostName"] = ::getHostName();
|
||||||
|
@ -51,6 +53,10 @@ public:
|
||||||
msg["Arch"] = ::getArch();
|
msg["Arch"] = ::getArch();
|
||||||
msg["Instance"] = instance;
|
msg["Instance"] = instance;
|
||||||
msg["ID"] = id;
|
msg["ID"] = id;
|
||||||
|
if (username.has_value())
|
||||||
|
msg["Username"] = username.value();
|
||||||
|
if (password.has_value())
|
||||||
|
msg["Password"] = password.value();
|
||||||
msg["SnapStreamProtocolVersion"] = 2;
|
msg["SnapStreamProtocolVersion"] = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,6 +128,22 @@ public:
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @return the username
|
||||||
|
std::optional<std::string> getUsername() const
|
||||||
|
{
|
||||||
|
if (!msg.contains("Username"))
|
||||||
|
return std::nullopt;
|
||||||
|
return msg["Username"];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @return the password
|
||||||
|
std::optional<std::string> getPassword() const
|
||||||
|
{
|
||||||
|
if (!msg.contains("Password"))
|
||||||
|
return std::nullopt;
|
||||||
|
return msg["Password"];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace msg
|
} // namespace msg
|
||||||
|
|
|
@ -118,11 +118,15 @@ Sample JSON payload (whitespace added for readability):
|
||||||
"Instance": 1,
|
"Instance": 1,
|
||||||
"MAC": "00:11:22:33:44:55",
|
"MAC": "00:11:22:33:44:55",
|
||||||
"OS": "Arch Linux",
|
"OS": "Arch Linux",
|
||||||
|
"Username": "Badaix",
|
||||||
|
"Password": "$ecret",
|
||||||
"SnapStreamProtocolVersion": 2,
|
"SnapStreamProtocolVersion": 2,
|
||||||
"Version": "0.17.1"
|
"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
|
### Client Info
|
||||||
|
|
||||||
| Field | Type | Description |
|
| Field | Type | Description |
|
||||||
|
|
|
@ -310,8 +310,10 @@ void Server::onMessageReceived(StreamSession* streamSession, const msg::BaseMess
|
||||||
streamSession->clientId = helloMsg.getUniqueId();
|
streamSession->clientId = helloMsg.getUniqueId();
|
||||||
LOG(INFO, LOG_TAG) << "Hello from " << streamSession->clientId << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion()
|
LOG(INFO, LOG_TAG) << "Hello from " << streamSession->clientId << ", host: " << helloMsg.getHostName() << ", v" << helloMsg.getVersion()
|
||||||
<< ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch()
|
<< ", ClientName: " << helloMsg.getClientName() << ", OS: " << helloMsg.getOS() << ", Arch: " << helloMsg.getArch()
|
||||||
<< ", Protocol version: " << helloMsg.getProtocolVersion() << "\n";
|
<< ", Protocol version: " << helloMsg.getProtocolVersion() << ", Userrname: " << helloMsg.getUsername().value_or("<not set>")
|
||||||
|
<< ", Password: " << (helloMsg.getPassword().has_value() ? "<password is set>" : "<not set>") << "\n";
|
||||||
|
streamSession->stop();
|
||||||
|
return;
|
||||||
bool newGroup(false);
|
bool newGroup(false);
|
||||||
GroupPtr group = Config::instance().getGroupFromClient(streamSession->clientId);
|
GroupPtr group = Config::instance().getGroupFromClient(streamSession->clientId);
|
||||||
if (group == nullptr)
|
if (group == nullptr)
|
||||||
|
|
Loading…
Add table
Reference in a new issue