diff --git a/package-lock.json b/package-lock.json index 1c4873cbb..bb3573a7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "exifreader": "^4.12.0", "express": "^4.18.2", "gpt3-tokenizer": "^1.1.5", + "ip-matching": "^2.1.2", "ipaddr.js": "^2.0.1", "jimp": "^0.22.7", "jquery": "^3.6.4", @@ -1190,6 +1191,11 @@ "version": "2.0.4", "license": "ISC" }, + "node_modules/ip-matching": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ip-matching/-/ip-matching-2.1.2.tgz", + "integrity": "sha512-/ok+VhKMasgR5gvTRViwRFQfc0qYt9Vdowg6TO4/pFlDCob5ZjGPkwuOoQVCd5OrMm20zqh+1vA8KLJZTeWudg==" + }, "node_modules/ipaddr.js": { "version": "2.0.1", "license": "MIT", diff --git a/package.json b/package.json index 5391cf508..42aaf0150 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "exifreader": "^4.12.0", "express": "^4.18.2", "gpt3-tokenizer": "^1.1.5", + "ip-matching": "^2.1.2", "ipaddr.js": "^2.0.1", "jimp": "^0.22.7", "jquery": "^3.6.4", diff --git a/server.js b/server.js index 7083bb49b..784402968 100644 --- a/server.js +++ b/server.js @@ -86,6 +86,7 @@ const AIHorde = require("./src/horde"); const ai_horde = new AIHorde({ client_agent: getVersion()?.agent || 'SillyTavern:UNKNOWN:Cohee#1207', }); +const ipMatching = require('ip-matching'); var Client = require('node-rest-client').Client; var client = new Client(); @@ -243,7 +244,7 @@ app.use(function (req, res, next) { //Security } //clientIp = req.connection.remoteAddress.split(':').pop(); - if (whitelistMode === true && !whitelist.includes(clientIp)) { + if (whitelistMode === true && !whitelist.some(x => ipMatching.matches(clientIp, ipMatching.getMatch(x)))) { console.log('Forbidden: Connection attempt from ' + clientIp + '. If you are attempting to connect, please add your IP address in whitelist or disable whitelist mode in config.conf in root of SillyTavern folder.\n'); return res.status(403).send('Forbidden: Connection attempt from ' + clientIp + '. If you are attempting to connect, please add your IP address in whitelist or disable whitelist mode in config.conf in root of SillyTavern folder.'); }