Split up listen address configuration between IPv4 and IPv6

2445b6d9dc11fe8291d4db773d0b3f7233c1f4be

Kristan Schlikow <kristian@schlikow.de>

Signed
2 files changed, +23 -15Showing whitespace changes
default/config.yaml+2 -1
@@ -7,7 +7,8 @@ cardsCacheCapacity: 100
7# Listen for incoming connections7# Listen for incoming connections
8listen: false8listen: false
9# Listen on a specific address, supports IPv4 and IPv69# Listen on a specific address, supports IPv4 and IPv6
10listenAddress: 0.0.0.010listenAddressIPv6: [::]
11listenAddressIPv4: 0.0.0.0
11# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!12# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
12# - Use option "auto" to automatically detect support13# - Use option "auto" to automatically detect support
13# - Use true or false (no qoutes) to enable or disable each protocol14# - 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
131const DEFAULT_PORT = 8000;131const DEFAULT_PORT = 8000;
132const DEFAULT_AUTORUN = false;132const DEFAULT_AUTORUN = false;
133const DEFAULT_LISTEN = false;133const DEFAULT_LISTEN = false;
134const DEFAULT_LISTEN_ADDRESS = '';134const DEFAULT_LISTEN_ADDRESS_IPV6 = '[::]';
135const DEFAULT_LISTEN_ADDRESS_IPV4 = '0.0.0.0';
135const DEFAULT_CORS_PROXY = false;136const DEFAULT_CORS_PROXY = false;
136const DEFAULT_WHITELIST = true;137const DEFAULT_WHITELIST = true;
137const DEFAULT_ACCOUNTS = false;138const DEFAULT_ACCOUNTS = false;
@@ -187,10 +188,14 @@ const cliArguments = yargs(hideBin(process.argv))
187 type: 'boolean',188 type: 'boolean',
188 default: null,189 default: null,
189 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}]`,190 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}]`,
190 }).option('listenAddress', {191 }).option('listenAddressIPv6', {
191 type: 'string',192 type: 'string',
192 default: null,193 default: null,
193 describe: 'Set SillyTavern to listen to a specific address. If not set, it will fallback to listen to all.\n[config default: empty ]',194 describe: 'Set SillyTavern to listen to a specific IPv6 address. If not set, it will fallback to listen to all.\n[config default: [::] ]',
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 ]',
194 }).option('corsProxy', {199 }).option('corsProxy', {
195 type: 'boolean',200 type: 'boolean',
196 default: null,201 default: null,
@@ -261,7 +266,9 @@ const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTOR
261/** @type {boolean} */266/** @type {boolean} */
262const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN);267const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN);
263/** @type {string} */268/** @type {string} */
264const listenAddress = cliArguments.listenAddress ?? getConfigValue('listenAddress', DEFAULT_LISTEN_ADDRESS);269const listenAddressIPv6 = cliArguments.listenAddressIPv6 ?? getConfigValue('listenAddressIPv6', DEFAULT_LISTEN_ADDRESS_IPV6);
270/** @type {string} */
271const listenAddressIPv4 = cliArguments.listenAddressIPv4 ?? getConfigValue('listenAddressIPv4', DEFAULT_LISTEN_ADDRESS_IPV4);
265/** @type {boolean} */272/** @type {boolean} */
266const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY);273const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY);
267const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST);274const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST);
@@ -716,13 +723,13 @@ app.use('/api/azure', azureRouter);
716723
717const tavernUrlV6 = new URL(724const tavernUrlV6 = new URL(
718 (cliArguments.ssl ? 'https://' : 'http://') +725 (cliArguments.ssl ? 'https://' : 'http://') +
719 (listen ? (ipRegex.v6({ exact: true }).test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') +726 (listen ? (ipRegex.v6({ exact: true }).test(listenAddressIPv6) ? listenAddressIPv6 : '[::]') : '[::1]') +
720 (':' + server_port),727 (':' + server_port),
721);728);
722729
723const tavernUrl = new URL(730const tavernUrl = new URL(
724 (cliArguments.ssl ? 'https://' : 'http://') +731 (cliArguments.ssl ? 'https://' : 'http://') +
725 (listen ? (ipRegex.v4({ exact: true }).test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') +732 (listen ? (ipRegex.v4({ exact: true }).test(listenAddressIPv4) ? listenAddressIPv4 : '0.0.0.0') : '127.0.0.1') +
726 (':' + server_port),733 (':' + server_port),
727);734);
728735
@@ -789,10 +796,10 @@ const preSetupTasks = async function () {
789async function getAutorunHostname(useIPv6, useIPv4) {796async function getAutorunHostname(useIPv6, useIPv4) {
790 if (autorunHostname === 'auto') {797 if (autorunHostname === 'auto') {
791 if (listen) {798 if (listen) {
792 if (ipRegex.v4({ exact: true }).test(listenAddress)) {799 if (ipRegex.v6({ exact: true }).test(listenAddressIPv6)) {
793 return listenAddress;800 return listenAddressIPv6;
794 } else if (ipRegex.v6({ exact: true }).test(listenAddress)) {801 } else if (ipRegex.v4({ exact: true }).test(listenAddressIPv4)) {
795 return `[${listenAddress}]`;802 return listenAddressIPv4;
796 }803 }
797 }804 }
798805
@@ -858,13 +865,13 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
858 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');865 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
859866
860 if (listen) {867 if (listen) {
861 if (ipRegex.v4({ exact: true }).test(listenAddress)) {868 if (ipRegex.v6({ exact: true }).test(listenAddressIPv6)) {
862 console.log(869 console.log(
863 `SillyTavern is listening on the address ${listenAddress}. 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`,870 `SillyTavern is listening on the address ${listenAddressIPv6}. 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`,
864 );871 );
865 } else if (ipRegex.v6({ exact: true }).test(listenAddress)) {872 } else if (ipRegex.v4({ exact: true }).test(listenAddressIPv4)) {
866 console.log(873 console.log(
867 `SillyTavern is listening on the address [${listenAddress}]. 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`,874 `SillyTavern is listening on the address ${listenAddressIPv4}. 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`,
868 );875 );
869 } else {876 } else {
870 console.log(877 console.log(