-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (119 loc) · 3.23 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
import * as maxmind from "maxmind";
import isIP from "./src/isIP.js";
import express from "express";
const app = express();
let asn_db;
let country_db;
let city_db;
const maxmindDbLocation = {
asn: "./database/GeoLite2-ASN.mmdb",
country: "./database/GeoLite2-Country.mmdb",
city: "./database/GeoLite2-City.mmdb",
};
app.get("/", (req, res) => {
res.status(200).json({
message: "Welcome to Maxmind API",
});
});
app.get("/asn/:query_ip", (req, res) => {
let query_ip = req.params.query_ip;
if (isIP(query_ip)) {
const data = asn_db.get(query_ip);
if (data) {
res.status(200).json(data);
} else {
res.status(422).send("Maxmind doesn't have any information for that IP.");
}
} else {
res.status(400).json({
error: "Not a valid IP.",
});
}
});
app.get("/country/:query_ip", (req, res) => {
let query_ip = req.params.query_ip;
if (isIP(query_ip)) {
const data = country_db.get(query_ip);
if (data) {
res.status(200).json(data);
} else {
res.status(422).send("Maxmind doesn't have any information for that IP.");
}
} else {
res.status(400).json({
error: "Not a valid IP.",
});
}
});
app.get("/city/:query_ip", (req, res) => {
let query_ip = req.params.query_ip;
if (isIP(query_ip)) {
const data = city_db.get(query_ip);
if (data) {
res.status(200).json(data);
} else {
res.status(422).send("Maxmind doesn't have any information for that IP.");
}
} else {
res.status(400).json({
error: "Not a valid IP.",
});
}
});
app.get("/geolocation/:query_ip", (req, res) => {
let query_ip = req.params.query_ip;
if (isIP(query_ip)) {
const asn_data = asn_db.get(query_ip);
const city_data = city_db.get(query_ip);
if (asn_data && city_data) {
const data = {
ISP: asn_data.autonomous_system_organization,
registered_country_code: city_data.registered_country?.iso_code,
registered_country: city_data.registered_country?.names?.en,
city: city_data.city?.names?.en,
country_code: city_data.country?.iso_code,
country: city_data.country?.names?.en,
continent_code: city_data.continent?.code,
continent: city_data.continent?.names?.en,
location: city_data.location,
};
res.status(200).json(data);
} else {
res.status(422).send("Maxmind doesn't have any information for that IP.");
}
} else {
res.status(400).json({
error: "Not a valid IP.",
});
}
});
app.get("/isp", (req, res) => {
let query_ip = req.headers["x-forwarded-for"];
if (isIP(query_ip)) {
const data = asn_db.get(query_ip);
if (data) {
res.status(200).json(data);
} else {
res.status(422).send("Maxmind doesn't have any information for that IP.");
}
} else {
res.status(400).json({
error: "Not a valid IP.",
});
}
});
app.get("*", (req, res) => {
res.status(404).json({
error: "Not Found",
});
});
app.listen(3000, async () => {
try {
asn_db = await maxmind.open(maxmindDbLocation.asn);
country_db = await maxmind.open(maxmindDbLocation.country);
city_db = await maxmind.open(maxmindDbLocation.city);
console.log("API started");
} catch (error) {
console.error(error);
}
});