Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow serving under a non-root base path #19911

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/extension/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class Frontend extends Extension {
private mqttBaseTopic = settings.get().mqtt.base_topic;
private host = settings.get().frontend.host;
private port = settings.get().frontend.port;
private path = settings.get().frontend.path;
private sslCert = settings.get().frontend.ssl_cert;
private sslKey = settings.get().frontend.ssl_key;
private authToken = settings.get().frontend.auth_token;
Expand Down Expand Up @@ -71,7 +72,7 @@ export default class Frontend extends Extension {
},
};
this.fileServer = gzipStatic(frontend.getPath(), options);
this.wss = new WebSocket.Server({noServer: true});
this.wss = new WebSocket.Server({noServer: true, path: this.path});
this.wss.on('connection', this.onWebSocketConnection);

if (this.host.startsWith('/')) {
Expand All @@ -97,8 +98,12 @@ export default class Frontend extends Extension {
}

@bind private onRequest(request: http.IncomingMessage, response: http.ServerResponse): void {
// @ts-ignore
this.fileServer(request, response, finalhandler(request, response));
if (request.url === this.path) {
// @ts-ignore
this.fileServer(request, response, finalhandler(request, response));
} else {
response.writeHead(404).end();
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use a 302 and point it to the path, that way if no reverse proxy is used (e.g. curl on localhost) it would end up on the correct spot.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use a 302 and point it to the path, that way if no reverse proxy is used (e.g. curl on localhost) it would end up on the correct spot.

Can we accept this? This is a QOL comment on a real issue.

}
}

private authenticate(request: http.IncomingMessage, cb: (authenticate: boolean) => void): void {
Expand Down
1 change: 1 addition & 0 deletions lib/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ declare global {
auth_token?: string,
host?: string,
port?: number,
path?: string,
url?: string,
ssl_cert?: string,
ssl_key?: string,
Expand Down
8 changes: 8 additions & 0 deletions lib/util/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@
"default": "0.0.0.0",
"requiresRestart": true
},
"path":{
"type": "string",
"title": "Path",
"description": "Path under which the frontend is available.",
"examples": ["/zigbee2mqtt"],
"default": "/",
"requiresRestart": true
},
"auth_token": {
"type": ["string", "null"],
"title": "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, host: '0.0.0.0', path: '/'};
const s = typeof _settingsWithDefaults.frontend === 'object' ? _settingsWithDefaults.frontend : {};
// @ts-ignore
_settingsWithDefaults.frontend = {};
Expand Down
16 changes: 13 additions & 3 deletions test/frontend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ const mockNodeStatic = {
events: {},
};

const mockRequest = {
url: '/',
};

const mockResponse = {
writeHead: jest.fn(() => ({
end: jest.fn()
})),
};

jest.mock('http', () => ({
createServer: jest.fn().mockImplementation((onRequest) => {
mockHTTP.variables.onRequest = onRequest;
Expand Down Expand Up @@ -99,7 +109,7 @@ describe('Frontend', () => {
data.writeDefaultConfiguration();
data.writeDefaultState();
settings.reRead();
settings.set(['frontend'], {port: 8081, host: "127.0.0.1"});
settings.set(['frontend'], {port: 8081, host: "127.0.0.1", path: "/"});
settings.set(['homeassistant'], true);
zigbeeHerdsman.devices.bulb.linkquality = 10;
});
Expand Down Expand Up @@ -269,9 +279,9 @@ describe('Frontend', () => {
mockWS.implementation.handleUpgrade.mock.calls[0][3](99);
expect(mockWS.implementation.emit).toHaveBeenCalledWith('connection', 99, {"url": "http://localhost:8080/api"});

mockHTTP.variables.onRequest(1, 2);
mockHTTP.variables.onRequest(mockRequest, mockResponse);
expect(mockNodeStatic.implementation).toHaveBeenCalledTimes(1);
expect(mockNodeStatic.implementation).toHaveBeenCalledWith(1, 2, expect.any(Function));
expect(mockNodeStatic.implementation).toHaveBeenCalledWith(mockRequest, mockResponse, expect.any(Function));
});

it('Static server', async () => {
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, host: '0.0.0.0', path: '/'})
});

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