Use IP Regex package, update default

ad8f0f564f238937f02933eda2c0f78377734536

Kristan Schlikow <kristian@schlikow.de>

Signed
4 files changed, +22 -9Showing whitespace changes
default/config.yaml+1 -1
@@ -7,7 +7,7 @@ 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: 127.0.0.110listenAddress: 0.0.0.0
11# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!11# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
12# - Use option "auto" to automatically detect support12# - Use option "auto" to automatically detect support
13# - Use true or false (no qoutes) to enable or disable each protocol13# - Use true or false (no qoutes) to enable or disable each protocol
package-lock.json+13 -0
@@ -41,6 +41,7 @@
41 "html-entities": "^2.5.2",41 "html-entities": "^2.5.2",
42 "iconv-lite": "^0.6.3",42 "iconv-lite": "^0.6.3",
43 "ip-matching": "^2.1.2",43 "ip-matching": "^2.1.2",
44 "ip-regex": "^5.0.0",
44 "ipaddr.js": "^2.0.1",45 "ipaddr.js": "^2.0.1",
45 "jimp": "^0.22.10",46 "jimp": "^0.22.10",
46 "localforage": "^1.10.0",47 "localforage": "^1.10.0",
@@ -4628,6 +4629,18 @@
4628 "integrity": "sha512-/ok+VhKMasgR5gvTRViwRFQfc0qYt9Vdowg6TO4/pFlDCob5ZjGPkwuOoQVCd5OrMm20zqh+1vA8KLJZTeWudg==",4629 "integrity": "sha512-/ok+VhKMasgR5gvTRViwRFQfc0qYt9Vdowg6TO4/pFlDCob5ZjGPkwuOoQVCd5OrMm20zqh+1vA8KLJZTeWudg==",
4629 "license": "LGPL-3.0-only"4630 "license": "LGPL-3.0-only"
4630 },4631 },
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 },
4631 "node_modules/ipaddr.js": {4644 "node_modules/ipaddr.js": {
4632 "version": "2.1.0",4645 "version": "2.1.0",
4633 "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.1.0.tgz",4646 "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.1.0.tgz",
package.json+1 -0
@@ -31,6 +31,7 @@
31 "html-entities": "^2.5.2",31 "html-entities": "^2.5.2",
32 "iconv-lite": "^0.6.3",32 "iconv-lite": "^0.6.3",
33 "ip-matching": "^2.1.2",33 "ip-matching": "^2.1.2",
34 "ip-regex": "^5.0.0",
34 "ipaddr.js": "^2.0.1",35 "ipaddr.js": "^2.0.1",
35 "jimp": "^0.22.10",36 "jimp": "^0.22.10",
36 "localforage": "^1.10.0",37 "localforage": "^1.10.0",
server.js+7 -8
@@ -30,6 +30,7 @@ import bodyParser from 'body-parser';
3030
31// net related library imports31// net related library imports
32import fetch from 'node-fetch';32import fetch from 'node-fetch';
33import ipRegex from 'ip-regex';
3334
34// Unrestrict console logs display limit35// Unrestrict console logs display limit
35util.inspect.defaultOptions.maxArrayLength = null;36util.inspect.defaultOptions.maxArrayLength = null;
@@ -713,17 +714,15 @@ app.use('/api/backends/scale-alt', scaleAltRouter);
713app.use('/api/speech', speechRouter);714app.use('/api/speech', speechRouter);
714app.use('/api/azure', azureRouter);715app.use('/api/azure', azureRouter);
715716
716const 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;
717const tavernUrlV6 = new URL(717const tavernUrlV6 = new URL(
718 (cliArguments.ssl ? 'https://' : 'http://') +718 (cliArguments.ssl ? 'https://' : 'http://') +
719 (listen ? (ipv6_regex.test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') +719 (listen ? (ipRegex.v6({ exact: true }).test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') +
720 (':' + server_port),720 (':' + server_port),
721);721);
722722
723const 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;
724const tavernUrl = new URL(723const tavernUrl = new URL(
725 (cliArguments.ssl ? 'https://' : 'http://') +724 (cliArguments.ssl ? 'https://' : 'http://') +
726 (listen ? (ipv4_regex.test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') +725 (listen ? (ipRegex.v4({ exact: true }).test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') +
727 (':' + server_port),726 (':' + server_port),
728);727);
729728
@@ -790,9 +789,9 @@ const preSetupTasks = async function () {
790async function getAutorunHostname(useIPv6, useIPv4) {789async function getAutorunHostname(useIPv6, useIPv4) {
791 if (autorunHostname === 'auto') {790 if (autorunHostname === 'auto') {
792 if (listen) {791 if (listen) {
793 if (ipv4_regex.test(listenAddress)) {792 if (ipRegex.v4({ exact: true }).test(listenAddress)) {
794 return listenAddress;793 return listenAddress;
795 } else if (ipv6_regex.test(listenAddress)) {794 } else if (ipRegex.v6({ exact: true }).test(listenAddress)) {
796 return `[${listenAddress}]`;795 return `[${listenAddress}]`;
797 }796 }
798 }797 }
@@ -859,11 +858,11 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
859 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');858 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
860859
861 if (listen) {860 if (listen) {
862 if (ipv4_regex.test(listenAddress)) {861 if (ipRegex.v4({ exact: true }).test(listenAddress)) {
863 console.log(862 console.log(
864 `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`,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`,
865 );864 );
866 } else if (ipv6_regex.test(listenAddress)) {865 } else if (ipRegex.v6({ exact: true }).test(listenAddress)) {
867 console.log(866 console.log(
868 `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`,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`,
869 );868 );