2020-04-26 15:22:43 +02:00
|
|
|
let express = require("express");
|
2020-04-26 16:24:48 +02:00
|
|
|
let morgan = require("morgan");
|
2020-04-26 18:55:21 +02:00
|
|
|
let cookieParser = require("cookie-parser");
|
|
|
|
let bodyParser = require('body-parser');
|
|
|
|
let session = require("express-session");
|
2020-04-26 16:24:48 +02:00
|
|
|
let fs = require("fs");
|
|
|
|
let passwordHash = require('password-hash');
|
2020-04-26 18:56:38 +02:00
|
|
|
let multer = require("multer");
|
|
|
|
let crypto = require("crypto");
|
|
|
|
let path = require("path");
|
2020-04-26 15:22:43 +02:00
|
|
|
|
2020-04-26 18:55:21 +02:00
|
|
|
let app = express();
|
2020-04-26 18:56:38 +02:00
|
|
|
let storage = multer.diskStorage({
|
|
|
|
destination: './images/',
|
|
|
|
filename: function (req, file, cb) {
|
|
|
|
crypto.pseudoRandomBytes(16, function (err, raw) {
|
|
|
|
if (err) return cb(err)
|
|
|
|
cb(null, raw.toString('hex') + path.extname(file.originalname))
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
);
|
|
|
|
let upload = multer({storage: storage});
|
2020-04-26 16:24:48 +02:00
|
|
|
|
|
|
|
if (!fs.existsSync("users.json")) {
|
|
|
|
fs.writeFileSync("users.json", "{}");
|
|
|
|
console.log("Register first user");
|
|
|
|
addUser();
|
|
|
|
} else if (Object.keys(JSON.parse(fs.readFileSync("users.json"))).length === 0) {
|
|
|
|
console.log("No user found, adding a new one");
|
|
|
|
addUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addUser() {
|
2020-04-26 18:55:21 +02:00
|
|
|
let rl = require("readline").createInterface({input: process.stdin, output: process.stdout, terminal: false});
|
2020-04-26 16:24:48 +02:00
|
|
|
let username;
|
|
|
|
let password;
|
|
|
|
file = JSON.parse(fs.readFileSync("users.json"));
|
|
|
|
do {
|
|
|
|
username = await new Promise(resolve => rl.question("Username: ", resolve));
|
|
|
|
} while (username in file || ["", null].indexOf(username) >= 0);
|
|
|
|
password = passwordHash.generate(await new Promise(resolve => rl.question("Password: ", resolve)));
|
|
|
|
file[username] = password;
|
|
|
|
fs.writeFileSync("users.json", JSON.stringify(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-26 18:55:21 +02:00
|
|
|
function isAuth(req, res, next) {
|
|
|
|
if (req.session.login) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
res.redirect("/login");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 15:22:43 +02:00
|
|
|
|
2020-04-26 18:55:21 +02:00
|
|
|
app.use(morgan("dev"))
|
|
|
|
.use(express.static("public"))
|
|
|
|
.use(bodyParser.urlencoded({ extended: true }))
|
2020-04-26 18:56:38 +02:00
|
|
|
.use(cookieParser(process.env.SECRET))
|
2020-04-26 18:55:21 +02:00
|
|
|
.use(session({
|
|
|
|
secret: process.env.SECRET,
|
|
|
|
resave: false,
|
|
|
|
saveUninitialized: false,
|
|
|
|
cookie: { secure: "auto" }
|
|
|
|
}))
|
|
|
|
.set("trust proxy", 1)
|
|
|
|
.set("view engine", "pug")
|
|
|
|
.get("/", isAuth, (req, res) => {
|
|
|
|
res.render("index");
|
|
|
|
})
|
2020-04-26 18:56:38 +02:00
|
|
|
.post("/upload", isAuth, upload.single("image"), (req, res) => {
|
2020-04-26 19:15:53 +02:00
|
|
|
res.redirect("/images/" + req.file.filename);
|
2020-04-26 18:56:38 +02:00
|
|
|
})
|
2020-04-26 18:55:21 +02:00
|
|
|
.get("/login", (req, res) => {
|
|
|
|
let fail = false;
|
|
|
|
if ("fail" in req.query) {
|
|
|
|
fail = true;
|
|
|
|
}
|
|
|
|
res.render("login", {title: "login", fail: fail});
|
2020-04-26 19:12:25 +02:00
|
|
|
})
|
|
|
|
.post("/login", (req, res) => {
|
2020-04-26 18:55:21 +02:00
|
|
|
if ("username" in req.body && "password" in req.body) {
|
|
|
|
file = JSON.parse(fs.readFileSync("users.json"));
|
|
|
|
if (req.body.username in file && passwordHash.verify(req.body.password, file[req.body.username])) {
|
|
|
|
req.session.login = true;
|
|
|
|
req.session.save();
|
|
|
|
res.redirect("/");
|
|
|
|
} else {
|
|
|
|
res.redirect("/login?fail");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.redirect("/login?fail");
|
|
|
|
}
|
|
|
|
})
|
2020-04-26 19:12:25 +02:00
|
|
|
.get("/images/:name", (req, res) => {
|
|
|
|
if ("name" in req.params && fs.existsSync("./images/"+req.params.name)) {
|
|
|
|
res.sendfile("./images/"+req.params.name);
|
|
|
|
} else {
|
|
|
|
res.status(404).send("Image not found :/");
|
|
|
|
}
|
|
|
|
})
|
2020-04-26 18:55:21 +02:00
|
|
|
.listen(8080);
|
2020-04-26 15:22:43 +02:00
|
|
|
|