first commit

This commit is contained in:
Florian Herrengt 2024-05-16 20:14:08 +01:00
commit f3b1fb7324
21 changed files with 129783 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
node_modules
ip_ranges

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
node_modules
ip_ranges

23
Dockerfile Normal file
View File

@ -0,0 +1,23 @@
FROM node:21
# Install dependencies
RUN apt-get update && \
apt-get install -y bgpq3 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
RUN node buildIpRange.js
# Expose port and start the application
EXPOSE 3000
CMD [ "node", "server.js" ]

0
README.md Normal file
View File

126445
as.csv Normal file

File diff suppressed because it is too large Load Diff

66
buildIpRange.js Normal file
View File

@ -0,0 +1,66 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import * as csv from "csv-parse";
import { exec } from "child_process";
import { getCloudASN } from "./getCloudASN.js";
import { generateWellKnownIPs } from "./wellKnown";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const getCloudASN = async () =>
new Promise(async (resolve, reject) => {
const results = [];
const cloudList = (
await fs.promises.readFile(path.join(__dirname, "./cloud_list.md"))
)
.toString()
.split("\n");
fs.createReadStream("./as.csv")
.pipe(csv.parse({ delimiter: ",", from_line: 2 }))
.on("data", function ([asn, handle, description]) {
if (cloudList.includes(handle) || cloudList.includes(description)) {
results.push({ asn, handle, description });
}
})
.on("end", function () {
resolve(results);
})
.on("error", function (error) {
console.log(error.message);
reject(error.message);
});
});
const buildIpRange = async () => {
const dir = path.join(__dirname, "ip_ranges");
if (!fs.existsSync(path.join(__dirname, "ip_ranges"))) {
fs.mkdirSync(path.join(__dirname, "ip_ranges"));
}
const cloudASM = await getCloudASN();
for (const { asn, handle } of cloudASM) {
try {
await new Promise((resolve, reject) => {
exec(
`bgpq3 -h whois.radb.net -j AS${asn} > ip_ranges/${handle}.json`,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
reject(error);
return;
}
resolve();
}
);
});
} catch (error) {
console.error(`${asn}, ${handle} failed`);
}
}
generateWellKnownIPs();
};
buildIpRange();

25
checkIp.js Normal file
View File

@ -0,0 +1,25 @@
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
import ipCheck from "ip";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ipDir = path.join(__dirname, "./ip_ranges");
const database = {};
for (const file of fs.readdirSync(ipDir)) {
database[path.basename(file, ".json")] = JSON.parse(
fs.readFileSync(path.join(ipDir, file)).toString()
)["NN"].map(({ prefix }) => prefix);
}
export const checkIp = (ip) => {
for (const [name, cidrs] of Object.entries(database)) {
for (const cidr of cidrs) {
if (ipCheck.cidrSubnet(cidr).contains(ip)) {
return name;
}
}
}
return null;
};

1602
cloud_list.md Normal file

File diff suppressed because it is too large Load Diff

8
config.js Normal file
View File

@ -0,0 +1,8 @@
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const config = {
dbPath: path.join(__dirname, "ip_ranges.db"),
};

32
getCloudASN.js Normal file
View File

@ -0,0 +1,32 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import * as csv from "csv-parse";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
export const getCloudASN = async () =>
new Promise(async (resolve, reject) => {
const results = [];
const cloudList = (
await fs.promises.readFile(path.join(__dirname, "./cloud_list.md"))
)
.toString()
.split("\n");
fs.createReadStream("./as.csv")
.pipe(csv.parse({ delimiter: ",", from_line: 2 }))
.on("data", function ([asn, handle, description]) {
if (cloudList.includes(handle) || cloudList.includes(description)) {
results.push({ asn, handle, description });
}
})
.on("end", function () {
resolve(results);
})
.on("error", function (error) {
console.log(error.message);
reject(error.message);
});
});

1350
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

28
package.json Normal file
View File

@ -0,0 +1,28 @@
{
"name": "datacenters-ip-addresses",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.6.8",
"csv": "^6.3.9",
"csv-parser": "^3.0.0",
"csv-writer": "^1.6.0",
"fastify": "^4.27.0",
"ip": "^2.0.1",
"ip-address": "^9.0.5",
"lodash": "^4.17.21",
"node-html-parser": "^6.1.13",
"progress": "^2.0.3",
"range-inclusive": "^1.0.2",
"rocksdb": "^5.2.1",
"whois": "^2.14.0"
}
}

53
server.js Normal file
View File

@ -0,0 +1,53 @@
import Fastify from "fastify";
import helmet from "@fastify/helmet";
import rateLimit from "@fastify/rate-limit";
import cors from "@fastify/cors";
import { checkIp } from "./checkIp.js";
const fastify = Fastify();
await fastify.register(helmet);
await fastify.register(rateLimit, {
max: (request) => (request.headers["x-api-key"] ? 100 : 1),
timeWindow: "1 second",
keyGenerator: (request) => request.headers["x-api-key"] || request.ip,
addHeaders: {
"x-ratelimit-limit": true,
"x-ratelimit-remaining": true,
"x-ratelimit-reset": true,
},
});
await fastify.register(cors);
fastify.get(
"/api/ip/:ip",
{
schema: {
params: {
type: "object",
properties: {
ip: { type: "string", format: "ipv4" },
},
required: ["ip"],
},
},
},
async function handler(request, reply) {
try {
const datacenter = checkIp(request.params.ip);
reply.header("Content-Type", "application/json");
reply.send({ datacenter });
} catch (err) {
reply.code(500).send({ error: "Internal Server Error" });
}
}
);
try {
await fastify.listen({ port: 3000 });
} catch (err) {
fastify.log.error(err);
process.exit(1);
}

22
wellKnown/aws.js Normal file
View File

@ -0,0 +1,22 @@
import axios from "axios";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
export const aws = async () => {
const { data } = await axios.get(
"https://ip-ranges.amazonaws.com/ip-ranges.json"
);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
await fs.promises.writeFile(
path.join(__dirname, "../ip_ranges/AWS.json"),
JSON.stringify({
NN: [
...data.prefixes.map(({ ip_prefix }) => ip_prefix),
...data.ipv6_prefixes.map(({ ipv6_prefix }) => ipv6_prefix),
].map((prefix) => ({ prefix })),
})
);
};
// (async () => await aws())();

23
wellKnown/azure.js Normal file
View File

@ -0,0 +1,23 @@
import axios from "axios";
import _ from "lodash";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
export const azure = async () => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const { data } = await axios.get(
"https://download.microsoft.com/download/7/1/D/71D86715-5596-4529-9B13-DA13A5DE5B63/ServiceTags_Public_20240513.json"
);
const ips = _.flatten(
data.values.map(({ properties }) => properties.addressPrefixes)
);
await fs.promises.writeFile(
path.join(__dirname, "../ip_ranges/AZURE.json"),
JSON.stringify({
NN: ips.map((prefix) => ({ prefix })),
})
);
};
// (async () => console.log(await azure()))();

19
wellKnown/cloudflare.js Normal file
View File

@ -0,0 +1,19 @@
import axios from "axios";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
export const cloudflare = async () => {
const [{ data: ipv4 }, { data: ipv6 }] = await Promise.all([
axios.get("https://www.cloudflare.com/ips-v4"),
axios.get("https://www.cloudflare.com/ips-v6"),
]);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ips = [...ipv4.split("\n"), ...ipv6.split("\n")];
await fs.promises.writeFile(
path.join(__dirname, "../ip_ranges/CLOUDFLARE.json"),
JSON.stringify({
NN: ips.map((prefix) => ({ prefix })),
})
);
};

21
wellKnown/gcp.js Normal file
View File

@ -0,0 +1,21 @@
import axios from "axios";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
export const gcp = async () => {
const { data } = await axios.get(
"https://www.gstatic.com/ipranges/cloud.json"
);
const ips = data.prefixes.map(
({ ipv4Prefix, ipv6Prefix }) => ipv4Prefix || ipv6Prefix
);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
await fs.promises.writeFile(
path.join(__dirname, "../ip_ranges/GCP.json"),
JSON.stringify({
NN: ips.map((prefix) => ({ prefix })),
})
);
};

31
wellKnown/ibm.js Normal file
View File

@ -0,0 +1,31 @@
import axios from "axios";
import _ from "lodash";
import htmlParser from "node-html-parser";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
export const ibm = async () => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const { data } = await axios.get(
"https://ondeck.console.cloud.ibm.com/docs/cloud-infrastructure?topic=cloud-infrastructure-ibm-cloud-ip-ranges"
);
const root = htmlParser.parse(data);
const ips = _.flattenDeep(
["#section-front-end-network", "#section-load-balancer-ips"].map(
(cssSelector) =>
root
.querySelectorAll(`${cssSelector} td:nth-child(3)`)
.map((e) => e.text.split("\n").map((l) => l.trim()))
)
).filter((e) => e);
await fs.promises.writeFile(
path.join(__dirname, "../ip_ranges/CLOUDFLARE.json"),
JSON.stringify({
NN: ips.map((prefix) => ({ prefix })),
})
);
};
// (async () => console.log(await ibm()))();

9
wellKnown/index.js Normal file
View File

@ -0,0 +1,9 @@
import { aws } from "./aws.js";
import { azure } from "./azure.js";
import { cloudflare } from "./cloudflare.js";
import { gcp } from "./gcp.js";
import { ibm } from "./ibm.js";
import { oracle } from "./oracle.js";
export const generateWellKnownIPs = () =>
Promise.all([aws(), azure(), cloudflare(), gcp(), ibm(), oracle()]);

22
wellKnown/oracle.js Normal file
View File

@ -0,0 +1,22 @@
import axios from "axios";
import _ from "lodash";
import fs from "fs";
import { fileURLToPath } from "url";
import path from "path";
export const oracle = async () => {
const { data } = await axios.get(
"https://docs.oracle.com/en-us/iaas/tools/public_ip_ranges.json"
);
const ips = _.flatten(
data.regions.map(({ cidrs }) => cidrs.map(({ cidr }) => cidr))
);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
await fs.promises.writeFile(
path.join(__dirname, "../ip_ranges/ORACLE.json"),
JSON.stringify({
NN: ips.map((prefix) => ({ prefix })),
})
);
};