/// <summary> /// Deletes the DNS entry from the cachce and stores it in the JSON /// </summary> /// <param name="domain"></param> /// <returns></returns> public string DeleteDNSEntry(string domain) { //delete DNS record string[] domainSplit = domain.Split('.').Reverse().ToArray(); DNSRecord recordToDelete = LookupByName(domainSplit); DNSRecord recordToDeleteFrom = LookupByName(domainSplit.SubArray(0, domainSplit.Length - 1)); if (recordToDelete == null) { return($"Domain {domain} not found"); } string ip = recordToDelete.Address; if (recordToDeleteFrom.SubDomains.Contains(recordToDelete)) { recordToDeleteFrom.SubDomains.Remove(recordToDelete); } //delete ARPA record DNSRecord recordARP = LookupByIp(ip); if (recordARP.GetSubRecord(domain) != null) { //this record exists, delete it recordARP.SubDomains.Remove(recordARP.GetSubRecord(domain)); } else { return($"No ARPA entry found for {domain} by ip {ip}"); } SaveDomainNameTable(); return($"Removed the entry's for domain {domain}"); }
/// <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."); } }
/// <summary> /// Do a DNS Lookup by ip, this function searches through the ARPA ipv4 entry's /// </summary> /// <param name="ip"></param> /// <returns></returns> public DNSRecord LookupByIp(string ip) { DNSRecord arpaIPv4 = Root.GetSubRecord("arpa").GetSubRecord("ipv4"); string[] ipList = ip.Split('.'); //return null if any of these values are not correct if (ipList.Count() != 4 || arpaIPv4 == null) { return(null); } for (int i = 0; i < 4; i++) { arpaIPv4 = arpaIPv4.GetSubRecord(ipList[i]); if (arpaIPv4 == null) { return(null); } } return(arpaIPv4); }
/// <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()); }
/// <summary> /// Do a DNS Lookup by domain name, this function searches through all entry's except the ARPA ipv4 entry's /// </summary> /// <param name="args"></param> /// <returns></returns> public DNSRecord LookupByName(string[] args) { if (args[0] == "arpa") { return(null); } DNSRecord _record = Root; for (int i = 0; i < args.Length; i++) { _record = _record.GetSubRecord(args[i]); if (_record == null) { return(null); } } return(_record); }