Exemple #1
0
 public string LocalizedString(string key, string defaultText = null)
 {
     return(__LocalizedStrings.LocalizedString(key, defaultText));
 }
Exemple #2
0
        /// <summary>
        ///     Checks if keys should be re-generated and regenerate them (if necessary).
        /// </summary>
        /// <remarks>
        ///     All exceptions will be logged and suppressed.
        /// </remarks>
        /// <returns>
        ///     TRUE - if key upgrade not required or upgrade was success
        ///     FALSE - when upgrade was failed
        /// </returns>
        private async Task <bool> DoRegenerateKeysIfNecessary()
        {
            // turn off timer
            __CheckTimer.Change(Timeout.Infinite, Timeout.Infinite);

            if (!IsCanUpdateKey() || !__AppSettings.IsUserLoggedIn())
            {
                return(true);
            }

            bool isError     = false;
            bool isGenerated = false;

            try
            {
                if (__AppSettings.IsWireGuardCredentialsAvailable() && KeysExpiryDate <= DateTime.Now)
                {
                    OnProgress(__LocalizedStrings.LocalizedString("WG_Label_KeysGenerating", "Generating new keys..."));

                    string[] keys = await VpnProtocols.WireGuard.Keys.GenerateKeysAsync();

                    string newPrivateKey = keys[0];
                    string newPublicKey  = keys[1];

                    OnProgress(__LocalizedStrings.LocalizedString("WG_Label_KeysUploading", "Uploading key to IVPN server..."));

                    try
                    {
                        var       calcelationSource = new CancellationTokenSource();
                        IPAddress ip = await ApiServices.Instance.WireguardKeySet(
                            publicKey : newPublicKey,
                            old_key : __AppSettings.WireGuardClientPublicKey,
                            calcelationSource.Token);

                        __AppSettings.SetWireGuardCredentials(newPrivateKey, newPublicKey, false, ip.ToString());
                        isGenerated = true;
                    }
                    catch (RESTApi.IVPNRestRequestApiException restEx)
                    {
                        throw restEx;
                    }
                }
            }
            catch (RESTApi.IVPNRestRequestApiException)
            {
                throw;
            }
            catch (Exception ex)
            {
                isError = true;
                Logging.Info("ERROR: failed to regenerate WireGuard keys: " + ex);
            }
            finally
            {
                TimeSpan interval = KeysExpiryDate - DateTime.Now;
                if (isError == false && interval.TotalMinutes >= RetryCheckDelayOnFailMins)
                {
                    // No errors. (Update succes or no regeneration required)
                    if (isGenerated) // If new key was generated
                    {
                        __AppSettings.Save();
                    }

                    // Start next check on KeysExpiryDate (execute timer only once)
                    __CheckTimer.Change(interval, new TimeSpan(0, 0, 0, 0, -1));
                }
                else
                {
                    // Update failed - try to regenerate in 'RetryCheckDelayOnFailMins'
                    TimeSpan retryInterval = new TimeSpan(0, (int)RetryCheckDelayOnFailMins, 0);
                    if (interval.Ticks > 0 && retryInterval > interval)
                    {
                        retryInterval = interval;
                    }
                    __CheckTimer.Change(retryInterval, new TimeSpan(0, 0, 0, 0, -1));
                }

                OnProgress("");
            }
            return(!isError);
        }