Split up listen address configuration between IPv4 and IPv6

2445b6d9dc11fe8291d4db773d0b3f7233c1f4be

Kristan Schlikow <kristian@schlikow.de>

Signed
2 files changed, +23 -15Ignore whitespace
default/config.yaml+2 -1
@@ -7,7 +7,8 @@ cardsCacheCapacity: 100
77# Listen for incoming connections
88listen: false
99# Listen on a specific address, supports IPv4 and IPv6
10-listenAddress: 0.0.0.0
10+listenAddressIPv6: [::]
11+listenAddressIPv4: 0.0.0.0
1112# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
1213# - Use option "auto" to automatically detect support
1314# - Use true or false (no qoutes) to enable or disable each protocol
server.js+21 -14
@@ -131,7 +131,8 @@ if (process.versions && process.versions.node && process.versions.node.match(/20
131131const DEFAULT_PORT = 8000;
132132const DEFAULT_AUTORUN = false;
133133const DEFAULT_LISTEN = false;
134134const DEFAULT_LISTEN_ADDRESSDEFAULT_LISTEN_ADDRESS_IPV6 = '[::]';
135+const DEFAULT_LISTEN_ADDRESS_IPV4 = '0.0.0.0';
135136const DEFAULT_CORS_PROXY = false;
136137const DEFAULT_WHITELIST = true;
137138const DEFAULT_ACCOUNTS = false;
@@ -187,10 +188,14 @@ const cliArguments = yargs(hideBin(process.argv))
187188 type: 'boolean',
188189 default: null,
189190 describe: `SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). If false, will limit it only to internal localhost (127.0.0.1).\nIf not provided falls back to yaml config 'listen'.\n[config default: ${DEFAULT_LISTEN}]`,
190191 }).option('listenAddresslistenAddressIPv6', {
191192 type: 'string',
192193 default: null,
193194 describe: 'Set SillyTavern to listen to a specific IPv6 address. If not set, it will fallback to listen to all.\n[config default: empty[::] ]',
195+ }).option('listenAddressIPv4', {
196+ type: 'string',
197+ default: null,
198+ describe: 'Set SillyTavern to listen to a specific IPv4 address. If not set, it will fallback to listen to all.\n[config default: 0.0.0.0 ]',
194199 }).option('corsProxy', {
195200 type: 'boolean',
196201 default: null,
@@ -261,7 +266,9 @@ const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTOR
261266/** @type {boolean} */
262267const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN);
263268/** @type {string} */
264269const listenAddresslistenAddressIPv6 = cliArguments.listenAddresslistenAddressIPv6 ?? getConfigValue('listenAddresslistenAddressIPv6', DEFAULT_LISTEN_ADDRESSDEFAULT_LISTEN_ADDRESS_IPV6);
270+/** @type {string} */
271+const listenAddressIPv4 = cliArguments.listenAddressIPv4 ?? getConfigValue('listenAddressIPv4', DEFAULT_LISTEN_ADDRESS_IPV4);
265272/** @type {boolean} */
266273const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY);
267274const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST);
@@ -716,13 +723,13 @@ app.use('/api/azure', azureRouter);
716723
717724const tavernUrlV6 = new URL(
718725 (cliArguments.ssl ? 'https://' : 'http://') +
719726 (listen ? (ipRegex.v6({ exact: true }).test(listenAddresslistenAddressIPv6) ? `[${listenAddress}]`listenAddressIPv6 : '[::]') : '[::1]') +
720727 (':' + server_port),
721728);
722729
723730const tavernUrl = new URL(
724731 (cliArguments.ssl ? 'https://' : 'http://') +
725732 (listen ? (ipRegex.v4({ exact: true }).test(listenAddresslistenAddressIPv4) ? listenAddresslistenAddressIPv4 : '0.0.0.0') : '127.0.0.1') +
726733 (':' + server_port),
727734);
728735
@@ -789,10 +796,10 @@ const preSetupTasks = async function () {
789796async function getAutorunHostname(useIPv6, useIPv4) {
790797 if (autorunHostname === 'auto') {
791798 if (listen) {
792799 if (ipRegex.v4v6({ exact: true }).test(listenAddresslistenAddressIPv6)) {
793800 return listenAddresslistenAddressIPv6;
794801 } else if (ipRegex.v6v4({ exact: true }).test(listenAddresslistenAddressIPv4)) {
795- return `[${listenAddress}]`;
802+ return listenAddressIPv4;
796803 }
797804 }
798805
@@ -858,13 +865,13 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
858865 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
859866
860867 if (listen) {
861868 if (ipRegex.v4v6({ exact: true }).test(listenAddresslistenAddressIPv6)) {
862869 console.log(
863870 `SillyTavern is listening on the address ${listenAddresslistenAddressIPv6}. If you want to limit it only to internal localhost ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false". Check "access.log" file in the SillyTavern directory if you want to inspect incoming connections.\n`,
864871 );
865872 } else if (ipRegex.v6v4({ exact: true }).test(listenAddresslistenAddressIPv4)) {
866873 console.log(
867874 `SillyTavern is listening on the address [${listenAddresslistenAddressIPv4}]. If you want to limit it only to internal localhost ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false". Check "access.log" file in the SillyTavern directory if you want to inspect incoming connections.\n`,
868875 );
869876 } else {
870877 console.log(