sui_core/traffic_controller/
nodefw_client.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Debug, Clone)]
8pub struct BlockAddresses {
9    pub addresses: Vec<BlockAddress>,
10}
11
12#[derive(Serialize, Deserialize, Debug, Clone, Hash, PartialEq, Eq)]
13pub struct BlockAddress {
14    pub source_address: String,
15    pub destination_port: u16,
16    pub ttl: u64,
17}
18
19pub struct NodeFWClient {
20    client: reqwest::Client,
21    remote_fw_url: String,
22}
23
24impl NodeFWClient {
25    pub fn new(remote_fw_url: String) -> Self {
26        Self {
27            client: reqwest::Client::new(),
28            remote_fw_url,
29        }
30    }
31
32    pub async fn block_addresses(&self, addresses: BlockAddresses) -> Result<(), reqwest::Error> {
33        let response = self
34            .client
35            .post(format!("{}/block_addresses", self.remote_fw_url))
36            .json(&addresses)
37            .send()
38            .await?;
39        match response.error_for_status() {
40            Ok(_) => Ok(()),
41            Err(e) => Err(e),
42        }
43    }
44
45    pub async fn list_addresses(&self) -> Result<BlockAddresses, reqwest::Error> {
46        self.client
47            .get(format!("{}/list_addresses", self.remote_fw_url))
48            .send()
49            .await?
50            .error_for_status()?
51            .json::<BlockAddresses>()
52            .await
53    }
54}