-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
179 lines (170 loc) · 5.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const { LOADIPHLPAPI } = require("dns");
const express = require("express");
const app = express();
const fs = require("fs");
require("dotenv").config();
var API_KEY = process.env["API_KEY"];
var DOMAIN = "sandbox35661a599b8d41118479a26a2da62b8f.mailgun.org"; // not confidential
var mailgun = require("mailgun-js")({ apiKey: API_KEY, domain: DOMAIN });
const fetch = require("node-fetch");
// middleware
app.use(express.static("public"));
app.set("view engine", "ejs");
app.use(express.json());
global.users = {
users: [],
};
const getIPinfo = async (ip) => {
const getIpInfoURL = "https://api.hackertarget.com/geoip/?q=";
let resInfoIP = await fetch("https://api.hackertarget.com/geoip/?q=" + ip, {
headers: {
accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"accept-language":
"en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7,ar-MA;q=0.6,ar;q=0.5",
"cache-control": "max-age=0",
"sec-ch-ua":
'"Chromium";v="86", "\\"Not\\\\A;Brand";v="99", "Google Chrome";v="86"',
"sec-ch-ua-mobile": "?0",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"sec-gpc": "1",
"upgrade-insecure-requests": "1",
},
method: "GET",
mode: "cors",
credentials: "omit",
});
let infos = await resInfoIP.text();
fs.readFile("db.json", "utf8", function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
var obj = JSON.parse(data); //now it an object
global.users.users = obj.users;
console.log("----");
console.log(global.users.users);
console.log("----");
const found = global.users.users.some((el) => el.userIP == ip);
if (!found) {
console.log("not found, New user detected !");
let newVisitor = { userIP: ip, Nbvisits: 1, infos: infos };
global.users.users.push(newVisitor);
var json = JSON.stringify(global.users); //convert it back to json
fs.writeFile("db.json", json, "utf8", () => {
console.log("written to DB successfully !");
}); // write it back
// send email notification
const data = {
from: "News portfolio <[email protected]>",
to: "[email protected]",
subject: `New visitor Notification !`,
text: `${JSON.stringify(newVisitor)}`,
};
mailgun.messages().send(data, (error, body) => {
console.log(`message sent successfully !`);
console.table(body);
});
} else {
console.log("found");
let oldVisitor = global.users.users.find((x) => x.userIP == ip);
oldVisitor.Nbvisits += 1;
console.log("db:");
console.log(global.users.users);
var json1 = JSON.stringify(global.users); //convert it back to json
fs.writeFile("db.json", json1, "utf8", () => {
console.log("written to DB successfully !");
});
// console.log(users);
}
// console.log(global.users);
}
});
return global.users;
};
// let counter = 0;
app.get("/", async function (req, res) {
res.render("index");
});
app.post("/admin/get_visitors", async function (req, res) {
let { ip: user_ip } = req.body;
// console.log(user_ip);
let new_users = await getIPinfo(user_ip, users);
// console.log(new_users);
res.json({ new_users });
});
let en_count_dl = 0;
let fr_count_dl = 0;
app.get("/admin/downloaded", (req, res) => {
const { en, fr } = req.query;
if (en || fr) {
en_count_dl += parseInt(en);
fr_count_dl += parseInt(fr);
}
if (en > 0) {
const data = {
from: "portfolio news: EN Resume's been downloaded ! <[email protected]>",
to: "[email protected]",
subject: `New en Resume download Notification !`,
text: `EN resume has been downloaded ${en_count_dl} times`,
};
mailgun.messages().send(data, (error, body) => {
console.log(`message sent successfully !`);
// console.table(body);
});
res.json({
msg: "Downloads incremented for the English Resume !",
nb_dow: en_count_dl,
msg_to_geeks:
"This is just to know how many times my cv got downloaded !",
});
} else if (fr > 0) {
const data = {
from: "portfolio news: French CV's been downloaded ! <[email protected]>",
to: "[email protected]",
subject: `New french CV download Notification !`,
text: `FR CV has been downloaded ${fr_count_dl} times`,
};
mailgun.messages().send(data, (error, body) => {
console.log(`message sent successfully !`);
// console.table(body);
});
res.json({
msg: "Downloads incremented for the French CV !",
nb_dow: fr_count_dl,
msg_to_geeks:
"This is just to know how many times my cv got downloaded !",
});
} else {
res.json({
fr: fr_count_dl,
en: en_count_dl,
msg_to_geeks:
"This is just to know how many times my cv got downloaded !",
});
}
});
app.get("/admin/visitors", (req, res) => {
fs.readFile("db.json", "utf8", function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
var users = JSON.parse(data); //now it an object
res.json(users);
}
});
// res.json({ users });
});
app.listen(process.env.PORT || 3000, "0.0.0.0", () => {
console.log("Listening on ", process.env.PORT || 3000);
});
const request = require("request");
const ping = () =>
request("https://abdelhamid-pro.herokuapp.com/", (error, response, body) => {
console.log("error:", error); // Print the error if one occurred
console.log("statusCode:", response && response.statusCode); // Print the response status code if a response was received
// console.log("body:", body); // Print body of response received
});
setInterval(ping, 4.5 * 60 * 1000); // I have set to 20 mins interval