diff --git a/config.json b/config.json new file mode 100644 index 000000000..b24016f3c --- /dev/null +++ b/config.json @@ -0,0 +1,5 @@ +{ + "port": 8000, + "whitelist": ["127.0.0.1"], + "whitelistMode": true +} diff --git a/public/index.html b/public/index.html index 0bf1d3a32..e19a7131c 100644 --- a/public/index.html +++ b/public/index.html @@ -37,7 +37,7 @@ create_date: 0, mes: '\n*You went inside. The air smelled of fried meat, tobacco and a hint of wine. A dim light was cast by candles, and a fire crackled in the fireplace. It seems to be a very pleasant place. Behind the wooden bar is an elf waitress, she is smiling. Her ears are very pointy, and there is a twinkle in her eye. She wears glasses and a white apron. As soon as she noticed you, she immediately came right up close to you.*\n\n' + ' Hello there! How is your evening going?\n' + - '\n@@@TavernAI v'+VERSION+'@@@
Cloud




' + '\n@@@TavernAI v'+VERSION+'@@@
Cloud




' }]; var chat_create_date = 0; diff --git a/server.js b/server.js index 03e97079d..9563a7f0d 100644 --- a/server.js +++ b/server.js @@ -16,11 +16,16 @@ const sharp = require('sharp'); sharp.cache(false); const path = require('path'); +const config = require('./config.json'); +const server_port = config.port; +const whitelist = config.whitelist; +const whitelistMode = config.whitelistMode; + var Client = require('node-rest-client').Client; var client = new Client(); var api_server = "";//"http://127.0.0.1:5000"; -var server_port = 8000; +//var server_port = 8000; var api_novelai = "https://api.novelai.net"; @@ -45,38 +50,17 @@ const jsonParser = express.json({limit: '100mb'}); const urlencodedParser = express.urlencoded({extended: true, limit: '100mb'}); - - -app.post("/getlastversion", jsonParser, function(request, response_getlastversion = response){ - if(!request.body) return response_getlastversion.sendStatus(400); - - const repo = 'TavernAI/TavernAI'; - - const req = https.request({ - hostname: 'github.com', - path: `/${repo}/releases/latest`, - method: 'HEAD' - }, (res) => { - if(res.statusCode === 302) { - const glocation = res.headers.location; - const versionStartIndex = glocation.lastIndexOf('@')+1; - const version = glocation.substring(versionStartIndex); - //console.log(version); - response_getlastversion.send({version: version}); - }else{ - response_getlastversion.send({version: 'error'}); - } - }); - - req.on('error', (error) => { - console.error(error); - response_getlastversion.send({version: 'error'}); - }); - - req.end(); - +app.use(function (req, res, next) { //Security + const 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.json in root of TavernAI folder.\nExample for add several IP in whitelist: "whitelist": ["127.0.0.1", "'+ clientIp+'"]\nExample for disable whitelist mode: "whitelistMode": false'); + 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.json in root of TavernAI folder.
Example for add several IP in whitelist: "whitelist": ["127.0.0.1", "'+ clientIp+'"]
Example for disable whitelist mode: "whitelistMode": false'); + } + next(); }); + + app.use(express.static(__dirname + "/public", { refresh: true })); app.use('/backgrounds', (req, res) => { const filePath = path.join(process.cwd(), 'public/backgrounds', req.url); @@ -108,6 +92,35 @@ app.get("/", function(request, response){ app.get("/notes/*", function(request, response){     response.sendFile(__dirname + "/public"+request.url+".html");      //response.send("

Главная страница

"); +}); +app.post("/getlastversion", jsonParser, function(request, response_getlastversion = response){ + if(!request.body) return response_getlastversion.sendStatus(400); + + const repo = 'TavernAI/TavernAI'; + let req; + req = https.request({ + hostname: 'github.com', + path: `/${repo}/releases/latest`, + method: 'HEAD' + }, (res) => { + if(res.statusCode === 302) { + const glocation = res.headers.location; + const versionStartIndex = glocation.lastIndexOf('@')+1; + const version = glocation.substring(versionStartIndex); + //console.log(version); + response_getlastversion.send({version: version}); + }else{ + response_getlastversion.send({version: 'error'}); + } + }); + + req.on('error', (error) => { + console.error(error); + response_getlastversion.send({version: 'error'}); + }); + + req.end(); + }); //**************Kobold api app.post("/generate", jsonParser, function(request, response_generate = response){