mirror of
https://github.com/g4klx/MMDVMHost
synced 2025-12-23 00:35:53 +08:00
Added Authentication for MQTT
This commit is contained in:
26
Conf.cpp
26
Conf.cpp
@@ -122,6 +122,9 @@ m_mqttHost("127.0.0.1"),
|
|||||||
m_mqttPort(1883),
|
m_mqttPort(1883),
|
||||||
m_mqttKeepalive(60U),
|
m_mqttKeepalive(60U),
|
||||||
m_mqttName("mmdvm"),
|
m_mqttName("mmdvm"),
|
||||||
|
m_mqttAuthEnabled(false),
|
||||||
|
m_mqttUser("mqttuser"),
|
||||||
|
m_mqttPass("mqttpass"),
|
||||||
m_cwIdEnabled(false),
|
m_cwIdEnabled(false),
|
||||||
m_cwIdTime(10U),
|
m_cwIdTime(10U),
|
||||||
m_cwIdCallsign(),
|
m_cwIdCallsign(),
|
||||||
@@ -609,6 +612,13 @@ bool CConf::read()
|
|||||||
m_mqttKeepalive = (unsigned int)::atoi(value);
|
m_mqttKeepalive = (unsigned int)::atoi(value);
|
||||||
else if (::strcmp(key, "Name") == 0)
|
else if (::strcmp(key, "Name") == 0)
|
||||||
m_mqttName = value;
|
m_mqttName = value;
|
||||||
|
else if (::strcmp(key, "Auth") == 0)
|
||||||
|
m_mqttAuthEnabled = ::atoi(value) == 1;
|
||||||
|
else if (::strcmp(key, "User") == 0)
|
||||||
|
m_mqttUser = value;
|
||||||
|
else if (::strcmp(key, "Pass") == 0)
|
||||||
|
m_mqttPass = value;
|
||||||
|
|
||||||
} else if (section == SECTION_CWID) {
|
} else if (section == SECTION_CWID) {
|
||||||
if (::strcmp(key, "Enable") == 0)
|
if (::strcmp(key, "Enable") == 0)
|
||||||
m_cwIdEnabled = ::atoi(value) == 1;
|
m_cwIdEnabled = ::atoi(value) == 1;
|
||||||
@@ -1322,6 +1332,22 @@ std::string CConf::getMQTTName() const
|
|||||||
return m_mqttName;
|
return m_mqttName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CConf::getMQTTAuthEnabled() const
|
||||||
|
{
|
||||||
|
return m_mqttAuthEnabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CConf::getMQTTUser() const
|
||||||
|
{
|
||||||
|
return m_mqttUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CConf::getMQTTPass() const
|
||||||
|
{
|
||||||
|
return m_mqttPass;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool CConf::getCWIdEnabled() const
|
bool CConf::getCWIdEnabled() const
|
||||||
{
|
{
|
||||||
return m_cwIdEnabled;
|
return m_cwIdEnabled;
|
||||||
|
|||||||
8
Conf.h
8
Conf.h
@@ -57,6 +57,10 @@ public:
|
|||||||
unsigned short getMQTTPort() const;
|
unsigned short getMQTTPort() const;
|
||||||
unsigned int getMQTTKeepalive() const;
|
unsigned int getMQTTKeepalive() const;
|
||||||
std::string getMQTTName() const;
|
std::string getMQTTName() const;
|
||||||
|
bool getMQTTAuthEnabled() const;
|
||||||
|
std::string getMQTTUser() const;
|
||||||
|
std::string getMQTTPass() const;
|
||||||
|
|
||||||
|
|
||||||
// The CW ID section
|
// The CW ID section
|
||||||
bool getCWIdEnabled() const;
|
bool getCWIdEnabled() const;
|
||||||
@@ -408,6 +412,10 @@ private:
|
|||||||
unsigned short m_mqttPort;
|
unsigned short m_mqttPort;
|
||||||
unsigned int m_mqttKeepalive;
|
unsigned int m_mqttKeepalive;
|
||||||
std::string m_mqttName;
|
std::string m_mqttName;
|
||||||
|
bool m_mqttAuthEnabled;
|
||||||
|
std::string m_mqttUser;
|
||||||
|
std::string m_mqttPass;
|
||||||
|
|
||||||
|
|
||||||
bool m_cwIdEnabled;
|
bool m_cwIdEnabled;
|
||||||
unsigned int m_cwIdTime;
|
unsigned int m_cwIdTime;
|
||||||
|
|||||||
@@ -28,6 +28,9 @@ DisplayLevel=1
|
|||||||
[MQTT]
|
[MQTT]
|
||||||
Host=127.0.0.1
|
Host=127.0.0.1
|
||||||
Port=1883
|
Port=1883
|
||||||
|
Auth=0
|
||||||
|
User=mmdvm
|
||||||
|
Pass=mmdvm
|
||||||
Keepalive=60
|
Keepalive=60
|
||||||
Name=mmdvm
|
Name=mmdvm
|
||||||
|
|
||||||
|
|||||||
@@ -368,7 +368,7 @@ int CMMDVMHost::run()
|
|||||||
subscriptions.push_back(std::make_pair("ax25-in", CMMDVMHost::onAX25));
|
subscriptions.push_back(std::make_pair("ax25-in", CMMDVMHost::onAX25));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_mqtt = new CMQTTConnection(m_conf.getMQTTHost(), m_conf.getMQTTPort(), m_conf.getMQTTName(), subscriptions, m_conf.getMQTTKeepalive());
|
m_mqtt = new CMQTTConnection(m_conf.getMQTTHost(), m_conf.getMQTTPort(), m_conf.getMQTTName(), m_conf.getMQTTAuthEnabled(), m_conf.getMQTTUser(), m_conf.getMQTTPass(), subscriptions, m_conf.getMQTTKeepalive());
|
||||||
ret = m_mqtt->open();
|
ret = m_mqtt->open();
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
::fprintf(stderr, "MMDVMHost: unable to start the MQTT Publisher\n");
|
::fprintf(stderr, "MMDVMHost: unable to start the MQTT Publisher\n");
|
||||||
|
|||||||
@@ -23,10 +23,13 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
|
CMQTTConnection::CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& user, const std::string& pass, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos) :
|
||||||
m_host(host),
|
m_host(host),
|
||||||
m_port(port),
|
m_port(port),
|
||||||
m_name(name),
|
m_name(name),
|
||||||
|
m_authEnabled(authEnabled),
|
||||||
|
m_user(user),
|
||||||
|
m_pass(pass),
|
||||||
m_subs(subs),
|
m_subs(subs),
|
||||||
m_keepalive(keepalive),
|
m_keepalive(keepalive),
|
||||||
m_qos(qos),
|
m_qos(qos),
|
||||||
@@ -54,6 +57,9 @@ bool CMQTTConnection::open()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_authEnabled) {
|
||||||
|
::mosquitto_username_pw_set(m_mosq, m_user.c_str(), m_pass.c_str());
|
||||||
|
}
|
||||||
::mosquitto_connect_callback_set(m_mosq, onConnect);
|
::mosquitto_connect_callback_set(m_mosq, onConnect);
|
||||||
::mosquitto_subscribe_callback_set(m_mosq, onSubscribe);
|
::mosquitto_subscribe_callback_set(m_mosq, onSubscribe);
|
||||||
::mosquitto_message_callback_set(m_mosq, onMessage);
|
::mosquitto_message_callback_set(m_mosq, onMessage);
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ enum MQTT_QOS {
|
|||||||
|
|
||||||
class CMQTTConnection {
|
class CMQTTConnection {
|
||||||
public:
|
public:
|
||||||
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE);
|
CMQTTConnection(const std::string& host, unsigned short port, const std::string& name, const bool authEnabled, const std::string& user, const std::string& pass, const std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>>& subs, unsigned int keepalive, MQTT_QOS qos = MQTT_QOS_EXACTLY_ONCE);
|
||||||
~CMQTTConnection();
|
~CMQTTConnection();
|
||||||
|
|
||||||
bool open();
|
bool open();
|
||||||
@@ -47,6 +47,9 @@ private:
|
|||||||
std::string m_host;
|
std::string m_host;
|
||||||
unsigned short m_port;
|
unsigned short m_port;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
|
bool m_authEnabled;
|
||||||
|
std::string m_user;
|
||||||
|
std::string m_pass;
|
||||||
std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> m_subs;
|
std::vector<std::pair<std::string, void (*)(const unsigned char*, unsigned int)>> m_subs;
|
||||||
unsigned int m_keepalive;
|
unsigned int m_keepalive;
|
||||||
MQTT_QOS m_qos;
|
MQTT_QOS m_qos;
|
||||||
|
|||||||
Reference in New Issue
Block a user