Exemplo n.º 1
0
        /// <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 WireGuard.IPCMessage(WireGuard.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);
                    }
                }
            }
            catch (Exception e)
            {
                ErrorHandling.ErrorHandler.Handle(e, ErrorHandling.LogLevel.Error);
                return(false);
            }
            finally
            {
                if (tunnelPipe != null && tunnelPipe.IsConnected)
                {
                    tunnelPipe.Close();
                }
            }

            return(true);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Tunnel"/> class.
 /// </summary>
 public Tunnel()
 {
     brokerIPC = new IPC(new NamedPipeClientStream(".", ProductConstants.InternalAppName, direction: PipeDirection.InOut, options: PipeOptions.Asynchronous));
     brokerIPC.StartClientListenerThread();
 }