新增运营商数据解析

This commit is contained in:
metowolf
2025-02-07 12:03:34 +08:00
parent 04684a9d53
commit 147c73a5d6
4 changed files with 71 additions and 126 deletions

59
src/plugins/isp.js Normal file
View File

@@ -0,0 +1,59 @@
const vinyl = require('vinyl')
const IPDB = require('ipdb')
const ipdb_range = require('@ipdb/range')
const ProgressBar = require('progress')
const ISP_MAP = {
// 中国电信
'chinatelecom.com.cn': 'chinatelecom',
// 中国联通
'chinaunicom.com': 'chinaunicom',
// 中国移动
'chinamobile.com': 'chinamobile',
// 中国教育网
'cernet.edu.cn': 'cernet',
}
const plugin = (through2, file, cb) => {
console.log('Parse ipdb')
const ipdb = new IPDB(file.contents, {
patches: [ipdb_range]
})
let bar = new ProgressBar(':bar :current/:total', { total: ipdb.meta.node_count })
let result = []
let ip = '0.0.0.0'
while (true) {
const info = ipdb.find(ip).data
const isp = info.isp_domain || ''
if (isp.length > 0 && ISP_MAP[isp]) {
const ispName = ISP_MAP[isp]
if (!result[ispName]) {
result[ispName] = []
}
result[ispName].push(`${info.range.from}/${info.bitmask}`)
}
bar.tick()
ip = info.range.next
if (ip === '0.0.0.0') break
}
console.log()
for (let [isp, cidrs] of Object.entries(result)) {
let temp = new vinyl({
cwd: '/',
base: '/',
path: `/${isp}.txt`,
contents: new Buffer.from(cidrs.join('\n'))
})
through2.push(temp)
}
cb()
}
module.exports = plugin