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
77# Listen for incoming connections
88listen: false
99# Listen on a specific address, supports IPv4 and IPv6
1010listenAddress: 1270.0.0.10
1111# Enables IPv6 and/or IPv4 protocols. Need to have at least one enabled!
1212# - Use option "auto" to automatically detect support
1313# - 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+1 -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",
server.js+7 -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;
@@ -713,17 +714,15 @@ app.use('/api/backends/scale-alt', scaleAltRouter);
713714app.use('/api/speech', speechRouter);
714715app.use('/api/azure', azureRouter);
715716
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;
717717const tavernUrlV6 = new URL(
718718 (cliArguments.ssl ? 'https://' : 'http://') +
719719 (listen ? (ipv6_regexipRegex.v6({ exact: true }).test(listenAddress) ? `[${listenAddress}]` : '[::]') : '[::1]') +
720720 (':' + server_port),
721721);
722722
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;
724723const tavernUrl = new URL(
725724 (cliArguments.ssl ? 'https://' : 'http://') +
726725 (listen ? (ipv4_regexipRegex.v4({ exact: true }).test(listenAddress) ? listenAddress : '0.0.0.0') : '127.0.0.1') +
727726 (':' + server_port),
728727);
729728
@@ -790,9 +789,9 @@ const preSetupTasks = async function () {
790789async function getAutorunHostname(useIPv6, useIPv4) {
791790 if (autorunHostname === 'auto') {
792791 if (listen) {
793792 if (ipv4_regexipRegex.v4({ exact: true }).test(listenAddress)) {
794793 return listenAddress;
795794 } else if (ipv6_regexipRegex.v6({ exact: true }).test(listenAddress)) {
796795 return `[${listenAddress}]`;
797796 }
798797 }
@@ -859,11 +858,11 @@ const postSetupTasks = async function (v6Failed, v4Failed, useIPv6, useIPv4) {
859858 console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
860859
861860 if (listen) {
862861 if (ipv4_regexipRegex.v4({ exact: true }).test(listenAddress)) {
863862 console.log(
864863 `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`,
865864 );
866865 } else if (ipv6_regexipRegex.v6({ exact: true }).test(listenAddress)) {
867866 console.log(
868867 `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`,
869868 );