/// <summary> /// Handles the Opened event of the TaskDialog control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private async void StatusDialog_Opened(object sender, EventArgs e) { // Finish drawing dialog. _isWorking = true; Application.DoEvents(); // Do async work and catch errors/cancellation. try { Text = Strings.StatusDialogTextAuth; await Task.Delay(700, _cts.Token); // Wait for UI to catch up. await _api.AuthenticateAsync(_cts.Token); // Login to API to validate user/token. ProgressBarValue = 10; Text = Strings.StatusDialogTextDomain; await _api.ValidateDomainAsync(_cts.Token); // Check if user owns the domain. ProgressBarValue = 20; Text = Strings.StatusDialogTextLogout; await _api.LogoutAsync(_cts.Token); ProgressBarValue = _progressMax; await Task.Delay(700, _cts.Token); } catch (QueryAPIException err) { lock ( _padlock ) { Icon = TaskDialogStandardIcon.Error; ProgressBarState = TaskDialogProgressBarState.Error; InstructionText = Strings.StatusDialogHeadingError; string text = String.Format("Error {0}: {1}", err.Code.ToString(), err.RMessage); if (err.Details != null) { text += "\n\n" + err.Details; } if (err.Url != null) { text += "\n\n" + String.Format(Strings.StatusDialogMoreInfo, err.Url); } Text = text; _isWorking = false; } return; } catch (OperationCanceledException) { lock ( _padlock ) { _isWorking = false; Close(TaskDialogResult.Cancel); } } // No errors, this must mean validation was successful. lock ( _padlock ) { _isWorking = false; Close(TaskDialogResult.Ok); } }
/// <summary> /// Called from a thread delegate. Does the actual work by querying the registrar's API through UDDNSQuery.dll /// and then sleeps according to the interval set in the registry. /// </summary> /// <param name="regPack">The reg pack.</param> private async void PollingThreadWorker(IDictionary <string, string> regPack) { while (!_cts.IsCancellationRequested) { int sleep = Convert.ToInt32(regPack["interval"]) * 60 * 1000; // [minutes] * seconds * milliseconds LogSingleton.I.Debug(String.Format("Sleep set to {0} milliseconds.", sleep)); try { LogSingleton.I.Debug("Initializing QueryAPI object."); using (_api = QueryAPIIndex.I.Factory(regPack["registrar"])) { // Pass credentials to class instance. LogSingleton.I.Debug("Setting registrar credentials to object instance."); _api.Credentials(regPack["userName"], regPack["apiToken"].Replace("ENCRYPTED:", ""), regPack["domain"] ); // Read only. LogSingleton.I.Debug("Executing GetCurrentIPAsync()"); await _api.GetCurrentIPAsync(_cts.Token); LogSingleton.I.Debug("Executing AuthenticateAsync()"); await _api.AuthenticateAsync(_cts.Token); LogSingleton.I.Debug("Executing ValidateDomainAsync()"); await _api.ValidateDomainAsync(_cts.Token); LogSingleton.I.Debug("Executing GetRecordsAsync()"); await _api.GetRecordsAsync(_cts.Token); // Check if DNS is outdated. LogSingleton.I.Debug("Recorded IP(s): " + string.Join(",", _api.RecordedIP.Values)); if (_api.RecordedIP.Count != 1 || _api.RecordedIP.Values.First() != _api.CurrentIP) { LogSingleton.I.Info( 999, String.Format("Updating {0} with the current IP address of {1}.", regPack["domain"], _api.CurrentIP) ); LogSingleton.I.Debug("Executing UpdateRecordAsync()"); await _api.UpdateRecordAsync(_cts.Token); } LogSingleton.I.Debug("Executing LogoutAsync()"); await _api.LogoutAsync(_cts.Token); } _api = null; } catch (QueryAPIException err) { string text = String.Format("Error {0}: {1}", err.Code.ToString(), err.RMessage); if (err.Details != null) { text += "\n\n" + err.Details; } if (err.Url != null) { text += "\n\n" + err.Url; } LogSingleton.I.Error(err.Code, text); sleep /= 4; LogSingleton.I.Debug(String.Format("Sleep set to {0} milliseconds.", sleep)); } catch (OperationCanceledException) { LogSingleton.I.Debug("Caught OperationCanceledException"); return; } // Give OnStop a chance to Abort thread before logging the below statement. try { await Task.Delay(1000, _cts.Token); } catch (OperationCanceledException) { return; } LogSingleton.I.Debug(String.Format("Sleeping {0} milliseconds.", sleep)); try { await Task.Delay(sleep, _cts.Token); } catch (OperationCanceledException) { return; } } }
/// <summary> /// Called from a thread delegate. Does the actual work by querying the registrar's API through UDDNSQuery.dll /// and then sleeps according to the interval set in the registry. /// </summary> /// <param name="regPack">The reg pack.</param> private async void PollingThreadWorker( IDictionary<string, string> regPack ) { while ( !_cts.IsCancellationRequested ) { int sleep = Convert.ToInt32( regPack["interval"] ) * 60 * 1000; // [minutes] * seconds * milliseconds LogSingleton.I.Debug( String.Format( "Sleep set to {0} milliseconds.", sleep ) ); try { LogSingleton.I.Debug( "Initializing QueryAPI object." ); using ( _api = QueryAPIIndex.I.Factory( regPack["registrar"] ) ) { // Pass credentials to class instance. LogSingleton.I.Debug( "Setting registrar credentials to object instance." ); _api.Credentials( regPack["userName"], regPack["apiToken"].Replace( "ENCRYPTED:", "" ), regPack["domain"] ); // Read only. LogSingleton.I.Debug( "Executing GetCurrentIPAsync()" ); await _api.GetCurrentIPAsync( _cts.Token ); LogSingleton.I.Debug( "Executing AuthenticateAsync()" ); await _api.AuthenticateAsync( _cts.Token ); LogSingleton.I.Debug( "Executing ValidateDomainAsync()" ); await _api.ValidateDomainAsync( _cts.Token ); LogSingleton.I.Debug( "Executing GetRecordsAsync()" ); await _api.GetRecordsAsync( _cts.Token ); // Check if DNS is outdated. LogSingleton.I.Debug( "Recorded IP(s): " + string.Join( ",", _api.RecordedIP.Values ) ); if ( _api.RecordedIP.Count != 1 || _api.RecordedIP.Values.First() != _api.CurrentIP ) { LogSingleton.I.Info( 999, String.Format( "Updating {0} with the current IP address of {1}.", regPack["domain"], _api.CurrentIP ) ); LogSingleton.I.Debug( "Executing UpdateRecordAsync()" ); await _api.UpdateRecordAsync( _cts.Token ); } LogSingleton.I.Debug( "Executing LogoutAsync()" ); await _api.LogoutAsync( _cts.Token ); } _api = null; } catch ( QueryAPIException err ) { string text = String.Format( "Error {0}: {1}", err.Code.ToString(), err.RMessage ); if ( err.Details != null ) text += "\n\n" + err.Details; if ( err.Url != null ) text += "\n\n" + err.Url; LogSingleton.I.Error( err.Code, text ); sleep /= 4; LogSingleton.I.Debug( String.Format( "Sleep set to {0} milliseconds.", sleep ) ); } catch ( OperationCanceledException ) { LogSingleton.I.Debug( "Caught OperationCanceledException" ); return; } // Give OnStop a chance to Abort thread before logging the below statement. try { await Task.Delay( 1000, _cts.Token ); } catch ( OperationCanceledException ) { return; } LogSingleton.I.Debug( String.Format( "Sleeping {0} milliseconds.", sleep ) ); try { await Task.Delay( sleep, _cts.Token ); } catch ( OperationCanceledException ) { return; } } }