ip whitelist check fix

This commit is contained in:
Ash
2023-02-17 02:58:07 +04:00
parent 7b9837fd06
commit 6cf2d95144
3 changed files with 57 additions and 29 deletions

30
package-lock.json generated
View File

@@ -12,6 +12,7 @@
"cors": "^2.8.5", "cors": "^2.8.5",
"csrf-csrf": "^2.2.3", "csrf-csrf": "^2.2.3",
"express": "^4.18.2", "express": "^4.18.2",
"ipaddr.js": "^2.0.1",
"multer": "^1.4.5-lts.1", "multer": "^1.4.5-lts.1",
"node-rest-client": "^3.1.1", "node-rest-client": "^3.1.1",
"open": "^8.4.0", "open": "^8.4.0",
@@ -715,11 +716,11 @@
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew=="
}, },
"node_modules/ipaddr.js": { "node_modules/ipaddr.js": {
"version": "1.9.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.0.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", "integrity": "sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng==",
"engines": { "engines": {
"node": ">= 0.10" "node": ">= 10"
} }
}, },
"node_modules/is-arrayish": { "node_modules/is-arrayish": {
@@ -1065,6 +1066,14 @@
"node": ">= 0.10" "node": ">= 0.10"
} }
}, },
"node_modules/proxy-addr/node_modules/ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/pump": { "node_modules/pump": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
@@ -2052,9 +2061,9 @@
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew==" "integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew=="
}, },
"ipaddr.js": { "ipaddr.js": {
"version": "1.9.1", "version": "2.0.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.0.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" "integrity": "sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng=="
}, },
"is-arrayish": { "is-arrayish": {
"version": "0.3.2", "version": "0.3.2",
@@ -2307,6 +2316,13 @@
"requires": { "requires": {
"forwarded": "0.2.0", "forwarded": "0.2.0",
"ipaddr.js": "1.9.1" "ipaddr.js": "1.9.1"
},
"dependencies": {
"ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g=="
}
} }
}, },
"pump": { "pump": {

View File

@@ -1,30 +1,30 @@
{ {
"dependencies": { "dependencies": {
"express": "^4.18.2", "cookie-parser": "^1.4.6",
"multer": "^1.4.5-lts.1", "cors": "^2.8.5",
"node-rest-client": "^3.1.1", "csrf-csrf": "^2.2.3",
"open": "^8.4.0", "express": "^4.18.2",
"png-chunk-text": "^1.0.0", "ipaddr.js": "^2.0.1",
"png-chunks-encode": "^1.0.0", "multer": "^1.4.5-lts.1",
"png-chunks-extract": "^1.0.0", "node-rest-client": "^3.1.1",
"rimraf": "^3.0.2", "open": "^8.4.0",
"sharp": "^0.31.3", "png-chunk-text": "^1.0.0",
"csrf-csrf": "^2.2.3", "png-chunks-encode": "^1.0.0",
"cookie-parser": "^1.4.6", "png-chunks-extract": "^1.0.0",
"cors": "^2.8.5" "rimraf": "^3.0.2",
"sharp": "^0.31.3"
}, },
"name": "TavernAI", "name": "TavernAI",
"version": "1.2.8", "version": "1.2.8",
"bin": { "bin": {
"TavernAI": "server.js" "TavernAI": "server.js"
}, },
"rules":{ "rules": {
"no-path-concat":"off", "no-path-concat": "off",
"no-var":"off" "no-var": "off"
}, },
"pkg":{ "pkg": {
"assets":[ "assets": [
"node_modules/sharp/build/Release/**/*", "node_modules/sharp/build/Release/**/*",
"node_modules/sharp/vendor/lib/**/*", "node_modules/sharp/vendor/lib/**/*",
"node_modules/open/xdg-open/", "node_modules/open/xdg-open/",

View File

@@ -18,7 +18,7 @@ const path = require('path');
const cookieParser = require('cookie-parser'); const cookieParser = require('cookie-parser');
const crypto = require('crypto'); const crypto = require('crypto');
const ipaddr = require('ipaddr.js');
const config = require(path.join(process.cwd(), './config.conf')); const config = require(path.join(process.cwd(), './config.conf'));
const server_port = config.port; const server_port = config.port;
@@ -26,6 +26,8 @@ const whitelist = config.whitelist;
const whitelistMode = config.whitelistMode; const whitelistMode = config.whitelistMode;
const autorun = config.autorun; const autorun = config.autorun;
var Client = require('node-rest-client').Client; var Client = require('node-rest-client').Client;
var client = new Client(); var client = new Client();
@@ -96,7 +98,17 @@ const CORS = cors({
app.use(CORS); app.use(CORS);
app.use(function (req, res, next) { //Security app.use(function (req, res, next) { //Security
const clientIp = req.connection.remoteAddress.split(':').pop(); let clientIp = req.connection.remoteAddress;
const ip = ipaddr.parse(clientIp);
// Check if the IP address is IPv4-mapped IPv6 address
if (ip.kind() === 'ipv6' && ip.isIPv4MappedAddress()) {
const ipv4 = ip.toIPv4Address().toString();
clientIp = ipv4;
} else {
clientIp = ip;
}
//clientIp = req.connection.remoteAddress.split(':').pop();
if (whitelistMode === true && !whitelist.includes(clientIp)) { if (whitelistMode === true && !whitelist.includes(clientIp)) {
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 TavernAI folder.\n'); 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 TavernAI 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 TavernAI folder.'); 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 TavernAI folder.');