Example #1
0
        public void Load(DhtNetwork network)
        {
            // if the user has multiple ops, the lookup network is setup with the same settings
            // so it is easy to find and predictable for other's bootstrapping
            // we put it in local settings so we safe-guard these settings from moving to other computers
            // and having dupe dht lookup ids on the network
            try
            {
                var serializer = new XmlSerializer(typeof(PortsConfig));

                using (var reader = new StreamReader(PortsConfigPath))
                    Ports = (PortsConfig)serializer.Deserialize(reader);
            }
            catch
            {
                Ports = new PortsConfig();
            }

            if (Ports.UserID == 0 || network.Core.Sim != null)
            {
                Ports.UserID = Utilities.StrongRandUInt64(network.Core.StrongRndGen);
            }

            // keep tcp/udp the same by default
            if (Ports.Tcp == 0 || network.Core.Sim != null)
            {
                Ports.Tcp = (ushort)network.Core.RndGen.Next(3000, 15000);
            }

            if (Ports.Udp == 0 || network.Core.Sim != null)
            {
                Ports.Udp = Ports.Tcp;
            }


            // dont want instances saving and loading same lookup file
            if (network.Core.Sim == null && File.Exists(BootstrapPath))
            {
                try
                {
                    using (IVCryptoStream crypto = IVCryptoStream.Load(BootstrapPath, BootstrapKey))
                    {
                        PacketStream stream = new PacketStream(crypto, network.Protocol, FileAccess.Read);

                        G2Header root = null;

                        while (stream.ReadPacket(ref root))
                        {
                            if (root.Name == IdentityPacket.LookupCachedIP)
                            {
                                network.Cache.AddSavedContact(CachedIP.Decode(root));
                            }

                            if (root.Name == IdentityPacket.LookupCachedWeb)
                            {
                                network.Cache.AddWebCache(WebCache.Decode(root));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    network.UpdateLog("Exception", "LookupSettings::Load " + ex.Message);
                }
            }
        }
Example #2
0
        public void Load(LoadModeType loadMode)
        {
            RijndaelManaged Password = new RijndaelManaged();

            Password.Key = PasswordKey;

            byte[] iv   = new byte[16];
            byte[] salt = new byte[4];

            OpCore lookup = null;

            if (Core != null)
            {
                lookup = Core.Context.Lookup;
            }

            try
            {
                using (TaggedStream file = new TaggedStream(ProfilePath, Protocol, ProcessSplash)) // tagged with splash
                {
                    // first 16 bytes IV, next 4 bytes is salt
                    file.Read(iv, 0, 16);
                    file.Read(salt, 0, 4);
                    Password.IV = iv;

                    using (CryptoStream crypto = new CryptoStream(file, Password.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        PacketStream stream = new PacketStream(crypto, Protocol, FileAccess.Read);

                        G2Header root = null;
                        while (stream.ReadPacket(ref root))
                        {
                            if (loadMode == LoadModeType.Settings)
                            {
                                if (root.Name == IdentityPacket.OperationSettings)
                                {
                                    Settings = SettingsPacket.Decode(root);
                                }

                                if (root.Name == IdentityPacket.UserInfo && Core != null &&
                                    (Core.Sim == null || !Core.Sim.Internet.FreshStart))
                                {
                                    Core.IndexInfo(UserInfo.Decode(root));
                                }

                                // save icon to identity file because only root node saves icon/splash to link file
                                // to minimize link file size, but allow user to set custom icon/splash if there are not overrides
                                if (root.Name == IdentityPacket.Icon)
                                {
                                    OpIcon = IconPacket.Decode(root).OpIcon;
                                }
                            }

                            if (lookup != null && (loadMode == LoadModeType.AllCaches || loadMode == LoadModeType.LookupCache))
                            {
                                if (root.Name == IdentityPacket.LookupCachedIP)
                                {
                                    lookup.Network.Cache.AddSavedContact(CachedIP.Decode(root));
                                }

                                if (root.Name == IdentityPacket.LookupCachedWeb)
                                {
                                    lookup.Network.Cache.AddWebCache(WebCache.Decode(root));
                                }
                            }

                            if (loadMode == LoadModeType.AllCaches)
                            {
                                if (root.Name == IdentityPacket.OpCachedIP)
                                {
                                    Core.Network.Cache.AddSavedContact(CachedIP.Decode(root));
                                }

                                if (root.Name == IdentityPacket.OpCachedWeb)
                                {
                                    Core.Network.Cache.AddWebCache(WebCache.Decode(root));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }