Skip to content

Commit

Permalink
fix: Make frontend listen on both IPv4 and IPv6 by default (#19660)
Browse files Browse the repository at this point in the history
* Change: Set frontend host to null by default

* Change: Allow frontend to listen on port without host

* Change: Unit test

* Add: Unit test
  • Loading branch information
n-thumann authored Nov 10, 2023
1 parent da04d22 commit 45ac44d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/extension/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ export default class Frontend extends Extension {
this.wss = new WebSocket.Server({noServer: true});
this.wss.on('connection', this.onWebSocketConnection);

if (this.host.startsWith('/')) {
if (!this.host) {
this.server.listen(this.port);
logger.info(`Started frontend on port ${this.port}`);
} else if (this.host.startsWith('/')) {
this.server.listen(this.host);
logger.info(`Started frontend on socket ${this.host}`);
} else {
Expand Down
5 changes: 2 additions & 3 deletions lib/util/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,10 @@
"requiresRestart": true
},
"host": {
"type": "string",
"type": ["string", "null"],
"title": "Bind host",
"description": "Frontend binding host. Binds to a unix socket when an absolute path is given instead.",
"examples": ["127.0.0.1", "/run/zigbee2mqtt/zigbee2mqtt.sock"],
"default": "0.0.0.0",
"examples": ["127.0.0.1", "::1", "/run/zigbee2mqtt/zigbee2mqtt.sock"],
"requiresRestart": true
},
"auth_token": {
Expand Down
2 changes: 1 addition & 1 deletion lib/util/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function loadSettingsWithDefaults(): void {
}

if (_settingsWithDefaults.frontend) {
const defaults = {port: 8080, auth_token: false, host: '0.0.0.0'};
const defaults = {port: 8080, auth_token: false};
const s = typeof _settingsWithDefaults.frontend === 'object' ? _settingsWithDefaults.frontend : {};
// @ts-ignore
_settingsWithDefaults.frontend = {};
Expand Down
24 changes: 24 additions & 0 deletions test/frontend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ describe('Frontend', () => {
mockHTTPS.implementation.listen.mockClear();
});

it('Start/stop without host', async () => {
settings.set(['frontend'], {port: 8081});
controller = new Controller(jest.fn(), jest.fn());
await controller.start();
expect(mockNodeStatic.variables.path).toBe("my/dummy/path");
expect(mockHTTP.implementation.listen).toHaveBeenCalledWith(8081);
const mockWSClient = {
implementation: {
terminate: jest.fn(),
send: jest.fn(),
},
events: {},
};
mockWS.implementation.clients.push(mockWSClient.implementation);
await controller.stop();
expect(mockWSClient.implementation.terminate).toHaveBeenCalledTimes(1);
expect(mockHTTP.implementation.close).toHaveBeenCalledTimes(1);
expect(mockWS.implementation.close).toHaveBeenCalledTimes(1);
mockWS.implementation.close.mockClear();
mockHTTP.implementation.close.mockClear();
mockHTTP.implementation.listen.mockClear();
mockHTTPS.implementation.listen.mockClear();
});

it('Start/stop unix socket', async () => {
settings.set(['frontend'], {host: "/tmp/zigbee2mqtt.sock"});
controller = new Controller(jest.fn(), jest.fn());
Expand Down
2 changes: 1 addition & 1 deletion test/settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,7 +935,7 @@ describe('Settings', () => {
});

settings.reRead();
expect(settings.get().frontend).toStrictEqual({port: 8080, auth_token: false, host: '0.0.0.0'})
expect(settings.get().frontend).toStrictEqual({port: 8080, auth_token: false})
});

it('Baudrate config', () => {
Expand Down

0 comments on commit 45ac44d

Please sign in to comment.