#bgp #bgpkit #function #collector #data-structures #country #collection

bin+lib bgpkit-commons

BGP 相关常见数据和函数的库

11 个不稳定版本 (5 个破坏性更新)

0.6.0 2024 年 6 月 26 日
0.5.2 2024 年 3 月 21 日
0.5.0 2024 年 1 月 30 日
0.4.3 2023 年 11 月 29 日
0.1.0 2023 年 9 月 3 日

#484 in 网络编程


用于 bgpkit-broker

MIT 许可证

74KB
1.5K SLoC

BGPKIT Commons

此说明是由库的文档注释使用 cargo-readme 生成的。请参阅 Rust 文档网站以获取完整文档

Crates.io Docs.rs License Discord

概述

BGPKIT-Commons 是一个用于 BGP 相关常见数据和函数的库。

分类

MRT 收集器

此 crate 提供了三个函数来从 RouteViews 和 RIPE RIS 获取 MRT 收集器的完整列表

  • get_routeviews_collectors()
  • get_riperis_collectors()
  • get_all_collectors()

数据结构

收集器被抽象为以下结构体

use chrono::NaiveDateTime;
use bgpkit_commons::collectors::MrtCollectorProject;
 /// MRT collector meta information
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct MrtCollector {
    /// name of the collector
    pub name: String,
    /// collector project
    pub project: MrtCollectorProject,
    /// MRT data files root URL
    pub data_url: String,
    /// collector activation timestamp
    pub activated_on: NaiveDateTime,
    /// collector deactivation timestamp (None for active collectors)
    pub deactivated_on: Option<NaiveDateTime>,
    /// country where the collect runs in
    pub country: String,
}

其中 MrtCollectorProject 定义为

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum MrtCollectorProject {
    RouteViews,
    RipeRis,
}

使用示例

请参阅以下示例以了解使用方法

use bgpkit_commons::collectors::get_routeviews_collectors;

println!("get route views collectors");
let mut routeviews_collectors = get_routeviews_collectors().unwrap();
routeviews_collectors.sort();
let earliest = routeviews_collectors.first().unwrap();
let latest = routeviews_collectors.last().unwrap();
println!("\t total of {} collectors", routeviews_collectors.len());
println!(
    "\t earliest collector: {} (activated on {})",
    earliest.name, earliest.activated_on
);
println!(
    "\t latest collector: {} (activated on {})",
    latest.name, latest.activated_on
);

AS 名称和国家

asnames 是一个用于自治系统 (AS) 名称和国家查找的模块

数据源

数据结构

use serde::{Deserialize, Serialize};
#[derive(Debug, Clone)]
pub struct AsName {
    pub asn: u32,
    pub name: String,
    pub country: String,
    pub population: Option<AsnPopulationData>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct As2orgInfo {
    pub name: String,
    pub country: String,
    pub org_id: String,
    pub org_name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AsnPopulationData {
    pub user_count: i64,
    pub percent_country: f64,
    pub percent_global: f64,
    pub sample_count: i64,
}

使用示例

use std::collections::HashMap;
use bgpkit_commons::asnames::{AsName, get_asnames};

let asnames: HashMap<u32, AsName> = get_asnames().unwrap();
assert_eq!(asnames.get(&3333).unwrap().name, "RIPE-NCC-AS Reseaux IP Europeens Network Coordination Centre (RIPE NCC)");
assert_eq!(asnames.get(&400644).unwrap().name, "BGPKIT-LLC");
assert_eq!(asnames.get(&400644).unwrap().country, "US");

国家详细信息

数据结构

pub struct Country {
    /// 2-letter country code
    pub code: String,
    /// 3-letter country code
    pub code3: String,
    /// Country name
    pub name: String,
    /// Capital city
    pub capital: String,
    /// Continent
    pub continent: String,
    /// Country's top-level domain
    pub ltd: Option<String>,
    /// Neighboring countries in 2-letter country code
    pub neighbors: Vec<String>,
}

使用示例

use bgpkit_commons::countries::Countries;

let countries = Countries::new().unwrap();
assert_eq!(
    countries.lookup_by_code("US").unwrap().name,
    "United States"
);
assert_eq!(countries.lookup_by_name("united states").len(), 2);
assert_eq!(countries.lookup_by_name("united kingdom").len(), 1);

RPKI 工具

数据源

使用示例

使用Cloudflare RPKI门户检查当前RPKI验证
use std::str::FromStr;
use ipnet::IpNet;
use bgpkit_commons::rpki::{RpkiTrie, RpkiValidation};

let trie = RpkiTrie::from_cloudflare().unwrap();
let prefix = IpNet::from_str("1.1.1.0/24").unwrap();
assert_eq!(trie.validate(&prefix, 13335), RpkiValidation::Valid);
检查给定日期的RPKI验证
use std::str::FromStr;
use chrono::NaiveDate;
use ipnet::IpNet;
use bgpkit_commons::rpki::{RpkiTrie, RpkiValidation};

let rpki = RpkiTrie::from_ripe_historical(NaiveDate::from_ymd_opt(2023, 1, 1).unwrap()).unwrap();
// let rpki = RpkiTrie::from_rpkiviews_historical(NaiveDate::from_ymd_opt(2023, 1, 1).unwrap()).unwrap();
let prefix = IpNet::from_str("1.1.1.0/24").unwrap();
assert_eq!(rpki.validate(&prefix, 13335), RpkiValidation::Valid);

Bogon工具

我们提供了一种工具来检查IP前缀或ASN是否是bogon。

数据源

IANA特殊注册表

使用示例

use bgpkit_commons::bogons::Bogons;

let bogons = Bogons::new().unwrap();
assert!(bogons.matches_str("10.0.0.0/9"));
assert!(bogons.matches_str("112"));
assert!(bogons.is_bogon_prefix(&"2001::/24".parse().unwrap()));
assert!(bogons.is_bogon_asn(65535));

AS级关系

bgpkit-commons提供了访问由BGPKIT生成的AS级关系数据。

数据源

数据结构

pub enum AsRelationship {
    ProviderCustomer,
    CustomerProvider,
    PeerPeer,
}

pub struct As2relBgpkitData {
    pub rel: AsRelationship,
    pub peers_count: u32,
    pub max_peer_count: u32,
}

使用示例

let bgpkit = bgpkit_commons::as2rel::As2relBgpkit::new().unwrap();
let (v4_data, v6_data) = bgpkit.lookup_pair(400644, 54825);
assert!(v4_data.is_none());
assert!(v6_data.is_some());
assert_eq!(v6_data.unwrap().rel, bgpkit_commons::as2rel::AsRelationship::CustomerProvider);

功能标志

  • rustls:使用rustls而不是native-tls进行底层HTTPS请求

命令行工具

此crate还提供了一个命令行工具bgpkit-commons,便于访问数据和工具。

安装

在macOS上

brew install bgpkit/tap/bgpkit-commons

在其他平台

cargo binstall bgpkit-commons

将所有数据导出为JSON

bgpkit-commons export --help
Export data to local files

 Usage: bgpkit-commons export [OPTIONS]

 Options:
   -o, --output-dir <OUTPUT_DIR>  output directory [default: .]
   -h, --help                     Print help
   -V, --version                  Print version

由BGPKIT团队用❤️构建

https://bgpkit.com/favicon.ico

许可证

MIT

依赖项

~7–24MB
~391K SLoC