Browse Source

aggiunto redis

pull/665/head
flavio 3 years ago
parent
commit
7ed75d1312
  1. 41
      docker-compose.dev.yml
  2. 8
      docker-compose.yml
  3. 39
      src/lib/Server.js
  4. 3324
      src/package-lock.json
  5. 7
      src/package.json

41
docker-compose.dev.yml

@ -1,10 +1,41 @@
version: "3.8"
services:
wg-easy:
image: wg-easy
command: npm run serve
environment:
# ⚠️ Required:
# Change this to your host's public address
- WG_HOST=0.0.0.0
# Optional:
# - PASSWORD=foobar123
# - WG_PORT=51820
# - WG_DEFAULT_ADDRESS=10.8.0.x
# - WG_DEFAULT_DNS=1.1.1.1
# - WG_MTU=1420
# - WG_ALLOWED_IPS=192.168.15.0/24, 10.0.1.0/24
# - WG_PRE_UP=echo "Pre Up" > /etc/wireguard/pre-up.txt
# - WG_POST_UP=echo "Post Up" > /etc/wireguard/post-up.txt
# - WG_PRE_DOWN=echo "Pre Down" > /etc/wireguard/pre-down.txt
# - WG_POST_DOWN=echo "Post Down" > /etc/wireguard/post-down.txt
build: .
container_name: wg-easy
volumes:
- .:/etc/wireguard
- ./src/:/app/
environment:
# - PASSWORD=p
- WG_HOST=192.168.1.233
ports:
- "51820:51820/udp"
- "51821:51821/tcp"
restart: unless-stopped
cap_add:
- NET_ADMIN
- SYS_MODULE
sysctls:
- net.ipv4.ip_forward=1
- net.ipv4.conf.all.src_valid_mark=1
depends_on:
- redis
redis:
image: redis
ports:
- "6379:6379"

8
docker-compose.yml

@ -18,7 +18,7 @@ services:
# - WG_PRE_DOWN=echo "Pre Down" > /etc/wireguard/pre-down.txt
# - WG_POST_DOWN=echo "Post Down" > /etc/wireguard/post-down.txt
image: weejewel/wg-easy
build: .
container_name: wg-easy
volumes:
- .:/etc/wireguard
@ -32,3 +32,9 @@ services:
sysctls:
- net.ipv4.ip_forward=1
- net.ipv4.conf.all.src_valid_mark=1
depends_on:
- redis
redis:
image: redis
ports:
- "6379:6379"

39
src/lib/Server.js

@ -13,6 +13,13 @@ const WireGuard = require('../services/WireGuard');
// Needed to open users.json file
const fs = require('fs').promises;
// Redis
const redis = require('redis');
const redisClient = redis.createClient({
url: 'redis://redis:6379'
});
redisClient.connect();
// Getting enviroment variables
const {
PORT,
@ -53,21 +60,29 @@ module.exports = class Server {
password,
} = req.body;
// Check if the credentials are correct
const usersJson = await fs.readFile(path.join(USERS_PATH, 'users.json'), 'utf8');
const users = JSON.parse(usersJson);
const user = users.find(findUser => findUser.username === username);
if (typeof user !== 'object') {
throw new ServerError('Wrong Username', 401);
}
if (user.password !== password) {
throw new ServerError('Wrong Password', 401);
let dbPassword = await redisClient.get(username);
if (dbPassword !== password || dbPassword == null) {
const usersJson = await fs.readFile(path.join(USERS_PATH, 'users.json'), 'utf8');
const users = JSON.parse(usersJson);
for (let i = 0; i < users.length; i++) {
await redisClient.set(users[i].username, users[i].password);
}
dbPassword = await redisClient.get(username);
if (dbPassword == null) {
throw new ServerError('Wrong Username', 401);
}
if (dbPassword !== password) {
throw new ServerError('Wrong Password', 401);
}
}
req.session.authenticated = true;
req.session.username = user.username;
req.session.username = username;
req.session.save();
debug(`New Session: ${req.session.id}`);

3324
src/package-lock.json

File diff suppressed because it is too large

7
src/package.json

@ -13,9 +13,10 @@
"license": "GPL",
"dependencies": {
"debug": "^4.3.1",
"express": "^4.17.1",
"express": "^4.18.2",
"express-session": "^1.17.1",
"qrcode": "^1.4.4",
"qrcode": "^1.5.3",
"redis": "^4.6.11",
"uuid": "^8.3.2"
},
"devDependencies": {
@ -30,4 +31,4 @@
"engines": {
"node": "14"
}
}
}

Loading…
Cancel
Save