diff options
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/config.rs | 3 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/wireguard/wireguard_go.rs | 20 | ||||
| -rw-r--r-- | wireguard/libwg/libwg.go | 22 |
3 files changed, 45 insertions, 0 deletions
diff --git a/talpid-core/src/tunnel/wireguard/config.rs b/talpid-core/src/tunnel/wireguard/config.rs index cd87a90124..ed285742c4 100644 --- a/talpid-core/src/tunnel/wireguard/config.rs +++ b/talpid-core/src/tunnel/wireguard/config.rs @@ -148,6 +148,9 @@ impl Config { .add("public_key", peer.public_key.as_bytes().as_ref()) .add("endpoint", peer.endpoint.to_string().as_str()) .add("replace_allowed_ips", "true"); + if let Some(ref psk) = peer.psk { + wg_conf.add("preshared_key", psk.as_bytes().as_ref()); + } for addr in &peer.allowed_ips { wg_conf.add("allowed_ip", addr.to_string().as_str()); } diff --git a/talpid-core/src/tunnel/wireguard/wireguard_go.rs b/talpid-core/src/tunnel/wireguard/wireguard_go.rs index a3d34acc0e..4fbdcefcdd 100644 --- a/talpid-core/src/tunnel/wireguard/wireguard_go.rs +++ b/talpid-core/src/tunnel/wireguard/wireguard_go.rs @@ -13,8 +13,10 @@ use futures::SinkExt; use ipnetwork::IpNetwork; use std::{ ffi::{c_void, CStr}, + future::Future, os::raw::c_char, path::Path, + pin::Pin, }; #[cfg(windows)] use talpid_types::BoxedError; @@ -354,6 +356,21 @@ impl Tunnel for WgGoTunnel { fn stop(mut self: Box<Self>) -> Result<()> { self.stop_tunnel() } + + fn set_config( + &self, + config: Config, + ) -> Pin<Box<dyn Future<Output = std::result::Result<(), super::TunnelError>> + Send>> { + let wg_config_str = config.to_userspace_format(); + let handle = self.handle.unwrap(); + Box::pin(async move { + let status = unsafe { wgSetConfig(handle, wg_config_str.as_ptr() as *const i8) }; + if status != 0 { + return Err(TunnelError::SetConfigError); + } + Ok(()) + }) + } } fn check_wg_status(wg_code: i32) -> Result<()> { @@ -422,6 +439,9 @@ extern "C" { // Returns the file descriptor of the tunnel IPv4 socket. fn wgGetConfig(handle: i32) -> *mut std::os::raw::c_char; + // Sets the config of the WireGuard interface. + fn wgSetConfig(handle: i32, settings: *const i8) -> i32; + // Frees a pointer allocated by the go runtime - useful to free return value of wgGetConfig fn wgFreePtr(ptr: *mut c_void); diff --git a/wireguard/libwg/libwg.go b/wireguard/libwg/libwg.go index 82c6e8205f..e26ea7b7da 100644 --- a/wireguard/libwg/libwg.go +++ b/wireguard/libwg/libwg.go @@ -13,6 +13,7 @@ import ( "bufio" "bytes" "runtime" + "strings" "unsafe" "github.com/mullvad/mullvadvpn-app/wireguard/libwg/tunnelcontainer" @@ -59,6 +60,27 @@ func wgGetConfig(tunnelHandle int32) *C.char { return C.CString(settings.String()) } +//export wgSetConfig +func wgSetConfig(tunnelHandle int32, cSettings *C.char) int32 { + tunnel, err := tunnels.Get(tunnelHandle) + if err != nil { + return ERROR_GENERAL_FAILURE + } + if cSettings == nil { + tunnel.Logger.Errorf("cSettings is null\n") + return ERROR_GENERAL_FAILURE + } + settings := C.GoString(cSettings) + + setError := tunnel.Device.IpcSetOperation(bufio.NewReader(strings.NewReader(settings))) + if setError != nil { + tunnel.Logger.Errorf("Failed to set device configuration\n") + tunnel.Logger.Errorf("%s\n", setError) + return ERROR_GENERAL_FAILURE + } + return 0 +} + //export wgFreePtr func wgFreePtr(ptr unsafe.Pointer) { C.free(ptr) |
