Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server CA TLS support (NotAuthorized error) #40

Open
KhazAkar opened this issue Oct 15, 2024 · 0 comments
Open

Server CA TLS support (NotAuthorized error) #40

KhazAkar opened this issue Oct 15, 2024 · 0 comments

Comments

@KhazAkar
Copy link

KhazAkar commented Oct 15, 2024

Hello,
I'm trying to use this library (while learning D :D) to write mqtt client for emqx.com serverless broker. It uses 8883 port (MQTT over TLS), but it uses username/password for auth, not certificates. It provides public CA certificate, but that's all. I've tried to adopt your publish example, but it doesn't seem to work with TLS that nicely. Probably it's skill issue, but best to write here. :)

My example:

import std.datetime;
import std.conv;
import std.array;
import std.stdio;
import std.string : format;

import mqttd;

void main()
{
        import vibe.core.log : setLogFormat, FileLogger;
        import vibe.core.core : sleep, runApplication, runTask;

        setLogFormat(FileLogger.Format.threadTime);

        auto settings = Settings();
        settings.clientId = "DlangPublisher";
        settings.reconnect = 1.seconds;
        settings.userName = "user";
        settings.password = "password";
        settings.host = "emqx-broker.com";
        settings.port = 8883;
        settings.useSsl = true;
        settings.trustedCertificateFile = "cert.pem";
        settings.onConnAck = (scope MqttClient ctx, in ConnAck ack) {
                if (ack.returnCode != ConnectReturnCode.ConnectionAccepted)
                        return;

                auto publisherQ0 = runTask(() nothrow{
                        try
                        {
                                while (ctx.connected)
                                {
                                        ctx.publish("chat0", "QoS0 message");
                                        sleep(3.seconds());
                                }
                        }
                        catch (Exception ex)
                                assert(0, format!"PublisherQ0 error: %s"(ex.msg));
                });

                auto publisherQ1 = runTask(() nothrow{
                        try
                        {
                                sleep(1.seconds());
                                while (ctx.connected)
                                {
                                        ctx.publish("chat1", "QoS1 message", QoSLevel.QoS1);

                                        sleep(3.seconds());
                                }
                        }
                        catch (Exception ex)
                                assert(0, format!"PublisherQ1 error: %s"(ex.msg));
                });

                auto publisherQ2 = runTask(() nothrow{
                        try
                        {
                                sleep(2.seconds());
                                while (ctx.connected)
                                {
                                        ctx.publish("chat2", "QoS2 message", QoSLevel.QoS2);
                                        sleep(3.seconds());
                                }
                        }
                        catch (Exception ex)
                                assert(0, format!"PublisherQ2 error: %s"(ex.msg));
                });
        };
        auto mqtt = new MqttClient(settings);
        mqtt.connect();
        scope (exit)
                mqtt.disconnect();

        runApplication();
}

Outcome:

khazakar@msi-bravo-15:~/test/dlang$ ./mqtt
[2024-10-15 17:53:40.520 main(bUVI) ERR] Connection refused: NotAuthorized
[2024-10-15 17:53:41.642 main(VLUv) ERR] Connection refused: NotAuthorized
[2024-10-15 17:53:42.765 main(sQDf) ERR] Connection refused: NotAuthorized
[2024-10-15 17:53:43.882 main(v9R9) ERR] Connection refused: NotAuthorized
^CReceived signal 2. Shutting down.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant