-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
158 lines (131 loc) · 4.55 KB
/
Copy pathmain.js
File metadata and controls
158 lines (131 loc) · 4.55 KB
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
const express = require("express");
const fs = require("fs");
const path = require("path");
require('dotenv').config();
const app = express();
const PORT = 80;
const mainDirname = __dirname;
const cacheFilename = "ipCache.json";
// Add url & api key to the .env to config
const discordWebhookUrl = process.env.DISCORD_WEBHOOK_URL;
const abuseIpDbAPIKey = process.env.ABUSE_IP_DB_API_KEY;
app.use(abuseIpCheck); // middleware
app.get('/', (req, res) => {
res.send("Hello World");
})
app.listen(PORT);
async function abuseIpCheck(req, res, next) {
const ipAddress = req.headers["x-forwarded-for"];
const method = req.method;
const endpoint = req.path;
const url = "https://api.abuseipdb.com/api/v2/check";
next();
const fetchRes = await fetch(url + "?ipAddress=" + ipAddress, {
method: "GET",
headers: {
Accept: "application/json",
Key: abuseIpDbAPIKey
}
})
const json = await fetchRes.json();
const ipCache = getCache(cacheFilename);
if (ipCache[ipAddress]) {
ipCache[ipAddress]['count']++;
}
else {
ipCache[ipAddress] = {count: 1};
}
writeCache(ipCache, cacheFilename);
if (json['errors']) {
console.error(json['errors'][0]['status'], json['errors'][0]['detail']);
return 1;
}
json['method'] = method;
json['endpoint'] = endpoint;
json['count'] = ipCache[ipAddress]['count'];
json['count'] = 1;
sendEmbedIpCheck(json);
}
async function sendEmbedIpCheck(json) {
const data = json['data'];
let title = "";
if (json['count'] == 1) { title = `${data['ipAddress']} (has visited __${json['count']}__ time)`} else { title = `${data['ipAddress']} (has visited __${json['count']}__ times)`}
let lastReportedAt = "";
if (!data['lastReportedAt']) { lastReportedAt = "null" } else {lastReportedAt = "> <t:" + new Date(data['lastReportedAt']).getTime() / 1000 + ":f>"};
const embed = {
title: title,
description: `**[LINK](https://www.abuseipdb.com/check/${data['ipAddress']})** \n\n`,
fields: [
{
name: "Method",
value: `**\`${json['method']}\`**`,
inline: true
},
{
name: "Path",
value: `**\`${json['endpoint']}\`**`,
inline: true
},
{
name: "Confidence of abuse",
value: `**\`${data['abuseConfidenceScore']}\` %**`
},
{
name: "Total reports",
value: `> **\`${data['totalReports']}\`** reports by **\`${data['numDistinctUsers']}\`** users`,
inline: true
},
{
name: "Latest report",
value: lastReportedAt,
inline: true
},
{
name: "ISP",
value: "> " + data['isp']
},
{
name: "Usage type",
value: "> " + data['usageType']
},
{
name: "Domain name",
value: "> " + data['domain']
},
{
name: "Country",
value: "> " + `:flag_${data['countryCode'].toLowerCase()}:`
},
{
name: "Hostnames",
value: "> " + data['hostnames'].toString() || "0"
}
],
color: getColor(data['abuseConfidenceScore']),
thumbnail: {
url: "https://cdn.discordapp.com/attachments/1024287372881440848/1072521498440503336/abuseipdb-logo.png"
},
footer: {
text: "Source • https://YOUR_WEBSITE_DOMAIN_NAME_HERE"
}
};
fetch(discordWebhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({embeds: [embed]})
})
}
function getColor(score) {
const colors = { green: "2664261", yellow: "16766720", orange: "16745755", red: "16711937" };
if (!score) {return colors['green']} else if (score <= 25) {return colors['yellow']} else if (score < 80) {return colors['orange']} else {return colors['red']}
}
function getCache() {
return JSON.parse(fs.readFileSync(path.join(mainDirname, cacheFilename)));
}
function writeCache(data) {
var cacheContent = getCache(cacheFilename);
Object.assign(cacheContent, data); // shift data to cacheContent JSON object.
fs.writeFileSync(path.join(mainDirname, cacheFilename), JSON.stringify(cacheContent));
}