Пример #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 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);
        }
Пример #2
0
        /// <summary>
        /// Sends a command to the broker service to initiate the captive portal detection.
        /// </summary>
        public void DetectCaptivePortal()
        {
            var ipcDetectCaptivePortalMsg = new IPCMessage(IPCCommand.IpcDetectCaptivePortal);

            ipcDetectCaptivePortalMsg.AddAttribute("ip", Manager.Settings.Network.CaptivePortalDetectionIp);
            brokerIPC.WriteToPipe(ipcDetectCaptivePortalMsg, promptRestartBrokerServiceOnFail: false);
        }
Пример #3
0
        /// <summary>
        /// Saves a config file to the users' AppData folder and sends a connect command to the Broker.
        /// </summary>
        /// <returns>True if successfully sent connection command.</returns>
        public bool Connect()
        {
            var configFilePath = ProductConstants.FirefoxPrivateNetworkConfFile;
            var connectMessage = new IPCMessage(IPCCommand.IpcConnect);

            connectMessage.AddAttribute("config", configFilePath);

            var writeToPipeResult = brokerIPC.WriteToPipe(connectMessage);

            if (writeToPipeResult)
            {
                uptimeStart = DateTime.MinValue;
                SetConnecting();
            }

            return(writeToPipeResult);
        }
Пример #4
0
        /// <summary>
        /// Saves a config file to the users' AppData folder and sends a connect command to the Broker.
        /// </summary>
        /// <returns>True if successfully sent connection command.</returns>
        public bool Connect()
        {
            uptimeStart = DateTime.MinValue;
            SetConnecting();
            Manager.Broker.ReportHeartbeat();

            ErrorHandling.DebugLogger.LogDebugMsg("Starting broker");
            if (!InitiateBroker())
            {
                IsConnecting    = false;
                IsDisconnecting = false;
                return(false);
            }

            var configFilePath = ProductConstants.FirefoxPrivateNetworkConfFile;
            var connectMessage = new IPCMessage(IPCCommand.IpcConnect);

            connectMessage.AddAttribute("config", configFilePath);
            brokerIPC.WriteToPipe(connectMessage);

            return(true);
        }