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",
"csrf-csrf": "^2.2.3",
"express": "^4.18.2",
"ipaddr.js": "^2.0.1",
"multer": "^1.4.5-lts.1",
"node-rest-client": "^3.1.1",
"open": "^8.4.0",
@@ -715,11 +716,11 @@
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew=="
},
"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==",
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.0.1.tgz",
"integrity": "sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng==",
"engines": {
"node": ">= 0.10"
"node": ">= 10"
}
},
"node_modules/is-arrayish": {
@@ -1065,6 +1066,14 @@
"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": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
@@ -2052,9 +2061,9 @@
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew=="
},
"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=="
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.0.1.tgz",
"integrity": "sha512-1qTgH9NG+IIJ4yfKs2e6Pp1bZg8wbDbKHT21HrLIeYBTRLgMYKnMTPAuI3Lcs61nfx5h1xlXnbJtH1kX5/d/ng=="
},
"is-arrayish": {
"version": "0.3.2",
@@ -2307,6 +2316,13 @@
"requires": {
"forwarded": "0.2.0",
"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": {

View File

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

View File

@@ -18,7 +18,7 @@ const path = require('path');
const cookieParser = require('cookie-parser');
const crypto = require('crypto');
const ipaddr = require('ipaddr.js');
const config = require(path.join(process.cwd(), './config.conf'));
const server_port = config.port;
@@ -26,6 +26,8 @@ const whitelist = config.whitelist;
const whitelistMode = config.whitelistMode;
const autorun = config.autorun;
var Client = require('node-rest-client').Client;
var client = new Client();
@@ -96,7 +98,17 @@ const CORS = cors({
app.use(CORS);
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)) {
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.');