Compare commits
7 Commits
Author | SHA1 | Date |
---|---|---|
AnzeBlaBla | e0a6a1ff62 | |
AnzeBlaBla | 5bca9cd5f7 | |
AnzeBlaBla | c81bc06b8d | |
AnzeBlaBla | 427cf7aa94 | |
AnzeBlaBla | fa5dc6c535 | |
AnzeBlaBla | 99f69e141e | |
Gasper Spagnolo | 401dc44348 |
|
@ -19,11 +19,10 @@ COPY . ./
|
|||
#RUN npm install -g nodemon
|
||||
|
||||
RUN mkdir /uploads && chown appuser:appgroup /uploads
|
||||
RUN mkdir /pcaps
|
||||
|
||||
RUN apt-get install tcpdump -y
|
||||
|
||||
EXPOSE 3000
|
||||
|
||||
USER appuser
|
||||
|
||||
CMD ["node", "server.js"]
|
||||
# CMD ["nodemon", "server.js"]
|
|
@ -1,9 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
packet=$((9000))
|
||||
|
||||
while true
|
||||
do
|
||||
tcpdump -c 1000 -i any -w "/pcaps/traffic-2$packet.pcap"
|
||||
packet=$((packet + 1))
|
||||
done
|
|
@ -14,13 +14,17 @@ router.get('/login', function (req, res, next) {
|
|||
|
||||
router.post('/login', async (req, res, next) => {
|
||||
const { email, password } = req.body;
|
||||
console.log("/login: " + JSON.stringify(req.body));
|
||||
const r = await db.query('SELECT * FROM users WHERE email=$1', [email]);
|
||||
if (r.rowCount < 1 || r.rows[0].password !== password) {
|
||||
res.locals.errormsg = 'Wrong credentials';
|
||||
console.log("login failed");
|
||||
return res.status(403).render('login');
|
||||
}
|
||||
const token = jwt.sign({ email }, PRIV_KEY, { algorithm: 'RS256' });
|
||||
|
||||
console.log("Login success");
|
||||
|
||||
res.cookie('session', token);
|
||||
res.redirect('/');
|
||||
});
|
||||
|
@ -31,6 +35,7 @@ router.get('/register', function (req, res, next) {
|
|||
|
||||
router.post('/register', async (req, res, next) => {
|
||||
const { email, nickname, password } = req.body;
|
||||
console.log("/register: " + JSON.stringify(req.body));
|
||||
|
||||
const reg = /^[\w\.@]{4,40}$/;
|
||||
|
||||
|
@ -47,9 +52,12 @@ router.post('/register', async (req, res, next) => {
|
|||
} catch (error) {
|
||||
res.locals.errormsg = 'Email or nickname already used';
|
||||
res.clearCookie('session');
|
||||
console.log("register fail");
|
||||
return res.render('register');
|
||||
}
|
||||
|
||||
console.log("register success");
|
||||
|
||||
res.redirect('/');
|
||||
});
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@ const db = require('../db');
|
|||
const router = express.Router();
|
||||
|
||||
async function create_message(channel_id, msg, author) {
|
||||
console.log("create_message: " + channel_id + " - " + msg + " - " + author);
|
||||
let r = await db.query('SELECT * FROM channels WHERE id=$1', [channel_id]);
|
||||
|
||||
if (r.rowCount < 1) {
|
||||
|
@ -30,6 +31,7 @@ async function create_message(channel_id, msg, author) {
|
|||
}
|
||||
|
||||
async function list_channels(user) {
|
||||
console.log("list_channel: " + JSON.stringify(user));
|
||||
const r = await db.query('SELECT id, detail, private FROM channels WHERE private=FALSE UNION SELECT id, detail, private FROM channels, allowed_users WHERE id = channel_id AND user_email = $1', [user]);
|
||||
return r.rows;
|
||||
}
|
||||
|
@ -65,6 +67,7 @@ router.get('/channel/:id', async (req, res, next) => {
|
|||
});
|
||||
|
||||
router.post('/new_channel', async (req, res, next) => {
|
||||
console.log("/new_chanell: " + JSON.stringify(req.body));
|
||||
const { channelid, detail } = req.body;
|
||||
const user = req.user.email;
|
||||
|
||||
|
@ -81,6 +84,7 @@ router.post('/new_channel', async (req, res, next) => {
|
|||
|
||||
router.post('/new_message', async (req, res, next) => {
|
||||
const { msg, channelid } = req.body;
|
||||
console.log("/new_message: ", + JSON.stringify(req.body));
|
||||
const author = req.user.email;
|
||||
|
||||
try {
|
||||
|
@ -95,6 +99,7 @@ router.post('/new_message', async (req, res, next) => {
|
|||
router.post('/invite', async (req, res, next) => {
|
||||
const { channelid, user } = req.body;
|
||||
const logged_user = req.user.email;
|
||||
console.log("/invite: ", +JSON.stringify(req.body));
|
||||
|
||||
let r = await db.query('SELECT user_email FROM allowed_users WHERE user_email=$1 AND channel_id=$2', [logged_user, channelid]);
|
||||
|
||||
|
@ -125,6 +130,7 @@ router.get('/broadcast', async (req, res, next) => {
|
|||
});
|
||||
|
||||
router.post('/broadcast', async (req, res, next) => {
|
||||
console.log("/broadcast: ", JSON.stringify(req.body));
|
||||
let { msg } = req.body;
|
||||
const author = req.user.email;
|
||||
const channels = Object.keys(req.cookies);
|
||||
|
@ -134,7 +140,8 @@ router.post('/broadcast', async (req, res, next) => {
|
|||
|
||||
let promises = [];
|
||||
for (const c of channels) {
|
||||
promises.push(create_message(c, msg, author));
|
||||
if (c !== 'session')
|
||||
promises.push(create_message(c, msg, author));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
|
|
|
@ -8,6 +8,16 @@ const PORT = 3000;
|
|||
|
||||
const app = express();
|
||||
|
||||
// Log all possible data about request
|
||||
const logRequest = (req, res, next) => {
|
||||
console.log('Request URL: ' + req.url);
|
||||
console.log('Request method: ' + req.method);
|
||||
console.log('Request headers: ' + JSON.stringify(req.headers));
|
||||
console.log('Request body: ' + JSON.stringify(req.body));
|
||||
next();
|
||||
};
|
||||
|
||||
|
||||
// const indexRouter = require('./routes/index');
|
||||
const authRouter = require('./routes/auth');
|
||||
const channelRouter = require('./routes/channel');
|
||||
|
@ -17,6 +27,9 @@ app.set('view engine', 'ejs');
|
|||
app.use(express.urlencoded());
|
||||
app.use(cookieParser());
|
||||
|
||||
app.use(logRequest);
|
||||
|
||||
|
||||
app.use((req, res, next) => {
|
||||
|
||||
if (!req.headers.httpversion || req.headers.httpversion !== 'HTTP/3.0') {
|
||||
|
|
|
@ -1,50 +1,13 @@
|
|||
CREATE TABLE IF NOT EXISTS channels (
|
||||
id VARCHAR(40),
|
||||
detail TEXT,
|
||||
private BOOL,
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS channels (id VARCHAR(40), detail TEXT, private BOOL, PRIMARY KEY(id));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
email VARCHAR(40),
|
||||
nickname VARCHAR(40) UNIQUE,
|
||||
password VARCHAR(40),
|
||||
propic TEXT,
|
||||
PRIMARY KEY(email)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS users (email VARCHAR(40), nickname VARCHAR(40) UNIQUE, password VARCHAR(40), propic TEXT, PRIMARY KEY(email));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS allowed_users (
|
||||
user_email VARCHAR(40),
|
||||
channel_id VARCHAR(40),
|
||||
PRIMARY KEY(user_email, channel_id),
|
||||
FOREIGN KEY(user_email) REFERENCES users(email)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS allowed_users (user_email VARCHAR(40), channel_id VARCHAR(40), PRIMARY KEY(user_email,channel_id),
|
||||
FOREIGN KEY(user_email) REFERENCES users(email));
|
||||
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id VARCHAR(40),
|
||||
channel_id VARCHAR(40),
|
||||
data TEXT,
|
||||
author VARCHAR(40),
|
||||
ts timestamp NOT NULL DEFAULT NOW(),
|
||||
PRIMARY KEY(id),
|
||||
FOREIGN KEY(author) REFERENCES users(email)
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS messages (id VARCHAR(40), channel_id VARCHAR(40), data TEXT, author VARCHAR(40),ts timestamp NOT NULL DEFAULT NOW(), PRIMARY KEY(id),
|
||||
FOREIGN KEY(author) REFERENCES users(email));
|
||||
|
||||
INSERT INTO
|
||||
channels (id, detail, private)
|
||||
VALUES
|
||||
('Crypto', 'web3 without crypto?', FALSE);
|
||||
|
||||
INSERT INTO
|
||||
channels (id, detail, private)
|
||||
VALUES
|
||||
('Random', 'whatever', FALSE);
|
||||
|
||||
INSERT INTO
|
||||
channels (id, detail, private)
|
||||
VALUES
|
||||
(
|
||||
'Brews',
|
||||
'Share your best caffeine based concoction',
|
||||
FALSE
|
||||
);
|
||||
INSERT INTO channels (id, detail, private) VALUES ('Crypto','web3 without crypto?',FALSE);
|
||||
INSERT INTO channels (id, detail, private) VALUES ('Random','whatever',FALSE);
|
||||
INSERT INTO channels (id, detail, private) VALUES ('Brews','Share your best caffeine based concoction',FALSE);
|
||||
|
|
|
@ -7,7 +7,6 @@ services:
|
|||
- db
|
||||
volumes:
|
||||
- "vol-uploads:/uploads"
|
||||
- "/pcaps_s3:/pcaps"
|
||||
restart: on-failure
|
||||
frontend:
|
||||
build: ./frontend
|
||||
|
|
Loading…
Reference in New Issue