Beispiel #1
0
        public List<HostDomain> GetDomanList(Recognizer rec)
        {
            try
            {
                List<HostDomain> resultList = new List<HostDomain>();
                IEnumerable<string> lines = _helper.GetAllLines();
                Regex reg = new Regex(@"(^[\s#]*)([\d\.]+)\s*([\w\d\.-]+)\s*(#[^#\r\n%]*)?(\s*#%([^%]*)%)?\s*$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                foreach (string line in lines)
                {
                    Match match = reg.Match(line.Trim());
                    if (match.Success)
                    {
                        HostDomain domain = new HostDomain();
                        domain.IsDisabled = !string.IsNullOrEmpty(match.Groups[1].Value);
                        domain.Ip = match.Groups[2].Value;
                        domain.DomainName = match.Groups[3].Value;
                        domain.Remark = match.Groups[4].Value.Trim();

                        string targetStr = match.Groups[6].Value.Trim();
                        TargetType type = TargetType.Other;
                        if (Enum.TryParse(targetStr, out type))
                            domain.Target = type;
                        else
                            domain.Target = TargetType.Other;
                        domain.SiteType = rec.GetSiteType(domain.DomainName);
                        resultList.Add(domain);
                    }
                }
                return resultList;
            }
            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;
            }
        }
Beispiel #3
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();
        }