internal void ResearchFile(SharedFile file) { if (Core.InvokeRequired) { Core.RunInCoreAsync(() => ResearchFile(file)); return; } DhtSearch search = Core.Network.Searches.Start(file.FileID, "Share Search: " + file.Name, ServiceID, DataTypeLocation, null, new EndSearchHandler(EndLocationSearch)); search.Carry = file; }
internal void SendFile(string path, ulong user, ushort client) { if (Core.InvokeRequired) { Core.RunInCoreAsync(() => SendFile(path, user, client)); return; } // add to share list SharedFile file = new SharedFile(Core.Network.Local.ClientID); file.Name = Path.GetFileName(path); file.SystemPath = path; file.Completed = true; file.Sources.Add(Core.Network.Local); file.FileStatus = "Processing..."; AddTargets(file.ToRequest, user, client); // so user can see hash progress Local.Files.SafeAdd(file); Core.RunInGuiThread(GuiFileUpdate, file); ProcessFileShare(file); }
internal void OpenFile(ulong user, SharedFile file) { if (!File.Exists(GetFilePath(file))) return; // check if already exists, if it does open if (file.SystemPath != null && File.Exists(file.SystemPath)) { Process.Start(file.SystemPath); return; } lock (OpenQueue) { if (OpenQueue.Contains(file)) return; OpenQueue.AddLast(file); } // hashing if (OpenFilesHandle == null || !OpenFilesHandle.IsAlive) { OpenFilesHandle = new Thread(OpenFiles); OpenFilesHandle.Start(); } }
internal void RemoveFile(SharedFile file) { if (file.Hash != null) { // stop any current transfer of file Core.Transfers.CancelDownload(ServiceID, file.Hash, file.Size); try { File.Delete(GetFilePath(file)); } catch { } } Local.Files.SafeRemove(file); lock (ProcessingQueue) if (ProcessingQueue.Contains(file)) ProcessingQueue.Remove(file); lock(OpenQueue) if (OpenQueue.Contains(file)) OpenQueue.Remove(file); Core.RunInGuiThread(GuiFileUpdate, file); }
internal string GetFileLink(ulong user, SharedFile file) { // riseop://op/file/filename/opid~size~hash~key/targetlist string link = "riseop://" + HttpUtility.UrlEncode(Core.User.Settings.Operation) + "/file/" + HttpUtility.UrlEncode(file.Name) + "/"; byte[] endtag = Core.User.Settings.InviteKey; // 8 endtag = Utilities.CombineArrays(endtag, BitConverter.GetBytes(file.Size)); // 8 endtag = Utilities.CombineArrays(endtag, file.Hash); // 20 endtag = Utilities.CombineArrays(endtag, file.FileKey); // 32 link += Utilities.ToBase64String(endtag) + "/"; byte[] sources = null; // if local shared file, get the sources we know of if (user == Core.UserID && file.ClientID == Core.Network.Local.ClientID) { foreach (DhtClient client in file.Sources) sources = (sources == null) ? client.ToBytes() : Utilities.CombineArrays(sources, client.ToBytes()); } // else getting link from remote share, so add it's address as a location else sources = new DhtClient(user, file.ClientID).ToBytes(); if(sources != null) link += Utilities.ToBase64String(sources); return link; }
internal string GetFilePath(SharedFile file) { if (file.Hash == null) return ""; return SharePath + Utilities.CryptFilename(Core, Core.UserID, file.Hash); }
internal void DownloadFile(ulong user, SharedFile file) { // donwloading from a different user, make a copy of their share, and activate download // copies are checked for if (File.Exists(GetFilePath(file))) return; SharedFile localCopy = new SharedFile(file, Core.Network.Local.ClientID); localCopy.Sources.Add(new DhtClient(user, file.ClientID)); DownloadFile(localCopy); }
private void ReceiveFileRequest(RudpSession session, SharedFile file) { DhtClient client = new DhtClient(session.UserID, session.ClientID); // check if file hash already on sharelist, if it is ignore bool alertUser = true; Local.Files.LockReading(() => { SharedFile existing = Local.Files.Where(s => Utilities.MemCompare(s.Hash, file.Hash)).FirstOrDefault(); // transfer exists, but this is from another source, or we started up and someone is trying // to resend file to us, which this auto adds the new source if (existing != null) { if (!existing.Ignore) StartTransfer(client, existing); alertUser = false; } }); if (!alertUser) return; file.Ignore = true; // turned off once accepted, allowing this item to be saved to header file.FileID = OpTransfer.GetFileID(ServiceID, file.Hash, file.Size); Local.Files.SafeAdd(file); Core.RunInGuiThread(GuiFileUpdate, file); file.TransferStatus = "Request received from " + Core.GetName(session.UserID); Core.RunInGuiThread((System.Windows.Forms.MethodInvoker) delegate() { new AcceptFileForm(Core, client, file).ShowDialog(); }); }
internal static SharedFile Decode(G2Header header, ushort client) { SharedFile root = new SharedFile(client); G2Header child = new G2Header(header.Data); while (G2Protocol.ReadNextChild(header, child) == G2ReadResult.PACKET_GOOD) { if (child.Name == Packet_Public) root.Public = true; if (!G2Protocol.ReadPayload(child)) continue; switch (child.Name) { case Packet_Name: root.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; 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_FileKey: root.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_SystemPath: root.SystemPath = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; } } return root; }
internal void AcceptRequest(DhtClient client, SharedFile file) { if (Core.InvokeRequired) { Core.RunInCoreAsync(() => AcceptRequest(client, file)); return; } file.Ignore = false; StartTransfer(client, file); ResearchFile(file); SaveHeaders(); }
internal SharedFile(SharedFile copy, ushort client) { ClientID = client; Name = copy.Name; Hash = copy.Hash; Size = copy.Size; FileKey = copy.FileKey; FileID = OpTransfer.GetFileID((uint)ServiceIDs.Sharing, Hash, Size); }
private OpTransfer StartTransfer(DhtClient client, SharedFile file) { FileDetails details = new FileDetails(ServiceID, DataTypeShare, file.Hash, file.Size, null); object[] args = new object[] { file }; OpTransfer transfer = Core.Transfers.StartDownload(client.UserID, details, args, new EndDownloadHandler(FileDownloadFinished)); transfer.AddPeer(client); file.TransferStatus = "Starting download from " + Core.GetName(client.UserID); return transfer; }
private void SendFileRequest(RudpSession session, SharedFile file) { foreach (DhtClient taraget in file.ToRequest.Where(c => c.UserID == session.UserID && c.ClientID == session.ClientID).ToArray()) file.ToRequest.Remove(taraget); file.SaveLocal = false; session.SendData(ServiceID, DataTypeSession, file, true); file.TransferStatus = "Request sent to " + Core.GetName(session.UserID); }
internal void TrySendRequest(SharedFile file, DhtClient target) { RudpSession session = Network.RudpControl.GetActiveSession(target); if (session == null) { Network.RudpControl.Connect(target); file.TransferStatus = "Connecting to " + Core.GetName(target.UserID); } else SendFileRequest(session, file); }
internal void DownloadLink(string link) { if(!link.StartsWith("riseop://")) throw new Exception("Invalid Link"); string[] parts = link.Substring(9).Split('/'); if (parts.Length < 4) return; if (parts[1] != "file") throw new Exception("Invalid Link"); SharedFile file = new SharedFile(Core.Network.Local.ClientID); file.Name = HttpUtility.UrlDecode(parts[2]); byte[] endtag = Utilities.FromBase64String(parts[3]); if(endtag.Length < (8 + 8 + 20 + 32)) throw new Exception("Invalid Link"); byte[] inviteKey = Utilities.ExtractBytes(endtag, 0, 8); if(!Utilities.MemCompare(inviteKey, Core.User.Settings.InviteKey)) throw new Exception("File Link is not for this Op"); file.Size = BitConverter.ToInt64(Utilities.ExtractBytes(endtag, 8, 8), 0); file.Hash = Utilities.ExtractBytes(endtag, 8 + 8, 20); file.FileKey = Utilities.ExtractBytes(endtag, 8 + 8 + 20, 32); file.FileID = OpTransfer.GetFileID(ServiceID, file.Hash, file.Size); if (parts.Length >= 5) { byte[] sources = Utilities.FromBase64String(parts[4]); for (int i = 0; i < sources.Length; i += 10) file.Sources.Add(DhtClient.FromBytes(sources, i)); } DownloadFile(file); }
void DownloadFile(SharedFile file) { SharedFile existing = null; Local.Files.LockReading(() => existing = Local.Files.Where(s => Utilities.MemCompare(s.Hash, file.Hash)).FirstOrDefault()); // just add new targets if we already have this file if (existing != null) { existing.Sources.AddRange(file.Sources); file = existing; } else Local.Files.SafeAdd(file); // if downloading form another client of self if (file.ClientID != Core.Network.Local.ClientID) { file.ClientID = Core.Network.Local.ClientID; Core.RunInGuiThread(GuiCollectionUpdate, Core.UserID); } Core.RunInCoreAsync(() => { SaveHeaders(); if (file.Sources.Count > 0) StartTransfer(file.Sources[0], file); }); ResearchFile(file); file.FileStatus = "Incomplete"; Core.RunInGuiThread(GuiFileUpdate, file); }
private void ProcessFileShare(SharedFile file) { // enqueue file for processing lock (ProcessingQueue) ProcessingQueue.AddLast(file); // hashing if (ProcessFilesHandle == null || !ProcessFilesHandle.IsAlive) { ProcessFilesHandle = new Thread(ProcessFiles); ProcessFilesHandle.Start(); } }