Skip to content

Commit 45ac44d

Browse files
authored
fix: Make frontend listen on both IPv4 and IPv6 by default (#19660)
* Change: Set frontend host to null by default * Change: Allow frontend to listen on port without host * Change: Unit test * Add: Unit test
1 parent da04d22 commit 45ac44d

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

lib/extension/frontend.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ export default class Frontend extends Extension {
7474
this.wss = new WebSocket.Server({noServer: true});
7575
this.wss.on('connection', this.onWebSocketConnection);
7676

77-
if (this.host.startsWith('/')) {
77+
if (!this.host) {
78+
this.server.listen(this.port);
79+
logger.info(`Started frontend on port ${this.port}`);
80+
} else if (this.host.startsWith('/')) {
7881
this.server.listen(this.host);
7982
logger.info(`Started frontend on socket ${this.host}`);
8083
} else {

lib/util/settings.schema.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,10 @@
371371
"requiresRestart": true
372372
},
373373
"host": {
374-
"type": "string",
374+
"type": ["string", "null"],
375375
"title": "Bind host",
376376
"description": "Frontend binding host. Binds to a unix socket when an absolute path is given instead.",
377-
"examples": ["127.0.0.1", "/run/zigbee2mqtt/zigbee2mqtt.sock"],
378-
"default": "0.0.0.0",
377+
"examples": ["127.0.0.1", "::1", "/run/zigbee2mqtt/zigbee2mqtt.sock"],
379378
"requiresRestart": true
380379
},
381380
"auth_token": {

lib/util/settings.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function loadSettingsWithDefaults(): void {
150150
}
151151

152152
if (_settingsWithDefaults.frontend) {
153-
const defaults = {port: 8080, auth_token: false, host: '0.0.0.0'};
153+
const defaults = {port: 8080, auth_token: false};
154154
const s = typeof _settingsWithDefaults.frontend === 'object' ? _settingsWithDefaults.frontend : {};
155155
// @ts-ignore
156156
_settingsWithDefaults.frontend = {};

test/frontend.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ describe('Frontend', () => {
135135
mockHTTPS.implementation.listen.mockClear();
136136
});
137137

138+
it('Start/stop without host', async () => {
139+
settings.set(['frontend'], {port: 8081});
140+
controller = new Controller(jest.fn(), jest.fn());
141+
await controller.start();
142+
expect(mockNodeStatic.variables.path).toBe("my/dummy/path");
143+
expect(mockHTTP.implementation.listen).toHaveBeenCalledWith(8081);
144+
const mockWSClient = {
145+
implementation: {
146+
terminate: jest.fn(),
147+
send: jest.fn(),
148+
},
149+
events: {},
150+
};
151+
mockWS.implementation.clients.push(mockWSClient.implementation);
152+
await controller.stop();
153+
expect(mockWSClient.implementation.terminate).toHaveBeenCalledTimes(1);
154+
expect(mockHTTP.implementation.close).toHaveBeenCalledTimes(1);
155+
expect(mockWS.implementation.close).toHaveBeenCalledTimes(1);
156+
mockWS.implementation.close.mockClear();
157+
mockHTTP.implementation.close.mockClear();
158+
mockHTTP.implementation.listen.mockClear();
159+
mockHTTPS.implementation.listen.mockClear();
160+
});
161+
138162
it('Start/stop unix socket', async () => {
139163
settings.set(['frontend'], {host: "/tmp/zigbee2mqtt.sock"});
140164
controller = new Controller(jest.fn(), jest.fn());

test/settings.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ describe('Settings', () => {
935935
});
936936

937937
settings.reRead();
938-
expect(settings.get().frontend).toStrictEqual({port: 8080, auth_token: false, host: '0.0.0.0'})
938+
expect(settings.get().frontend).toStrictEqual({port: 8080, auth_token: false})
939939
});
940940

941941
it('Baudrate config', () => {

0 commit comments

Comments
 (0)