mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-18 22:16:18 -05:00
122 lines
2.8 KiB
Rust
122 lines
2.8 KiB
Rust
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use specta::Type;
|
|
|
|
use crate::{
|
|
gateway::Gateway,
|
|
session::{GatewayFailureSummary, GatewaySessionSummary, SessionInfo},
|
|
};
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
|
|
pub struct ConnectInfo {
|
|
portal: String,
|
|
gateway: Gateway,
|
|
gateways: Vec<Gateway>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Type, Clone)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ConnectedInfo {
|
|
info: Box<ConnectInfo>,
|
|
session_info: Option<SessionInfo>,
|
|
connected_at: u32,
|
|
members: Vec<GatewaySessionSummary>,
|
|
failures: Vec<GatewayFailureSummary>,
|
|
}
|
|
|
|
impl ConnectedInfo {
|
|
pub fn new(info: ConnectInfo, session_info: Option<SessionInfo>) -> Self {
|
|
Self {
|
|
info: Box::new(info),
|
|
session_info,
|
|
connected_at: unix_timestamp(),
|
|
members: vec![],
|
|
failures: vec![],
|
|
}
|
|
}
|
|
|
|
pub fn with_members(mut self, members: Vec<GatewaySessionSummary>, failures: Vec<GatewayFailureSummary>) -> Self {
|
|
self.members = members;
|
|
self.failures = failures;
|
|
self
|
|
}
|
|
|
|
pub fn members(&self) -> &[GatewaySessionSummary] {
|
|
&self.members
|
|
}
|
|
|
|
pub fn failures(&self) -> &[GatewayFailureSummary] {
|
|
&self.failures
|
|
}
|
|
|
|
pub fn info(&self) -> &ConnectInfo {
|
|
&self.info
|
|
}
|
|
|
|
pub fn session_info(&self) -> Option<&SessionInfo> {
|
|
self.session_info.as_ref()
|
|
}
|
|
|
|
pub fn connected_at(&self) -> u32 {
|
|
self.connected_at
|
|
}
|
|
}
|
|
|
|
fn unix_timestamp() -> u32 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_secs() as u32
|
|
}
|
|
|
|
impl ConnectInfo {
|
|
pub fn new(portal: String, gateway: Gateway, gateways: Vec<Gateway>) -> Self {
|
|
Self {
|
|
portal,
|
|
gateway,
|
|
gateways,
|
|
}
|
|
}
|
|
|
|
pub fn gateway(&self) -> &Gateway {
|
|
&self.gateway
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub enum VpnState {
|
|
Disconnected,
|
|
Failed(String),
|
|
Connecting(Box<ConnectInfo>),
|
|
Connected(Box<ConnectedInfo>),
|
|
Disconnecting,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn connected_state_serializes_session_info() {
|
|
let gateway = Gateway::new("vpn".to_string(), "vpn.example.com".to_string());
|
|
let connect_info = ConnectInfo::new("portal.example.com".to_string(), gateway.clone(), vec![gateway]);
|
|
let session_info = SessionInfo {
|
|
lifetime_secs: Some(43_200),
|
|
allow_extend_session: true,
|
|
..Default::default()
|
|
};
|
|
|
|
let value = serde_json::to_value(VpnState::Connected(Box::new(ConnectedInfo::new(
|
|
connect_info,
|
|
Some(session_info),
|
|
))))
|
|
.unwrap();
|
|
|
|
assert_eq!(value["connected"]["sessionInfo"]["lifetimeSecs"], 43_200);
|
|
assert_eq!(value["connected"]["sessionInfo"]["allowExtendSession"], true);
|
|
assert!(value["connected"]["connectedAt"].as_u64().is_some());
|
|
}
|
|
}
|