コード例 #1
0
        public DnsPTRRecord(string domain)
        {
            DnsClient.IsDomainNameValid(domain, true);

            _domain = domain;
        }
コード例 #2
0
        public DnsNSRecord(string nameServer)
        {
            DnsClient.IsDomainNameValid(nameServer, true);

            _nameServer = nameServer;
        }
コード例 #3
0
ファイル: DnsCNAMERecord.cs プロジェクト: dungtri/DnsServer
        public DnsCNAMERecord(string cnameDomainName)
        {
            DnsClient.IsDomainNameValid(cnameDomainName, true);

            _cnameDomainName = cnameDomainName;
        }
コード例 #4
0
ファイル: DnsPTRRecord.cs プロジェクト: dungtri/DnsServer
        public DnsPTRRecord(string ptrDomainName)
        {
            DnsClient.IsDomainNameValid(ptrDomainName, true);

            _ptrDomainName = ptrDomainName;
        }
コード例 #5
0
        private Queue <string> ReadListFile(Uri listUrl, bool isAllowList)
        {
            Queue <string> domains = new Queue <string>();

            try
            {
                LogManager log = _dnsServer.LogManager;
                if (log != null)
                {
                    log.Write("DNS Server is reading " + (isAllowList ? "allow" : "block") + " list from: " + listUrl.AbsoluteUri);
                }

                using (FileStream fS = new FileStream(GetBlockListFilePath(listUrl), FileMode.Open, FileAccess.Read))
                {
                    //parse hosts file and populate block zone
                    StreamReader sR = new StreamReader(fS, true);
                    string       line;
                    string       firstWord;
                    string       secondWord;
                    string       hostname;

                    while (true)
                    {
                        line = sR.ReadLine();
                        if (line == null)
                        {
                            break; //eof
                        }
                        line = line.TrimStart(' ', '\t');

                        if (line.Length == 0)
                        {
                            continue; //skip empty line
                        }
                        if (line.StartsWith("#"))
                        {
                            continue; //skip comment line
                        }
                        firstWord = PopWord(ref line);

                        if (line.Length == 0)
                        {
                            hostname = firstWord;
                        }
                        else
                        {
                            secondWord = PopWord(ref line);

                            if (secondWord.Length == 0)
                            {
                                hostname = firstWord;
                            }
                            else
                            {
                                hostname = secondWord;
                            }
                        }

                        hostname = hostname.Trim('.').ToLower();

                        switch (hostname)
                        {
                        case "":
                        case "localhost":
                        case "localhost.localdomain":
                        case "local":
                        case "broadcasthost":
                        case "ip6-localhost":
                        case "ip6-loopback":
                        case "ip6-localnet":
                        case "ip6-mcastprefix":
                        case "ip6-allnodes":
                        case "ip6-allrouters":
                        case "ip6-allhosts":
                            continue;     //skip these hostnames
                        }

                        if (!DnsClient.IsDomainNameValid(hostname))
                        {
                            continue;
                        }

                        if (IPAddress.TryParse(hostname, out _))
                        {
                            continue; //skip line when hostname is IP address
                        }
                        domains.Enqueue(hostname);
                    }
                }

                if (log != null)
                {
                    log.Write("DNS Server read " + (isAllowList ? "allow" : "block") + " list file (" + domains.Count + " domains) from: " + listUrl.AbsoluteUri);
                }
            }
            catch (Exception ex)
            {
                LogManager log = _dnsServer.LogManager;
                if (log != null)
                {
                    log.Write("DNS Server failed to read " + (isAllowList ? "allow" : "block") + " list from: " + listUrl.AbsoluteUri + "\r\n" + ex.ToString());
                }
            }

            return(domains);
        }
コード例 #6
0
        private void UpdateDnsAuthZone(bool add, Scope scope, Lease lease)
        {
            if (_authoritativeZoneRoot == null)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(scope.DomainName))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(lease.HostName))
            {
                return;
            }

            if (!DnsClient.IsDomainNameValid(lease.HostName))
            {
                return;
            }

            if (add)
            {
                //update forward zone
                {
                    if (!_authoritativeZoneRoot.ZoneExists(scope.DomainName))
                    {
                        //create forward zone
                        _authoritativeZoneRoot.SetRecords(scope.DomainName, DnsResourceRecordType.SOA, 14400, new DnsResourceRecordData[] { new DnsSOARecord(_authoritativeZoneRoot.ServerDomain, "hostmaster." + scope.DomainName, 1, 14400, 3600, 604800, 900) });
                        _authoritativeZoneRoot.SetRecords(scope.DomainName, DnsResourceRecordType.NS, 14400, new DnsResourceRecordData[] { new DnsNSRecord(_authoritativeZoneRoot.ServerDomain) });

                        _authoritativeZoneRoot.MakeZoneInternal(scope.DomainName);
                    }

                    _authoritativeZoneRoot.SetRecords(lease.HostName, DnsResourceRecordType.A, scope.DnsTtl, new DnsResourceRecordData[] { new DnsARecord(lease.Address) });
                }

                //update reverse zone
                {
                    if (!_authoritativeZoneRoot.ZoneExists(scope.ReverseZone))
                    {
                        //create reverse zone
                        _authoritativeZoneRoot.SetRecords(scope.ReverseZone, DnsResourceRecordType.SOA, 14400, new DnsResourceRecordData[] { new DnsSOARecord(_authoritativeZoneRoot.ServerDomain, "hostmaster." + scope.ReverseZone, 1, 14400, 3600, 604800, 900) });
                        _authoritativeZoneRoot.SetRecords(scope.ReverseZone, DnsResourceRecordType.NS, 14400, new DnsResourceRecordData[] { new DnsNSRecord(_authoritativeZoneRoot.ServerDomain) });

                        _authoritativeZoneRoot.MakeZoneInternal(scope.ReverseZone);
                    }

                    _authoritativeZoneRoot.SetRecords(Scope.GetReverseZone(lease.Address, 32), DnsResourceRecordType.PTR, scope.DnsTtl, new DnsResourceRecordData[] { new DnsPTRRecord(lease.HostName) });
                }
            }
            else
            {
                //remove from forward zone
                _authoritativeZoneRoot.DeleteRecords(lease.HostName, DnsResourceRecordType.A);

                //remove from reverse zone
                _authoritativeZoneRoot.DeleteRecords(Scope.GetReverseZone(lease.Address, 32), DnsResourceRecordType.PTR);
            }
        }
コード例 #7
0
        public DnsNSRecord(string nsDomainName)
        {
            DnsClient.IsDomainNameValid(nsDomainName, true);

            _nsDomainName = nsDomainName;
        }
コード例 #8
0
        public NetProxyBypassItem(string value)
        {
            _originalValue = value;

            if (IPAddress.TryParse(value, out _ipAddress))
            {
                _type = NetProxyBypassItemType.IpAddress;
            }
            else if (value.Contains("/"))
            {
                string[] network = value.Split(new char[] { '/' }, 2);

                if (IPAddress.TryParse(network[0], out _networkAddress) && int.TryParse(network[1], out _networkMaskWidth))
                {
                    switch (_networkAddress.AddressFamily)
                    {
                    case AddressFamily.InterNetwork:
                        if (_networkMaskWidth > 32)
                        {
                            throw new NetProxyException("Invalid proxy bypass value: " + value);
                        }

                        if (_networkMaskWidth == 32)
                        {
                            _type      = NetProxyBypassItemType.IpAddress;
                            _ipAddress = _networkAddress;
                        }
                        else
                        {
                            _type = NetProxyBypassItemType.NetworkAddress;
                        }

                        break;

                    case AddressFamily.InterNetworkV6:
                        if (_networkMaskWidth > 128)
                        {
                            throw new NetProxyException("Invalid proxy bypass value: " + value);
                        }

                        if (_networkMaskWidth == 128)
                        {
                            _type      = NetProxyBypassItemType.IpAddress;
                            _ipAddress = _networkAddress;
                        }
                        else
                        {
                            _type = NetProxyBypassItemType.NetworkAddress;
                        }

                        break;

                    default:
                        throw new NotSupportedException("Address family not supported.");
                    }
                }
                else
                {
                    throw new NetProxyException("Invalid proxy bypass value: " + value);
                }
            }
            else if (DnsClient.IsDomainNameValid(value))
            {
                _type       = NetProxyBypassItemType.DomainName;
                _domainName = value;
            }
            else
            {
                throw new NetProxyException("Invalid proxy bypass value: " + value);
            }
        }
コード例 #9
0
        private void LoadBlockListFile(Zone blockedZoneRoot, Uri blockListUrl)
        {
            string blockListAbsoluteUrl = blockListUrl.AbsoluteUri;

            try
            {
                string blockListFilePath = GetBlockListFilePath(blockListUrl);
                int    count             = 0;

                _log.Write("DNS Server is loading blocked zone from: " + blockListAbsoluteUrl);

                using (FileStream fS = new FileStream(blockListFilePath, FileMode.Open, FileAccess.Read))
                {
                    //parse hosts file and populate block zone
                    StreamReader sR = new StreamReader(fS, true);

                    while (true)
                    {
                        string line = sR.ReadLine();
                        if (line == null)
                        {
                            break; //eof
                        }
                        line = line.TrimStart(' ', '\t');

                        if (line == "")
                        {
                            continue; //skip empty line
                        }
                        if (line.StartsWith("#"))
                        {
                            continue; //skip comment line
                        }
                        string firstWord  = PopWord(ref line);
                        string secondWord = PopWord(ref line);

                        string strIpAddress = null;
                        string hostname;

                        if (secondWord == "")
                        {
                            hostname = firstWord;
                        }
                        else
                        {
                            strIpAddress = firstWord;
                            hostname     = secondWord;
                        }

                        if (!DnsClient.IsDomainNameValid(hostname, false))
                        {
                            continue;
                        }

                        switch (hostname.ToLower())
                        {
                        case "":
                        case "localhost":
                        case "localhost.localdomain":
                        case "local":
                        case "broadcasthost":
                        case "ip6-localhost":
                        case "ip6-loopback":
                        case "ip6-localnet":
                        case "ip6-mcastprefix":
                        case "ip6-allnodes":
                        case "ip6-allrouters":
                        case "ip6-allhosts":
                            continue;     //skip these hostnames
                        }

                        if (IPAddress.TryParse(hostname, out IPAddress host))
                        {
                            continue; //skip line when hostname is IP address
                        }
                        IPAddress ipAddress;

                        if (string.IsNullOrEmpty(strIpAddress) || !IPAddress.TryParse(strIpAddress, out ipAddress))
                        {
                            ipAddress = IPAddress.Any;
                        }

                        if (ipAddress.Equals(IPAddress.Any) || ipAddress.Equals(IPAddress.Loopback) || ipAddress.Equals(IPAddress.IPv6Any) || ipAddress.Equals(IPAddress.IPv6Loopback))
                        {
                            BlockZone(hostname, blockedZoneRoot, blockListAbsoluteUrl);
                            count++;
                        }
                    }
                }

                _log.Write("DNS Server blocked zone was loaded (" + count + " domains) from: " + blockListAbsoluteUrl);
            }
            catch (Exception ex)
            {
                _log.Write("DNS Server failed to load block list from: " + blockListAbsoluteUrl + "\r\n" + ex.ToString());
            }
        }