/// <summary> /// Send a request for connection statistics from the tunnel service via named pipe. /// </summary> /// <returns>Connection status received from the tunnel named pipe.</returns> private Models.ConnectionStatus QueryConnectionStatisticsFromTunnel() { NamedPipeClientStream tunnelPipe = null; try { tunnelPipe = ConnectWGTunnelNamedPipe(); if (tunnelPipe != null) { IPC.WriteToPipe(tunnelPipe, new WireGuard.IPCMessage(WireGuard.IPCCommand.WgGet)); var ret = ParseStatusResponse(IPC.ReadFromPipe(tunnelPipe)); tunnelPipe.Close(); return(ret); } } catch (Exception e) { ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Debug); } finally { if (tunnelPipe != null && tunnelPipe.IsConnected) { tunnelPipe.Close(); } } return(new Models.ConnectionStatus() { Status = Models.ConnectionState.Protected, ConnectionStability = Models.ConnectionStability.NoSignal }); }
/// <summary> /// Switches the VPN server to a different endpoint. /// </summary> /// <param name="endpoint">Endpoint (VPN server) IP address.</param> /// <param name="publicKey">Public key of remote VPN server.</param> /// <returns>True if successfully sent the switch command.</returns> public bool SwitchServer(string endpoint, string publicKey) { NamedPipeClientStream tunnelPipe = null; try { using (tunnelPipe = ConnectWGTunnelNamedPipe()) { if (tunnelPipe == null) { return(false); } var serverSwitchRequest = new IPCMessage(IPCCommand.WgSet); serverSwitchRequest.AddAttribute("replace_peers", "true"); serverSwitchRequest.AddAttribute("public_key", BitConverter.ToString(Convert.FromBase64String(publicKey)).Replace("-", string.Empty).ToLower()); serverSwitchRequest.AddAttribute("endpoint", endpoint); var allowedIPs = ProductConstants.AllowedIPs.Split(',').Select(ip => ip.Trim()).ToList(); allowedIPs.ForEach(ip => serverSwitchRequest.AddAttribute("allowed_ip", ip)); IPC.WriteToPipe(tunnelPipe, serverSwitchRequest); var response = IPC.ReadFromPipe(tunnelPipe); var errno = response["errno"].FirstOrDefault(); if (errno == null || errno != "0") { throw new Exception("Set request UAPI error " + errno); } // Update IP info. Manager.IPInfoUpdater.ForceUpdate(); } } catch (Exception e) { ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Error); return(false); } finally { if (tunnelPipe != null && tunnelPipe.IsConnected) { tunnelPipe.Close(); } } return(true); }