예제 #1
0
        // ReSharper disable once ExcessiveIndentation
        public static IAddress[] StringToAddresses(string str)
        {
            var remoteAddresses = new List <IAddress>();

            foreach (var remoteAddress in str.Split(','))
            {
                if (SpecialAddress.TryParse(remoteAddress, out var specialAddress))
                {
                    remoteAddresses.Add(specialAddress);
                }
                else if (SingleIP.TryParse(remoteAddress, out SingleIP ip))
                {
                    remoteAddresses.Add(ip);
                }
                else if (IPRange.TryParse(remoteAddress, out var range))
                {
                    remoteAddresses.Add(range);
                }
                else if (NetworkAddress.TryParse(remoteAddress, out var network))
                {
                    remoteAddresses.Add(network);
                }
            }

            return(remoteAddresses.ToArray());
        }
        public void Parse()
        {
            Assert.Throws <FormatException>(() => _ = NetworkAddress.Parse("[::1]:uh"));

            var address = NetworkAddress.Parse("127.0.0.1:5701");

            Assert.That(address.HostName, Is.EqualTo("127.0.0.1"));
            Assert.That(address.Port, Is.EqualTo(5701));

            Assert.That(NetworkAddress.TryParse("712.548", out var _), Is.False);

            var addresses = GetAddresses("127.0.0.1");

            Console.WriteLine("127.0.0.1");
            AssertAddresses(addresses, "127.0.0.1", false);

            addresses = GetAddresses("localhost");
            Console.WriteLine("localhost");
            AssertAddresses(addresses, "127.0.0.1", false);

            // on Windows, this gets 127.0.0.1 but on Linux it gets what the host name
            // maps to in /etc/hosts and by default on some systems (eg Debian) it can
            // be 127.0.1.1 instead of 127.0.0.1
            //
            addresses = GetAddresses(Dns.GetHostName());
            Console.Write(Dns.GetHostName());
            var n = Dns.GetHostAddresses(Dns.GetHostName()).First(x => x.AddressFamily == AddressFamily.InterNetwork).ToString();

            Console.WriteLine(" -> " + n);
            AssertAddresses(addresses, n, false);

            addresses = GetAddresses("::1");
            Console.WriteLine("::1");
            AssertAddresses(addresses, "[::1]", true);

            // on Windows, this gets the various fe80 local addresses (but not the random one
            // that we specified) - on Linux this gets nothing and it may eventually be an issue?
            // there are various issues corresponding to this situation,
            // see https://github.com/dotnet/runtime/issues/27534
            // and fixes seem to be in the 5.0 milestone = n/a yet.

            addresses = GetAddresses("fe80::bd0f:a8bc:6480:238b");
            Console.WriteLine("fe80::bd0f:a8bc:6480:238b");
            if (OS.IsWindows)
            {
                // test the first 3, we might get more depending on NICs
                AssertAddresses(addresses, "*", true);
            }
            else
            {
                foreach (var a in addresses)
                {
                    Console.WriteLine("  " + a);
                }
            }
        }
예제 #3
0
        public void Parse()
        {
            Assert.Throws <FormatException>(() => _ = NetworkAddress.Parse("[::1]:uh"));

            var address = NetworkAddress.Parse("127.0.0.1:5701");

            Assert.That(address.HostName, Is.EqualTo("127.0.0.1"));
            Assert.That(address.Port, Is.EqualTo(5701));

            Assert.That(NetworkAddress.TryParse("712.548", out IEnumerable <NetworkAddress> _), Is.False);
예제 #4
0
        public static IAddress[] StringToAddresses(string str)
        {
            var remoteAddresses = new List <IAddress>();

            foreach (var remoteAddress in str.Split(','))
            {
                DNSService     dns;
                DHCPService    dhcp;
                WINSService    wins;
                LocalSubnet    localSubnet;
                DefaultGateway defaultGateway;
                IPRange        range;
                SingleIP       ip;
                NetworkAddress network;
                if (DNSService.TryParse(remoteAddress, out dns))
                {
                    remoteAddresses.Add(dns);
                }
                else if (DHCPService.TryParse(remoteAddress, out dhcp))
                {
                    remoteAddresses.Add(dhcp);
                }
                else if (WINSService.TryParse(remoteAddress, out wins))
                {
                    remoteAddresses.Add(wins);
                }
                else if (LocalSubnet.TryParse(remoteAddress, out localSubnet))
                {
                    remoteAddresses.Add(localSubnet);
                }
                else if (DefaultGateway.TryParse(remoteAddress, out defaultGateway))
                {
                    remoteAddresses.Add(defaultGateway);
                }
                else if (IPRange.TryParse(remoteAddress, out range))
                {
                    remoteAddresses.Add(range);
                }
                else if (SingleIP.TryParse(remoteAddress, out ip))
                {
                    remoteAddresses.Add(ip);
                }
                else if (NetworkAddress.TryParse(remoteAddress, out network))
                {
                    remoteAddresses.Add(network);
                }
            }
            return(remoteAddresses.ToArray());
        }
        public NetProxyBypassItem(string value)
        {
            _originalValue = value;

            if (IPAddress.TryParse(value, out _ipAddress))
            {
                _type = NetProxyBypassItemType.IpAddress;
            }
            else if (NetworkAddress.TryParse(value, out _networkAddress))
            {
                switch (_networkAddress.Address.AddressFamily)
                {
                case AddressFamily.InterNetwork:
                    if (_networkAddress.PrefixLength == 32)
                    {
                        _type           = NetProxyBypassItemType.IpAddress;
                        _ipAddress      = _networkAddress.Address;
                        _networkAddress = null;
                        return;
                    }

                    break;

                case AddressFamily.InterNetworkV6:
                    if (_networkAddress.PrefixLength == 128)
                    {
                        _type           = NetProxyBypassItemType.IpAddress;
                        _ipAddress      = _networkAddress.Address;
                        _networkAddress = null;
                        return;
                    }

                    break;
                }

                _type = NetProxyBypassItemType.NetworkAddress;
            }
            else if (DnsClient.IsDomainNameValid(value))
            {
                _type       = NetProxyBypassItemType.DomainName;
                _domainName = value;
            }
            else
            {
                throw new NetProxyException("Invalid proxy bypass value: " + value);
            }
        }
        public void CanTryParse(string s, bool succeeds, string toString)
        {
            var result = NetworkAddress.TryParse(s, out NetworkAddress networkAddress);

            if (succeeds)
            {
                Assert.IsTrue(result);
            }
            else
            {
                Assert.IsFalse(result);
            }
            if (succeeds && toString != null)
            {
                Assert.AreEqual(toString, networkAddress.ToString());
            }
        }
        // ReSharper disable once ExcessiveIndentation
        public static IAddress[] StringToAddresses(string str)
        {
            var remoteAddresses = new List <IAddress>();

            foreach (var remoteAddress in str.Split(','))
            {
                if (DNSService.TryParse(remoteAddress, out var dns))
                {
                    remoteAddresses.Add(dns);
                }
                else if (DHCPService.TryParse(remoteAddress, out var dhcp))
                {
                    remoteAddresses.Add(dhcp);
                }
                else if (WINSService.TryParse(remoteAddress, out var wins))
                {
                    remoteAddresses.Add(wins);
                }
                else if (LocalSubnet.TryParse(remoteAddress, out var localSubnet))
                {
                    remoteAddresses.Add(localSubnet);
                }
                else if (DefaultGateway.TryParse(remoteAddress, out var defaultGateway))
                {
                    remoteAddresses.Add(defaultGateway);
                }
                else if (IPRange.TryParse(remoteAddress, out var range))
                {
                    remoteAddresses.Add(range);
                }
                else if (SingleIP.TryParse(remoteAddress, out SingleIP ip))
                {
                    remoteAddresses.Add(ip);
                }
                else if (NetworkAddress.TryParse(remoteAddress, out var network))
                {
                    remoteAddresses.Add(network);
                }
            }

            return(remoteAddresses.ToArray());
        }