コード例 #1
0
ファイル: Program.cs プロジェクト: nasa03/IP2Country
        static async Task Main()
        {
            // Download all registry delegation latest files and store (cache) them in a temp directory
            using (var downloader = new CachingWebClient())
            {
                var temppath = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "delegationcache")).FullName;
                await Task.WhenAll(
                    downloader.DownloadAsync(new Uri("http://ftp.ripe.net/ripe/stats/delegated-ripencc-extended-latest"), Path.Combine(temppath, "ripe.dat")),
                    downloader.DownloadAsync(new Uri("http://ftp.apnic.net/pub/stats/apnic/delegated-apnic-extended-latest"), Path.Combine(temppath, "apnic.dat")),
                    downloader.DownloadAsync(new Uri("http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest"), Path.Combine(temppath, "arin.dat")),
                    downloader.DownloadAsync(new Uri("http://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-extended-latest"), Path.Combine(temppath, "lacnic.dat")),
                    downloader.DownloadAsync(new Uri("http://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-extended-latest"), Path.Combine(temppath, "afrinic.dat"))
                    ).ConfigureAwait(false);

                // Initialize resolver with all data files
                var resolver = new IP2CountryBatchResolver(
                    new IP2CountryResolver(
                        Directory.GetFiles(temppath, "*.dat").Select(f => new RegistryCSVFileSource(f))
                        )
                    );

                // A bunch of semi-"random" IPv4/IPv6 IP's from random countries for demonstration purposes...
                var ips = new[] {
                    "172.217.17.110", "31.13.91.36",
                    "2607:f8b0:4005:80b:0:0:0:200e", "2a03:2880:f11b:83:face:b00c:0:25de"
                };

                var results = resolver.Resolve(ips);

                // Now show the IP -> Country results
                for (int i = 0; i < ips.Length; i++)
                {
                    Console.WriteLine($"IP: {ips[i],40}\tCountry: {results[i]?.Country ?? "Unknown"}");
                }
            }
        }
コード例 #2
0
        public List <NodeInfoSet> GetRankedList()
        {
            var list = new List <NodeInfoSet>();

            foreach (var id in bb.PrimaryAuthorizers)
            {
                if (bb.ActiveNodes.Any(a => a.AccountID == id) && nodeStatus.ContainsKey(id))       // bug in billboard. or error-proof
                {
                    var     x   = bb.ActiveNodes.FirstOrDefault(a => a.AccountID == id);
                    decimal vts = x == null ? 0 : x.Votes;
                    list.Add(new NodeInfoSet
                    {
                        ID        = id,
                        IsPrimary = true,
                        Votes     = (long)vts,
                        Status    = nodeStatus[id]
                    });
                }
            }

            var list2           = new List <NodeInfoSet>();
            var nonPrimaryNodes = nodeStatus.Where(a => !bb.PrimaryAuthorizers.Contains(a.Key));

            foreach (var node in nonPrimaryNodes)
            {
                var     x   = bb.ActiveNodes.FirstOrDefault(a => a.AccountID == node.Key);
                decimal vts = x == null ? 0 : x.Votes;
                list2.Add(new NodeInfoSet
                {
                    ID        = node.Key,
                    IsPrimary = false,
                    Votes     = (long)vts,
                    Status    = node.Value
                });
            }

            list.AddRange(list2);

            var result = list
                         .Where(a => bb.ActiveNodes.Any(b => b.AccountID == a.ID))
                         .OrderByDescending(a => a.Votes)
                         .ThenBy(b => b.ID)
                         .Zip(Enumerable.Range(1, int.MaxValue - 1),
                              (o, i) => o.With(new { Index = i }))
                         .ToList();

            // lookup IP geo location
            try
            {
                var resolver = new IP2CountryBatchResolver(new IP2CountryResolver(
                                                               new MarkusGoCSVFileSource(ipDbFn)
                                                               ));

                var iplist  = result.Select(a => bb.NodeAddresses[a.ID]);
                var geoList = resolver.Resolve(iplist);
                for (int i = 0; i < result.Count; i++)
                {
                    result[i].Country = geoList[i] == null ? "" : geoList[i].Country;
                }
            }
            catch (Exception)
            { }

            return(result);
        }