public static ShareCollection Decode(G2Header header, ulong user) { ShareCollection root = new ShareCollection(user); G2Header child = new G2Header(header.Data); while (G2Protocol.ReadNextChild(header, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) { continue; } switch (child.Name) { case Packet_Hash: root.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Size: root.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Key: root.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; } } return(root); }
private void ReceivePublicDetails(RudpSession session, ShareCollection file) { ShareCollection collection; if (!Collections.SafeTryGetValue(session.UserID, out collection)) { return; } collection.Key = file.Key; collection.Size = file.Size; collection.Hash = file.Hash; foreach (DhtClient done in collection.ToRequest.Where(t => t.ClientID == session.ClientID).ToArray()) { collection.ToRequest.Remove(done); } FileDetails details = new FileDetails(ServiceID, DataTypePublic, file.Hash, file.Size, null); object[] args = new object[] { collection, (object)session.ClientID }; DhtClient client = new DhtClient(session.UserID, session.ClientID); OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, GetPublicPath(collection), new EndDownloadHandler(CollectionDownloadFinished), args); transfer.AddPeer(client); transfer.DoSearch = false; collection.Status = "Starting List Download"; }
public void GetPublicList(ulong user) { if (Core.InvokeRequired) { Core.RunInCoreAsync(() => GetPublicList(user)); return; } ShareCollection collection; if (!Collections.SafeTryGetValue(user, out collection)) { collection = new ShareCollection(user); Collections.SafeAdd(user, collection); } AddTargets(collection.ToRequest, user, 0); foreach (DhtClient target in collection.ToRequest) { RudpSession session = Network.RudpControl.GetActiveSession(target); if (session == null) { Network.RudpControl.Connect(target); collection.Status = "Connecting to " + Core.GetName(target.UserID); } else { SendPublicRequest(session, collection); } } }
public void CollectionDownloadFinished(object[] args) { ShareCollection collection = args[0] as ShareCollection; ushort client = (ushort)args[1]; collection.Files.LockWriting(() => { foreach (SharedFile file in collection.Files.Where(c => c.ClientID == client).ToArray()) { collection.Files.Remove(file); } }); try { string finalpath = GetPublicPath(collection); using (TaggedStream tagged = new TaggedStream(finalpath, Network.Protocol)) using (IVCryptoStream crypto = IVCryptoStream.Load(tagged, collection.Key)) { PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read); G2Header root = null; collection.Files.LockWriting(() => { while (stream.ReadPacket(ref root)) { if (root.Name == SharePacket.File) { SharedFile file = SharedFile.Decode(root, client); // dont add dupes from diff client ids if (collection.Files.Any(f => f.Size == file.Size && Utilities.MemCompare(f.Hash, file.Hash))) { continue; } file.FileID = OpTransfer.GetFileID(ServiceID, file.Hash, file.Size); collection.Files.SafeAdd(file); } } }); } } catch (Exception ex) { Core.Network.UpdateLog("Mail", "Error loading local mail " + ex.Message); } collection.Status = collection.Files.SafeCount + " Files Shared"; Core.RunInGuiThread(GuiCollectionUpdate, collection.UserID); }
void Session_Data(RudpSession session, byte[] data) { G2Header root = new G2Header(data); if (G2Protocol.ReadPacket(root)) { switch (root.Name) { case SharePacket.PublicRequest: ReceivePublicRequest(session); break; case SharePacket.Collection: ReceivePublicDetails(session, ShareCollection.Decode(root, session.UserID)); break; } } }
public ShareService(OpCore core) { Core = core; Network = core.Network; string rootPath = Core.User.RootPath + Path.DirectorySeparatorChar + "Data" + Path.DirectorySeparatorChar + ServiceID.ToString() + Path.DirectorySeparatorChar; SharePath = rootPath + DataTypeShare.ToString() + Path.DirectorySeparatorChar; Directory.CreateDirectory(SharePath); HeaderPath = SharePath + Utilities.CryptFilename(Core, "ShareHeaders"); PublicPath = rootPath + DataTypePublic.ToString() + Path.DirectorySeparatorChar; Directory.CreateDirectory(PublicPath); DownloadPath = Core.User.RootPath + Path.DirectorySeparatorChar + "Downloads" + Path.DirectorySeparatorChar; Core.SecondTimerEvent += Core_SecondTimer; // data Network.RudpControl.SessionUpdate += new SessionUpdateHandler(Session_Update); Network.RudpControl.SessionData[ServiceID, DataTypeSession] += new SessionDataHandler(Session_Data); Core.Transfers.FileSearch[ServiceID, DataTypeShare] += new FileSearchHandler(Transfers_FileSearch); Core.Transfers.FileRequest[ServiceID, DataTypeShare] += new FileRequestHandler(Transfers_FileRequest); Core.Transfers.FileSearch[ServiceID, DataTypePublic] += new FileSearchHandler(Transfers_PublicSearch); Core.Transfers.FileRequest[ServiceID, DataTypePublic] += new FileRequestHandler(Transfers_PublicRequest); // location TempLocation = new TempCache(Network, ServiceID, DataTypeLocation); Local = new ShareCollection(Core.UserID); Collections.SafeAdd(Core.UserID, Local); LoadHeaders(); }
private void SendPublicRequest(RudpSession session, ShareCollection collection) { collection.Status = "Requesting List"; session.SendData(ServiceID, DataTypeSession, new PublicShareRequest()); }
private string GetPublicPath(ShareCollection collection) { return(PublicPath + Utilities.CryptFilename(Core, collection.UserID, collection.Hash)); }
private void LoadHeaders() { List <string> goodPaths = new List <string>(); // load shared file lists try { goodPaths.Add(HeaderPath); if (!File.Exists(HeaderPath)) { return; } using (IVCryptoStream crypto = IVCryptoStream.Load(HeaderPath, Core.User.Settings.FileKey)) { PacketStream stream = new PacketStream(crypto, Network.Protocol, FileAccess.Read); G2Header root = null; while (stream.ReadPacket(ref root)) { if (root.Name == SharePacket.Collection) { ShareCollection copy = ShareCollection.Decode(root, Core.UserID); //crit - ensure public path exists before loading up Local.Hash = copy.Hash; Local.Size = copy.Size; Local.Key = copy.Key; } else if (root.Name == SharePacket.File) { SharedFile file = SharedFile.Decode(root, Core.Network.Local.ClientID); if (file.SystemPath != null && !File.Exists(GetFilePath(file))) { file.SystemPath = null; } file.FileID = OpTransfer.GetFileID(ServiceID, file.Hash, file.Size); if (File.Exists(GetFilePath(file))) { file.Completed = true; file.Sources.Add(Core.Network.Local); } // incomplete, ensure partial file is saved into next run if need be else { foreach (OpTransfer partial in Core.Transfers.Partials.Where(p => p.FileID == file.FileID)) { partial.SavePartial = true; } } file.FileStatus = file.Completed ? "Secured" : "Incomplete"; Local.Files.SafeAdd(file); // unhashed files aren't saved anymore /* if app previous closed without hashing share, hash now * if (share.Hash == null && share.SystemPath != null && * File.Exists(share.SystemPath)) * ProcessFileShare(share);*/ } } } // clears most of files in direcotry, others shared public lists are not persisted between runs foreach (string testPath in Directory.GetFiles(SharePath)) { if (!goodPaths.Contains(testPath)) { try { File.Delete(testPath); } catch { } } } } catch (Exception ex) { Network.UpdateLog("VersionedFile", "Error loading data " + ex.Message); } }
private string GetPublicPath(ShareCollection collection) { return PublicPath + Utilities.CryptFilename(Core, collection.UserID, collection.Hash); }
public void GetPublicList(ulong user) { if (Core.InvokeRequired) { Core.RunInCoreAsync(() => GetPublicList(user)); return; } ShareCollection collection; if (!Collections.SafeTryGetValue(user, out collection)) { collection = new ShareCollection(user); Collections.SafeAdd(user, collection); } AddTargets(collection.ToRequest, user, 0); foreach (DhtClient target in collection.ToRequest) { RudpSession session = Network.RudpControl.GetActiveSession(target); if (session == null) { Network.RudpControl.Connect(target); collection.Status = "Connecting to " + Core.GetName(target.UserID); } else SendPublicRequest(session, collection); } }
public static ShareCollection Decode(G2Header header, ulong user) { ShareCollection root = new ShareCollection(user); G2Header child = new G2Header(header.Data); while (G2Protocol.ReadNextChild(header, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) continue; switch (child.Name) { case Packet_Hash: root.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Size: root.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Key: root.Key = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; } } return root; }
private void ReceivePublicDetails(RudpSession session, ShareCollection file) { ShareCollection collection; if (!Collections.SafeTryGetValue(session.UserID, out collection)) return; collection.Key = file.Key; collection.Size = file.Size; collection.Hash = file.Hash; foreach (DhtClient done in collection.ToRequest.Where(t => t.ClientID == session.ClientID).ToArray()) collection.ToRequest.Remove(done); FileDetails details = new FileDetails(ServiceID, DataTypePublic, file.Hash, file.Size, null); object[] args = new object[] { collection, (object) session.ClientID }; DhtClient client = new DhtClient(session.UserID, session.ClientID); OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, GetPublicPath(collection), new EndDownloadHandler(CollectionDownloadFinished), args); transfer.AddPeer(client); transfer.DoSearch = false; collection.Status = "Starting List Download"; }