diff --git a/Server/Common/AES.cs b/Server/Common/AES.cs index cac98c2..f6bd9cc 100644 --- a/Server/Common/AES.cs +++ b/Server/Common/AES.cs @@ -2,7 +2,6 @@ using System.Security.Cryptography; namespace Common; - /// /// 与目标服务器通信将会加密 /// @@ -55,7 +54,7 @@ public class AESHelper 0xB6, ]; - public static byte[] EncryptStringToBytes_Aes(string plainText) + public static byte[] EncryptStringToBytes_Aes(byte[] plainText) { // Check arguments. if (plainText == null || plainText.Length <= 0) @@ -74,11 +73,7 @@ public class AESHelper // Create the streams used for encryption. using MemoryStream msEncrypt = new(); - using CryptoStream csEncrypt = new( - msEncrypt, - encryptor, - CryptoStreamMode.Write - ); + using CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write); using (StreamWriter swEncrypt = new(csEncrypt)) { //Write all data to the stream. diff --git a/Server/Common/ConnectPipeline.cs b/Server/Common/ConnectPipeline.cs index 4e4e4d8..2d5315a 100644 --- a/Server/Common/ConnectPipeline.cs +++ b/Server/Common/ConnectPipeline.cs @@ -126,7 +126,7 @@ public class WebSocPipeLine(TSocket socket, bool isAES) : AbsPipeLine(i string msgStr = JsonSerializer.Serialize(msg); await Socket.SendAsync( IsAES - ? AESHelper.EncryptStringToBytes_Aes(msgStr) + ? AESHelper.EncryptStringToBytes_Aes(Encoding.UTF8.GetBytes(msgStr)) : new ArraySegment(Encoding.UTF8.GetBytes(msgStr)), WebSocketMessageType.Text, true, diff --git a/Server/ServerTest/TestPipe.cs b/Server/ServerTest/TestPipe.cs new file mode 100644 index 0000000..c520c78 --- /dev/null +++ b/Server/ServerTest/TestPipe.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Text; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using Common; + +namespace ServerTest +{ + public class TestPipe(bool isAES) : AbsPipeLine(isAES) + { + private readonly BlockingCollection> EventQueue = + new BlockingCollection>(); + private readonly CancellationTokenSource Cts = new CancellationTokenSource(); + public TestPipe? Other; + + public override async IAsyncEnumerable Work( + Func receiveCb, + string addr = "" + ) + { + yield return 0; + await Listen(receiveCb); + yield return 1; + } + + public override async Task UploadFile( + string filePath, + string dst, + Func progressCb + ) + { + await Task.Run(() => + { + progressCb(100); + System.IO.File.Copy(filePath, dst, true); + }); + } + + public override async Task Close(string? CloseReason) + { + await Task.Run(() => + { + Cts.Cancel(); + }); + } + + public new async Task ReceiveMsg(SyncMsg msg) + { + await Task.Run(() => + { + EventQueue.Add(() => + { + var r = JsonSerializer.SerializeToUtf8Bytes(msg); + if (IsAES) + { + return AESHelper.EncryptStringToBytes_Aes(r); + } + else + { + return r; + } + }); + }); + } + + public override async Task SendMsg(SyncMsg msg) + { + if (Other == null) + { + throw new Exception("can't be null"); + } + await Other.ReceiveMsg(msg); + } + + protected override async Task Listen(Func receiveCb) + { + while (!Cts.Token.IsCancellationRequested) + { + Func eventTask = EventQueue.Take(Cts.Token); + if (eventTask != null) + { + await Task.Run(() => + { + var r = eventTask(); + receiveCb(r); + }); + } + } + } + } +}