From caaf2de385462e3a5eb357a0873dc66d7c5a92b0 Mon Sep 17 00:00:00 2001 From: luzzi <christopher.luzzi@student.reutlingen-university.de> Date: Tue, 8 Nov 2022 21:07:14 +0100 Subject: [PATCH] secured connection --- README.md | 2 +- crypto.js | 32 ----------------------------- index.html | 9 ++++++++- server.js | 31 +++++++++++++++++++++++----- views/login.html | 36 ++++++++++++++++++++++++++++++++- views/register.html | 49 ++++++++++++++++++++++++++++++++++++++------- 6 files changed, 112 insertions(+), 47 deletions(-) delete mode 100644 crypto.js diff --git a/README.md b/README.md index 26ea272..81a174b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Socket.io Chatroom for Cloud-Computing Diagramm provided in the repository. Code is commentet. -Used technologies: Node.js, Express, Mongoose, Socket.io, JWT, Cookie-Session, Crypto, ejs, bcrypt +Used technologies: Node.js, Express, Mongoose, Socket.io, JWT, Cookie-Session, ejs, bcrypt Installation diff --git a/crypto.js b/crypto.js deleted file mode 100644 index 95d5876..0000000 --- a/crypto.js +++ /dev/null @@ -1,32 +0,0 @@ -//this is to define the encryption and decryption functions -//i got this script from this tutorial: https://attacomsian.com/blog/nodejs-encrypt-decrypt-data -const crypto = require('crypto') - -const algorithm = 'aes-256-ctr' -const secretKey = 'vOVH6sdmpNWjRRIqCc7rdxs01lwHzfr3' - -const encrypt = text => { - const iv = crypto.randomBytes(16) - - const cipher = crypto.createCipheriv(algorithm, secretKey, iv) - - const encrypted = Buffer.concat([cipher.update(text), cipher.final()]) - - return { - iv: iv.toString('hex'), - content: encrypted.toString('hex') - } -} - -const decrypt = hash => { - const decipher = crypto.createDecipheriv(algorithm, secretKey, Buffer.from(hash.iv, 'hex')) - - const decrpyted = Buffer.concat([decipher.update(Buffer.from(hash.content, 'hex')), decipher.final()]) - - return decrpyted.toString() -} - -module.exports = { - encrypt, - decrypt -} \ No newline at end of file diff --git a/index.html b/index.html index 12819f9..3d50083 100644 --- a/index.html +++ b/index.html @@ -18,7 +18,7 @@ <body> <ul id="messages"></ul> <form id="form" action=""> - <input id="input" autocomplete="off" /><button>Send</button> + <input id="input" autocomplete="off" /><button>Send</button> <input type="file" onchange="upload(files)"> </form> <script type="application/javascript" src="/socket.io/socket.io.js"></script> <script> @@ -28,6 +28,13 @@ var messages = document.getElementById('messages'); var form = document.getElementById('form'); var input = document.getElementById('input'); + + function upload(files) { + socket.emit("upload", files[0], (status) => { + console.log(status); + }); + } + form.addEventListener('submit', function(e) { e.preventDefault(); diff --git a/server.js b/server.js index ea517bc..c5d2500 100644 --- a/server.js +++ b/server.js @@ -5,11 +5,23 @@ const cookieSession = require('cookie-session') const path = require('path') const ejs = require('ejs') const app = express() -const http = require('http') -const server = http.createServer(app); +const https = require('https') +const fs = require('fs') +var key = fs.readFileSync(__dirname + '/server.key') +var cert = fs.readFileSync(__dirname + '/server.cert') +const {writeFile} = require('fs') + + +const credentials = { + key: key, + cert: cert +} +const server = https.createServer(credentials,app); const { Server } = require("socket.io"); const io = new Server(server); -const { encrypt, decrypt } = require('./crypto') + + + //Setting up Cross Origin @@ -75,13 +87,22 @@ app.get('/index', (req, res) => { //connecting to the socket io.on('connection', (socket) => { socket.on('chat message', (msg) => { - //Here we encrypt the msg before sending it to the client - io.emit('chat message', encrypt(msg).content); + io.emit('chat message', msg); }); + socket.on("upload", (file, callback) => { + console.log(file); + writeFile("/tmp/upload", file, (err) => { + callback({ message: err ? "failure" : "success" }); + }); + }); + }); + + + //Importing routes require('./app/routes/auth.routes')(app); require('./app/routes/user.routes')(app); diff --git a/views/login.html b/views/login.html index 131b4e5..e3c8d53 100644 --- a/views/login.html +++ b/views/login.html @@ -4,6 +4,40 @@ <meta charset="UTF-8"/> <meta name="viewport" content="width=dive-width, initial-scale=1.0"/> <title>Login to Chat</title> + <style> + body {background-color:rgb(66, 66, 187); + width: 20%; + margin-left:auto; + margin-right:auto; + margin-top: 150px;} + input {width: 100%; + padding: 12px 20px; + margin: 8px 0; + box-sizing: border-box;} + input[type=submit]{ + font-family: Arial, Helvetica, sans-serif; + font-weight: bold; + width: 100%; + padding: 14px 20px; + margin: 8px 0; + border: none; + border-radius: 4px; + background-color: rgb(0, 128, 255); + } + input[type=submit]:hover { + background-color: #263c5c; + } + div{ + padding-top: 100px; + } + h2 { text-align: center; + color: white; + font-family: Arial, Helvetica, sans-serif; + + } + + + </style> </head> <body> <body> @@ -11,7 +45,7 @@ <form action="/api/auth/signin", method="post"> <input type="text" name="username" id="username" placeholder="Name" required /> <input type="password" name="password" id="password" placeholder="Password " required/> - <button>Login</button> + <input type="submit" value="Login"> </form> </body> diff --git a/views/register.html b/views/register.html index d700291..6f78e0a 100644 --- a/views/register.html +++ b/views/register.html @@ -4,19 +4,54 @@ <meta charset="UTF-8"/> <meta name="viewport" content="width=dive-width, initial-scale=1.0"/> <title>Register User</title> + <style> + body {background-color:rgb(66, 66, 187); + width: 20%; + margin-left:auto; + margin-right:auto; + margin-top: 150px;} + input {width: 100%; + padding: 12px 20px; + margin: 8px 0; + box-sizing: border-box;} + input[type=submit]{ + font-family: Arial, Helvetica, sans-serif; + font-weight: bold; + width: 100%; + padding: 14px 20px; + margin: 8px 0; + border: none; + border-radius: 4px; + background-color: rgb(0, 128, 255); + } + input[type=submit]:hover { + background-color: #263c5c; + } + div{ + padding-top: 100px; + } + h2 { text-align: center; + color: white; + font-family: Arial, Helvetica, sans-serif; + + } + + + </style> </head> <body> - <body> <h2>Register</h2> <form action="/api/auth/signup", method="post"> - <input type="text" name="username" id="username" placeholder="Name" required /> - <input type="email" name="email" id="email" placeholder="E-Mail " required /> - <input type="password" name="password" id="password" placeholder="Password " required/> - <button>Sign Up</button> + <input type="text" name="username" id="username" placeholder="Name" required /> <br> + <input type="email" name="email" id="email" placeholder="E-Mail " required /> <br> + <input type="password" name="password" id="password" placeholder="Password " required/> <br> + <input type="submit" value="Sign Up"> </form> - <h2>Allready a Member?</h2> + <div> + <h2>Allready a Member?</h2> <form action="/login", method="get"> - <button>Login</button> + <input type="submit" value="Login"> </form> + </div> </body> </html> \ No newline at end of file -- GitLab