Esempio n. 1
0
 public OpLink(OpTrust trust, uint project)
 {
     Trust = trust;
     Project = project;
 }
Esempio n. 2
0
        private void Process_UplinkReq(DataReq data, SignedData signed, UplinkRequest request)
        {
            Core.IndexKey(request.KeyID, ref request.Key);
            Core.IndexKey(request.TargetID, ref request.Target);

            if (!Utilities.CheckSignedData(request.Key, signed.Data, signed.Signature))
                return;

            OpTrust requesterTrust = GetTrust(request.KeyID);

            if (requesterTrust != null && requesterTrust.Loaded && requesterTrust.File.Header.Version > request.LinkVersion)
                return;

            // check if target in linkmap, if not add
            OpTrust targetTrust = GetTrust(request.TargetID);

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

            if (targetTrust.Loaded && targetTrust.File.Header.Version > request.TargetVersion)
                return;

            request.Signed = signed.Encode(Network.Protocol); // so we can send it in results / save, later on

            // check for duplicate requests
            OpLink targetLink = targetTrust.GetLink(request.ProjectID);

            if (targetLink != null)
            {
                foreach (UplinkRequest compare in targetLink.Requests)
                    if (Utilities.MemCompare(compare.Signed, request.Signed))
                        return;
            }
            else
            {
                targetTrust.AddProject(request.ProjectID, true);
                targetLink = targetTrust.GetLink(request.ProjectID);
            }

            // add
            targetLink.Requests.Add(request);

            // if target is marked as linked or focused, update link of target and sender
            if (targetTrust.Loaded && (targetTrust.InLocalLinkTree || Core.KeepData.SafeContainsKey(targetTrust.UserID)))
            {
                if (targetTrust.File.Header.Version < request.TargetVersion)
                    Cache.Research(targetTrust.UserID);

                if (requesterTrust == null)
                {
                    requesterTrust = new OpTrust(new OpVersionedFile(request.Key));
                    TrustMap.SafeAdd(request.KeyID, requesterTrust);
                }

                // once new version of requester's link file has been downloaded, interface will be updated
                if (!requesterTrust.Loaded || (requesterTrust.File.Header.Version < request.LinkVersion))
                    Cache.Research(requesterTrust.UserID);
            }

            RunSaveUplinks = true;
        }
Esempio n. 3
0
        public TrustService(OpCore core)
        {
            Core = core;
            Core.Trust = this;

            Store = Core.Network.Store;
            Network = Core.Network;

            Core.SecondTimerEvent += Core_SecondTimer;
            Core.MinuteTimerEvent += Core_MinuteTimer;
            Core.KeepDataCore += new KeepDataHandler(Core_KeepData);

            Cache = new VersionedCache(Network, ServiceID, DataTypeFile, false);

            Network.CoreStatusChange += new StatusChange(Network_StatusChange);

            // piggyback searching for uplink requests on cache file data
            Store.StoreEvent[ServiceID, DataTypeFile] += new StoreHandler(Store_Local);
            Network.Searches.SearchEvent[ServiceID, DataTypeFile] += new SearchRequestHandler(Search_Local);

            Core.Locations.KnowOnline += new KnowOnlineHandler(Location_KnowOnline);

            Cache.FileAquired += new FileAquiredHandler(Cache_FileAquired);
            Cache.FileRemoved += new FileRemovedHandler(Cache_FileRemoved);
            Cache.Load();

            ProjectNames.SafeAdd(0, Core.User.Settings.Operation);

            LinkPath = Core.User.RootPath + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar + ServiceID.ToString();
            Directory.CreateDirectory(LinkPath);

            LocalFileKey = Core.User.Settings.FileKey;

            LoadUplinkReqs();

            LocalTrust = GetTrust(Core.UserID);

            if (LocalTrust == null)
            {
                LocalTrust = new OpTrust(new OpVersionedFile(Core.User.Settings.KeyPublic));
                TrustMap.SafeAdd(Core.UserID, LocalTrust);
            }

            if (!LocalTrust.Loaded)
            {
                LocalTrust.Name = Core.User.Settings.UserName;
                LocalTrust.AddProject(0, true); // operation

                SaveLocal();
            }
        }
Esempio n. 4
0
        private void Process_ProjectData(OpTrust trust, SignedData signed, ProjectData project)
        {
            if (!Utilities.CheckSignedData(trust.File.Key, signed.Data, signed.Signature))
                return;

            if (project.ID != 0 && !ProjectNames.SafeContainsKey(project.ID))
                ProjectNames.SafeAdd(project.ID, project.Name);

            trust.AddProject(project.ID, true);

            if (project.ID == 0)
            {
                trust.Name = project.UserName;
                Core.IndexName(trust.UserID, trust.Name);
            }

            OpLink link = trust.GetLink(project.ID);
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
0
        private void Cache_FileAquired(OpVersionedFile cachefile)
        {
            try
            {
                // get link directly, even if in unloaded state we need the same reference
                OpTrust trust = null;
                TrustMap.SafeTryGetValue(cachefile.UserID, out trust);

                if (trust == null)
                {
                    trust = new OpTrust(cachefile);
                    TrustMap.SafeAdd(cachefile.UserID, trust);
                }
                else
                    trust.File = cachefile;

                // clean roots, if link has loopID, remove loop node entirely, it will be recreated if needed later
                ProjectRoots.LockReading(delegate()
                {
                    foreach (uint project in ProjectRoots.Keys)
                    {
                        OpLink link = trust.GetLink(project);

                        if (link == null)
                            continue;

                        ThreadedList<OpLink> roots = ProjectRoots[project];

                        roots.SafeRemove(link);

                        // remove loop node
                        if (link.LoopRoot != null)
                            roots.LockReading(delegate()
                            {
                                foreach (OpLink root in roots)
                                    if (root.UserID == link.LoopRoot.UserID)
                                    {
                                        roots.SafeRemove(root); // root is a loop node

                                        // remove associations with loop node
                                        foreach (OpLink downlink in root.Downlinks)
                                            downlink.LoopRoot = null;

                                        break;
                                    }
                            });
                    }
                });

                trust.Reset();

                // load data from link file
                string inheritName = null;
                string inheritOp = null;
                Bitmap inheritIcon = null;
                byte[] inheritSplash = null;

                using (TaggedStream file = new TaggedStream(Cache.GetFilePath(cachefile.Header), Network.Protocol))
                using (IVCryptoStream crypto = IVCryptoStream.Load(file, cachefile.Header.FileKey))
                {
                    PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read);

                    G2Header packetRoot = null;

                    while (stream.ReadPacket(ref packetRoot))
                    {
                        if (packetRoot.Name == DataPacket.SignedData)
                        {
                            SignedData signed = SignedData.Decode(packetRoot);
                            G2Header embedded = new G2Header(signed.Data);

                            // figure out data contained
                            if (G2Protocol.ReadPacket(embedded))
                            {
                                if (embedded.Name == TrustPacket.ProjectData)
                                {
                                    ProjectData project = ProjectData.Decode(embedded);
                                    Process_ProjectData(trust, signed, project);

                                    if (project.ID == 0)
                                    {
                                        inheritName = project.UserName;
                                        inheritOp = project.Name;
                                    }
                                }

                                else if (embedded.Name == TrustPacket.LinkData)
                                    Process_LinkData(trust, signed, LinkData.Decode(embedded));
                            }
                        }

                        else if (packetRoot.Name == TrustPacket.WebCache)
                            Network.Cache.AddWebCache(WebCache.Decode(packetRoot));

                        else if (packetRoot.Name == TrustPacket.Icon)
                            inheritIcon = IconPacket.Decode(packetRoot).OpIcon;

                        else if (packetRoot.Name == TrustPacket.Splash)
                        {
                            LargeDataPacket splash = LargeDataPacket.Decode(packetRoot);

                            if (splash.Size > 0)
                                inheritSplash = LargeDataPacket.Read(splash, stream, TrustPacket.Splash);
                        }
                    }
                }

                // set new header
                trust.Loaded = true;

                // set as root if node has no uplinks
                foreach (OpLink link in trust.Links.Values)
                    if (link.Uplink == null)
                        AddRoot(link);
                    // if uplink is unknown - process link data will search for the unknown parent

                // if loop created, create new loop node with unique ID, assign all nodes in loop the ID and add as downlinks
                foreach (OpLink link in trust.Links.Values)
                    if (IsLooped(link))
                    {
                        uint project = link.Project;

                        OpLink loop = new OpTrust(project, (ulong)Core.RndGen.Next()).GetLink(project);
                        loop.IsLoopRoot = true;

                        List<ulong> uplinks = GetUnconfirmedUplinkIDs(trust.UserID, project);
                        uplinks.Add(trust.UserID);

                        foreach (ulong uplink in uplinks)
                        {
                            OpLink member = GetLink(uplink, project);

                            if (member == null)
                                continue;

                            member.LoopRoot = loop;

                            loop.Downlinks.Add(member);
                            loop.Confirmed.Add(member.UserID); //needed for getlowers
                        }

                        AddRoot(loop);
                    }

                trust.CheckRequestVersions();

                if (LinkUpdate != null)
                    LinkUpdate.Invoke(trust);

                if (Core.NewsWorthy(trust.UserID, 0, false))
                    Core.MakeNews(ServiceIDs.Trust, "Trust updated by " + Core.GetName(trust.UserID), trust.UserID, 0, true);

                // update subs
                if (Network.Established)
                {
                    List<LocationData> locations = new List<LocationData>();

                    ProjectRoots.LockReading(delegate()
                    {
                        foreach (uint project in ProjectRoots.Keys)
                            if (Core.UserID == trust.UserID || IsHigher(trust.UserID, project))
                                GetLocsBelow(Core.UserID, project, locations);
                    });

                    Store.PublishDirect(locations, trust.UserID, ServiceID, 0, cachefile.SignedHeader);
                }

                // inherit local settings
                if(Core.UserID == trust.UserID)
                {
                    if (inheritName != null)
                        Core.User.Settings.UserName = inheritName;
                }

                // inherit settings from highest node, first node in loop
                if (IsInheritNode(trust.UserID))
                {
                    if (inheritOp != null)
                        Core.User.Settings.Operation = inheritOp;

                    if (inheritIcon != null)
                    {
                        Core.User.OpIcon = inheritIcon;
                        Core.User.IconUpdate();
                    }

                    if (inheritSplash != null)
                        Core.User.OpSplash = (Bitmap)Bitmap.FromStream(new MemoryStream(inheritSplash));
                    else
                        Core.User.OpSplash = null;
                }

                // update interface node
                Core.RunInGuiThread(GuiUpdate, trust.UserID);

                foreach (OpLink link in trust.Links.Values)
                    foreach (OpLink downlink in link.Downlinks)
                        Core.RunInGuiThread(GuiUpdate, downlink.UserID);

            }
            catch (Exception ex)
            {
                Network.UpdateLog("Link", "Error loading file " + ex.Message);
            }
        }
Esempio n. 7
0
        public void Trust_Update(OpTrust trust)
        {
            if (!ActiveUsers.Contains(trust.UserID))
                return;

            IMStatus status = null;
            if (IMMap.SafeTryGetValue(trust.UserID, out status))
                Update(status);
        }
Esempio n. 8
0
        void Trust_Update(OpTrust trust)
        {
            // update working projects (add)
            if (trust.UserID == Core.UserID)
            {
                OpStorage local = GetStorage(Core.UserID);

                foreach (uint project in Trust.LocalTrust.Links.Keys)
                    if (!Working.ContainsKey(project))
                    {
                        if(local != null)
                            LoadHeaderFile(GetWorkingPath(project), local, false, true);

                        Working[project] = new WorkingStorage(this, project);
                    }
            }

            // remove all higher changes, reload with new highers (cause link changed
            foreach (WorkingStorage working in Working.Values )
                if (Core.UserID == trust.UserID || Trust.IsHigher(trust.UserID, working.ProjectID))
                {
                    working.RemoveAllHigherChanges();

                    foreach (ulong uplink in Trust.GetAutoInheritIDs(Core.UserID, working.ProjectID))
                        working.RefreshHigherChanges(uplink);
                }
        }