Skip to content

Commit 1ca11a4

Browse files
committed
feat: add self info to get status
1 parent 67efc87 commit 1ca11a4

File tree

6 files changed

+64
-1
lines changed

6 files changed

+64
-1
lines changed

src/client/http/models.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,27 @@ use serde::Serialize;
55
/// Complete status response structure
66
#[derive(Serialize, Debug, Clone)]
77
pub struct StatusResponse {
8+
pub self_info: Option<SelfInfo>,
89
pub traffic: TrafficStats,
910
pub relay: RelayStatusInfo,
1011
pub p2p: P2PStatus,
1112
pub cluster_peers: Vec<ClusterPeerInfo>,
1213
}
1314

15+
/// Self/client information
16+
#[derive(Serialize, Debug, Clone)]
17+
pub struct SelfInfo {
18+
pub identity: String,
19+
pub private_ip: String,
20+
pub mask: String,
21+
pub gateway: String,
22+
pub ciders: Vec<String>,
23+
pub ipv6: String,
24+
pub port: u16,
25+
pub stun_ip: String,
26+
pub stun_port: u16,
27+
}
28+
1429
/// Traffic statistics
1530
#[derive(Serialize, Debug, Clone)]
1631
pub struct TrafficStats {

src/client/prettylog.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ pub async fn build_status_response(
177177
peer: Option<&PeerHandler>,
178178
dev: &DeviceHandler,
179179
) -> StatusResponse {
180+
// Self information from relay
181+
let self_info = relay.get_self_info().await;
182+
180183
// Traffic stats
181184
let traffic = TrafficStats {
182185
receive_bytes: dev.tx_bytes as u64,
@@ -290,6 +293,7 @@ pub async fn build_status_response(
290293
.collect();
291294

292295
StatusResponse {
296+
self_info,
293297
traffic,
294298
relay,
295299
p2p,

src/client/relay.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use crate::client::Args;
22
use crate::client::prettylog::log_handshake_success;
3+
use crate::client::http::SelfInfo;
34
use crate::codec::frame::{Frame, HandshakeFrame, HandshakeReplyFrame, KeepAliveFrame};
45
use crate::crypto::Block;
56
use crate::network::{create_connection, Connection, ConnectionConfig, TCPConnectionConfig};
67
use crate::utils;
78
use std::sync::Arc;
89
use std::time::Instant;
9-
use tokio::sync::mpsc;
10+
use tokio::sync::{mpsc, RwLock};
1011
use tokio::time::{Duration, interval};
1112

1213
const OUTBOUND_BUFFER_SIZE: usize = 1000;
@@ -211,6 +212,9 @@ pub struct RelayHandler {
211212
inbound_tx: mpsc::Sender<Frame>,
212213
block: Arc<Box<dyn Block>>,
213214
metrics: RelayStatus,
215+
// Self information
216+
config: Option<RelayClientConfig>,
217+
handshake_reply: Arc<RwLock<Option<HandshakeReplyFrame>>>,
214218
}
215219

216220
impl RelayHandler {
@@ -222,11 +226,37 @@ impl RelayHandler {
222226
inbound_tx,
223227
block,
224228
metrics: Default::default(),
229+
config: None,
230+
handshake_reply: Arc::new(RwLock::new(None)),
231+
}
232+
}
233+
234+
/// Get self information
235+
pub async fn get_self_info(&self) -> Option<SelfInfo> {
236+
let reply_guard = self.handshake_reply.read().await;
237+
match (&self.config, reply_guard.as_ref()) {
238+
(Some(cfg), Some(reply)) => {
239+
Some(SelfInfo {
240+
identity: cfg.identity.clone(),
241+
private_ip: reply.private_ip.clone(),
242+
mask: reply.mask.clone(),
243+
gateway: reply.gateway.clone(),
244+
ciders: reply.ciders.clone(),
245+
ipv6: cfg.ipv6.clone(),
246+
port: cfg.port,
247+
stun_ip: cfg.stun_ip.clone(),
248+
stun_port: cfg.stun_port,
249+
})
250+
}
251+
_ => None,
225252
}
226253
}
227254

228255
pub fn run_client(&mut self, cfg: RelayClientConfig,
229256
on_ready: mpsc::Sender<HandshakeReplyFrame>) {
257+
// Store config
258+
self.config = Some(cfg.clone());
259+
230260
let (outbound_tx, outbound_rx) = mpsc::channel(cfg.outbound_buffer_size);
231261
let mut client = RelayClient::new(
232262
cfg.clone(),
@@ -235,6 +265,9 @@ impl RelayHandler {
235265
self.block.clone(),
236266
);
237267
self.outbound_tx = Some(outbound_tx);
268+
269+
// Store handshake reply when received
270+
let handshake_reply = self.handshake_reply.clone();
238271

239272
tokio::spawn(async move {
240273
loop {
@@ -258,6 +291,12 @@ impl RelayHandler {
258291

259292
tracing::info!("Handshake complete with {} peers", frame.peer_details.len());
260293

294+
// Store handshake reply in handler
295+
{
296+
let mut guard = handshake_reply.write().await;
297+
*guard = Some(frame.clone());
298+
}
299+
261300
if let Err(e) = on_ready.send(frame.clone()).await {
262301
tracing::error!("on ready send fail: {}", e);
263302
}

src/codec/frame.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ pub struct HandshakeReplyFrame {
153153
/// Used for routing traffic within the VPN
154154
pub gateway: String,
155155

156+
/// ciders to me
157+
pub(crate) ciders: Vec<String>,
158+
156159
/// List of other peers in the same cluster
157160
///
158161
/// Each PeerDetail contains routing information for a peer node,

src/codec/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ impl Parser {
4141
let payload_size = u16::from_be_bytes([buf[6], buf[7]]);
4242

4343
if !Self::validate(magic, version, payload_size, buf) {
44+
tracing::warn!("validate header fail: magic = {} version={} payload_size={} buf size={}", magic, version, payload_size, buf.len());
4445
return Err(FrameError::Invalid.into());
4546
}
4647

src/server/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl Handler {
144144
private_ip: client_config.private_ip.clone(),
145145
mask: client_config.mask.clone(),
146146
gateway: client_config.gateway.clone(),
147+
ciders: client_config.ciders.clone(),
147148
peer_details: route_items,
148149
}))
149150
.await?;

0 commit comments

Comments
 (0)