152 lines
3.7 KiB
TypeScript
152 lines
3.7 KiB
TypeScript
import bgpq4 from "./bgpq4";
|
|
import fs from "node:fs";
|
|
import YAML from "yaml";
|
|
import { getNetName } from "./pdb";
|
|
import LRUCache from "lru-cache";
|
|
|
|
import axios from "axios";
|
|
import { env } from "node:process";
|
|
|
|
const cache = new LRUCache({
|
|
max: 500,
|
|
ttl: 1000 * 60 * 5,
|
|
});
|
|
|
|
const main = async () => {
|
|
const config: {
|
|
"MNT-BY": string[];
|
|
"TECH-C": string[];
|
|
"ADMIN-C": string[];
|
|
"AS-SETS": {
|
|
[asSetsName: string]: {
|
|
group: [groupName: string][];
|
|
expect: number[];
|
|
include: string[];
|
|
remarks: string[];
|
|
};
|
|
};
|
|
members: {
|
|
[member: string]: [groupName: string][];
|
|
}[];
|
|
} = YAML.parse(fs.readFileSync("../config.yaml", "utf8"));
|
|
|
|
const asSets: {
|
|
"AS-SET": string;
|
|
"MNT-BY": string[];
|
|
"TECH-C": string[];
|
|
"ADMIN-C": string[];
|
|
members: number[];
|
|
remarks: string[];
|
|
include: string[];
|
|
isChanged?: boolean;
|
|
}[] = [];
|
|
|
|
console.log("INFO: start AS-SETS generation.");
|
|
|
|
let i = 1;
|
|
|
|
for (let asSetName in config["AS-SETS"]) {
|
|
let isChanged = true;
|
|
console.log(
|
|
`INFO: generating AS-SETS <${asSetName}>... (${i}/${Object.keys(config["AS-SETS"]).length
|
|
})\n`
|
|
);
|
|
|
|
let members = ([...new Set((await Promise.all(
|
|
config.members.map(async (member) => {
|
|
const setName = Object.keys(member)[0];
|
|
const setGroup = Object.values(member)[0];
|
|
|
|
if (
|
|
config["AS-SETS"][asSetName].group.filter(
|
|
(e) => setGroup.includes(e) || (e as any) === "*"
|
|
).length == 0
|
|
)
|
|
return undefined;
|
|
|
|
const answer = await bgpq4(setName);
|
|
|
|
console.log("Add", setName, answer, "to", asSetName);
|
|
|
|
const asnList = answer
|
|
.filter(
|
|
(as) => !(config["AS-SETS"][asSetName].expect ?? [0]).includes(as)
|
|
)
|
|
.filter(Boolean)
|
|
.sort();
|
|
|
|
asnList.forEach((k) => {
|
|
if (!cache.has(k)) cache.set(k, setName);
|
|
});
|
|
|
|
return asnList;
|
|
})
|
|
)).flat(Infinity))].filter(Boolean) as any).sort((a: number, b: number) => a - b);
|
|
|
|
if (JSON.stringify(members) === JSON.stringify(await bgpq4(asSetName))) {
|
|
console.log(`[INFO] AS-SET <${asSetName}> is not changed.`);
|
|
isChanged = false;
|
|
}
|
|
|
|
config["AS-SETS"][asSetName].remarks.push(
|
|
`Generated by git.186526.xyz/186526/net186-as-set.`
|
|
);
|
|
config["AS-SETS"][asSetName].remarks.push(
|
|
`For more details, please visit go.186526.xyz/AS-NET186`
|
|
);
|
|
config["AS-SETS"][asSetName].remarks.push(
|
|
`Updated on ${new Date().toISOString()}.`
|
|
);
|
|
|
|
asSets.push({
|
|
"AS-SET": asSetName,
|
|
"MNT-BY": config["MNT-BY"],
|
|
"TECH-C": config["TECH-C"],
|
|
"ADMIN-C": config["ADMIN-C"],
|
|
remarks: config["AS-SETS"][asSetName].remarks,
|
|
members: [...new Set(members.flat(Infinity))].filter(Boolean) as any,
|
|
include: (config["AS-SETS"][asSetName].include ?? []).sort(),
|
|
isChanged: isChanged,
|
|
});
|
|
i++;
|
|
console.log("");
|
|
}
|
|
const answer = await Promise.all(
|
|
asSets.map(
|
|
async (k) => {
|
|
if (!k.isChanged) return "";
|
|
return `as-set: ${k["AS-SET"]}
|
|
${k["MNT-BY"].map((k) => `mnt-by: ${k}`).join("\n")}
|
|
${k["TECH-C"].map((k) => `tech-c: ${k}`).join("\n")}
|
|
${k["ADMIN-C"].map((k) => `admin-c: ${k}`).join("\n")}
|
|
${k.remarks.map((k) => `remarks: ${k}`).join("\n")}
|
|
${(
|
|
await Promise.all(
|
|
k.members.map(
|
|
async (k) =>
|
|
`remarks: ${await getNetName(k)} from ${cache.get(k)}\nmembers: AS${k}`
|
|
)
|
|
)
|
|
).join("\n")}${k.include.map((k) => `\nmembers: ${k}`)}
|
|
source: RIPE\npassword: ${env["RIPE_PASSWORD"]}\n
|
|
`
|
|
}
|
|
)
|
|
);
|
|
console.log("INFO: sending update to RIPE Syncupdates...");
|
|
const update = await axios.post(
|
|
"https://syncupdates.db.ripe.net",
|
|
`DATA=${answer.join("\n")}`,
|
|
{
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"User-Agent": "net186-as-set/1.0.0 axios curl",
|
|
},
|
|
}
|
|
);
|
|
console.log("INFO: Updates finished.\n");
|
|
console.log(update.data);
|
|
};
|
|
|
|
main();
|