nullnet_libwallguard/
lib.rs

1use std::time::Duration;
2use tonic::Request;
3pub use tonic::Streaming;
4use tonic::transport::Channel;
5
6use crate::proto::wallguard::wall_guard_client::WallGuardClient;
7pub use crate::proto::wallguard::*;
8
9mod proto;
10
11#[derive(Clone)]
12pub struct WallGuardGrpcInterface {
13    client: WallGuardClient<Channel>,
14}
15
16// static CA_CERT: std::sync::LazyLock<Certificate> = std::sync::LazyLock::new(|| {
17//     Certificate::from_pem(
18//         std::fs::read_to_string("tls/ca.pem").expect("Failed to read CA certificate"),
19//     )
20// });
21
22impl WallGuardGrpcInterface {
23    #[allow(clippy::missing_panics_doc)]
24    pub async fn new(addr: &str, port: u16) -> Self {
25        // let tls = ClientTlsConfig::new().ca_certificate(CA_CERT.to_owned());
26        let s = format!("http://{addr}:{port}");
27
28        let Ok(channel) = Channel::from_shared(s)
29            .expect("Failed to parse address")
30            .timeout(Duration::from_secs(10))
31            // .tls_config(tls)
32            // .expect("Failed to configure up TLS")
33            .connect()
34            .await
35        else {
36            log::warn!("Failed to connect to the server. Retrying in 10 seconds...");
37            tokio::time::sleep(std::time::Duration::from_secs(10)).await;
38            return Box::pin(WallGuardGrpcInterface::new(addr, port)).await;
39        };
40
41        Self {
42            client: WallGuardClient::new(channel).max_decoding_message_size(50 * 1024 * 1024),
43        }
44    }
45
46    #[allow(clippy::missing_errors_doc)]
47    pub async fn heartbeat(
48        &mut self,
49        app_id: String,
50        app_secret: String,
51        device_version: String,
52        device_uuid: String,
53    ) -> Result<Streaming<HeartbeatResponse>, String> {
54        self.client
55            .heartbeat(Request::new(HeartbeatRequest {
56                app_id,
57                app_secret,
58                device_version,
59                device_uuid,
60            }))
61            .await
62            .map(tonic::Response::into_inner)
63            .map_err(|e| e.to_string())
64    }
65
66    #[allow(clippy::missing_errors_doc)]
67    pub async fn handle_packets(&mut self, message: Packets) -> Result<CommonResponse, String> {
68        self.client
69            .handle_packets(Request::new(message))
70            .await
71            .map(tonic::Response::into_inner)
72            .map_err(|e| e.to_string())
73    }
74
75    #[allow(clippy::missing_errors_doc)]
76    pub async fn handle_config(
77        &mut self,
78        message: ConfigSnapshot,
79    ) -> Result<CommonResponse, String> {
80        self.client
81            .handle_config(Request::new(message))
82            .await
83            .map(tonic::Response::into_inner)
84            .map_err(|e| e.to_string())
85    }
86
87    #[allow(clippy::missing_errors_doc)]
88    pub async fn handle_logs(&mut self, message: Logs) -> Result<CommonResponse, String> {
89        self.client
90            .handle_logs(Request::new(message))
91            .await
92            .map(tonic::Response::into_inner)
93            .map_err(|e| e.to_string())
94    }
95
96    #[allow(clippy::missing_errors_doc)]
97    pub async fn handle_system_resources(
98        &mut self,
99        message: SystemResources,
100    ) -> Result<CommonResponse, String> {
101        self.client
102            .handle_system_resources(Request::new(message))
103            .await
104            .map(tonic::Response::into_inner)
105            .map_err(|e| e.to_string())
106    }
107
108    #[allow(clippy::missing_errors_doc)]
109    pub async fn request_control_channel(
110        &mut self,
111        token: String,
112        session_type: String,
113    ) -> Result<ControlChannelResponse, String> {
114        let response = self
115            .client
116            .request_control_channel(Request::new(ControlChannelRequest {
117                token,
118                session_type,
119            }))
120            .await
121            .map(tonic::Response::into_inner)
122            .map_err(|e| e.to_string())?;
123
124        Ok(response)
125    }
126}