简体   繁体   English

如何通过服务器隧道浏览器端 mqtt?

[英]How to tunnel browser-side mqtt through the server?

Browser uses this to connect to a mosquitto (websockets mode):浏览器使用它来连接到 mosquitto(websockets 模式):

new Paho.MQTT.Client('localhost', 9001, '');

As I understand, it's a direct connection from the browser to the broker.据我了解,这是从浏览器到代理的直接连接。 It won't work in the normal setup when the browser is on a different machine.当浏览器在另一台机器上时,它不会在正常设置中工作。

In normal setup nodejs and the broker are on the same machine.在正常设置中,nodejs 和代理在同一台机器上。 How to route that via nodejs, so nodejs could forward the connections to the localhost:9001 ?如何通过 nodejs 路由它,以便 nodejs 可以将连接转发到localhost:9001 And I don't want to write a lot of code for repacking messages and reimplementing different subscription API.而且我不想编写大量代码来重新打包消息和重新实现不同的订阅 API。

localhost is a special case hostname that refers to the machine some code is running on. localhost是一种特殊情况的主机名,指的是运行某些代码的机器。

Assuming the broker you want to connect to is on the same machine that hosted the webpage you can use the location variable to get the information you need.假设您要连接的代理在托管网页的同一台机器上,您可以使用location变量来获取您需要的信息。

var client = new Paho.MQTT.Client(location.hostname, 9001, '');

Assuming you are proxying the webapp then the websockets can be proxied as well on the same port which would end up with:假设您正在代理 webapp,那么 websockets 也可以在同一端口上进行代理,最终结果为:

var client = new Paho.MQTT.Client(location.hostname, parseInt(location.port), '');

To proxy the mqtt as well (using nginx):还要代理 mqtt(使用 nginx):

location /mqtt {
    proxy_pass          http://127.0.0.1:9001/;
    proxy_http_version  1.1;
    proxy_set_header    Upgrade $http_upgrade;
    proxy_set_header    Connection "upgrade";
}

You could just open up another port and then setup an SSH Tunnel (ie SSH Tunnel the MQTT Data).您可以打开另一个端口,然后设置一个 SSH 隧道(即 SSH 隧道 MQTT 数据)。 The advantage of doing this is that it allows you to exposed or not expose this port to the internet with ease and it is secure (encrypted).这样做的好处是它允许您轻松地将此端口公开或不公开到互联网,并且它是安全的(加密的)。

This tutorial explains how to do this.本教程介绍了如何执行此操作。 Obviously as the MQTT data is transmitted over SSH (through the SSH Tunnel) it is 100% encrypted and therefore secure.显然,由于 MQTT 数据通过 SSH(通过 SSH 隧道)传输,因此它是 100% 加密的,因此是安全的。 Also you could of course use RSA keys (making the SSH connection very secure indeed) and then write a simple BASH script to automation all of this process I guess.当然,您也可以使用 RSA 密钥(确实使 SSH 连接非常安全),然后编写一个简单的 BASH 脚本来自动化所有这个过程,我猜。 Tutorial Link: SSH Tunnel MQTT教程链接: SSH 隧道 MQTT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM