public void ChangeDomainAndRemarkTest()
 {
     string oldDomain = "backoffice.db.qa.elmae";
     string ip = "10.81.152.31";
     string newDomain = "emims.db.qa.elmae";
     HostRemark remark = new HostRemark() { SiteType = SiteType.UNK, Target = TargetType.QA1, Comment = "emims data base." };
     bool writetofile = true;
     target.ChangeDomainAndRemark(oldDomain, ip, newDomain, remark, writetofile);
 }
        public void ChangeDomainAndRemark(string oldDomain, string ip, string newdomain, HostRemark remark, bool iswritetofile)
        {
            try
            {
                string newIp = EnvironmentMnager.GetIpAdress(remark.SiteType, remark.Target);

                _helper.ChangeDomainAndRemark(oldDomain, ip, newdomain, remark, iswritetofile);
            }
            catch (Exception ex)
            {
                _loger.WriteLine(ex.ToString());
                throw;
            }
        }
 public void ChangeComment(HostDNS host, string newcomment)
 {
     try
     {
         if (host == null || host.Remark == null)
         {
             throw new ArgumentException("Invald domain argument.");
         }
         HostRemark remark = new HostRemark() { SiteType = host.Remark.SiteType, Target = host.Remark.Target, Comment = newcomment };
         _helper.ChangeRemark(host.IP, host.DomainName, new HostRemark(), true);
     }
     catch (Exception ex)
     {
         _loger.WriteLine(ex.ToString());
         throw;
     }
 }
        public List<HostDNS> GetDomanList(Recognizer rec)
        {
            try
            {
                List<HostDNS> resultList = new List<HostDNS>();
                IEnumerable<string> lines = _helper.GetAllLines();
                Regex reg = new Regex(@"(^[#\s]*)((\d{1,3}\.){3}\d{1,3})\s*([\d\w\.-]+)[^\S\n]*(#[^\n]*)?$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                foreach (string line in lines)
                {
                    Match match = reg.Match(line.Trim());
                    if (match.Success)
                    {
                        HostDNS domain = new HostDNS();
                        domain.IsDisabled = !string.IsNullOrEmpty(match.Groups[1].Value.Trim());
                        domain.IP = match.Groups[2].Value;
                        domain.DomainName = match.Groups[4].Value;
                        string remarkStr = match.Groups[5].Value;

                        if (string.IsNullOrWhiteSpace(domain.IP) || string.IsNullOrWhiteSpace(domain.DomainName)) continue;
                        if (string.IsNullOrWhiteSpace(remarkStr.Trim('#')))
                        {
                            HostRemark remark = new HostRemark() { SiteType = rec.GetSiteType(domain.DomainName,domain.IP), Target = TargetType.Other, Comment = "" };
                            domain.Remark = remark;
                        }
                        else
                        {
                            try
                            {
                                domain.Remark = JSONHelper.JsonDeserialize<HostRemark>(remarkStr.Trim('#'));
                            }
                            catch (Exception e)
                            {
                                HostRemark newmark = new HostRemark() { SiteType = rec.GetSiteType(domain.DomainName,domain.IP), Target = TargetType.Other, Comment = remarkStr.Trim('#') };
                                domain.Remark = newmark;
                            }
                        }

                        resultList.Add(domain);
                    }
                }
                return resultList;
            }
            catch (Exception ex)
            {
                _loger.WriteLine(ex.ToString());
                throw;
            }
        }
 public void ChangeTargetType(HostDNS host, TargetType newTargetType)
 {
     try
     {
         if (host == null || host.Remark == null)
         {
             throw new ArgumentException("Invald domain argument.");
         }
         else if (host.Remark.Target == newTargetType)
         {
             return;
         }
         else
         {
             HostRemark newRemark = new HostRemark() { SiteType = host.Remark.SiteType, Target = newTargetType, Comment = host.Remark.Comment };
             string newIp = EnvironmentMnager.GetIpAdress(host.Remark.SiteType, newTargetType);
             if (!string.IsNullOrEmpty(newIp))
             {
                 _helper.ChangeIpAndRemark(host.DomainName, host.IP, newIp, newRemark, true);
                 DNSManager.FulshDNS();
             }
             else
             {
                 _helper.ChangeRemark(host.IP, host.DomainName, newRemark, true);
             }
         }
     }
     catch (Exception ex)
     {
         _loger.WriteLine(ex.ToString());
         throw;
     }
 }
        public void ChangeRemark(HostDNS domain, HostRemark newRemark, bool isWriteToFile)
        {
            try
            {
                if (newRemark.SiteType != domain.Remark.SiteType || newRemark.Target != domain.Remark.Target)//siteType or targetType has changed
                {
                    string newIp = EnvironmentMnager.GetIpAdress(newRemark.SiteType, newRemark.Target);
                    if (String.IsNullOrWhiteSpace(newIp))
                    {
                        _helper.ChangeRemark(domain.IP, domain.DomainName, newRemark, true);

                    }
                    else
                    {
                        _helper.ChangeIpAndRemark(domain.DomainName, domain.IP, newIp, newRemark, true);
                    }
                }
                else//only comment is changed
                {
                    _helper.ChangeRemark(domain.IP, domain.DomainName, newRemark, true);
                }
            }
            catch (Exception ex)
            {
                _loger.WriteLine(ex.ToString());
                throw;
            }
        }
Example #7
0
 public bool SaveHostDomain(string oldDomain, HostDomain newDomain, bool isenable, HostRemark remark, bool isWriteToFile)
 {
     try
     {
         _helper.ChangeLineByDomain(oldDomain, newDomain.Ip, remark, isenable, isWriteToFile);
         return true;
     }
     catch (Exception ex)
     {
         _loger.WriteLine(ex.ToString());
         throw;
     }
 }
Example #8
0
 public void ChangeIpAndRemark(string domain, string oldIp, string newIp, HostRemark remak, bool writetofile)
 {
     ChangeIp(oldIp, newIp, domain, false);
     ChangeRemark(newIp, domain, remak, writetofile);
 }
Example #9
0
 public void ChangeDomainAndRemark(string oldDomain, string ip, string newDomain, HostRemark remark, bool writetofile)
 {
     ChangeDomain(ip, oldDomain, newDomain, false);
     ChangeRemark(ip, newDomain, remark, writetofile);
 }
Example #10
0
        public void FormatHost(Recognizer recognizer, bool writetofile)
        {
            IEnumerable<string> HostDnses = GetAllLines();
            StringBuilder hostStr = new StringBuilder();

            Regex reg = new Regex(@"(^[#\s]*)((\d{1,3}\.){3}\d{1,3})\s*([\d\w\.-]+)[^\S\n]*(#[^\n]*)?$", RegexOptions.IgnoreCase);
            foreach (var host in HostDnses)
            {
                Match match = reg.Match(host.ToString());
                string enable = match.Groups[1].Value;
                string ip = match.Groups[2].Value;
                string domainName = match.Groups[4].Value;
                string remark = match.Groups[5].Value;

                if (string.IsNullOrWhiteSpace(ip) || string.IsNullOrWhiteSpace(domainName)) continue;
                string formatRemark;
                if (string.IsNullOrWhiteSpace(remark.Trim('#')))
                {
                    formatRemark = "";
                    HostRemark newmark = new HostRemark() { SiteType = recognizer.GetSiteType(domainName, ip), Target = recognizer.GetTargetType(domainName, ip), Comment = "" };
                    formatRemark = "#" + JSONHelper.JsonSerializer<HostRemark>(newmark);
                }
                else
                {
                    try
                    {
                        HostRemark hostRemark = JSONHelper.JsonDeserialize<HostRemark>(remark.Trim('#'));
                        if (hostRemark.SiteType == SiteType.UNK)
                        {
                            hostRemark.SiteType = recognizer.GetSiteType(domainName, ip);
                        }
                        if (hostRemark.Target == TargetType.Other || hostRemark.Target == TargetType.DEFAULT)
                        {
                            hostRemark.Target = recognizer.GetTargetType(domainName, ip);
                        }
                        formatRemark = "#" + JSONHelper.JsonSerializer<HostRemark>(hostRemark);
                    }
                    catch (Exception e)
                    {
                        HostRemark newmark = new HostRemark() { SiteType = recognizer.GetSiteType(domainName, ip), Target = recognizer.GetTargetType(domainName, ip), Comment = remark.Trim('#') };
                        formatRemark = "#" + JSONHelper.JsonSerializer<HostRemark>(newmark);
                    }
                }

                hostStr.AppendLine(reg.Replace(host, "${1}${2}" + GetFormatEmpyStr(enable + ip) + "\t\t${4}\t\t" + formatRemark));
            }
            HostsStr = hostStr;
            if (writetofile)
                WriteToFile();
        }
Example #11
0
        public void ChangeRemark(string ip, string domain, HostRemark newremark, bool writetofile)
        {
            if (newremark == null) return;
            if (string.IsNullOrWhiteSpace(ip) || string.IsNullOrWhiteSpace(domain))
            {
                throw new ArgumentException("Invalid ip or domain name.");
            }

            string remark = JSONHelper.JsonSerializer<HostRemark>(newremark);
            Regex reg = new Regex(@"(^[#\s]*)(" + ip + @")\s*(" + domain + @")[^\S\n]*(#[^\n]*)?$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
            HostsStr = new StringBuilder(reg.Replace(HostsStr.ToString(), "${1}${2}\t\t${3}\t\t#" + remark));
            if (writetofile)
                WriteToFile();
        }
Example #12
0
        /// <summary>
        /// modify the host by delete the line first and append new host line
        /// </summary>
        /// <param name="domain">Domain name</param>
        /// <param name="newip">ip address</param>
        /// <param name="newremark">remark for the domain</param>
        /// <param name="isenable">a value indicate whether the new host is enabled</param>
        /// <param name="writetofile">a value indicate whether to apply changes to the hosts file immediately</param>
        public void ChangeLineByDomain(string domain, string newip, HostRemark newremark, bool isenable, bool writetofile)
        {
            if (string.IsNullOrWhiteSpace(newip))
                throw new ArgumentException("The new IpAdress can not be empty or whiteSpace.");
            else if (!Regex.Match(newip, @"((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)", RegexOptions.IgnoreCase).Success)
            {
                throw new ArgumentException("Invalid IpAdress.");
            }

            string remark = "#";
            if (newremark != null) remark += JSONHelper.JsonSerializer<HostRemark>(newremark);
            string enableStr = isenable ? "" : "#";
            string newline = string.Format("{0}{1}\t\t{2}\t\t{3}\r\n", enableStr, newip, domain, remark);

            ReplaceLineByDomain(domain, newline, false);

            if (writetofile)
                WriteToFile();
        }
 public void ChangeIpAndRemarkTest()
 {
     string domain = "compufund.db.qa.elmae";
     string oldIp = "222.222.222.222";
     string newIp = "127.0.0.1";
     HostRemark remak = new HostRemark() { SiteType = SiteType.TPO, Target = TargetType.QA1, Comment = "www.tpoyemol.com" };
     bool writetofile = true;
     target.ChangeIpAndRemark(domain, oldIp, newIp, remak, writetofile);
 }
 public void ChangeRemarkTest()
 {
     string ip = "10.81.152.31";
     string domain = "misc.db.qa.elmae";
     HostRemark newremark = new HostRemark() { SiteType = SiteType.TPO, Target = TargetType.QA1, Comment = "localhost" };
     bool writetofile = true;
     target.ChangeRemark(ip, domain, newremark, writetofile);
 }
 public void ChangeLineByDomainTest()
 {
     string domain = "docsengine.elliemae.com";
     string newip = "222.222.222.222";
     HostRemark newremark = new HostRemark() { SiteType = SiteType.TPO, Target = TargetType.QA1, Comment = "www.tpoyemol.com" };
     bool isenable = false;
     bool writetofile = true;
     target.ChangeLineByDomain(domain, newip, newremark, isenable, writetofile);
 }