Split up listen address configuration between IPv4 and IPv6

This commit is contained in:
Kristan Schlikow 2025-02-14 19:58:59 +01:00
parent ad8f0f564f
commit 2445b6d9dc
No known key found for this signature in database
GPG Key ID: E4FCAC81C405F555
2 changed files with 23 additions and 15 deletions

View File

@ -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

View File

@ -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(