Skip to content

Commit d2b6479

Browse files
committed
Add CLI flags for external client access when hosted
1 parent d6fbe50 commit d2b6479

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

src/clientApp.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ const argv = (() => {
6060
default: defaultPort,
6161
help: `Changes the port. (default: ${defaultPort})`,
6262
});
63+
parser.add_argument('--public_hostname', {
64+
nargs: '?',
65+
type: 'str',
66+
help: 'The hostname that clients can use to access the client; useful when running in a container.',
67+
});
68+
parser.add_argument('--public_port', {
69+
nargs: '?',
70+
type: 'int',
71+
help: 'The port that clients can use to access the client; useful when running in a container.',
72+
});
73+
parser.add_argument('--public_tls', {
74+
action: 'store_true',
75+
default: false,
76+
help: 'Whether the public address should use TLS; useful when running in a container.',
77+
});
78+
parser.add_argument('--use_subdomains', {
79+
action: 'store_true',
80+
default: false,
81+
help: 'Whether the server links should use subdomains off of the public address.',
82+
});
6383
parser.add_argument('--internal_backend', {
6484
nargs: '?',
6585
type: 'str',
@@ -89,6 +109,9 @@ const argv = (() => {
89109
})();
90110

91111
const hostAddress = argv.host === '0.0.0.0' ? localhost : argv.host;
112+
const publicHostAddress = argv.public_hostname ? argv.public_hostname : hostAddress;
113+
const publicPort = argv.public_port ? argv.public_port : argv.port;
114+
const publicProtocol = argv.public_tls ? 'https' : 'http';
92115

93116
const getProxyTarget = (backend: string) =>
94117
argv.internal_backend && backend.includes(localhost) ? argv.internal_backend : backend;
@@ -132,9 +155,15 @@ const koa = new Koa();
132155
const { host, port } = argv;
133156
const server = koa.listen(port, host);
134157
server.on('error', (err) => handleServerError(err, argv.debug));
135-
server.on('listening', () =>
136-
console.log('🌐', chalk.dim('Ready', arrow), chalk.white(`http://${hostAddress}:${port}/`)),
137-
);
158+
server.on('listening', () => {
159+
const hostport = port == 80 ? hostAddress : `${hostAddress}:${port}`;
160+
console.log('🌐', chalk.dim('Ready', arrow), chalk.white(`http://${hostport}/`));
161+
if ((publicHostAddress != hostAddress) || (publicPort != argv.port) ) {
162+
const protocolPort = argv.public_tls ? 443 : 80;
163+
const public_hostport = publicPort == protocolPort ? publicHostAddress : `${publicHostAddress}:${publicPort}`;
164+
console.log('🌐', chalk.dim('Public', arrow), chalk.white(`${publicProtocol}://${public_hostport}/`));
165+
}
166+
});
138167

139168
// Get system path for public files dir
140169
const indexFile = 'index.ejs';
@@ -158,7 +187,8 @@ koa.use(async (ctx, next) => {
158187
koa.use(async (context, next) => {
159188
if (['/', 'index.html'].includes(context.path)) {
160189
const communityPages = getCommunityPages();
161-
let serverList = await getServerListConfig(__dirname, hostAddress, port, argv.server_list);
190+
const useSubdomains = publicHostAddress == 'localhost' || argv.use_subdomains;
191+
let serverList = await getServerListConfig(__dirname, publicProtocol, publicHostAddress, publicPort, useSubdomains, argv.server_list);
162192
await context.render(indexFile, { serverList, communityPages });
163193
}
164194

src/utils/utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function generateScriptTag(func: Function, args: { [key: string]: unknown
7575
/**
7676
* Utility to get the server list configuration.
7777
*/
78-
export async function getServerListConfig(dirname: string, host: string, port: number, serverListPath?: string) {
78+
export async function getServerListConfig(dirname: string, protocol: string, host: string, port: number, useSubdomains: boolean, serverListPath?: string) {
7979
if (!serverListPath) {
8080
const serverListFile = 'server_list.json';
8181
serverListPath = path.join(dirname, `../settings/${serverListFile}`);
@@ -90,12 +90,15 @@ export async function getServerListConfig(dirname: string, host: string, port: n
9090
const serversOfType = serverConfig
9191
.filter((server) => server.type === type)
9292
.map((server) => {
93-
const subdomain = host === 'localhost' && server.subdomain ? `${server.subdomain}.` : '';
93+
const subdomain = useSubdomains && server.subdomain ? `${server.subdomain}.` : '';
9494
const { origin, pathname } = new URL(server.url);
9595
const urlpath = pathname.endsWith('/') ? pathname : `${pathname}/`;
9696

97-
const url = `http://${subdomain}${host}:${port}/(${origin})${urlpath}`;
98-
const api = `http://${host}:${port}/(${origin})${urlpath}api/version`;
97+
const protocolPort = protocol == 'https' ? 443 : 80;
98+
const hostport = port == protocolPort ? host : `${host}:${port}`;
99+
100+
const url = `${protocol}://${subdomain}${hostport}/(${origin})${urlpath}`;
101+
const api = `${protocol}://${hostport}/(${origin})${urlpath}api/version`;
99102
return { ...server, url, api };
100103
});
101104

0 commit comments

Comments
 (0)