예제 #1
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);
        }
예제 #2
0
        public bool AddRouterInfo(I2PRouterInfo info)
        {
            if (!ValidateRI(info))
            {
                return(false);
            }

            if (RouterInfos.TryGetValue(info.Identity.IdentHash, out var indb))
            {
                if (((DateTime)info.PublishedDate - (DateTime)indb.Router.PublishedDate).TotalSeconds > 2)
                {
                    if (!info.VerifySignature())
                    {
                        Logging.LogDebug($"NetDb: RouterInfo failed signature check: {info.Identity.IdentHash.Id32}");
                        return(false);
                    }

                    var meta = indb.Meta;
                    meta.Deleted = false;
                    meta.Updated = true;
                    var re = new RouterEntry(info, meta);
                    RouterInfos[info.Identity.IdentHash] = re;
                    if (re.IsFloodfill)
                    {
                        FloodfillInfos[info.Identity.IdentHash] = re;
                    }
                    Logging.LogDebugData($"NetDb: Updated RouterInfo for: {info.Identity.IdentHash}");
                }
                else
                {
                    return(true);
                }
            }
            else
            {
                if (!info.VerifySignature())
                {
                    Logging.LogDebug($"NetDb: RouterInfo failed signature check: {info.Identity.IdentHash.Id32}");
                    return(false);
                }

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

                var meta = new RouterInfoMeta(info.Identity.IdentHash)
                {
                    Updated = true
                };
                var re = new RouterEntry(info, meta);
                RouterInfos[info.Identity.IdentHash] = re;
                if (re.IsFloodfill)
                {
                    FloodfillInfos[info.Identity.IdentHash] = re;
                }
                Logging.LogDebugData($"NetDb: Added RouterInfo for: {info.Identity.IdentHash}");

                Statistics.IsFirewalledUpdate(
                    info.Identity.IdentHash,
                    info.Adresses
                    .Any(a =>
                         a.Options.Any(o =>
                                       o.Key.ToString() == "ihost0")));
            }

            if (RouterInfoUpdates != null)
            {
                ThreadPool.QueueUserWorkItem(a => RouterInfoUpdates(info));
            }

            return(true);
        }