Пример #1
0
        private I2PIdentHash GetRandomRouter(
            RouletteSelection <I2PRouterInfo, I2PIdentHash> r,
            ConcurrentBag <I2PIdentHash> exclude,
            bool exploratory)
        {
            I2PIdentHash result;
            var          me = RouterContext.Inst.MyRouterIdentity.IdentHash;

            if (exploratory)
            {
                var subset = RouterInfos
                             .Where(k => !exclude.Contains(k.Key));
                do
                {
                    result = subset
                             .Random()
                             .Key;
                } while (result == me);

                return(result);
            }

            var  retries = 0;
            bool tryagain;

            do
            {
                result   = r.GetWeightedRandom(r.Wheel.Count() < 300 ? null : exclude);
                tryagain = result == me;
            } while (tryagain && ++retries < 20);

            //Logging.LogInformation( $"GetRandomRouter selected {result}: {exclude.Any( k2 => k2 == result )}" );

            return(result);
        }
Пример #2
0
        private void IsFirewalledUpdate()
        {
            var fw = RouterInfos.Where(ri =>
                                       ri.Value.Router.Adresses.Any(a =>
                                                                    a.Options.Any(o =>
                                                                                  o.Key.ToString() == "ihost0")));

            foreach (var ri in fw)
            {
                Statistics.IsFirewalledUpdate(ri.Key, true);
            }
        }
Пример #3
0
        private void RemoveOldRouterInfos()
        {
            var inactive = Statistics.GetInactive();

            var old = RouterInfos.Where(ri =>
                                        (DateTime.Now - (DateTime)ri.Value.Router.PublishedDate).TotalDays > 1)
                      .Select(p => p.Key);

            inactive.UnionWith(old);

            if (RouterInfos.Count - inactive.Count < 400)
            {
                inactive = new HashSet <I2PIdentHash>(
                    inactive
                    .Where(k => RouterInfos.ContainsKey(k))
                    .OrderBy(k => (DateTime)RouterInfos[k].Router.PublishedDate)
                    .Take(RouterInfos.Count - 400));
            }

            RemoveRouterInfo(inactive);
        }
Пример #4
0
        void Load()
        {
            using (var s = GetStore())
            {
                var sw2 = new Stopwatch();
                sw2.Start();
                var ix = 0;
                while (s != null && (ix = s.Next(ix)) > 0)
                {
                    var reader     = new BufRefLen(s.Read(ix));
                    var recordtype = (StoreRecordId)reader.Read32();

                    try
                    {
                        switch (recordtype)
                        {
                        case StoreRecordId.StoreIdRouterInfo:
                            var one = new I2PRouterInfo(reader, false);

                            if (!ValidateRI(one))
                            {
                                s.Delete(ix);
                                RouterInfos.TryRemove(one.Identity.IdentHash, out _);
                                FloodfillInfos.TryRemove(one.Identity.IdentHash, out _);
                                Statistics.DestinationInformationFaulty(one.Identity.IdentHash);

                                continue;
                            }

                            if (!RouterContext.Inst.UseIpV6)
                            {
                                if (!one.Adresses.Any(a => a.Options.ValueContains("host", ".")))
                                {
                                    Logging.LogDebug($"NetDb: RouterInfo have no IPV4 address: {one.Identity.IdentHash.Id32}");
                                    s.Delete(ix);

                                    continue;
                                }
                            }

                            var re = new RouterEntry(
                                one,
                                new RouterInfoMeta(ix));
                            RouterInfos[one.Identity.IdentHash] = re;
                            if (re.IsFloodfill)
                            {
                                FloodfillInfos[one.Identity.IdentHash] = re;
                            }
                            break;

                        case StoreRecordId.StoreIdConfig:
                            AccessConfig(delegate(Dictionary <I2PString, I2PString> settings)
                            {
                                var key       = new I2PString(reader);
                                settings[key] = new I2PString(reader);
                            });
                            break;

                        default:
                            s.Delete(ix);
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.LogDebug($"NetDb: Load: Store exception, ix [{ix}] removed. {ex}");
                        s.Delete(ix);
                    }
                }
                sw2.Stop();
                Logging.Log($"Store: {sw2.Elapsed}");
            }

            ImportNetDbFiles();

            if (RouterInfos.Count < 20)
            {
                Logging.LogWarning($"WARNING: NetDB database contains " +
                                   $"{RouterInfos.Count} routers. Add router files to {NetDbPath}.");

                DoBootstrap();
            }

            Statistics.Load();
            IsFirewalledUpdate();

#if DEBUG
            ShowDebugDatabaseInfo();
#endif

            UpdateSelectionProbabilities();

            Save(true);
        }
Пример #5
0
        void Save(bool onlyupdated)
        {
            var created = 0;
            var updated = 0;
            var deleted = 0;

            var sw = new Stopwatch();

            sw.Start();

            RemoveOldRouterInfos();

            using (var s = GetStore())
            {
                foreach (var one in RouterInfos.ToArray())
                {
                    try
                    {
                        if (one.Value.Meta.Deleted)
                        {
                            if (one.Value.Meta.StoreIx > 0)
                            {
                                s.Delete(one.Value.Meta.StoreIx);
                            }
                            RouterInfos.TryRemove(one.Key, out _);
                            FloodfillInfos.TryRemove(one.Key, out _);
                            ++deleted;
                            continue;
                        }

                        if (!onlyupdated || (onlyupdated && one.Value.Meta.Updated))
                        {
                            var rec = new BufLen[]
                            {
                                BufUtils.To32BL((int)StoreRecordId.StoreIdRouterInfo),
                                new BufLen(one.Value.Router.ToByteArray())
                            };

                            if (one.Value.Meta.StoreIx > 0)
                            {
                                s.Write(rec, one.Value.Meta.StoreIx);
                                ++updated;
                            }
                            else
                            {
                                one.Value.Meta.StoreIx = s.Write(rec);
                                ++created;
                            }
                            one.Value.Meta.Updated = false;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logging.LogDebug("NetDb: Save: Store exception: " + ex.ToString());
                        one.Value.Meta.StoreIx = -1;
                    }
                }

                SaveConfig(s);
            }

            Logging.Log($"NetDb.Save( {( onlyupdated ? "updated" : "all" )} ): " +
                        $"{created} created, {updated} updated, {deleted} deleted.");

            Statistics.RemoveOldStatistics();
            UpdateSelectionProbabilities();

            sw.Stop();
            Logging.Log($"NetDB: Save: {sw.Elapsed}");
        }
Пример #6
0
        private void ShowDebugDatabaseInfo()
        {
            Logging.LogDebug($"NetDb: Router count: {RouterInfos.Count}");

            var nohost = RouterInfos
                         .Where(ri => !ri.Value.Router.Adresses.Any(a =>
                                                                    a.Options.TryGet("host") != null));

            Logging.LogDebug($"NetDb: No host: {nohost.Count()}");

            var ipv4 = RouterInfos
                       .Where(ri => ri.Value.Router.Adresses.Any(a =>
                                                                 a.Options.TryGet("host")?.ToString().Contains(".") ?? false));

            Logging.LogDebug($"NetDb: IPV4: {ipv4.Count()}");

            var ipv6 = RouterInfos
                       .Where(ri => ri.Value.Router.Adresses.Any(a =>
                                                                 a.Options.TryGet("host")?.ToString().Contains(":") ?? false));

            Logging.LogDebug($"NetDb: IPV6: {ipv6.Count()}");

            var onlyipv4 = RouterInfos
                           .Where(ri =>
                                  ri.Value.Router.Adresses.Any(a =>
                                                               a.Options.TryGet("host")?.ToString().Contains(".") ?? false) &&
                                  !ri.Value.Router.Adresses.Any(a =>
                                                                a.Options.TryGet("host")?.ToString().Contains(":") ?? false));

            Logging.LogDebug($"NetDb: Only IPV4: {onlyipv4.Count()}");

            var onlyipv6 = RouterInfos
                           .Where(ri =>
                                  ri.Value.Router.Adresses.Any(a =>
                                                               a.Options.TryGet("host")?.ToString().Contains(":") ?? false) &&
                                  !ri.Value.Router.Adresses.Any(a =>
                                                                a.Options.TryGet("host")?.ToString().Contains(".") ?? false));

            Logging.LogDebug($"NetDb: Only IPV6: {onlyipv6.Count()}");

            var ntcp = RouterInfos
                       .Where(ri =>
                              ri.Value.Router.Adresses.Any(a => a.TransportStyle == "NTCP"));

            Logging.LogDebug($"NetDb: NTCP: {ntcp.Count()}");

            var ssu = RouterInfos
                      .Where(ri =>
                             ri.Value.Router.Adresses.Any(a => a.TransportStyle == "SSU"));

            Logging.LogDebug($"NetDb: SSU: {ssu.Count()}");

            var onlyntcp = RouterInfos
                           .Where(ri =>
                                  ri.Value.Router.Adresses.Any(a => a.TransportStyle == "NTCP") &&
                                  !ri.Value.Router.Adresses.Any(a => a.TransportStyle == "SSU"));

            Logging.LogDebug($"NetDb: Only NTCP: {onlyntcp.Count()}");

            var onlyssu = RouterInfos
                          .Where(ri =>
                                 ri.Value.Router.Adresses.Any(a => a.TransportStyle == "SSU") &&
                                 !ri.Value.Router.Adresses.Any(a => a.TransportStyle == "NTCP"));

            Logging.LogDebug($"NetDb: Only SSU: {onlyssu.Count()}");
        }