private string decrypt(string value)
        {
            var request = new OnDecryptRequestArgs(value);

            this.OnDecryptRequest?.Invoke(this, request);
            return(request.DecryptedValue);
        }
        public void Create(Configuration configuration)
        {
            this.config = configuration;

            this.client = CreateClient();

            this.client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;

            MqttClient CreateClient()
            {
                X509Certificate2 caCert     = null;
                X509Certificate2 clientCert = null;
                MqttSslProtocols protocol   = MqttSslProtocols.None;

                bool secure = bool.TryParse(this.config[SecureParam], out secure) && secure;

                if (secure)
                {
                    //MQTT can be made secure by a certificate from a Certificate Authority alone, or a CA certificate and a client certificate
                    //The CA certificate should never need a password
                    caCert = LoadCertificate(File.Parse(this.config[CACertParam]), string.Empty);

                    //The Client certificate should always require a password
                    if (!string.IsNullOrWhiteSpace(this.config[ClientCertParam]))
                    {
                        OnDecryptRequestArgs certDecryptArgs = new OnDecryptRequestArgs(this.config[CertPasswordParam]);
                        OnDecryptRequest(this, certDecryptArgs);
                        string clientPassword = certDecryptArgs.DecryptedValue;
                        clientCert = LoadCertificate(File.Parse(this.config[ClientCertParam]), clientPassword);
                    }

                    if (Enum.TryParse(this.config[SecureProtocolParam], out MqttSslProtocols parsedProtocol))
                    {
                        protocol = parsedProtocol;
                    }
                }

                return(new MqttClient(
                           this.config[BrokerParam]
                           , Int32.TryParse(this.config[BrokerPortParam], out int port) && port > 0 ? port : 1883
                           , secure
                           , caCert
                           , clientCert
                           , protocol
                           , null));
            }
        }
        public void Start()
        {
            if (this.client.IsConnected == false)
            {
                string clientId     = string.IsNullOrWhiteSpace(this.config[ClientIdParam]) ? Guid.NewGuid().ToString() : this.config[ClientIdParam];
                string authUsername = this.config[AuthUsernameParam];
                string authPassword = null;

                if (!string.IsNullOrWhiteSpace(this.config[AuthPasswordParam]))
                {
                    OnDecryptRequestArgs authDecryptArgs = new OnDecryptRequestArgs(this.config[AuthPasswordParam]);
                    OnDecryptRequest(this, authDecryptArgs);
                    authPassword = authDecryptArgs.DecryptedValue;
                }

                this.client.Connect(
                    clientId
                    , string.IsNullOrWhiteSpace(authUsername) ? null : authUsername
                    , string.IsNullOrWhiteSpace(authUsername) ? null : authPassword);
                this.client.Subscribe(new string[] { this.Topic }, new byte[] { (byte)Enum.Parse(typeof(QualityOfService), this.config[QOSParam]) });
            }
        }
Exemple #4
0
        public void Create(Configuration configuration)
        {
            this.config      = configuration;
            QualityOfService = (byte)Enum.Parse(typeof(QualityOfService), this.config[QOSParam]);
            this.client      = CreateClient();

            if (UseNestedObject)
            {
                Grid coGrid = new Grid
                {
                    Value = this.config[ObjectPropertiesParam]
                };

                //Store source/alias names for the complex object
                ComplexObjectAliases = coGrid.Rows.Select(r =>
                {
                    string source = (string)r[SourceNameColumn];
                    string alias  = (string)r[OutputNameColumn];
                    alias         = string.IsNullOrWhiteSpace(alias) ? source : alias;
                    return(new Tuple <string, string>(source, alias));
                }).ToArray();
            }

            MqttClient CreateClient()
            {
                X509Certificate2 caCert     = null;
                X509Certificate2 clientCert = null;
                MqttSslProtocols protocol   = MqttSslProtocols.None;

                bool secure = bool.TryParse(this.config[SecureParam], out secure) && secure;

                if (secure)
                {
                    //MQTT can be made secure by a certificate from a Certificate Authority alone, or a CA certificate and a client certificate
                    //The CA certificate should never need a password
                    caCert = ListenerAdvanced.LoadCertificate(File.Parse(this.config[CACertParam]), string.Empty);

                    //The Client certificate should always require a password
                    if (!string.IsNullOrWhiteSpace(this.config[ClientCertParam]))
                    {
                        OnDecryptRequestArgs certDecryptArgs = new OnDecryptRequestArgs(this.config[CertPasswordParam]);
                        OnDecryptRequest(this, certDecryptArgs);
                        string clientPassword = certDecryptArgs.DecryptedValue;
                        clientCert = ListenerAdvanced.LoadCertificate(File.Parse(this.config[ClientCertParam]), clientPassword);
                    }

                    if (Enum.TryParse(this.config[SecureProtocolParam], out MqttSslProtocols parsedProtocol))
                    {
                        protocol = parsedProtocol;
                    }
                }

                return(new MqttClient(
                           this.config[BrokerParam]
                           , Int32.TryParse(this.config[BrokerPortParam], out int port) && port > 0 ? port : 1883
                           , secure
                           , caCert
                           , clientCert
                           , protocol
                           , null));
            }
        }