Ejemplo n.º 1
0
        public int GetIpsOfNetrange(NetRange netrange)
        {
            var p1 = int.Parse(netrange.to.Split('.')[0]) - (int.Parse(netrange.from.Split('.')[0]));
            var p2 = int.Parse(netrange.to.Split('.')[1]) - (int.Parse(netrange.from.Split('.')[1]));
            var p3 = int.Parse(netrange.to.Split('.')[2]) - (int.Parse(netrange.from.Split('.')[2]));
            var p4 = int.Parse(netrange.to.Split('.')[3]) - (int.Parse(netrange.from.Split('.')[3]));

            if (p1 == 0)
            {
                p1 = 1;
            }
            if (p2 == 0)
            {
                p2 = 1;
            }
            if (p3 == 0)
            {
                p3 = 1;
            }
            if (p4 == 0)
            {
                p4 = 1;
            }

            return(p1 * p2 * p3 * p4);
        }
Ejemplo n.º 2
0
        private static NetRange API(string ip)
        {
            // send request to json endpoint
            HttpWebRequest r = (HttpWebRequest)HttpWebRequest.Create("https://stat.ripe.net/data/whois/data.json?resource=" + ip.ToString());

            r.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0";
            r.Headers.Add("Upgrade-Insecure-Requests: 1");
            r.Method = "GET";
            r.Headers.Add(HttpRequestHeader.Cookie, "serverid=www-plone-3; path=/");
            // parse response
            HttpWebResponse resp = (HttpWebResponse)r.GetResponse();

            string response;

            using (var sr = new StreamReader(resp.GetResponseStream())) {
                response = sr.ReadToEnd();
            }

            try
            {
                // get netrange (if any)
                JObject json     = JObject.Parse(response);
                string  netrange = json["data"]["records"][0][0]["value"].ToString();
                var     ips      = NetRange.GetNetRangeIPs(netrange);
                return(new NetRange(ips[0], ips[1], "netrangeObtainedFromRipe"));
            }
            catch (Exception)
            {
                // if there wasn't any netrange, return whole 0.0.0.0 to 255.255.255.255
                return(new NetRange("0.0.0.0", "255.255.255.255", "ripe_noNetRangeFound"));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validate if Ip is in NetRange.
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool IsIpInNetrange(string ip)
        {
            for (int i = 0; i < LstNetRange.Count; i++)
            {
                NetRange netrange = LstNetRange[i];
                if (netrange == null)
                {
                    return(true);
                }

                if (netrange.IsIpInNetrange(ip))
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        public List <string> GenerateIpsOfNetrange(NetRange netrange)
        {
            var lstIps = new List <string>();

            try
            {
                int[] rangeFrom =
                {
                    int.Parse(netrange.from.Split('.')[0]),
                    int.Parse(netrange.from.Split('.')[1]),
                    int.Parse(netrange.from.Split('.')[2]),
                    int.Parse(netrange.from.Split('.')[3])
                };

                int[] rangeTo =
                {
                    int.Parse(netrange.to.Split('.')[0]),
                    int.Parse(netrange.to.Split('.')[1]),
                    int.Parse(netrange.to.Split('.')[2]),
                    int.Parse(netrange.to.Split('.')[3])
                };

                while (
                    (
                        rangeFrom[0] + "." + rangeFrom[1] + "." + rangeFrom[2] + "." + rangeFrom[3])
                    !=
                    (rangeTo[0] + "." + rangeTo[1] + "." + rangeTo[2] + "." + rangeTo[3]))
                {
                    var ip = rangeFrom[0] + "." + rangeFrom[1] + "." + rangeFrom[2] + "." + rangeFrom[3];
                    lstIps.Add(ip);

                    if (rangeFrom[3] == 255)
                    {
                        rangeFrom[3] = 0;

                        if (rangeFrom[2] == 255)
                        {
                            rangeFrom[2] = 0;
                            if (rangeFrom[1] == 255)
                            {
                                rangeFrom[1] = 0;
                                rangeFrom[0]++;
                                if (rangeFrom[0] == 225 + 1)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                rangeFrom[1]++;
                            }
                        }
                        else
                        {
                            rangeFrom[2]++;
                        }
                    }
                    else
                    {
                        rangeFrom[3]++;
                    }
                }

                return(lstIps);
            }
            catch
            {
                return(lstIps);
            }
        }
Ejemplo n.º 5
0
 public NetRange GetNetrange(string ip)
 {
     return(NetRange.GetNetrange(ip));
 }