nullnet_libwallguard/
lib.rs1use 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
16impl WallGuardGrpcInterface {
23 #[allow(clippy::missing_panics_doc)]
24 pub async fn new(addr: &str, port: u16) -> Self {
25 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 .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 request_control_channel(
98 &mut self,
99 token: String,
100 session_type: String,
101 ) -> Result<ControlChannelResponse, String> {
102 let response = self
103 .client
104 .request_control_channel(Request::new(ControlChannelRequest {
105 token,
106 session_type,
107 }))
108 .await
109 .map(tonic::Response::into_inner)
110 .map_err(|e| e.to_string())?;
111
112 Ok(response)
113 }
114}