Skip to content

Commit d1d0d4d

Browse files
authored
fix: bind composer server to localhost (#9467)
* fix: bind composer server to localhost * Kick CI * Expose host to allow 0.0.0.0 binding * Increase sleep timeout * Adjust host for docker CI as well
1 parent 75054f5 commit d1d0d4d

File tree

12 files changed

+48
-20
lines changed

12 files changed

+48
-20
lines changed

Composer/packages/client/src/components/WebChat/WebChatPanel.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { WebChatHeader } from './WebChatHeader';
2222
import { WebChatComposer } from './WebChatComposer';
2323
import { BotSecret, ChatData, RestartOption } from './types';
2424

25-
const BASEPATH = process.env.PUBLIC_URL || 'http://localhost:3000/';
25+
const BASEPATH = process.env.PUBLIC_URL || `http://${location.hostname}:3000/`;
2626
// TODO: Refactor to include Webchat header component as a part of WebchatComposer to avoid this variable.
2727
const webChatHeaderHeight = '85px';
2828

@@ -67,7 +67,7 @@ export const WebChatPanel: React.FC<WebChatPanelProps> = ({
6767
const conversationServerPort = await conversationService.setUpConversationServer();
6868
try {
6969
// set up Web Chat traffic listener
70-
webChatTrafficChannel.current = new WebSocket(`ws://localhost:${conversationServerPort}/ws/traffic`);
70+
webChatTrafficChannel.current = new WebSocket(`ws://${location.hostname}:${conversationServerPort}/ws/traffic`);
7171
if (webChatTrafficChannel.current) {
7272
webChatTrafficChannel.current.onmessage = (event) => {
7373
const data:

Composer/packages/client/src/components/WebChat/utils/conversationService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class ConversationService {
8080
secret,
8181
domain: `${this.directlineHostUrl}/v3/directline`,
8282
webSocket: true,
83-
streamUrl: `ws://localhost:${this.restServerForWSPort}/ws/conversation/${conversationId}`,
83+
streamUrl: `ws://${location.hostname}:${this.restServerForWSPort}/ws/conversation/${conversationId}`,
8484
});
8585
return directLine;
8686
}

Composer/packages/client/src/constants.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ export const defaultTeamsManifest: TeamsManifest = {
527527
};
528528

529529
export const defaultBotPort = 3979;
530-
export const defaultBotEndpoint = `http://localhost:${defaultBotPort}/api/messages`;
530+
export const defaultBotEndpoint = `http://${location.hostname}:${defaultBotPort}/api/messages`;
531531

532532
const DAYS_IN_MS = 1000 * 60 * 60 * 24;
533533
export const SURVEY_PARAMETERS = {

Composer/packages/electron-server/src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,14 @@ const waitForMainWindowToShow = new Promise((resolve) => {
4141

4242
// webpack dev server runs on :3000
4343
const getBaseUrl = () => {
44+
const host = process.env.COMPOSER_HOST ?? 'localhost';
4445
if (isDevelopment) {
45-
return 'http://localhost:3000/';
46+
return `http://${host}:3000/`;
4647
}
4748
if (!serverPort) {
4849
throw new Error('getBaseUrl() called before serverPort is defined.');
4950
}
50-
return `http://localhost:${serverPort}/`;
51+
return `http://${host}:${serverPort}/`;
5152
};
5253

5354
// set production flag

Composer/packages/server/src/controllers/publisher.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import AssetService from '../services/asset';
1515
import logger from '../logger';
1616
import { LocationRef } from '../models/bot/interface';
1717
import { TelemetryService } from '../services/telemetry';
18+
import { serverListenHost, serverHostname } from '../settings/env';
1819

1920
const log = logger.extend('publisher-controller');
2021

@@ -318,7 +319,7 @@ export const PublishController = {
318319
const pluginMethod = ExtensionContext.extensions.publish[extensionName].methods.setupRuntimeLogServer;
319320
if (typeof pluginMethod === 'function') {
320321
try {
321-
const runtimeLogUrl = await pluginMethod.call(null, projectId);
322+
const runtimeLogUrl = await pluginMethod.call(null, projectId, serverHostname, serverListenHost);
322323
return res.status(200).send(runtimeLogUrl);
323324
} catch (ex) {
324325
res.status(400).json({

Composer/packages/server/src/directline/store/dlServerState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
import { serverHostname } from '../../settings/env';
5+
46
import { BotEndpoint } from './entities/botEndpoint';
57
import { Attachments } from './entities/attachments';
68
import { ConversationSet } from './entities/conversationSet';
@@ -27,7 +29,7 @@ class DLServerContext {
2729
conversations: new ConversationSet(),
2830
endpoints: new EndpointSet(),
2931
attachments: new Attachments(),
30-
serviceUrl: serverPort ? `http://localhost:${serverPort}` : '',
32+
serviceUrl: serverPort ? `http://${serverHostname}:${serverPort}` : '',
3133
dispatchers: {
3234
getDefaultEndpoint: this.getDefaultEndpoint,
3335
updateConversation: this.updateConversation,

Composer/packages/server/src/directline/utils/webSocketServer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
ConversationNetworkTrafficItem,
1414
} from '@botframework-composer/types';
1515

16+
import { serverListenHost } from '../../settings/env';
17+
1618
import log from './logger';
1719

1820
const socketTrafficChannelKey = 'DL_TRAFFIC_SOCKET';
@@ -87,7 +89,7 @@ export class WebSocketServer {
8789
});
8890
this.port = port;
8991
log(`Using ${port} port for directline`);
90-
this.restServer.listen(port);
92+
this.restServer.listen(port, serverListenHost);
9193

9294
app.use('/ws/conversation/:conversationId', (req: express.Request, res: express.Response) => {
9395
if (!(req as any).claimUpgrade) {

Composer/packages/server/src/server.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { mountDirectLineRoutes } from './directline/mountDirectlineRoutes';
3636
import { mountAttachmentRoutes } from './directline/mountAttachmentRoutes';
3737
import { cleanHostedBots } from './utility/cleanHostedBots';
3838
import { getVersion } from './utility/getVersion';
39+
import { serverListenHost, serverHostname } from './settings/env';
3940

4041
// eslint-disable-next-line @typescript-eslint/no-var-requires
4142
const session = require('express-session');
@@ -183,12 +184,14 @@ export async function start(electronContext?: ElectronContext): Promise<number |
183184
});
184185

185186
let server;
186-
await new Promise((resolve) => {
187-
server = app.listen(port, () => {
187+
await new Promise<void>((resolve) => {
188+
server = app.listen(port, serverListenHost, () => {
188189
if (process.env.NODE_ENV === 'production') {
189190
// We don't use the debug logger here because we always want it to be shown.
190191
// eslint-disable-next-line no-console
191-
console.log(`\n\n${chalk.green('Composer now running at:')}\n\n${chalk.blue(`http://localhost:${port}`)}\n`);
192+
console.log(
193+
`\n\n${chalk.green('Composer now running at:')}\n\n${chalk.blue(`http://${serverHostname}:${port}`)}\n`
194+
);
192195
}
193196
resolve();
194197
});

Composer/packages/server/src/settings/env.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import childProcess from 'child_process';
55

66
import { Path } from '../utility/path';
77

8+
export const serverListenHost = process.env.COMPOSER_HOST || 'localhost';
9+
export const serverHostname = serverListenHost === '0.0.0.0' || !serverListenHost ? 'localhost' : serverListenHost;
10+
811
export const absHosted = process.env.COMPOSER_AUTH_PROVIDER === 'abs-h';
912
export const absHostRoot = process.env.WEBSITE_HOSTNAME
1013
? `https://${process.env.WEBSITE_HOSTNAME}`
11-
: 'http://localhost:3978';
14+
: `http://${serverHostname}:3978`;
1215

1316
let folder = process.env.COMPOSER_BOTS_FOLDER;
1417
if (folder?.endsWith(':')) {

Dockerfile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
# before doing yarn install due to yarn workspace symlinking.
77
#
88
################
9-
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-focal as base
9+
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-focal as base
1010
RUN apt update \
1111
&& apt -y install curl dirmngr apt-transport-https lsb-release ca-certificates \
1212
&& curl -sL https://deb.nodesource.com/setup_14.x | bash - \
1313
&& apt install -y nodejs libgomp1 \
14-
&& npm install -g yarn
14+
&& corepack enable \
15+
&& corepack prepare [email protected] --activate \
16+
&& yarn --version
1517

1618
FROM base as build
1719
ARG YARN_ARGS
@@ -32,6 +34,7 @@ RUN yarn build:prod $YARN_ARGS
3234
ENV COMPOSER_REMOTE_EXTENSIONS_DIR "/src/remote-extensions"
3335
ENV COMPOSER_REMOTE_EXTENSION_DATA_DIR "/src/extension-data"
3436
ENV COMPOSER_EXTENSION_MANIFEST "/src/extensions.json"
37+
ENV COMPOSER_HOST="0.0.0.0"
3538
CMD ["yarn","start:server"]
3639

3740
FROM base as composerbasic
@@ -46,7 +49,7 @@ COPY --from=build /src/Composer/packages ./packages
4649
COPY --from=build /src/extensions ../extensions
4750

4851
ENV NODE_ENV "production"
49-
RUN yarn --production --immutable --force $YARN_ARGS && yarn cache clean
52+
RUN yarn install --immutable $YARN_ARGS && yarn cache clean
5053

5154
FROM base
5255
ENV NODE_ENV "production"
@@ -59,4 +62,5 @@ ENV COMPOSER_BUILTIN_EXTENSIONS_DIR "/app/extensions"
5962
ENV COMPOSER_REMOTE_EXTENSIONS_DIR "/app/remote-extensions"
6063
ENV COMPOSER_REMOTE_EXTENSION_DATA_DIR "/app/extension-data"
6164
ENV COMPOSER_EXTENSION_MANIFEST "/app/extensions.json"
65+
ENV COMPOSER_HOST="0.0.0.0"
6266
CMD ["yarn","start:server"]

extensions/localPublish/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,11 @@ class LocalPublisher implements PublishPlugin<PublishConfig> {
243243
}
244244
};
245245

246-
setupRuntimeLogServer = async (projectId: string) => {
246+
setupRuntimeLogServer = async (projectId: string, serverHostname?: string, serverListenHost?: string) => {
247247
await RuntimeLogServer.init({
248248
log: this.composer.log,
249+
hostname: serverHostname,
250+
boundHost: serverListenHost,
249251
});
250252
return RuntimeLogServer.getRuntimeLogStreamingUrl(projectId);
251253
};

extensions/localPublish/src/runtimeLogServer.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,21 @@ export class RuntimeLogServer {
1818
private static servers: WSServer = {};
1919
private static sockets: Record<string, WebSocket> = {};
2020
private static port: number;
21+
private static hostname: string;
2122

2223
public static getRuntimeLogStreamingUrl(projectId: string): string {
23-
return `ws://localhost:${this.port}/ws/runtimeLog/${projectId}`;
24+
return `ws://${this.hostname}:${this.port}/ws/runtimeLog/${projectId}`;
2425
}
2526

26-
public static async init({ log }: { log: Debugger }): Promise<number | void> {
27+
public static async init({
28+
log,
29+
boundHost = 'localhost',
30+
hostname = 'localhost',
31+
}: {
32+
log: Debugger;
33+
boundHost?: string;
34+
hostname?: string;
35+
}): Promise<number | void> {
2736
if (!this.restServer) {
2837
const app = express();
2938
this.restServer = http.createServer(app);
@@ -42,7 +51,7 @@ export class RuntimeLogServer {
4251
return preferredPort;
4352
});
4453
log(`Using ${port} port for runtime-log`);
45-
this.restServer.listen(port);
54+
this.restServer.listen(port, boundHost);
4655

4756
app.use('/ws/runtimeLog/:projectId', (req: Request, res: Response) => {
4857
if (!(req as any).claimUpgrade) {
@@ -74,6 +83,7 @@ export class RuntimeLogServer {
7483
}
7584
});
7685
this.port = port;
86+
this.hostname = hostname;
7787
return this.port;
7888
}
7989
}

0 commit comments

Comments
 (0)