使用mqtt-ngx通过websocket在tls中将其连接到代理

时间:2019-12-17 17:28:40

标签: angular mqtt angular8

我有一个远程蚊子代理(在带有Windows的aws ec2实例上),并且一切正常:端口可以访问,我可以按照我的acl规则发布和订阅。 我已将发布操作限制在.ne​​t核心服务器(使用Identityserver 4)上,而我的具有ngx-mqtt的angular8应用程序进行了订阅。

现在我正在尝试启用tls,但是在连接方面一直失败。

main-es2015.42b21e2ecd07be623604.js:1 WebSocket connection to 'wss://myserver/mqtt' failed: Error in connection establishment: net::ERR_CERT_INVALID

我的域具有有效的证书,Angular应用使用https连接。 对于mosquitto,我已经按照mosquitto上的文档对ca,服务器和客户端证书进行了自签名,但是仍然无法弄清丢失的内容:我应该将证书和密钥发送给已记录的客户端吗?我应该执行哪种流程?

acl:

# This affects access control for clients with no username.
# topic pattern you can subscribe to
topic read $SYS/#

# This only affects clients with username "roger".
user backend-username
topic write stage/#

user backend-username
topic write production/#


# This affects all clients.
pattern write $SYS/broker/connection/%c/state
pattern read stage/%u/openRequests
pattern read production/%u/openRequests

mosquitto.conf

port 1883
listener 8883
protocol websockets

connection_messages true

allow_anonymous false

acl_file C:\Program Files\mosquitto\aclfile.example

cafile C:\Program Files\mosquitto\certs\certificate_authority.crt

# Path to the PEM encoded server certificate.
certfile C:\Program Files\mosquitto\certs\broker.crt

# Path to the PEM encoded keyfile.
keyfile C:\Program Files\mosquitto\certs\broker.key

tls_version tlsv1.2

log_dest file C:\logs_and_keys\mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S

ngx-mqtt选项进行连接:

  this.mqttService.connect({
      hostname: environment.mqttHost,
      port: environment.mqttPort,
      path: environment.mqttBasePath,
      protocol: 'wss',
      username: username,
      password: 'useless-password',
      ca: certificate,
      cert: cert.toString(),
      key: key.toString()
    });

在测试的同时,我也在对ca.crt,client.crt和client.key进行硬编码

    const certificate = `-----BEGIN CERTIFICATE-----
MIIDPjCCAiagAwIBAgIJALR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qbyUKGZhL/xBnHKmwO2ij0Umk1f3Ovd8h4BP6UaPdVILeVG
vBFLxI3vvkWtSHS/SumdVoMQYdwRAiRL3kKb9WtnIaL9lP9cHgBrGu979l4MnwrT
0VRkPDk2d8gpRNr6ezFw1IfpC8ULR0+p8uk8lB6BaGJ1/o9JaVyXHsegPrS04iPk
SVsVak/mxKZm0rlcCvBTzKOnWtIcpc1rfX5yX5bqworz4YtY00LAsCshajPTQTB4
jUG1S2Bpj+7DJcKI+PrJZX1sAAGG6izArEtQlFJHI6Z0/VtuBITEElfKPrnUuLV9
AgMBAAEwDQYJKoZIhvcNAQELBQADggEBAJtQK9posyJ9+h8gCb/2RtKo2R75YTb7
VkaiJ1TDmfgLOpcm/1Jshq1CjQWSsTrgpmllHBN6UMEDJn6NsNvFekB/+26nDrRX
266I0tDgRPTA9Fughk0BQx63aRqVRXLasq6I5DhqLNSIcS/7SSZm1S7uG8cVoEqg
qjMCiUZWhe9LKF6PCuUmZQkDv5zpuD6vLUXOvEi1AGInsxtShJBHoxd5slmCOHUT
db3gC8kONOPRuU3F+mGasesbF38c1qx9jcPNC+wSFGIPBBFUCcikUkCnmsOX8NKh
sTne6BiSJ9NsIUb+A3cw9Nnv5wjI1EdICKMAoAMGW2oCUAgcAIbJhLs=
-----END CERTIFICATE-----
`; 

预先感谢

1 个答案:

答案 0 :(得分:0)

在将近一年的时间里,对该项目进行了许多更改,以至于这与我最初描述的场景不同。 我现在有一个无用的后端,因此我不托管mosquitto经纪人(因此我目前正在使用mosquitto的公共经纪人),并且我已经将Angular升级到了第十版(当时使用了Angular 8)。

现在,我仅使用以下配置即可使用:

hostname: 'test.mosquitto.org',
port: 8081,
protocol: 'wss',
path: '/mqtt'