/// <summary>
 /// Replaces the old profile with the specified ID with the specified
 /// new profile.
 /// </summary>
 /// <param name="oldProfileId">The ID of the old profile.</param>
 /// <param name="newProfile">The new profile.</param>
 /// <returns>A task which completes when the process is
 /// complete.</returns>
 private async Task ReplaceProfile(
     Guid oldProfileId,
     DynamicDnsProfile newProfile)
 {
     this.DeleteProfile(oldProfileId);
     await this.AddProfile(newProfile);
 }
예제 #2
0
        /// <summary>
        /// Creates a timer to perform scheduled updates for the specified
        /// profile.
        /// </summary>
        /// <param name="profile">The specified profile.</param>
        /// <returns>The created timer.</returns>
        private Timer CreateTimer(DynamicDnsProfile profile)
        {
            var timer = new Timer();

            timer.Tick    += this.OnTimerTick;
            timer.Interval = profile.GetUpdateIntervalInMilliseconds();
            timer.Start();
            timer.Tag = profile.Id;
            return(timer);
        }
 /// <summary>
 /// Initializes a new instance of the
 /// <see cref="DynamicDnsUpdateEventArgs"/> class.
 /// </summary>
 /// <param name="profile">The profile associated with the update
 /// attempt.</param>
 /// <param name="success">Whether the update succeeded.</param>
 /// <param name="ip">The IP associated with the update attempt.</param>
 /// <param name="dateTime">The date and time when the update attempt
 /// occurred.</param>
 /// <param name="response">The response from Namecheap following the
 /// update attempt.</param>
 /// <param name="exception">The exception associated with the update
 /// attempt, if any.</param>
 public DynamicDnsUpdateEventArgs(
     DynamicDnsProfile profile,
     bool success,
     IPAddress ip,
     DateTime dateTime,
     string response,
     DynamicDnsException exception)
 {
     this.UpdateProfile   = profile;
     this.Success         = success;
     this.Ip              = ip;
     this.UpdateDateTime  = dateTime;
     this.Response        = response;
     this.UpdateException = exception;
 }
예제 #4
0
        /// <summary>
        /// Performs a dynamic DNS update with Namecheap servers using the
        /// specified profile.
        /// </summary>
        /// <param name="profile">The specified profile, which contains
        /// information about the domain to update and the IP address to
        /// use.</param>
        /// <returns>The string containing the update response.</returns>
        private static async Task <Tuple <string, IPAddress> > GetUpdateResponse(
            DynamicDnsProfile profile)
        {
            try
            {
                // Use current machine's IPv4 address if requested
                var ip = profile.AutoDetectIPAddress
                         ? await GetIp()
                         : IPAddress.Parse(profile.UpdateIPAddress);

                // Get response and parse as XML
                HttpWebRequest request = WebRequest.CreateHttp(
                    "https://dynamicdns.park-your-domain.com/update?" +
                    "host=" + WebUtility.UrlEncode(profile.Host) +
                    "&domain=" + WebUtility.UrlEncode(profile.Domain) +
                    "&password="******"&ip=" + WebUtility.UrlEncode(ip.ToString()));
                var response = new XmlDocument();
                response.LoadXml(new StreamReader(
                                     request.GetResponse().GetResponseStream()).ReadToEnd());

                ValidateUpdateResponse(ip, response);

                return(new Tuple <string, IPAddress>(response.OuterXml, ip));
            }
            catch (WebException ex)
            {
                throw new DynamicDnsException(
                          "Dynamic DNS update response network error", ex);
            }
            catch (XmlException ex)
            {
                throw new DynamicDnsException(
                          "Failed to parse dynamic DNS update response as XML", ex);
            }
            catch (DynamicDnsException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw new DynamicDnsException("Unknown error", ex);
            }
        }
예제 #5
0
        /// <summary>
        /// Performs an update for the specified profile.
        /// </summary>
        /// <param name="profile">The specified profile.</param>
        /// <returns>A task which completes when the process is
        /// complete.</returns>
        public async Task PerformUpdate(DynamicDnsProfile profile)
        {
            try
            {
                var response = await GetUpdateResponse(profile);

                this.DynamicDnsUpdateEventHandler.Invoke(
                    this,
                    new DynamicDnsUpdateEventArgs(
                        profile,
                        true,
                        response.Item2,
                        DateTime.Now,
                        response.Item1,
                        null));
            }
            catch (DynamicDnsException ex)
            {
                this.DynamicDnsUpdateEventHandler.Invoke(
                    this,
                    new DynamicDnsUpdateEventArgs(
                        profile,
                        false,
                        null,
                        DateTime.Now,
                        ex.Response,
                        ex));
            }
            catch (Exception ex)
            {
                this.DynamicDnsUpdateEventHandler.Invoke(
                    this,
                    new DynamicDnsUpdateEventArgs(
                        profile,
                        false,
                        null,
                        DateTime.Now,
                        null,
                        new DynamicDnsException("Unknown error", ex)));
            }

            this.timers.TryGetValue(profile.Id, out Timer timer);
            timer?.Stop();
            this.timers[profile.Id] = this.CreateTimer(profile);
        }
        /// <summary>
        /// Adds the specified profile to the profiles list.
        /// </summary>
        /// <param name="profile">The specified profile.</param>
        /// <returns>A task which completes when the process is
        /// complete.</returns>
        private async Task AddProfile(DynamicDnsProfile profile)
        {
            var statusItem = new ListViewItem(profile.Label)
            {
                Tag = profile.Id
            };

            statusItem.SubItems.Add(new ListViewItem.ListViewSubItem(
                                        statusItem, string.Empty));
            statusItem.SubItems.Add(new ListViewItem.ListViewSubItem(
                                        statusItem, string.Empty));
            this.statusListView.Items.Add(statusItem);

            var profileItem = new ComboBoxItem(profile.Label, profile.Id);

            this.profilesComboBox.Items.Add(profileItem);

            await this.dynamicDns.AddProfile(profile);
        }
        /// <summary>
        /// Called when the Save button is clicked.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">The event arguments.</param>
        private async void OnSaveButtonClick(object sender, EventArgs e)
        {
            var mode  = this.mode;
            var index = profilesComboBox.SelectedIndex;

            var profile = new DynamicDnsProfile()
            {
                Host               = hostTextBox.Text,
                Domain             = domainTextBox.Text,
                DynamicDnsPassword = dynamicDnsPasswordTextBox.Text,
                Interval           =
                    (UpdateInterval)updateIntervalComboBox.SelectedIndex
            };

            IPAddress.TryParse(ipAddressTextBox.Text, out IPAddress ipAddress);
            profile.UpdateIPAddress     = ipAddress?.ToString() ?? string.Empty;
            profile.AutoDetectIPAddress = autoDetectCheckBox.Checked;

            if (profile.Host == string.Empty || profile.Domain == string.Empty ||
                profile.DynamicDnsPassword == string.Empty ||
                updateIntervalComboBox.SelectedIndex == -1 ||
                (profile.UpdateIPAddress == string.Empty &&
                 !profile.AutoDetectIPAddress))
            {
                MessageBox.Show(
                    "Profile information validation failed.",
                    "namecheap-dynamic-dns");
                return;
            }

            this.ChangeMode(ProfilesMode.NONE);
            if (mode == ProfilesMode.NEW)
            {
                await this.AddProfile(profile);
            }
            else if (mode == ProfilesMode.EDIT)
            {
                var id = ((ComboBoxItem)profilesComboBox.Items[index]).Id;
                await this.ReplaceProfile(id, profile);
            }
        }
예제 #8
0
 /// <summary>
 /// Adds the specified profile to the profiles list.
 /// </summary>
 /// <param name="profile">The specified profile.</param>
 /// <returns>A task which completes when the process is
 /// complete.</returns>
 public async Task AddProfile(DynamicDnsProfile profile)
 {
     this.profiles[profile.Id] = profile;
     await this.PerformUpdate(profile);
 }