Modifying MQTT Proxy


Janus MQTT Proxy is a service that I wrote on Go as a hobby project. He connects to the MQTT broker and subscribes to all events, and clients, in turn, connect to the proxy and communicate with him as with an MQTT broker.


It allows you to:


  • restrict customer access to various topics. Including separately restricting read and write access;
  • Replace topic names and event contents with regexp s.

Why is it necessary


MQTT . , - 1 /my/lamp/on, - On /my/lamp. , , . , .


Janus MQTT : , , MQTT- . / , .


. , /light/main. Β«, β€” , β€” , , Β». , .


, , , . Janus MQTT / , . , , , β€” .


Janus MQTT


, , β€” , .


Janus MQTT , . β€” :


broker_to_client: #       

  #    MQTT discovery.
  - topic: ^/devices/wb-gpio/controls/LIGHT_([^/]*)/meta/type$
    template: /homeassistant/light/{{.f1}}/config
    val_map:
      switch: >-
        {
        "command_topic":"/light/{{.f1}}/state",
        "state_topic":"/light/{{.f1}}/state",
        "name":"{{.f1}}"
        }

  - topic: ^/devices/wb-gpio/controls/LIGHT_([^/]*)$
    template: /light/{{.f1}}/state
    val_map: {0: OFF, 1: ON}

client_to_broker: #       

  - topic: ^/light/([^/]*)/state$
    template: /devices/wb-gpio/controls/LIGHT_{{.f1}}/on
    val_map: {OFF: 0, ON: 1}

, . .


, Home Assistant , .


MQTT discovery : Wiren Board , . , . switch. retained β€” . JSON-, , MQTT discovery .


Janus MQTT


Go paho.mqtt.golang. MQTT- . , , MQTT-.


: , , . . .. MITM.


:


  • QoS β€” 2 1 , ;
  • - QoS message_id, uint16 ;
  • Janus MQTT . , ;
  • , in-memory retained-.

MQTT- client.serveIncoming, TCP-. MQTT- :


  • ConnectPacket β€” Connack ;
  • SubscribePacket β€” Suback retained-;
  • UnsubscribePacket β€” Unsuback ;
  • PingreqPacket β€” Pingresp;
  • PublishPacket β€” ;
  • PubackPacket, PubrelPacket, PubcompPacket ;
  • DisconnectPacket β€” .

QoS. MQTT QoS:


  • QoS 0: ;
  • QoS 1: ;
  • QoS 2: .

, 1 2 , , , ( 2 , QoS 1, ).


publish- QOS=2


publish- QOS=2



Janus MQTT :


  • Wiren Board β€” ;
  • Home Assistant β€” ( );
  • Yandex2mqtt β€” .

Wiren Board


. . MQTT-.


Yandex2mqtt


oAuth API . . munrexio, bawdiest , .


, :


$ mosquitto_sub -h localhost -u yandex2mqtt -P yandex2mqtt -t '#' -v
/light/LIVING_TABLE/state 1


Home Assistant


. . , .


Home Assistant , , .


, Home Assistant MQTT Discovery β€” , Home Assistant MQTT. JSON-, .


Home Assistant :


mqtt:
  username: !env_var MQTT_USER
  password: !env_var MQTT_PASS
  broker: !env_var MQTT_HOST
  discovery: true
  discovery_prefix: /homeassistant

Janus MQTT


Janus MQTT Home Assistant . 100 , 17 , 6 (/) 5 . . .


Docker


NanoPi docker-compose.yml.




, , , .
10 M. Golang!


/ :
https://github.com/phoenix-mstu/janus-mqtt-proxy.


docker-compose :
https://github.com/phoenix-mstu/smart_home/tree/master/raspberry.


, : 52.59.242.204:26927 ( ), / β€” habr/habr, β€” MQTT, . . , :


  1. ( ).
  2. .

- β€” , . , , . , DoS- β€” . .


All Articles