示例#1
0
        public static LinkData Decode(G2Header root)
        {
            LinkData link = new LinkData();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                    continue;

                switch (child.Name)
                {
                    case Packet_Project:
                        link.Project = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                        break;

                    case Packet_Target:
                        link.Target = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        link.TargetID = Utilities.KeytoID(link.Target);
                        break;

                    case Packet_Uplink:
                        link.Uplink = BitConverter.ToBoolean(child.Data, child.PayloadPos);
                        break;

                    case Packet_Title:
                        link.Title = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return link;
        }
示例#2
0
        public static LinkData Decode(G2Header root)
        {
            LinkData link  = new LinkData();
            G2Header child = new G2Header(root.Data);

            while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD)
            {
                if (!G2Protocol.ReadPayload(child))
                {
                    continue;
                }

                switch (child.Name)
                {
                case Packet_Project:
                    link.Project = BitConverter.ToUInt32(child.Data, child.PayloadPos);
                    break;

                case Packet_Target:
                    link.Target   = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    link.TargetID = Utilities.KeytoID(link.Target);
                    break;

                case Packet_Uplink:
                    link.Uplink = BitConverter.ToBoolean(child.Data, child.PayloadPos);
                    break;

                case Packet_Title:
                    link.Title = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(link);
        }
示例#3
0
        private void Process_LinkData(OpTrust trust, SignedData signed, LinkData linkData)
        {
            if (!Utilities.CheckSignedData(trust.File.Key, signed.Data, signed.Signature))
                return;

            Core.IndexKey(linkData.TargetID, ref linkData.Target);

            uint project = linkData.Project;

            OpLink localLink = trust.GetLink(project);

            if (localLink == null)
                return;

            OpTrust targetTrust = GetTrust(linkData.TargetID, false);

            if (targetTrust == null)
            {
                targetTrust = new OpTrust(new OpVersionedFile(linkData.Target));
                TrustMap.SafeAdd(linkData.TargetID, targetTrust);
            }

            targetTrust.AddProject(project, false);
            OpLink targetLink = targetTrust.GetLink(project);

            if (linkData.Uplink)
            {
                localLink.Uplink = targetLink;

                targetLink.Downlinks.Add(localLink);

                // always know a link's trust structure to the top
                if (!targetTrust.Loaded)
                    Cache.Research(targetTrust.UserID);

                //if (targetLink.Uplink == null)
                //    AddRoot(targetLink);
            }

            else
            {
                if (linkData.Title != null)
                    localLink.Titles[targetLink.UserID] = linkData.Title;

                localLink.Confirmed.Add(targetLink.UserID);
            }
        }
示例#4
0
        public void SaveLocal()
        {
            if (Core.InvokeRequired)
            {
                Core.RunInCoreAsync(() => SaveLocal());
                return;
            }

            try
            {
                // create new link file in temp dir
                string tempPath = Core.GetTempPath();
                byte[] key = Utilities.GenerateKey(Core.StrongRndGen, 256);
                using (IVCryptoStream stream = IVCryptoStream.Save(tempPath, key))
                {
                    // project packets
                    foreach (OpLink link in LocalTrust.Links.Values)
                        if (link.Active)
                        {
                            ProjectData project = new ProjectData();
                            project.ID = link.Project;
                            project.Name = GetProjectName(link.Project);

                            if (link.Project == 0)
                                project.UserName = Core.User.Settings.UserName;

                            byte[] packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, project);
                            stream.Write(packet, 0, packet.Length);

                            // uplinks
                            if (link.Uplink != null)
                            {
                                LinkData data = new LinkData(link.Project, link.Uplink.Trust.File.Key);
                                packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, data);
                                stream.Write(packet, 0, packet.Length);
                            }

                            // downlinks
                            foreach (OpLink downlink in link.Downlinks)
                                if (link.Confirmed.Contains(downlink.UserID))
                                {
                                    string title;
                                    link.Titles.TryGetValue(downlink.UserID, out title);

                                    LinkData data = new LinkData(link.Project, downlink.Trust.File.Key, title);
                                    packet = SignedData.Encode(Network.Protocol, Core.User.Settings.KeyPair, data);
                                    stream.Write(packet, 0, packet.Length);
                                }
                        }

                    PacketStream streamEx = new PacketStream(stream, Network.Protocol, FileAccess.Write);

                    // save top 5 web caches
                    foreach (WebCache cache in Network.Cache.GetLastSeen(5))
                        streamEx.WritePacket(new WebCache(cache, TrustPacket.WebCache));

                    // save inheritable settings only if they can be inherited
                    if (IsInheritNode(Core.UserID))
                    {
                        if (Core.User.OpIcon != null)
                            streamEx.WritePacket(new IconPacket(TrustPacket.Icon, Core.User.OpIcon));

                        if (Core.User.OpSplash != null)
                        {
                            MemoryStream splash = new MemoryStream();
                            Core.User.OpSplash.Save(splash, ImageFormat.Jpeg);
                            LargeDataPacket.Write(streamEx, TrustPacket.Splash, splash.ToArray());
                        }
                    }

                    stream.WriteByte(0); // signal last packet
                    stream.FlushFinalBlock();
                }

                OpVersionedFile file = Cache.UpdateLocal(tempPath, key, null);

                Store.PublishDirect(GetLocsAbove(), Core.UserID, ServiceID, DataTypeFile, file.SignedHeader);

                SaveUplinkReqs();
            }
            catch (Exception ex)
            {
                Network.UpdateLog("LinkControl", "Error updating local " + ex.Message);
            }
        }