mirror of
				https://github.com/yuezk/GlobalProtect-openconnect.git
				synced 2025-05-20 07:26:58 -04:00 
			
		
		
		
	refactor: improve the code
This commit is contained in:
		
							
								
								
									
										3
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							| @@ -20,6 +20,7 @@ | |||||||
|     "stdio.h": "c", |     "stdio.h": "c", | ||||||
|     "openconnect.h": "c", |     "openconnect.h": "c", | ||||||
|     "compare": "c", |     "compare": "c", | ||||||
|     "stdlib.h": "c" |     "stdlib.h": "c", | ||||||
|  |     "vpn.h": "c" | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -1,4 +1,5 @@ | |||||||
| use std::ffi::c_void; | use std::ffi::c_void; | ||||||
|  | use tokio::sync::mpsc; | ||||||
|  |  | ||||||
| #[repr(C)] | #[repr(C)] | ||||||
| #[derive(Debug, Copy, Clone)] | #[derive(Debug, Copy, Clone)] | ||||||
| @@ -12,11 +13,24 @@ pub(crate) struct Options { | |||||||
| #[link(name = "vpn")] | #[link(name = "vpn")] | ||||||
| extern "C" { | extern "C" { | ||||||
|     #[link_name = "start"] |     #[link_name = "start"] | ||||||
|     pub(crate) fn connect( |     pub(crate) fn connect(options: *const Options) -> ::std::os::raw::c_int; | ||||||
|         options: *const Options, |  | ||||||
|         on_connected: extern "C" fn(i32, *mut c_void), |  | ||||||
|     ) -> ::std::os::raw::c_int; |  | ||||||
|  |  | ||||||
|     #[link_name = "stop"] |     #[link_name = "stop"] | ||||||
|     pub(crate) fn disconnect(); |     pub(crate) fn disconnect(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | #[no_mangle] | ||||||
|  | extern "C" fn on_vpn_connected(value: i32, sender: *mut c_void) { | ||||||
|  |     let sender = unsafe { &*(sender as *const mpsc::Sender<i32>) }; | ||||||
|  |     sender | ||||||
|  |         .blocking_send(value) | ||||||
|  |         .expect("Failed to send VPN connection code"); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Logger used in the C code. | ||||||
|  | #[no_mangle] | ||||||
|  | extern "C" fn vpn_log(level: i32, message: *const ::std::os::raw::c_char) { | ||||||
|  |     println!("{}: {:?}", level, unsafe { | ||||||
|  |         std::ffi::CStr::from_ptr(message) | ||||||
|  |     }); | ||||||
|  | } | ||||||
|   | |||||||
| @@ -1,5 +1,3 @@ | |||||||
| mod ffi; |  | ||||||
|  |  | ||||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||||
| use std::ffi::{c_void, CString}; | use std::ffi::{c_void, CString}; | ||||||
| use std::sync::Arc; | use std::sync::Arc; | ||||||
| @@ -7,13 +5,7 @@ use std::thread; | |||||||
| use tokio::sync::watch; | use tokio::sync::watch; | ||||||
| use tokio::sync::{mpsc, Mutex}; | use tokio::sync::{mpsc, Mutex}; | ||||||
|  |  | ||||||
| #[no_mangle] | mod ffi; | ||||||
| extern "C" fn on_connected(value: i32, sender: *mut c_void) { |  | ||||||
|     let sender = unsafe { &*(sender as *const mpsc::Sender<i32>) }; |  | ||||||
|     sender |  | ||||||
|         .blocking_send(value) |  | ||||||
|         .expect("Failed to send VPN connection code"); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] | #[derive(Debug, PartialEq, Clone, Copy, Serialize, Deserialize)] | ||||||
| #[serde(rename_all = "lowercase")] | #[serde(rename_all = "lowercase")] | ||||||
| @@ -119,7 +111,7 @@ impl Vpn { | |||||||
|  |  | ||||||
|             // Start the VPN connection, this will block until the connection is closed |             // Start the VPN connection, this will block until the connection is closed | ||||||
|             status_holder.blocking_lock().set(VpnStatus::Connecting); |             status_holder.blocking_lock().set(VpnStatus::Connecting); | ||||||
|             let ret = unsafe { ffi::connect(&oc_options, on_connected) }; |             let ret = unsafe { ffi::connect(&oc_options) }; | ||||||
|  |  | ||||||
|             println!("VPN connection closed with code: {}", ret); |             println!("VPN connection closed with code: {}", ret); | ||||||
|             status_holder.blocking_lock().set(VpnStatus::Disconnected); |             status_holder.blocking_lock().set(VpnStatus::Disconnected); | ||||||
|   | |||||||
| @@ -9,7 +9,6 @@ | |||||||
| #include "vpn.h" | #include "vpn.h" | ||||||
|  |  | ||||||
| void *g_user_data; | void *g_user_data; | ||||||
| on_connected_cb g_on_connected_cb; |  | ||||||
|  |  | ||||||
| static int g_cmd_pipe_fd; | static int g_cmd_pipe_fd; | ||||||
| const char *g_vpnc_script; | const char *g_vpnc_script; | ||||||
| @@ -43,19 +42,18 @@ static void print_progress(__attribute__((unused)) void *_vpninfo, int level, co | |||||||
| static void setup_tun_handler(void *_vpninfo) | static void setup_tun_handler(void *_vpninfo) | ||||||
| { | { | ||||||
|     openconnect_setup_tun_device(_vpninfo, g_vpnc_script, NULL); |     openconnect_setup_tun_device(_vpninfo, g_vpnc_script, NULL); | ||||||
|  |     on_vpn_connected(g_cmd_pipe_fd, g_user_data); | ||||||
|     if (g_on_connected_cb) |  | ||||||
|     { |  | ||||||
|         g_on_connected_cb(g_cmd_pipe_fd, g_user_data); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| /* Initialize VPN connection */ | /* Initialize VPN connection */ | ||||||
| int start(const Options *options, on_connected_cb cb) | int start(const Options *options) | ||||||
| { | { | ||||||
|     struct openconnect_info *vpninfo; |     struct openconnect_info *vpninfo; | ||||||
|     struct utsname utsbuf; |     struct utsname utsbuf; | ||||||
|  |  | ||||||
|  |     g_user_data = options->user_data; | ||||||
|  |     g_vpnc_script = options->script; | ||||||
|  |  | ||||||
|     vpninfo = openconnect_vpninfo_new("PAN GlobalProtect", validate_peer_cert, NULL, NULL, print_progress, NULL); |     vpninfo = openconnect_vpninfo_new("PAN GlobalProtect", validate_peer_cert, NULL, NULL, print_progress, NULL); | ||||||
|  |  | ||||||
|     if (!vpninfo) |     if (!vpninfo) | ||||||
| @@ -95,10 +93,6 @@ int start(const Options *options, on_connected_cb cb) | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     // Essential step |     // Essential step | ||||||
|     // openconnect_setup_tun_device(vpninfo, options->script, NULL); |  | ||||||
|     g_user_data = options->user_data; |  | ||||||
|     g_on_connected_cb = cb; |  | ||||||
|     g_vpnc_script = options->script; |  | ||||||
|     openconnect_set_setup_tun_handler(vpninfo, setup_tun_handler); |     openconnect_set_setup_tun_handler(vpninfo, setup_tun_handler); | ||||||
|  |  | ||||||
|     while (1) |     while (1) | ||||||
|   | |||||||
| @@ -1,5 +1,3 @@ | |||||||
| typedef void (*on_connected_cb)(int32_t, void *); |  | ||||||
|  |  | ||||||
| typedef struct Options { | typedef struct Options { | ||||||
|   const char *server; |   const char *server; | ||||||
|   const char *cookie; |   const char *cookie; | ||||||
| @@ -7,5 +5,8 @@ typedef struct Options { | |||||||
|   void *user_data; |   void *user_data; | ||||||
| } Options; | } Options; | ||||||
|  |  | ||||||
| int start(const Options *options, on_connected_cb cb); | int start(const Options *options); | ||||||
| void stop(); | void stop(); | ||||||
|  |  | ||||||
|  | extern void on_vpn_connected(int cmd_pipe_fd, void *user_data); | ||||||
|  | extern void vpn_log(int level, const char *msg); | ||||||
		Reference in New Issue
	
	Block a user