Koenkk / zigbee2mqtt

Zigbee 🐝 to MQTT bridge πŸŒ‰, get rid of your proprietary Zigbee bridges πŸ”¨
https://www.zigbee2mqtt.io
GNU General Public License v3.0
11.2k stars 1.6k forks source link

After upgrading to 1.37.0 zigbee2mqtt can't connect via IPv6 #22390

Open ytimenkov opened 2 weeks ago

ytimenkov commented 2 weeks ago

What happened?

After updating docker image to 1.37.0 zigbee2mqtt exits after start with:

May 02 05:05:17 heimdall systemd-zigbee[13674]: [2024-05-02T05:05:17+02:00] info:         z2m: Connecting to MQTT server at mqtt://[fd9b:9838:3fd5:2e::1]:1883
May 02 05:05:17 heimdall systemd-zigbee[13674]: [2024-05-02T05:05:17+02:00] error:         z2m: MQTT error: connect ECONNREFUSED /
May 02 05:05:17 heimdall systemd-zigbee[13674]: [2024-05-02T05:05:17+02:00] error:         z2m: MQTT failed to connect, exiting...
May 02 05:05:17 heimdall systemd-zigbee[13674]: [2024-05-02T05:05:17+02:00] info:         z2m: Stopping zigbee-herdsman...
May 02 05:05:18 heimdall systemd-zigbee[13674]: [2024-05-02T05:05:18+02:00] error:         z2m: MQTT error: connect ECONNREFUSED /
May 02 05:05:19 heimdall systemd-zigbee[13674]: [2024-05-02T05:05:19+02:00] error:         z2m: MQTT error: connect ECONNREFUSED /

After switching to ipv4 address, the daemon started.

What did you expect to happen?

No response

How to reproduce it (minimal and precise)

No response

Zigbee2MQTT version

1.37.0 (commit #46f34c8)

Adapter firmware version

not applicable

Adapter

not applicable

Setup

Podman with netavark stack

Debug log

May 02 05:06:42 heimdall systemd-zigbee[13996]: [2024-05-02T05:06:42+02:00] info:         z2m: Connecting to MQTT server at mqtt://[fd9b:9838:3fd5:2e::1]:1883
May 02 05:06:42 heimdall systemd-zigbee[13996]: [2024-05-02T05:06:42+02:00] debug:         z2m: Using MQTT anonymous login
May 02 05:06:42 heimdall systemd-zigbee[13996]: [2024-05-02T05:06:42+02:00] error:         z2m: MQTT error: connect ECONNREFUSED /
May 02 05:06:42 heimdall systemd-zigbee[13996]: [2024-05-02T05:06:42+02:00] error:         z2m: MQTT failed to connect, exiting...
klevin92 commented 1 week ago

same symptoms for me as well but using IPv4

SoSv73 commented 1 week ago

Hi, I have the same Problem

[2024-05-05 23:05:09] info: z2m: Connecting to MQTT server at http://192.168.2.144:1883 [2024-05-05 23:05:09] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:09] error: z2m: MQTT failed to connect, exiting... [2024-05-05 23:05:09] info: z2m: Stopping zigbee-herdsman... [2024-05-05 23:05:10] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:11] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:12] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:13] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:14] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:15] error: z2m: MQTT error: connect ECONNREFUSED / [2024-05-05 23:05:16] error: z2m: MQTT error: connect ECONNREFUSED /

Koenkk commented 1 week ago

@SoSv73 you shouldn't use http://, use mqtt://192.168.2.144:1883

SoSv73 commented 1 week ago

Thanks Koenkk, that was very usefull. Now it works like a charm. Thank you very much

Astu04 commented 1 week ago

I guess docker networking defaults to IPv6 as I'm having the same issue with the following server url: mqtt://mosquitto:1883

zigbee2mqtt  | [2024-05-07 22:11:05] info:      z2m: Connecting to MQTT server at http://mosquitto:1883/
zigbee2mqtt  | [2024-05-07 22:11:05] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:05] error:     z2m: MQTT failed to connect, exiting...
zigbee2mqtt  | [2024-05-07 22:11:05] info:      z2m: Stopping zigbee-herdsman...
zigbee2mqtt  | [2024-05-07 22:11:07] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:08] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:09] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:10] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:11] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:12] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:13] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:14] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:15] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:16] error:     z2m: MQTT error: connect ECONNREFUSED /
zigbee2mqtt  | [2024-05-07 22:11:17] error:     z2m: MQTT error: connect ECONNREFUSED /

After exposing 1883 to 0.0.0.0 and switching the server url to local_ipv4:1883 it works again.

Koenkk commented 1 week ago

Can you check with the latest dev branch?dev branch

Astu04 commented 1 week ago

I've just tested the image koenkk/zigbee2mqtt:latest-dev and it works as expected :)

zigbee2mqtt  | [2024-05-07 22:31:22] info:      z2m: Connecting to MQTT server at mqtt://mosquitto:1883/
mosquitto    | 1715113882: New connection from 172.31.0.2:44940 on port 1883.
mosquitto    | 1715113882: New client connected from 172.31.0.2:44940 as zigbee (p2, c1, k60, u'z2m_qtt').
zigbee2mqtt  | [2024-05-07 22:31:22] info:      z2m: Connected to MQTT server

Wonderful, thank you!

Koenkk commented 1 week ago

So something broke with a recent version of MQTT.js, can you do:

docker exec -it zigbee2mqtt CONTAINER_NAME sh
npm install mqtt@5.5.1
exit
docker restart CONTAINER_NAME

See if it still works, then bump the mqtt version until it breaks.

Astu04 commented 1 week ago

I had to apk add npm to the alpine image even when it's on the Dockerfile Testing with main image, not latest. 5.5.3 worked, 5.5.4 didn't.

Astu04 commented 1 week ago

5.5.5 works with a caveat server: 'mqtt://mosquitto:1883/' still gets a connection refused but 'mqtt://mosquitto:1883' works as expected.

Connection error: Error: connect ECONNREFUSED /
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1555:16) {
  errno: -111,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '/'
}

Given that I haven't seen documentation with the trailing slash, I think it's a just me issue.

I used this tiny script that may be handy for someone else:

const mqtt = require("mqtt");

// MQTT broker URL
// const brokerUrl = "mqtt://mosquitto:1883/";
const brokerUrl = "mqtt://mosquitto:1883";
// MQTT broker username and password
const options = {
  username: "z2m_qtt",
  password: "password"
};

// Connect to the MQTT broker
const client = mqtt.connect(brokerUrl, options);

client.on("connect", () => {
  // Subscribe to a topic
  client.subscribe("presence", (err) => {
    if (!err) {
      // Publish a message after successful subscription
      client.publish("presence", "Hello mqtt");
    } else {
      console.error("Subscription error:", err);
    }
  });
});

client.on("message", (topic, message) => {
  // Display received message
  console.log(message.toString());
  // Close the client connection
  client.end();
});

// Handle connection errors
client.on("error", (error) => {
  console.error("Connection error:", error);
});

Edit. I'm closing the PR, I've just noticed the regression from 5.5.5 to 5.5.0 and I'm not aware of why but you must know something I don't.

Koenkk commented 1 week ago

So it starts to break from 5.5.4?

ytimenkov commented 1 week ago

Sorry for late reply.

I tried latest-dev tag and can confirm that it fixes the problem (zigbee2mqtt connects to mosquitto via IPv6)

/app # grep version node_modules/mqtt/package.json 
  "version": "5.5.0",
ytimenkov commented 1 week ago

I wonder if it is related to https://github.com/mqttjs/MQTT.js/pull/1852 (or corresponding issue?) (based on fuzzy gpt-style text searching for most probable location 😁).

Digging a bit more I saw that you've downgraded mqtt.js version in dev. Then it makes more sense (I didn't pay attention to the versions and thought that you've bumped to the latest 5.5.5).

arcnmx commented 1 week ago

I wonder if it is related to mqttjs/MQTT.js#1852 (or corresponding issue?) (based on fuzzy gpt-style text searching for most probable location 😁).

Given that my error is slightly different it's clearly related, and thinks the address is a socket path or something silly:

z2m: Starting Zigbee2MQTT version 1.37.0 (commit #unknown) z2m: Starting zigbee-herdsman (0.45.0) ... z2m: Connecting to MQTT server at mqtt://[fd0c::4c]:1883 z2m: MQTT error: connect EAFNOSUPPORT / z2m: MQTT failed to connect, exiting...

(EAFNOSUPPORT presumably occurs when it's hardened under systemd to only allow RestrictAddressFamilies=AF_INET6)

Koenkk commented 6 days ago

@ytimenkov does 5.5.5 work for you?

ytimenkov commented 5 days ago

@Koenkk No, it doesn't:

/app # apk add npm
fetch https://dl-cdn.alpinelinux.org/alpine/v3.18/main/aarch64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.18/community/aarch64/APKINDEX.tar.gz
(1/1) Installing npm (9.6.6-r0)
Executing busybox-1.36.1-r2.trigger
OK: 77 MiB in 34 packages
/app # ls data/
configuration.yaml       coordinator_backup.json  database.db
/app # npm install mqtt@5.5.5

changed 1 package, and audited 209 packages in 1m

30 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
/app # node index.js 
[2024-05-12T06:02:26+00:00] error:      z2m: MQTT error: connect ECONNREFUSED /
[2024-05-12T06:02:26+00:00] error:      z2m: MQTT failed to connect, exiting...
[2024-05-12T06:02:27+00:00] error:      z2m: MQTT error: connect ECONNREFUSED /
[2024-05-12T06:02:28+00:00] error:      z2m: MQTT error: connect ECONNREFUSED /

The latest 1.37.1 works though. Release notes mention it as #605.

Koenkk commented 3 days ago