#data-stream #bgp #real-time #json #message #parser #ris-live

bin+lib ris-live-rs

RIS-Live 实时 BGP 数据流 crate

4 个版本 (2 个重大更新)

0.2.0 2022 年 7 月 9 日
0.1.1 2021 年 12 月 2 日
0.1.0 2021 年 11 月 28 日
0.0.1 2021 年 11 月 24 日

#293 in WebSocket

MIT 许可证

35KB
599

ris-live-rs

Rust

RIS-Live 实时 BGP 消息流 JSON 数据提供解析函数。

主要解析函数 parse_ris_live_message 将 JSON 格式的消息字符串转换为 BgpElem 的向量。

示例

use serde_json::json;
use tungstenite::{connect, Message};
use url::Url;
use ris_live_rs::error::ParserRisliveError;
use ris_live_rs::parse_ris_live_message;

const RIS_LIVE_URL: &str = "ws://ris-live.ripe.net/v1/ws/?client=rust-bgpkit-parser";

/// This is an example of subscribing to RIS-Live's streaming data.
///
/// For more RIS-Live details, check out their documentation at https://ris-live.ripe.net/manual/
fn main() {
    // connect to RIPE RIS Live websocket server
    let (mut socket, _response) =
        connect(Url::parse(RIS_LIVE_URL).unwrap())
            .expect("Can't connect to RIS Live websocket server");

    // subscribe to messages from one collector
    let msg = json!({"type": "ris_subscribe", "data": null}).to_string();
    socket.write_message(Message::Text(msg)).unwrap();

    loop {
        let msg = socket.read_message().expect("Error reading message").to_string();
        if msg.is_empty() {
            continue
        }
        match parse_ris_live_message(msg.as_str()) {
            Ok(elems) => {
                for e in elems {
                    println!("{}", e);
                }
            }
            Err(error) => {
                if let ParserRisliveError::ElemEndOfRibPrefix = error {
                    println!("{:?}", &error);
                    println!("{}", msg);
                    continue
                }
                break;
            }
        }
    }
}

过滤

ris-live-rs 支持通过组合自定义 ris-live 订阅消息来过滤消息。使用 compose_subscription_message 函数创建过滤消息。

pub fn compose_subscription_message(
    host: &String,
    msg_type: &Option<String>,
    require: &Option<String>,
    peer: &Option<String>,
    prefix: &Option<String>,
    path: &Option<String>,
    more_specific: &bool,
    less_specific: &bool,
) -> String {
    ...
}

// subscribe to messages from one collector
let msg = compose_subscription_message(
    &opts.host,
    &opts.msg_type,
    &opts.require,
    &opts.peer,
    &opts.prefix,
    &opts.path,
    &opts.more_specific,
    &opts.less_specific
);
println!("{}", &msg);
socket.write_message(Message::Text(msg)).unwrap();

ris-live-reader

ris-live-rs 库还附带了一个简单的命令行程序,它支持过滤和不同的输出格式:ris-live-reader

asciicast

完整命令行选项

ris-live-reader 0.2.0
ris-live-reader is a simple cli tool that can stream BGP data from RIS-Live project with websocket. Check out
https://ris-live.ripe.net/ for more data source information

USAGE:
    ris-live-reader [FLAGS] [OPTIONS]

FLAGS:
    -h, --help             Prints help information
        --json             Output as JSON objects
        --less-specific    Match prefixes that are less specific (contain) `prefix`
        --more-specific    Match prefixes that are more specific (part of) `prefix`
        --pretty           Pretty-print JSON output
        --raw              Print out raw message without parsing
    -V, --version          Prints version information

OPTIONS:
        --client <client>              client name to identify the stream [default: ris-live-rs]
        --host <host>                  Filter by RRC host: e.g. rrc01. Use "all" for the firehose [default: rrc21]
        --msg-type <msg-type>          Only include messages of a given BGP or RIS type: UPDATE, OPEN, NOTIFICATION,
                                       KEEPALIVE, or RIS_PEER_STATE
        --path <path>                  ASN or pattern to match against the AS PATH attribute
        --peer <peer>                  Only include messages sent by the given BGP peer
        --prefix <prefix>              Filter UPDATE messages by prefixes in announcements or withdrawals
        --require <require>            Only include messages containing a given key
        --update-type <update-type>    Only a given BGP update type: announcement (a) or withdrawal (w)

安装

使用 cargo 通过以下命令安装:

cargo install ris-live-rs

或者检出仓库并运行

cargo install --path .

程序 ris-live-reader 将安装到您的 $CARGO_HOME/bin (例如 ~/.cargo/bin)。

使用 Docker 运行

docker run --rm -it bgpkit/ris-live-reader --help

最低支持的 Rust 版本 (MSRV)

1.46.0

由 BGPKIT 团队用 ❤️ 构建

BGPKIT 是一个小型团队,专注于在 Rust 中构建最好的 BGP 数据工具。我们拥有超过 10 年的 BGP 数据工作经验,并相信我们的工作可以帮助更多公司在自己的领域开始跟踪 BGP 数据。了解更多关于我们提供的服务,请访问 https://bgpkit.com

https://bgpkit.com/favicon.ico

依赖项

~7MB
~147K SLoC