| 1 | using System.Text.Json.Nodes; |
| 2 | |
| 3 | namespace Cage.GuestClipboard; |
| 4 | |
| 5 | |
| 6 | |
| 7 | internal static class BrowserPasteInput |
| 8 | { |
| 9 | private sealed record Pending(string Tab,string Target,string Focus,uint Revision,int Length,DateTime Created,MemoryStream Bytes) |
| 10 | { internal int Sequence; internal string Mime = ""; internal readonly long Epoch = ClipboardAgent.Current?.SessionEpoch ?? 0; } |
| 11 | private static readonly Dictionary<string,Pending> Transfers = new(StringComparer.Ordinal); |
| 12 | internal static async Task<JsonObject> Start(JsonObject input,JsonObject proof,JsonObject focus) |
| 13 | { |
| 14 | Expire(); |
| 15 | var tab=input["id"]?.GetValue<string>()??"";var length=input["length"]?.GetValue<int>()??0; |
| 16 | var mime=input["mime"]?.GetValue<string>()??""; |
| 17 | if(Transfers.Count>=4||tab!=proof["id"]?.GetValue<string>()||length<0||length>(mime=="text/plain"?Protocol.TextLimit:Protocol.PngLimit)||mime is not ("" or "text/plain" or "image/png"))throw new InvalidDataException("image_target_invalid"); |
| 18 | var clipboard=ClipboardAgent.Current??throw new InvalidDataException("clipboard_unavailable"); |
| 19 | var revision=await clipboard.Revision(); |
| 20 | var id=Guid.NewGuid().ToString("N"); |
| 21 | Transfers[id]=new Pending(tab,proof["target"]!.GetValue<string>(),focus["focus"]!.GetValue<string>(),revision,length,DateTime.UtcNow,new MemoryStream(Math.Min(length,64<<10))){Mime=mime}; |
| 22 | return new JsonObject{["transfer"]=id}; |
| 23 | } |
| 24 | internal static JsonObject Chunk(JsonObject input) |
| 25 | { |
| 26 | var pending=Get(input);var text=input["data"]?.GetValue<string>()??""; |
| 27 | if(pending.Sequence==0&&pending.Mime=="")pending.Mime=input["mime"]?.GetValue<string>()??""; |
| 28 | if(pending.Mime is not ("text/plain" or "image/png")||input["mime"] is not null&&input["mime"]!.GetValue<string>()!=pending.Mime)throw new InvalidDataException("image_chunk_invalid"); |
| 29 | var limit=pending.Mime=="text/plain"?Protocol.TextLimit:Protocol.PngLimit; |
| 30 | if(pending.Length==0&&pending.Sequence==0) |
| 31 | { |
| 32 | var length=input["length"]?.GetValue<int>()??0; |
| 33 | if(length<1||length>limit)throw new InvalidDataException("image_chunk_invalid"); |
| 34 | pending=pending with {Length=length};Transfers[input["transfer"]!.GetValue<string>()]=pending; |
| 35 | } |
| 36 | if(pending.Length>limit)throw new InvalidDataException("image_chunk_invalid"); |
| 37 | if(text.Length>87384||text.Any(char.IsWhiteSpace))throw new InvalidDataException("image_chunk_invalid"); |
| 38 | var bytes=Convert.FromBase64String(text); |
| 39 | if(bytes.Length is <1 or >65536||pending.Bytes.Length+bytes.Length>pending.Length)throw new InvalidDataException("image_chunk_invalid"); |
| 40 | pending.Bytes.Write(bytes);pending.Sequence++; |
| 41 | return new JsonObject{["ack"]=pending.Sequence}; |
| 42 | } |
| 43 | internal static async Task<JsonObject> Finish(JsonObject input,JsonObject proof,JsonObject focus,Func<Task<JsonObject>> paste) |
| 44 | { |
| 45 | var pending=Get(input);Transfers.Remove(input["transfer"]!.GetValue<string>()); |
| 46 | using(pending.Bytes) |
| 47 | { |
| 48 | if(pending.Bytes.Length!=pending.Length||pending.Tab!=proof["id"]?.GetValue<string>()||pending.Target!=proof["target"]?.GetValue<string>()||pending.Focus!=focus["focus"]?.GetValue<string>())throw new InvalidDataException("image_target_changed"); |
| 49 | var clipboard=ClipboardAgent.Current??throw new InvalidDataException("clipboard_unavailable"); |
| 50 | var bytes=pending.Bytes.ToArray(); |
| 51 | JsonObject receipt; |
| 52 | if(pending.Mime=="text/plain")receipt=await clipboard.WriteBrowserText(pending.Revision,Protocol.Utf8.GetString(bytes)); |
| 53 | else {Protocol.Png(bytes);receipt=await clipboard.WriteBrowserPng(pending.Revision,bytes);} |
| 54 | await clipboard.PreparePaste(); |
| 55 | var result=await paste(); |
| 56 | if(result["error"] is not null)throw new InvalidDataException("image_paste_failed"); |
| 57 | result["clipboard"]=receipt;return result; |
| 58 | } |
| 59 | } |
| 60 | internal static void Cancel(string id){if(Transfers.Remove(id,out var pending))pending.Bytes.Dispose();} |
| 61 | private static Pending Get(JsonObject input) |
| 62 | { |
| 63 | Expire();var id=input["transfer"]?.GetValue<string>()??""; |
| 64 | if(!Transfers.TryGetValue(id,out var pending)||input["sequence"]?.GetValue<int>()!=pending.Sequence||pending.Epoch!=ClipboardAgent.Current?.SessionEpoch)throw new InvalidDataException("image_transfer_changed");return pending; |
| 65 | } |
| 66 | private static void Expire(){foreach(var item in Transfers.ToArray())if(DateTime.UtcNow-item.Value.Created>TimeSpan.FromMinutes(2))Cancel(item.Key);} |
| 67 | internal static void Close(){foreach(var item in Transfers.Values)item.Bytes.Dispose();Transfers.Clear();} |
| 68 | } |