Allow user to configure an address to listen to

83f74a5d225ce8aabb469e4ff8f11e64c9791e75

Kristan Schlikow <kristian@schlikow.de>

Signed
2 files changed, +40 -6Ignore whitespace
default/config.yaml+2 -0
@@ -6,6 +6,8 @@ cardsCacheCapacity: 100
66# -- SERVER CONFIGURATION --
77# Listen for incoming connections
88listen: false
9+# Listen on a specific address, supports IPv4 and IPv6
10+listenAddress: 127.0.0.1
911# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
1012# - Use option "auto" to automatically detect support
1113# - Use true or false (no qoutes) to enable or disable each protocol
server.js+38 -6
@@ -130,6 +130,7 @@ if (process.versions && process.versions.node && process.versions.node.match(/20
130130const DEFAULT_PORT = 8000;
131131const DEFAULT_AUTORUN = false;
132132const DEFAULT_LISTEN = false;
133+const DEFAULT_LISTEN_ADDRESS = '';
133134const DEFAULT_CORS_PROXY = false;
134135const DEFAULT_WHITELIST = true;
135136const DEFAULT_ACCOUNTS = false;
@@ -185,6 +186,10 @@ const cliArguments = yargs(hideBin(process.argv))
185186 type: 'boolean',
186187 default: null,
187188 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}]`,
189+ }).option('listenAddress', {
190+ type: 'string',
191+ default: null,
192+ describe: 'Set SillyTavern to listen to a specific address. If not set, it will fallback to listen to all.\n[config default: empty ]',
188193 }).option('corsProxy', {
189194 type: 'boolean',
190195 default: null,
@@ -254,6 +259,8 @@ const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getCon
254259const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl;
255260/** @type {boolean} */
256261const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN);
262+/** @type {string} */
263+const listenAddress = cliArguments.listenAddress ?? getConfigValue('listenAddress', DEFAULT_LISTEN_ADDRESS);
257264/** @type {boolean} */
258265const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY);
259266const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST);
@@ -706,15 +713,17 @@ app.use('/api/backends/scale-alt', scaleAltRouter);
706713app.use('/api/speech', speechRouter);
707714app.use('/api/azure', azureRouter);
708715
716+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;
709717const tavernUrlV6 = new URL(
710718 (cliArguments.ssl ? 'https://' : 'http://') +
711719 (listen ? (ipv6_regex.test(listenAddress) ? listenAddress : '[::]') : '[::1]') +
712720 (':' + server_port),
713721);
714722
723+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;
715724const tavernUrl = new URL(
716725 (cliArguments.ssl ? 'https://' : 'http://') +
717726 (listen ? (ipv4_regex.test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') +
718727 (':' + server_port),
719728);
720729
@@ -780,6 +789,10 @@ const preSetupTasks = async function () {
780789 */
781790async function getAutorunHostname(useIPv6, useIPv4) {
782791 if (autorunHostname === 'auto') {
792+ if (listen && (ipv4_regex.test(listenAddress) || ipv6_regex.test(listenAddress))) {
793+ return listenAddress;
794+ }
795+
783796 let localhostResolve = await canResolve('localhost', useIPv6, useIPv4);
784797
785798 if (useIPv6 && useIPv4) {
@@ -842,9 +855,15 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
842855 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
843856
844857 if (listen) {
845- console.log(
858+ if (ipv4_regex.test(listenAddress) || ipv6_regex.test(listenAddress)) {
846- '[::] 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',
859+ console.log(
847- );
860+ `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`,
861+ );
862+ } else {
863+ console.log(
864+ '[::] 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',
865+ );
866+ }
848867 }
849868
850869 if (basicAuthMode) {
@@ -909,6 +928,19 @@ function logSecurityAlert(message) {
909928}
910929
911930/**
931+ * Prints a warning message
932+ * @param {string} message The warning message to print
933+ * @returns {void}
934+ */
935+function logSecurityWarning(message) {
936+ if (basicAuthMode || enableWhitelist) return; // safe!
937+ console.error(color.yellow(message));
938+ if (getConfigValue('securityOverride', false)) {
939+ console.warn(color.red('Security has been overridden. If it\'s not a trusted network, change the settings.'));
940+ }
941+}
942+
943+/**
912944 * Handles the case where the server failed to start on one or both protocols.
913945 * @param {boolean} v6Failed If the server failed to start on IPv6
914946 * @param {boolean} v4Failed If the server failed to start on IPv4
@@ -1083,7 +1115,7 @@ async function verifySecuritySettings() {
10831115 }
10841116
10851117 if (!enableAccounts) {
10861118 logSecurityAlert('Your current SillyTavern isconfiguration currentlyis insecurelyinsecure open(listening to the publicnon-localhost). Enable whitelisting, basic authentication or user accounts.');
10871119 }
10881120
10891121 const users = await getAllEnabledUsers();