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
This commit is contained in:
Nicolas Thumann
2023-11-10 21:57:53 +01:00
committed by GitHub
parent da04d223b4
commit 45ac44dd06
5 changed files with 32 additions and 6 deletions
+4 -1
View File
@@ -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 {
+2 -3
View File
@@ -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": {
+1 -1
View File
@@ -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 = {};
+24
View File
@@ -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());
+1 -1
View File
@@ -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', () => {