mirror of
				https://github.com/yuezk/GlobalProtect-openconnect.git
				synced 2025-05-20 07:26:58 -04:00 
			
		
		
		
	refactor: rewrite
This commit is contained in:
		
							
								
								
									
										34
									
								
								common/src/cmd/connect.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								common/src/cmd/connect.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| use super::{Command, CommandContext, CommandError}; | ||||
| use crate::{ResponseData, VpnStatus}; | ||||
| use async_trait::async_trait; | ||||
| use serde::{Deserialize, Serialize}; | ||||
|  | ||||
| #[derive(Debug, Serialize, Deserialize, Clone)] | ||||
| pub struct Connect { | ||||
|     server: String, | ||||
|     cookie: String, | ||||
| } | ||||
|  | ||||
| impl Connect { | ||||
|     pub fn new(server: String, cookie: String) -> Self { | ||||
|         Self { server, cookie } | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[async_trait] | ||||
| impl Command for Connect { | ||||
|     async fn handle(&self, context: CommandContext) -> Result<ResponseData, CommandError> { | ||||
|         let vpn = context.server_context.vpn(); | ||||
|         let status = vpn.status().await; | ||||
|  | ||||
|         if status != VpnStatus::Disconnected { | ||||
|             return Err(format!("VPN is already in state: {:?}", status).into()); | ||||
|         } | ||||
|  | ||||
|         if let Err(err) = vpn.connect(&self.server, &self.cookie).await { | ||||
|             return Err(err.to_string().into()); | ||||
|         } | ||||
|  | ||||
|         Ok(ResponseData::Empty) | ||||
|     } | ||||
| } | ||||
							
								
								
									
										15
									
								
								common/src/cmd/disconnect.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								common/src/cmd/disconnect.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,15 @@ | ||||
| use super::{Command, CommandContext, CommandError}; | ||||
| use crate::ResponseData; | ||||
| use async_trait::async_trait; | ||||
| use serde::{Deserialize, Serialize}; | ||||
|  | ||||
| #[derive(Debug, Serialize, Deserialize, Clone)] | ||||
| pub struct Disconnect; | ||||
|  | ||||
| #[async_trait] | ||||
| impl Command for Disconnect { | ||||
|     async fn handle(&self, context: CommandContext) -> Result<ResponseData, CommandError> { | ||||
|         context.server_context.vpn().disconnect().await; | ||||
|         Ok(ResponseData::Empty) | ||||
|     } | ||||
| } | ||||
							
								
								
									
										54
									
								
								common/src/cmd/mod.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								common/src/cmd/mod.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,54 @@ | ||||
| use crate::{response::ResponseData, server::ServerContext}; | ||||
| use async_trait::async_trait; | ||||
| use core::fmt::Debug; | ||||
| use std::{ | ||||
|     fmt::{self, Display}, | ||||
|     sync::Arc, | ||||
| }; | ||||
|  | ||||
| mod connect; | ||||
| mod disconnect; | ||||
| mod status; | ||||
|  | ||||
| pub use connect::Connect; | ||||
| pub use disconnect::Disconnect; | ||||
| pub use status::Status; | ||||
|  | ||||
| #[derive(Debug)] | ||||
| pub(crate) struct CommandContext { | ||||
|     server_context: Arc<ServerContext>, | ||||
| } | ||||
|  | ||||
| impl From<Arc<ServerContext>> for CommandContext { | ||||
|     fn from(server_context: Arc<ServerContext>) -> Self { | ||||
|         Self { server_context } | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[derive(Debug)] | ||||
| pub(crate) struct CommandError { | ||||
|     message: String, | ||||
| } | ||||
|  | ||||
| impl From<String> for CommandError { | ||||
|     fn from(message: String) -> Self { | ||||
|         Self { message } | ||||
|     } | ||||
| } | ||||
|  | ||||
| impl Display for CommandError { | ||||
|     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||
|         write!(f, "CommandError {:#?}", self.message) | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[async_trait] | ||||
| pub(crate) trait Command: Send + Sync { | ||||
|     async fn handle(&self, context: CommandContext) -> Result<ResponseData, CommandError>; | ||||
| } | ||||
|  | ||||
| impl Debug for dyn Command { | ||||
|     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||||
|         write!(f, "Command") | ||||
|     } | ||||
| } | ||||
							
								
								
									
										16
									
								
								common/src/cmd/status.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								common/src/cmd/status.rs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,16 @@ | ||||
| use super::{Command, CommandContext, CommandError}; | ||||
| use crate::ResponseData; | ||||
| use async_trait::async_trait; | ||||
| use serde::{Deserialize, Serialize}; | ||||
|  | ||||
| #[derive(Debug, Serialize, Deserialize, Clone)] | ||||
| pub struct Status; | ||||
|  | ||||
| #[async_trait] | ||||
| impl Command for Status { | ||||
|     async fn handle(&self, context: CommandContext) -> Result<ResponseData, CommandError> { | ||||
|         let status = context.server_context.vpn().status().await; | ||||
|  | ||||
|         Ok(ResponseData::Status(status)) | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user