1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Internet_Security/app.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

2020-05-11 10:51:30 +02:00
let express = require("express");
2020-05-25 12:02:04 +02:00
let app = express();
let server = require("http").Server(app);
2020-05-11 10:51:30 +02:00
let morgan = require("morgan");
2020-05-25 12:02:04 +02:00
let io = require("socket.io")(server);
let https = require("https");
let fs = require("fs");
server.listen(process.env.PORT || 8080);
2020-05-11 10:51:30 +02:00
let indexRoute = require("./routes/index");
let notFoundRoute = require("./routes/notFound");
let errorRoute = require("./routes/error");
app.use(morgan("dev"))
.use(express.static("public"))
.set("view engine", "pug")
.use("/", indexRoute)
.use(notFoundRoute)
2020-05-25 12:02:04 +02:00
.use(errorRoute);
io.on("connection", (socket) => {
socket.emit("connected");
console.log("New connection !");
socket.on("checkMail", (data) => {
let key = fs.readFileSync("key", "utf-8");
let request = https.request({
hostname: "haveibeenpwned.com",
port: 443,
path: "/api/v3/breachedaccount/" + data.email,
method: "GET",
headers: {
Accept: 'application/json',
"user-agent": "InternetSecurity",
"hibp-api-key": key
}
}, res => {
let sendData = false;
res.setEncoding("utf-8");
res.on("data", d => {
socket.emit("resultMail", d);
sendData = true;
});
res.on("close", () => {
if (!sendData)
socket.emit("resultMail", []);
});
});
request.on("error", err => {
console.error(err);
socket.emit("resultMail", null);
});
request.end();
})
});