From 83f74a5d225ce8aabb469e4ff8f11e64c9791e75 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Thu, 13 Feb 2025 23:43:10 +0100 Subject: [PATCH 01/11] Allow user to configure an address to listen to --- default/config.yaml | 2 ++ server.js | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/default/config.yaml b/default/config.yaml index 94673ca11..813820035 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -6,6 +6,8 @@ cardsCacheCapacity: 100 # -- SERVER CONFIGURATION -- # Listen for incoming connections listen: false +# Listen on a specific address, supports IPv4 and IPv6 +listenAddress: 127.0.0.1 # Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled! # - Use option "auto" to automatically detect support # - Use true or false (no qoutes) to enable or disable each protocol diff --git a/server.js b/server.js index e62562e56..12b72bc69 100644 --- a/server.js +++ b/server.js @@ -130,6 +130,7 @@ if (process.versions && process.versions.node && process.versions.node.match(/20 const DEFAULT_PORT = 8000; const DEFAULT_AUTORUN = false; const DEFAULT_LISTEN = false; +const DEFAULT_LISTEN_ADDRESS = ''; const DEFAULT_CORS_PROXY = false; const DEFAULT_WHITELIST = true; const DEFAULT_ACCOUNTS = false; @@ -185,6 +186,10 @@ const cliArguments = yargs(hideBin(process.argv)) type: 'boolean', default: null, 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}]`, + }).option('listenAddress', { + type: 'string', + default: null, + describe: 'Set SillyTavern to listen to a specific address. If not set, it will fallback to listen to all.\n[config default: empty ]', }).option('corsProxy', { type: 'boolean', default: null, @@ -254,6 +259,8 @@ const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getCon const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl; /** @type {boolean} */ const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN); +/** @type {string} */ +const listenAddress = cliArguments.listenAddress ?? getConfigValue('listenAddress', DEFAULT_LISTEN_ADDRESS); /** @type {boolean} */ const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY); const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST); @@ -706,15 +713,17 @@ app.use('/api/backends/scale-alt', scaleAltRouter); app.use('/api/speech', speechRouter); app.use('/api/azure', azureRouter); +const ipv6_regex = /^(?:(?:[a-fA-F\d]{1,4}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,2}|:)|(?:[a-fA-F\d]{1,4}:){4}(?:(?::[a-fA-F\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,3}|:)|(?:[a-fA-F\d]{1,4}:){3}(?:(?::[a-fA-F\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,4}|:)|(?:[a-fA-F\d]{1,4}:){2}(?:(?::[a-fA-F\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,5}|:)|(?:[a-fA-F\d]{1,4}:){1}(?:(?::[a-fA-F\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,6}|:)|(?::(?:(?::[a-fA-F\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$/m; const tavernUrlV6 = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? '[::]' : '[::1]') + + (listen ? (ipv6_regex.test(listenAddress) ? listenAddress : '[::]') : '[::1]') + (':' + server_port), ); +const ipv4_regex = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/m; const tavernUrl = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? '0.0.0.0' : '127.0.0.1') + + (listen ? (ipv4_regex.test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') + (':' + server_port), ); @@ -780,6 +789,10 @@ const preSetupTasks = async function () { */ async function getAutorunHostname(useIPv6, useIPv4) { if (autorunHostname === 'auto') { + if (listen && (ipv4_regex.test(listenAddress) || ipv6_regex.test(listenAddress))) { + return listenAddress; + } + let localhostResolve = await canResolve('localhost', useIPv6, useIPv4); if (useIPv6 && useIPv4) { @@ -842,9 +855,15 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); if (listen) { - console.log( - '[::] or 0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). 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', - ); + if (ipv4_regex.test(listenAddress) || ipv6_regex.test(listenAddress)) { + console.log( + `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`, + ); + } else { + console.log( + '[::] or 0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). 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', + ); + } } if (basicAuthMode) { @@ -908,6 +927,19 @@ function logSecurityAlert(message) { process.exit(1); } +/** + * Prints a warning message + * @param {string} message The warning message to print + * @returns {void} + */ +function logSecurityWarning(message) { + if (basicAuthMode || enableWhitelist) return; // safe! + console.error(color.yellow(message)); + if (getConfigValue('securityOverride', false)) { + console.warn(color.red('Security has been overridden. If it\'s not a trusted network, change the settings.')); + } +} + /** * Handles the case where the server failed to start on one or both protocols. * @param {boolean} v6Failed If the server failed to start on IPv6 @@ -1083,7 +1115,7 @@ async function verifySecuritySettings() { } if (!enableAccounts) { - logSecurityAlert('Your SillyTavern is currently insecurely open to the public. Enable whitelisting, basic authentication or user accounts.'); + logSecurityAlert('Your current SillyTavern configuration is insecure (listening to non-localhost). Enable whitelisting, basic authentication or user accounts.'); } const users = await getAllEnabledUsers(); From dd55b2770a8203ebfa52f76cb5a963e69be37127 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Thu, 13 Feb 2025 23:51:18 +0100 Subject: [PATCH 02/11] Address issues with IPv6 binding --- server.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/server.js b/server.js index 12b72bc69..5a87d77c9 100644 --- a/server.js +++ b/server.js @@ -716,7 +716,7 @@ app.use('/api/azure', azureRouter); const ipv6_regex = /^(?:(?:[a-fA-F\d]{1,4}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,2}|:)|(?:[a-fA-F\d]{1,4}:){4}(?:(?::[a-fA-F\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,3}|:)|(?:[a-fA-F\d]{1,4}:){3}(?:(?::[a-fA-F\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,4}|:)|(?:[a-fA-F\d]{1,4}:){2}(?:(?::[a-fA-F\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,5}|:)|(?:[a-fA-F\d]{1,4}:){1}(?:(?::[a-fA-F\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,6}|:)|(?::(?:(?::[a-fA-F\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$/m; const tavernUrlV6 = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? (ipv6_regex.test(listenAddress) ? listenAddress : '[::]') : '[::1]') + + (listen ? (ipv6_regex.test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') + (':' + server_port), ); @@ -789,8 +789,12 @@ const preSetupTasks = async function () { */ async function getAutorunHostname(useIPv6, useIPv4) { if (autorunHostname === 'auto') { - if (listen && (ipv4_regex.test(listenAddress) || ipv6_regex.test(listenAddress))) { - return listenAddress; + if (listen) { + if (ipv4_regex.test(listenAddress)) { + return listenAddress; + } else if (ipv6_regex.test(listenAddress)) { + return `[${listenAddress}]`; + } } let localhostResolve = await canResolve('localhost', useIPv6, useIPv4); @@ -855,10 +859,14 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); if (listen) { - if (ipv4_regex.test(listenAddress) || ipv6_regex.test(listenAddress)) { + if (ipv4_regex.test(listenAddress)) { console.log( `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`, ); + } else if (ipv6_regex.test(listenAddress)) { + console.log( + `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`, + ); } else { console.log( '[::] or 0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). 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', From ad8f0f564f238937f02933eda2c0f78377734536 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 19:06:07 +0100 Subject: [PATCH 03/11] Use IP Regex package, update default --- default/config.yaml | 2 +- package-lock.json | 13 +++++++++++++ package.json | 1 + server.js | 15 +++++++-------- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/default/config.yaml b/default/config.yaml index 813820035..9c37e9bf0 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -7,7 +7,7 @@ cardsCacheCapacity: 100 # Listen for incoming connections listen: false # Listen on a specific address, supports IPv4 and IPv6 -listenAddress: 127.0.0.1 +listenAddress: 0.0.0.0 # Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled! # - Use option "auto" to automatically detect support # - Use true or false (no qoutes) to enable or disable each protocol diff --git a/package-lock.json b/package-lock.json index ddbffd0f1..3c5615536 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,6 +41,7 @@ "html-entities": "^2.5.2", "iconv-lite": "^0.6.3", "ip-matching": "^2.1.2", + "ip-regex": "^5.0.0", "ipaddr.js": "^2.0.1", "jimp": "^0.22.10", "localforage": "^1.10.0", @@ -4628,6 +4629,18 @@ "integrity": "sha512-/ok+VhKMasgR5gvTRViwRFQfc0qYt9Vdowg6TO4/pFlDCob5ZjGPkwuOoQVCd5OrMm20zqh+1vA8KLJZTeWudg==", "license": "LGPL-3.0-only" }, + "node_modules/ip-regex": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/ip-regex/-/ip-regex-5.0.0.tgz", + "integrity": "sha512-fOCG6lhoKKakwv+C6KdsOnGvgXnmgfmp0myi3bcNwj3qfwPAxRKWEuFhvEFF7ceYIz6+1jRZ+yguLFAmUNPEfw==", + "license": "MIT", + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/ipaddr.js": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.1.0.tgz", diff --git a/package.json b/package.json index 7a29b2c6c..6f687a384 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "html-entities": "^2.5.2", "iconv-lite": "^0.6.3", "ip-matching": "^2.1.2", + "ip-regex": "^5.0.0", "ipaddr.js": "^2.0.1", "jimp": "^0.22.10", "localforage": "^1.10.0", diff --git a/server.js b/server.js index 5a87d77c9..ac146bc2d 100644 --- a/server.js +++ b/server.js @@ -30,6 +30,7 @@ import bodyParser from 'body-parser'; // net related library imports import fetch from 'node-fetch'; +import ipRegex from 'ip-regex'; // Unrestrict console logs display limit util.inspect.defaultOptions.maxArrayLength = null; @@ -713,17 +714,15 @@ app.use('/api/backends/scale-alt', scaleAltRouter); app.use('/api/speech', speechRouter); app.use('/api/azure', azureRouter); -const ipv6_regex = /^(?:(?:[a-fA-F\d]{1,4}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,2}|:)|(?:[a-fA-F\d]{1,4}:){4}(?:(?::[a-fA-F\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,3}|:)|(?:[a-fA-F\d]{1,4}:){3}(?:(?::[a-fA-F\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,4}|:)|(?:[a-fA-F\d]{1,4}:){2}(?:(?::[a-fA-F\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,5}|:)|(?:[a-fA-F\d]{1,4}:){1}(?:(?::[a-fA-F\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,6}|:)|(?::(?:(?::[a-fA-F\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$/m; const tavernUrlV6 = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? (ipv6_regex.test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') + + (listen ? (ipRegex.v6({ exact: true }).test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') + (':' + server_port), ); -const ipv4_regex = /^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/m; const tavernUrl = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? (ipv4_regex.test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') + + (listen ? (ipRegex.v4({ exact: true }).test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') + (':' + server_port), ); @@ -790,9 +789,9 @@ const preSetupTasks = async function () { async function getAutorunHostname(useIPv6, useIPv4) { if (autorunHostname === 'auto') { if (listen) { - if (ipv4_regex.test(listenAddress)) { + if (ipRegex.v4({ exact: true }).test(listenAddress)) { return listenAddress; - } else if (ipv6_regex.test(listenAddress)) { + } else if (ipRegex.v6({ exact: true }).test(listenAddress)) { return `[${listenAddress}]`; } } @@ -859,11 +858,11 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); if (listen) { - if (ipv4_regex.test(listenAddress)) { + if (ipRegex.v4({ exact: true }).test(listenAddress)) { console.log( `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`, ); - } else if (ipv6_regex.test(listenAddress)) { + } else if (ipRegex.v6({ exact: true }).test(listenAddress)) { console.log( `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`, ); From 2445b6d9dc11fe8291d4db773d0b3f7233c1f4be Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 19:58:59 +0100 Subject: [PATCH 04/11] Split up listen address configuration between IPv4 and IPv6 --- default/config.yaml | 3 ++- server.js | 35 +++++++++++++++++++++-------------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/default/config.yaml b/default/config.yaml index 9c37e9bf0..c84251b16 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -7,7 +7,8 @@ cardsCacheCapacity: 100 # Listen for incoming connections listen: false # Listen on a specific address, supports IPv4 and IPv6 -listenAddress: 0.0.0.0 +listenAddressIPv6: [::] +listenAddressIPv4: 0.0.0.0 # Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled! # - Use option "auto" to automatically detect support # - Use true or false (no qoutes) to enable or disable each protocol diff --git a/server.js b/server.js index ac146bc2d..61ead966d 100644 --- a/server.js +++ b/server.js @@ -131,7 +131,8 @@ if (process.versions && process.versions.node && process.versions.node.match(/20 const DEFAULT_PORT = 8000; const DEFAULT_AUTORUN = false; const DEFAULT_LISTEN = false; -const DEFAULT_LISTEN_ADDRESS = ''; +const DEFAULT_LISTEN_ADDRESS_IPV6 = '[::]'; +const DEFAULT_LISTEN_ADDRESS_IPV4 = '0.0.0.0'; const DEFAULT_CORS_PROXY = false; const DEFAULT_WHITELIST = true; const DEFAULT_ACCOUNTS = false; @@ -187,10 +188,14 @@ const cliArguments = yargs(hideBin(process.argv)) type: 'boolean', default: null, 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}]`, - }).option('listenAddress', { + }).option('listenAddressIPv6', { type: 'string', default: null, - describe: 'Set SillyTavern to listen to a specific address. If not set, it will fallback to listen to all.\n[config default: empty ]', + describe: 'Set SillyTavern to listen to a specific IPv6 address. If not set, it will fallback to listen to all.\n[config default: [::] ]', + }).option('listenAddressIPv4', { + type: 'string', + default: null, + 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 ]', }).option('corsProxy', { type: 'boolean', default: null, @@ -261,7 +266,9 @@ const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTOR /** @type {boolean} */ const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN); /** @type {string} */ -const listenAddress = cliArguments.listenAddress ?? getConfigValue('listenAddress', DEFAULT_LISTEN_ADDRESS); +const listenAddressIPv6 = cliArguments.listenAddressIPv6 ?? getConfigValue('listenAddressIPv6', DEFAULT_LISTEN_ADDRESS_IPV6); +/** @type {string} */ +const listenAddressIPv4 = cliArguments.listenAddressIPv4 ?? getConfigValue('listenAddressIPv4', DEFAULT_LISTEN_ADDRESS_IPV4); /** @type {boolean} */ const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY); const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST); @@ -716,13 +723,13 @@ app.use('/api/azure', azureRouter); const tavernUrlV6 = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? (ipRegex.v6({ exact: true }).test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') + + (listen ? (ipRegex.v6({ exact: true }).test(listenAddressIPv6) ? listenAddressIPv6 : '[::]') : '[::1]') + (':' + server_port), ); const tavernUrl = new URL( (cliArguments.ssl ? 'https://' : 'http://') + - (listen ? (ipRegex.v4({ exact: true }).test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') + + (listen ? (ipRegex.v4({ exact: true }).test(listenAddressIPv4) ? listenAddressIPv4 : '0.0.0.0') : '127.0.0.1') + (':' + server_port), ); @@ -789,10 +796,10 @@ const preSetupTasks = async function () { async function getAutorunHostname(useIPv6, useIPv4) { if (autorunHostname === 'auto') { if (listen) { - if (ipRegex.v4({ exact: true }).test(listenAddress)) { - return listenAddress; - } else if (ipRegex.v6({ exact: true }).test(listenAddress)) { - return `[${listenAddress}]`; + if (ipRegex.v6({ exact: true }).test(listenAddressIPv6)) { + return listenAddressIPv6; + } else if (ipRegex.v4({ exact: true }).test(listenAddressIPv4)) { + return listenAddressIPv4; } } @@ -858,13 +865,13 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); if (listen) { - if (ipRegex.v4({ exact: true }).test(listenAddress)) { + if (ipRegex.v6({ exact: true }).test(listenAddressIPv6)) { console.log( - `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`, + `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`, ); - } else if (ipRegex.v6({ exact: true }).test(listenAddress)) { + } else if (ipRegex.v4({ exact: true }).test(listenAddressIPv4)) { console.log( - `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`, + `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`, ); } else { console.log( From a4c124dff06b3cb143d58d42c73139dd0156e0e8 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 20:00:15 +0100 Subject: [PATCH 05/11] Remove dead code --- server.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/server.js b/server.js index 61ead966d..61c940c09 100644 --- a/server.js +++ b/server.js @@ -941,19 +941,6 @@ function logSecurityAlert(message) { process.exit(1); } -/** - * Prints a warning message - * @param {string} message The warning message to print - * @returns {void} - */ -function logSecurityWarning(message) { - if (basicAuthMode || enableWhitelist) return; // safe! - console.error(color.yellow(message)); - if (getConfigValue('securityOverride', false)) { - console.warn(color.red('Security has been overridden. If it\'s not a trusted network, change the settings.')); - } -} - /** * Handles the case where the server failed to start on one or both protocols. * @param {boolean} v6Failed If the server failed to start on IPv6 From f5bfbce0ad825ffad701a9b4d7c2b1b6c6f4ff97 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 20:04:23 +0100 Subject: [PATCH 06/11] Group listenAddress for config --- default/config.yaml | 5 +++-- server.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/default/config.yaml b/default/config.yaml index c84251b16..d6e31e07d 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -7,8 +7,9 @@ cardsCacheCapacity: 100 # Listen for incoming connections listen: false # Listen on a specific address, supports IPv4 and IPv6 -listenAddressIPv6: [::] -listenAddressIPv4: 0.0.0.0 +listenAddress: + ipv4: 0.0.0.0 + ipv6: [::] # Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled! # - Use option "auto" to automatically detect support # - Use true or false (no qoutes) to enable or disable each protocol diff --git a/server.js b/server.js index 61c940c09..d0eb09187 100644 --- a/server.js +++ b/server.js @@ -266,9 +266,9 @@ const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTOR /** @type {boolean} */ const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN); /** @type {string} */ -const listenAddressIPv6 = cliArguments.listenAddressIPv6 ?? getConfigValue('listenAddressIPv6', DEFAULT_LISTEN_ADDRESS_IPV6); +const listenAddressIPv6 = cliArguments.listenAddressIPv6 ?? getConfigValue('listenAddress.ipv6', DEFAULT_LISTEN_ADDRESS_IPV6); /** @type {string} */ -const listenAddressIPv4 = cliArguments.listenAddressIPv4 ?? getConfigValue('listenAddressIPv4', DEFAULT_LISTEN_ADDRESS_IPV4); +const listenAddressIPv4 = cliArguments.listenAddressIPv4 ?? getConfigValue('listenAddress.ipv4', DEFAULT_LISTEN_ADDRESS_IPV4); /** @type {boolean} */ const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY); const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST); From b029ae98dc06baca3c725e434a846d8f90f3b311 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 20:06:45 +0100 Subject: [PATCH 07/11] Fix default config for IPv6 --- default/config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/default/config.yaml b/default/config.yaml index d6e31e07d..4bd5da802 100644 --- a/default/config.yaml +++ b/default/config.yaml @@ -9,7 +9,7 @@ listen: false # Listen on a specific address, supports IPv4 and IPv6 listenAddress: ipv4: 0.0.0.0 - ipv6: [::] + ipv6: '[::]' # Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled! # - Use option "auto" to automatically detect support # - Use true or false (no qoutes) to enable or disable each protocol From 13e38c7c8648695159fb31e8aa5bbba52ebffc09 Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 21:13:17 +0100 Subject: [PATCH 08/11] Add debug script in package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 6f687a384..fe79108b5 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ "version": "1.12.11", "scripts": { "start": "node server.js", + "debug": "node server.js --inspect", "start:deno": "deno run --allow-run --allow-net --allow-read --allow-write --allow-sys --allow-env server.js", "start:bun": "bun server.js", "start:no-csrf": "node server.js --disableCsrf", From 961a71877b89dc338fae820f1f49436fa66cf96c Mon Sep 17 00:00:00 2001 From: Kristan Schlikow Date: Fri, 14 Feb 2025 22:02:16 +0100 Subject: [PATCH 09/11] Remove extra resolve for autorun --- server.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/server.js b/server.js index d0eb09187..b693f8d6d 100644 --- a/server.js +++ b/server.js @@ -795,14 +795,6 @@ const preSetupTasks = async function () { */ async function getAutorunHostname(useIPv6, useIPv4) { if (autorunHostname === 'auto') { - if (listen) { - if (ipRegex.v6({ exact: true }).test(listenAddressIPv6)) { - return listenAddressIPv6; - } else if (ipRegex.v4({ exact: true }).test(listenAddressIPv4)) { - return listenAddressIPv4; - } - } - let localhostResolve = await canResolve('localhost', useIPv6, useIPv4); if (useIPv6 && useIPv4) { From c98d241f3c93e6b94d4fc66e99739bffebf55738 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 14 Feb 2025 23:09:36 +0200 Subject: [PATCH 10/11] Refactor logAddress check --- server.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/server.js b/server.js index b693f8d6d..fdabe0b80 100644 --- a/server.js +++ b/server.js @@ -857,19 +857,16 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); if (listen) { - if (ipRegex.v6({ exact: true }).test(listenAddressIPv6)) { - console.log( - `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`, - ); - } else if (ipRegex.v4({ exact: true }).test(listenAddressIPv4)) { - console.log( - `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`, - ); - } else { - console.log( - '[::] or 0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). 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', - ); - } + const logAddress = ipRegex.v6({ exact: true }).test(listenAddressIPv6) + ? listenAddressIPv6 + : ipRegex.v4({ exact: true }).test(listenAddressIPv4) + ? listenAddressIPv4 + : null; + console.log( + logAddress + ? `SillyTavern is listening on the address ${logAddress}. 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` + : '[::] or 0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). 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', + ); } if (basicAuthMode) { From dbbf069e85efc8c3e5df005f06aeb8c046c12c64 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 14 Feb 2025 23:53:31 +0200 Subject: [PATCH 11/11] Remove message redundancy --- server.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/server.js b/server.js index fdabe0b80..6f5634f41 100644 --- a/server.js +++ b/server.js @@ -852,22 +852,15 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) { const plainGoToLog = removeColorFormatting(goToLog); console.log(logListen); + if (listen) { + console.log(); + console.log('To limit connections to internal localhost only ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false".'); + console.log('Check the "access.log" file in the SillyTavern directory to inspect incoming connections.'); + } console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); console.log(goToLog); console.log('\n' + getSeparator(plainGoToLog.length) + '\n'); - if (listen) { - const logAddress = ipRegex.v6({ exact: true }).test(listenAddressIPv6) - ? listenAddressIPv6 - : ipRegex.v4({ exact: true }).test(listenAddressIPv4) - ? listenAddressIPv4 - : null; - console.log( - logAddress - ? `SillyTavern is listening on the address ${logAddress}. 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` - : '[::] or 0.0.0.0 means SillyTavern is listening on all network interfaces (Wi-Fi, LAN, localhost). 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', - ); - } if (basicAuthMode) { if (perUserBasicAuth && !enableAccounts) {