상속: DeOps.Implementation.Protocol.G2Packet
예제 #1
0
        private void Process_VersionedFile(DataReq data, SignedData signed, VersionedFileHeader header)
        {
            Core.IndexKey(header.KeyID, ref header.Key);

            OpVersionedFile current = GetFile(header.KeyID);

            // if link loaded
            if (current != null)
            {
                // lower version
                if (header.Version < current.Header.Version)
                {
                    if (data != null && data.Source != null)
                    {
                        Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, current.UserID, Service, DataType, current.SignedHeader));
                    }

                    return;
                }

                // higher version
                else if (header.Version > current.Header.Version)
                {
                    CacheFile(signed, header);
                }
            }

            // else load file, set new header after file loaded
            else
            {
                CacheFile(signed, header);
            }
        }
예제 #2
0
        private void EndDownload(object[] args)
        {
            SignedData          signedHeader = args[0] as SignedData;
            VersionedFileHeader header       = args[1] as VersionedFileHeader;

            CacheFile(signedHeader, header);
        }
예제 #3
0
        private void Download(SignedData signed, VersionedFileHeader header)
        {
            if (!Utilities.CheckSignedData(header.Key, signed.Data, signed.Signature))
            {
                Debug.Assert(false);
                return;
            }

            FileDetails details = new FileDetails(Service, DataType, header.FileHash, header.FileSize, null);

            Core.Transfers.StartDownload(header.KeyID, details, GetFilePath(header), new EndDownloadHandler(EndDownload), new object[] { signed, header });
        }
예제 #4
0
        public static VersionedFileHeader Decode(byte[] data)
        {
            G2Header root = new G2Header(data);

            if (!G2Protocol.ReadPacket(root))
            {
                return(null);
            }

            if (root.Name != DataPacket.VersionedFile)
            {
                return(null);
            }

            return(VersionedFileHeader.Decode(root));
        }
예제 #5
0
        public static VersionedFileHeader Decode(G2Header root)
        {
            VersionedFileHeader header = new VersionedFileHeader();
            G2Header            child  = new G2Header(root.Data);

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

                switch (child.Name)
                {
                case Packet_Key:
                    header.Key   = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    header.KeyID = Utilities.KeytoID(header.Key);
                    break;

                case Packet_Version:
                    header.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_FileHash:
                    header.FileHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_FileSize:
                    header.FileSize = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_FileKey:
                    header.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;

                case Packet_Extra:
                    header.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                    break;
                }
            }

            return(header);
        }
예제 #6
0
        void Store_Local(DataReq store)
        {
            // getting published to - search results - patch

            SignedData signed = SignedData.Decode(store.Data);

            if (signed == null)
            {
                return;
            }

            G2Header embedded = new G2Header(signed.Data);

            // figure out data contained
            if (G2Protocol.ReadPacket(embedded))
            {
                if (embedded.Name == DataPacket.VersionedFile)
                {
                    Process_VersionedFile(store, signed, VersionedFileHeader.Decode(signed.Data));
                }
            }
        }
예제 #7
0
        public static VersionedFileHeader Decode(G2Header root)
        {
            VersionedFileHeader header = new VersionedFileHeader();
            G2Header child = new G2Header(root.Data);

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

                switch (child.Name)
                {
                    case Packet_Key:
                        header.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        header.KeyID = Utilities.KeytoID(header.Key);
                        break;

                    case Packet_Version:
                        header.Version = CompactNum.ToUInt32(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_FileHash:
                        header.FileHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_FileSize:
                        header.FileSize = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_FileKey:
                        header.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;

                    case Packet_Extra:
                        header.Extra = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize);
                        break;
                }
            }

            return header;
        }
예제 #8
0
        private void Process_VersionedFile(DataReq data, SignedData signed, VersionedFileHeader header)
        {
            Core.IndexKey(header.KeyID, ref header.Key);

            OpVersionedFile current = GetFile(header.KeyID);

            // if link loaded
            if (current != null)
            {
                // lower version
                if (header.Version < current.Header.Version)
                {
                    if (data != null && data.Source != null)
                        Store.Send_StoreReq(data.Source, data.LocalProxy, new DataReq(null, current.UserID, Service, DataType, current.SignedHeader));

                    return;
                }

                // higher version
                else if (header.Version > current.Header.Version)
                {
                    CacheFile(signed, header);
                }
            }

            // else load file, set new header after file loaded
            else
                CacheFile(signed, header);
        }
예제 #9
0
        private void Download(SignedData signed, VersionedFileHeader header)
        {
            if (!Utilities.CheckSignedData(header.Key, signed.Data, signed.Signature))
            {
                Debug.Assert(false);
                return;
            }

            FileDetails details = new FileDetails(Service, DataType, header.FileHash, header.FileSize, null);

            Core.Transfers.StartDownload(header.KeyID, details, GetFilePath(header), new EndDownloadHandler(EndDownload), new object[] { signed, header } );
        }
예제 #10
0
        private void CacheFile(SignedData signedHeader, VersionedFileHeader header)
        {
            if (Core.InvokeRequired)
                Debug.Assert(false);

            try
            {
                // check if file exists
                string path = "";
                if (header.FileHash != null)
                {
                    path = GetFilePath(header);
                    if (!File.Exists(path))
                    {
                        Download(signedHeader, header);
                        return;
                    }
                }

                // get file
                OpVersionedFile prevFile = GetFile(header.KeyID);

                if (prevFile != null)
                    if (header.Version < prevFile.Header.Version)
                        return; // dont update with older version

                OpVersionedFile newFile = new OpVersionedFile(header.Key);

                // set new header
                newFile.Header = header;
                newFile.SignedHeader = signedHeader.Encode(Network.Protocol);
                newFile.Unique = !Network.Established;

                FileMap.SafeAdd(header.KeyID, newFile);

                RunSaveHeaders = true;

                if(FileAquired != null)
                    FileAquired.Invoke(newFile);

                // delete old file - do after aquired event so invoked (storage) can perform clean up operation
                if (prevFile != null && prevFile.Header.FileHash != null)
                {
                    string oldPath = GetFilePath(prevFile.Header);
                    if (path != oldPath && File.Exists(oldPath))
                        try { File.Delete(oldPath); }
                        catch { }
                }
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("VersionedFile", "Error caching data " + ex.Message);
            }
        }
예제 #11
0
        public OpVersionedFile UpdateLocal(string tempPath, byte[] key, byte[] extra)
        {
            OpVersionedFile vfile = null;

            if (Core.InvokeRequired)
            {
                // block until completed
                Core.RunInCoreBlocked(delegate() { vfile = UpdateLocal(tempPath, key, extra); });
                return vfile;
            }

            vfile = GetFile(Core.UserID);

            VersionedFileHeader header = null;
            if (vfile != null)
                header = vfile.Header;

            string oldFile = null;
            if (header != null && header.FileHash != null)
                oldFile = GetFilePath(header);

            if (header == null)
                header = new VersionedFileHeader();

            header.Key = Core.User.Settings.KeyPublic;
            header.KeyID = Core.UserID; // set so keycheck works
            header.Version++;
            header.FileKey = key;
            header.Extra = extra;

            // finish building header
            if (key != null)
            {
                Utilities.HashTagFile(tempPath, Network.Protocol, ref header.FileHash, ref header.FileSize);

                // move file, overwrite if need be
                string finalPath = GetFilePath(header);
                File.Move(tempPath, finalPath);
            }

            CacheFile(new SignedData(Network.Protocol, Core.User.Settings.KeyPair, header), header);

            SaveHeaders();

            if (oldFile != null && File.Exists(oldFile)) // delete after move to ensure a copy always exists (names different)
                try { File.Delete(oldFile); }
                catch { }

            vfile = GetFile(Core.UserID);

            if (!LocalSync)
                Core.Sync.UpdateLocal(); // calls this same function for local sync which publishes

            else if (GlobalIM)
                Core.Locations.UpdateLocation();

            else if (Network.Established)
            {
                Store.PublishNetwork(Core.UserID, Service, DataType, vfile.SignedHeader);
                Core.Locations.UpdateLocation();
            }

            else
                vfile.Unique = true; // publish when connected

            return vfile;
        }
예제 #12
0
 public string GetFilePath(VersionedFileHeader header)
 {
     return CachePath + Path.DirectorySeparatorChar + Utilities.CryptFilename(Core, header.KeyID, header.FileHash);
 }
예제 #13
0
 public string GetFilePath(VersionedFileHeader header)
 {
     return(CachePath + Path.DirectorySeparatorChar + Utilities.CryptFilename(Core, header.KeyID, header.FileHash));
 }
예제 #14
0
        private void CacheFile(SignedData signedHeader, VersionedFileHeader header)
        {
            if (Core.InvokeRequired)
            {
                Debug.Assert(false);
            }

            try
            {
                // check if file exists
                string path = "";
                if (header.FileHash != null)
                {
                    path = GetFilePath(header);
                    if (!File.Exists(path))
                    {
                        Download(signedHeader, header);
                        return;
                    }
                }

                // get file
                OpVersionedFile prevFile = GetFile(header.KeyID);

                if (prevFile != null)
                {
                    if (header.Version < prevFile.Header.Version)
                    {
                        return; // dont update with older version
                    }
                }
                OpVersionedFile newFile = new OpVersionedFile(header.Key);


                // set new header
                newFile.Header       = header;
                newFile.SignedHeader = signedHeader.Encode(Network.Protocol);
                newFile.Unique       = !Network.Established;

                FileMap.SafeAdd(header.KeyID, newFile);

                RunSaveHeaders = true;

                if (FileAquired != null)
                {
                    FileAquired.Invoke(newFile);
                }


                // delete old file - do after aquired event so invoked (storage) can perform clean up operation
                if (prevFile != null && prevFile.Header.FileHash != null)
                {
                    string oldPath = GetFilePath(prevFile.Header);
                    if (path != oldPath && File.Exists(oldPath))
                    {
                        try { File.Delete(oldPath); }
                        catch { }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("VersionedFile", "Error caching data " + ex.Message);
            }
        }
예제 #15
0
        public OpVersionedFile UpdateLocal(string tempPath, byte[] key, byte[] extra)
        {
            OpVersionedFile vfile = null;

            if (Core.InvokeRequired)
            {
                // block until completed
                Core.RunInCoreBlocked(delegate() { vfile = UpdateLocal(tempPath, key, extra); });
                return(vfile);
            }

            vfile = GetFile(Core.UserID);

            VersionedFileHeader header = null;

            if (vfile != null)
            {
                header = vfile.Header;
            }

            string oldFile = null;

            if (header != null && header.FileHash != null)
            {
                oldFile = GetFilePath(header);
            }

            if (header == null)
            {
                header = new VersionedFileHeader();
            }


            header.Key   = Core.User.Settings.KeyPublic;
            header.KeyID = Core.UserID; // set so keycheck works
            header.Version++;
            header.FileKey = key;
            header.Extra   = extra;


            // finish building header
            if (key != null)
            {
                Utilities.HashTagFile(tempPath, Network.Protocol, ref header.FileHash, ref header.FileSize);

                // move file, overwrite if need be
                string finalPath = GetFilePath(header);
                File.Move(tempPath, finalPath);
            }

            CacheFile(new SignedData(Network.Protocol, Core.User.Settings.KeyPair, header), header);

            SaveHeaders();

            if (oldFile != null && File.Exists(oldFile)) // delete after move to ensure a copy always exists (names different)
            {
                try { File.Delete(oldFile); }
                catch { }
            }

            vfile = GetFile(Core.UserID);

            if (!LocalSync)
            {
                Core.Sync.UpdateLocal(); // calls this same function for local sync which publishes
            }
            else if (GlobalIM)
            {
                Core.Locations.UpdateLocation();
            }

            else if (Network.Established)
            {
                Store.PublishNetwork(Core.UserID, Service, DataType, vfile.SignedHeader);
                Core.Locations.UpdateLocation();
            }

            else
            {
                vfile.Unique = true; // publish when connected
            }
            return(vfile);
        }
예제 #16
0
        public void LoadHeaders()
        {
            List <string> goodPaths = new List <string>();

            try
            {
                goodPaths.Add(HeaderPath);

                if (!File.Exists(HeaderPath))
                {
                    return;
                }

                using (IVCryptoStream crypto = IVCryptoStream.Load(HeaderPath, LocalKey))
                {
                    PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read);

                    G2Header root = null;

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


                            // figure out data contained
                            if (G2Protocol.ReadPacket(embedded))
                            {
                                if (embedded.Name == DataPacket.VersionedFile)
                                {
                                    VersionedFileHeader header = VersionedFileHeader.Decode(embedded);

                                    if (header.FileHash != null)
                                    {
                                        goodPaths.Add(GetFilePath(header));
                                    }

                                    Process_VersionedFile(null, signed, header);
                                }
                            }
                        }
                    }
                }

                // remove loose files
                foreach (string testPath in Directory.GetFiles(CachePath))
                {
                    if (!goodPaths.Contains(testPath))
                    {
                        try { File.Delete(testPath); }
                        catch { }
                    }
                }
            }
            catch (Exception ex)
            {
                Core.Network.UpdateLog("VersionedFile", "Error loading data " + ex.Message);
            }
        }