mirror of
https://github.com/metowolf/iplist.git
synced 2025-12-20 00:34:49 +08:00
新增运营商数据解析
This commit is contained in:
59
src/plugins/isp.js
Normal file
59
src/plugins/isp.js
Normal 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
|
||||
Reference in New Issue
Block a user