Пример #1
0
        /// <summary>
        /// Adds a standard DNS entry into the dns cache
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        private string AddDNSNameEntry(string domain, string ip)
        {
            //go to the deepest level possible:
            string[]  domainLevels  = domain.Split('.').Reverse().ToArray();
            DNSRecord deepestRecord = null;

            for (int i = 1; i <= domainLevels.Length; i++)
            {
                deepestRecord = LookupByName(domainLevels.SubArray(0, i));
                if (deepestRecord == null)
                {
                    //get the previous record and then break.
                    deepestRecord = LookupByName(domainLevels.SubArray(0, i - 1));
                    break;
                }

                if (!deepestRecord.HasSubDomains())
                {
                    break;
                }
            }

            // start creating from the latest deepestRecord
            if (deepestRecord != null)
            {
                int depthStartingIndex = Array.IndexOf(domainLevels, deepestRecord.Domain) + 1;

                if (depthStartingIndex == domainLevels.Length - 1) //means the last item in the array
                {
                    DNSRecord r = new DNSRecord
                    {
                        Domain  = domainLevels[depthStartingIndex],
                        Address = ip
                    };

                    deepestRecord.SubDomains.Add(r);

                    SaveDomainNameTable();
                    return(r.ToString());
                }
                else if (depthStartingIndex == domainLevels.Length)
                {
                    return($"Sub Domain {domainLevels[depthStartingIndex - 1]} already exists, please try editing instead of creating.");
                }
                else
                {
                    return($"Missing subdomain record for {domainLevels[depthStartingIndex - 1]}.");
                }
            }
            else
            {
                return("No proper domain name specified, please try again.");
            }
        }
Пример #2
0
        /// <summary>
        /// Reads the DNS entry's from the JSON stored file.
        /// </summary>
        public void LoadDomainNameTable()
        {
            //get the DNS records from a JSON file and put the data into the local DNS root record.
            DNSRecord _root = JsonStorageHelper.GetStorageFileContents <DNSRecord>(RootDNSFileStorageName);

            if (_root == null)
            {
                CreateDNSStructureFile();
                LoadDomainNameTable();
                return;
            }
            else if (_root.HasSubDomains())
            {
                Root.SubDomains = _root.SubDomains;
            }
        }