sui_core/traffic_controller/
nodefw_client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright (c) 2021, Facebook, Inc. and its affiliates
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BlockAddresses {
    pub addresses: Vec<BlockAddress>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
pub struct BlockAddress {
    pub source_address: String,
    pub destination_port: u16,
    pub ttl: u64,
}

pub struct NodeFWClient {
    client: reqwest::Client,
    remote_fw_url: String,
}

impl NodeFWClient {
    pub fn new(remote_fw_url: String) -> Self {
        Self {
            client: reqwest::Client::new(),
            remote_fw_url,
        }
    }

    pub async fn block_addresses(&self, addresses: BlockAddresses) -> Result<(), reqwest::Error> {
        let response = self
            .client
            .post(format!("{}/block_addresses", self.remote_fw_url))
            .json(&addresses)
            .send()
            .await?;
        match response.error_for_status() {
            Ok(_) => Ok(()),
            Err(e) => Err(e),
        }
    }

    pub async fn list_addresses(&self) -> Result<BlockAddresses, reqwest::Error> {
        self.client
            .get(format!("{}/list_addresses", self.remote_fw_url))
            .send()
            .await?
            .error_for_status()?
            .json::<BlockAddresses>()
            .await
    }
}