iota_core/traffic_controller/
nodefw_client.rs

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