Exemplo n.º 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.");
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an ARPA DNS entry into the dns cache
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        private string AddDNSIpEntry(string domain, string ip)
        {
            string[]  ipSection  = ip.Split('.');
            DNSRecord arpaRecord = Root.GetSubRecord("arpa").GetSubRecord("ipv4");

            if (arpaRecord == null || !ip.IsValidIpAddress()) //check if the arpa exists and the ip is valid
            {
                throw new Exception($"Error ARPA search for ip: {ip}");
            }

            //find or create the third ip segment
            for (int i = 0; i < 4; i++)
            {
                DNSRecord subDomain = arpaRecord.GetSubRecord(ipSection[i]);
                if (subDomain == null)
                {
                    DNSRecord r = new DNSRecord
                    {
                        Domain = ipSection[i]
                    };

                    arpaRecord.SubDomains.Add(r);
                    subDomain = arpaRecord.GetSubRecord(ipSection[i]);
                }

                arpaRecord = subDomain;
            }

            // now start adding the final arpa record
            if (arpaRecord.GetSubRecord(domain) == null) //means the arpa record doesnt exists yet
            {
                DNSRecord r = new DNSRecord
                {
                    Domain = domain
                };

                arpaRecord.SubDomains.Add(r);

                SaveDomainNameTable();
                return(r.ToString());
            }
            else
            {
                return($"Arpa record for {domain} already exists, please try editing instead of creating.");
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Lets you search for DNS entry's
 /// </summary>
 /// <param name="fDomainIp"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public string ShowDNSEntry(bool fDomainIp, string value)
 {
     if (fDomainIp)
     {
         DNSRecord recordDNS = LookupByName(value.Split('.').Reverse().ToArray());
         if (recordDNS == null)
         {
             return($"Domain {value} not found");
         }
         return(recordDNS.ToString());
     }
     else
     {
         DNSRecord recordARP = LookupByIp(value);
         if (recordARP == null)
         {
             return($"ARPA for {value} not found");
         }
         return(recordARP.ToString());
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// Edits the DNS entry specified with the flag parameter
        /// </summary>
        /// <param name="fDomainIp"></param>
        /// <param name="currentDomain"></param>
        /// <param name="newField"></param>
        /// <returns></returns>
        public string EditDNSEntry(bool fDomainIp, string currentDomain, string newField)
        {
            string[]  domainSplit = currentDomain.Split('.').Reverse().ToArray();
            DNSRecord recordDNS   = LookupByName(domainSplit);

            if (recordDNS == null)
            {
                return($"Domain {currentDomain} not found");
            }
            string ip = recordDNS.Address;

            if (fDomainIp) //search by domain, change the domain
            {
                recordDNS.Domain = newField;
                DNSRecord recordARP = LookupByIp(ip);
                recordARP = recordARP.GetSubRecord(currentDomain);
                if (recordARP != null)
                {
                    string[] newValueSplit = newField.Split('.').Reverse().ToArray();
                    recordARP.Domain = newValueSplit[0];
                }
            }
            else //search by domain name, change the ip
            {
                recordDNS.Address = newField;
                DNSRecord recordARP = LookupByIp(ip);
                if (recordARP.GetSubRecord(currentDomain) != null)
                {
                    //this record exists, delete it
                    recordARP.SubDomains.Remove(recordARP.GetSubRecord(currentDomain));
                    AddDNSIpEntry(currentDomain, newField);
                }
            }

            SaveDomainNameTable();
            return(recordDNS.ToString());
        }