Add wildcard IP whitelist matching

This commit is contained in:
SillyLossy
2023-05-16 19:01:56 +03:00
parent d5213b7d4e
commit 08d02117c5
3 changed files with 9 additions and 1 deletions

6
package-lock.json generated
View File

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

View File

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

View File

@@ -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('<b>Forbidden</b>: Connection attempt from <b>' + clientIp + '</b>. 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.');
}