Merge pull request #3478 from Dakraid/feature/set-listen-ip Feature: Allow user to configure an address to listen to

96d6a6df07a6ba945fc37f9eb5f9a01a8c7ae37b

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
4 files changed, +42 -8Showing whitespace changes
default/config.yaml+4 -0
@@ -6,6 +6,10 @@ cardsCacheCapacity: 100
66# -- SERVER CONFIGURATION --
77# Listen for incoming connections
88listen: false
9+# Listen on a specific address, supports IPv4 and IPv6
10+listenAddress:
11+ ipv4: 0.0.0.0
12+ ipv6: '[::]'
913# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
1014# - Use option "auto" to automatically detect support
1115# - Use true or false (no qoutes) to enable or disable each protocol
package-lock.json+13 -0
@@ -41,6 +41,7 @@
4141 "html-entities": "^2.5.2",
4242 "iconv-lite": "^0.6.3",
4343 "ip-matching": "^2.1.2",
44+ "ip-regex": "^5.0.0",
4445 "ipaddr.js": "^2.0.1",
4546 "jimp": "^0.22.10",
4647 "localforage": "^1.10.0",
@@ -4628,6 +4629,18 @@
46284629 "integrity": "sha512-/ok+VhKMasgR5gvTRViwRFQfc0qYt9Vdowg6TO4/pFlDCob5ZjGPkwuOoQVCd5OrMm20zqh+1vA8KLJZTeWudg==",
46294630 "license": "LGPL-3.0-only"
46304631 },
4632+ "node_modules/ip-regex": {
4633+ "version": "5.0.0",
4634+ "resolved": "https://registry.npmjs.org/ip-regex/-/ip-regex-5.0.0.tgz",
4635+ "integrity": "sha512-fOCG6lhoKKakwv+C6KdsOnGvgXnmgfmp0myi3bcNwj3qfwPAxRKWEuFhvEFF7ceYIz6+1jRZ+yguLFAmUNPEfw==",
4636+ "license": "MIT",
4637+ "engines": {
4638+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
4639+ },
4640+ "funding": {
4641+ "url": "https://github.com/sponsors/sindresorhus"
4642+ }
4643+ },
46314644 "node_modules/ipaddr.js": {
46324645 "version": "2.1.0",
46334646 "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.1.0.tgz",
package.json+2 -0
@@ -31,6 +31,7 @@
3131 "html-entities": "^2.5.2",
3232 "iconv-lite": "^0.6.3",
3333 "ip-matching": "^2.1.2",
34+ "ip-regex": "^5.0.0",
3435 "ipaddr.js": "^2.0.1",
3536 "jimp": "^0.22.10",
3637 "localforage": "^1.10.0",
@@ -89,6 +90,7 @@
8990 "version": "1.12.11",
9091 "scripts": {
9192 "start": "node server.js",
93+ "debug": "node server.js --inspect",
9294 "start:deno": "deno run --allow-run --allow-net --allow-read --allow-write --allow-sys --allow-env server.js",
9395 "start:bun": "bun server.js",
9496 "start:no-csrf": "node server.js --disableCsrf",
server.js+23 -8
@@ -30,6 +30,7 @@ import bodyParser from 'body-parser';
3030
3131// net related library imports
3232import fetch from 'node-fetch';
33+import ipRegex from 'ip-regex';
3334
3435// Unrestrict console logs display limit
3536util.inspect.defaultOptions.maxArrayLength = null;
@@ -130,6 +131,8 @@ if (process.versions && process.versions.node && process.versions.node.match(/20
130131const DEFAULT_PORT = 8000;
131132const DEFAULT_AUTORUN = false;
132133const DEFAULT_LISTEN = false;
134+const DEFAULT_LISTEN_ADDRESS_IPV6 = '[::]';
135+const DEFAULT_LISTEN_ADDRESS_IPV4 = '0.0.0.0';
133136const DEFAULT_CORS_PROXY = false;
134137const DEFAULT_WHITELIST = true;
135138const DEFAULT_ACCOUNTS = false;
@@ -185,6 +188,14 @@ const cliArguments = yargs(hideBin(process.argv))
185188 type: 'boolean',
186189 default: null,
187190 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}]`,
191+ }).option('listenAddressIPv6', {
192+ type: 'string',
193+ default: null,
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 ]',
188199 }).option('corsProxy', {
189200 type: 'boolean',
190201 default: null,
@@ -254,6 +265,10 @@ const server_port = cliArguments.port ?? process.env.SILLY_TAVERN_PORT ?? getCon
254265const autorun = (cliArguments.autorun ?? getConfigValue('autorun', DEFAULT_AUTORUN)) && !cliArguments.ssl;
255266/** @type {boolean} */
256267const listen = cliArguments.listen ?? getConfigValue('listen', DEFAULT_LISTEN);
268+/** @type {string} */
269+const listenAddressIPv6 = cliArguments.listenAddressIPv6 ?? getConfigValue('listenAddress.ipv6', DEFAULT_LISTEN_ADDRESS_IPV6);
270+/** @type {string} */
271+const listenAddressIPv4 = cliArguments.listenAddressIPv4 ?? getConfigValue('listenAddress.ipv4', DEFAULT_LISTEN_ADDRESS_IPV4);
257272/** @type {boolean} */
258273const enableCorsProxy = cliArguments.corsProxy ?? getConfigValue('enableCorsProxy', DEFAULT_CORS_PROXY);
259274const enableWhitelist = cliArguments.whitelist ?? getConfigValue('whitelistMode', DEFAULT_WHITELIST);
@@ -708,13 +723,13 @@ app.use('/api/azure', azureRouter);
708723
709724const tavernUrlV6 = new URL(
710725 (cliArguments.ssl ? 'https://' : 'http://') +
711726 (listen ? (ipRegex.v6({ exact: true }).test(listenAddressIPv6) ? listenAddressIPv6 : '[::]') : '[::1]') +
712727 (':' + server_port),
713728);
714729
715730const tavernUrl = new URL(
716731 (cliArguments.ssl ? 'https://' : 'http://') +
717732 (listen ? (ipRegex.v4({ exact: true }).test(listenAddressIPv4) ? listenAddressIPv4 : '0.0.0.0') : '127.0.0.1') +
718733 (':' + server_port),
719734);
720735
@@ -837,15 +852,15 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
837852 const plainGoToLog = removeColorFormatting(goToLog);
838853
839854 console.log(logListen);
855+ if (listen) {
856+ console.log();
857+ console.log('To limit connections to internal localhost only ([::1] or 127.0.0.1), change the setting in config.yaml to "listen: false".');
858+ console.log('Check the "access.log" file in the SillyTavern directory to inspect incoming connections.');
859+ }
840860 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
841861 console.log(goToLog);
842862 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
843863
844- if (listen) {
845- console.log(
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',
847- );
848- }
849864
850865 if (basicAuthMode) {
851866 if (perUserBasicAuth && !enableAccounts) {
@@ -1083,7 +1098,7 @@ async function verifySecuritySettings() {
10831098 }
10841099
10851100 if (!enableAccounts) {
10861101 logSecurityAlert('Your current SillyTavern isconfiguration currentlyis insecurelyinsecure open(listening to the publicnon-localhost). Enable whitelisting, basic authentication or user accounts.');
10871102 }
10881103
10891104 const users = await getAllEnabledUsers();