From 37f7bcf2c31eb5ec5ed187d43f57333b4702605d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E9=A1=BA=E6=98=8E?= Date: Fri, 15 Sep 2023 07:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E8=BF=9B=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mime/HttpMime.cs | 6 +- Mime/HttpMimeSocket.cs | 136 +------------------------ Mime/HttpMimeStream.cs | 218 ---------------------------------------- Mime/HttpWebSocket.cs | 97 +++++++++++++++++- Mime/HttpsMimeSocket.cs | 3 +- Mime/HttpsWebSocket.cs | 64 ++++++++++++ Program.cs | 42 ++++++-- Resources/dingtalk.html | 3 +- 8 files changed, 196 insertions(+), 373 deletions(-) delete mode 100644 Mime/HttpMimeStream.cs create mode 100644 Mime/HttpsWebSocket.cs diff --git a/Mime/HttpMime.cs b/Mime/HttpMime.cs index 2f98b4d..b7d5577 100644 --- a/Mime/HttpMime.cs +++ b/Mime/HttpMime.cs @@ -29,17 +29,17 @@ namespace UMC.Host protected abstract void WebSocket(UMC.Net.NetContext context); - public virtual void PrepareRespone(HttpMimeRequest httpMimeRequest) + public virtual void PrepareRespone(HttpMimeRequest request) { this.TimeOut = 300; - var context = new HttpMimeContext(httpMimeRequest, new HttpMimeResponse(this, httpMimeRequest)); + var context = new HttpMimeContext(request, new HttpMimeResponse(this, request)); try { context.ProcessRequest(); - if (httpMimeRequest.IsWebSocket) + if (request.IsWebSocket) { this.WebSocket(context); } diff --git a/Mime/HttpMimeSocket.cs b/Mime/HttpMimeSocket.cs index f1767ec..3bf258d 100644 --- a/Mime/HttpMimeSocket.cs +++ b/Mime/HttpMimeSocket.cs @@ -10,141 +10,7 @@ using UMC.Net; namespace UMC.Host { - abstract class WebSocket : UMC.Net.MimeRequest, IDisposable - { - public override bool IsWebSocket => true; - public abstract void Dispose(); - } - - class HttpsWebSocket : WebSocket - { - public byte[] buffer = System.Buffers.ArrayPool.Shared.Rent(0x600); - System.IO.Stream stream; - public HttpsWebSocket(HttpMime mime, System.IO.Stream stream) - { - mime1 = mime; - this.stream = stream; - WebSocketRead(); - } - public override void Receive(byte[] buffer, int offset, int size) - { - stream.Write(buffer, offset, size); - - } - HttpMime mime1; - async void WebSocketRead() - { - int size = 0; - try - { - while ((size = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) - { - mime1.Write(buffer, 0, size); - } - } - catch - { - this.Dispose(); - return; - } - this.Dispose(); - } - - - public override void Dispose() - { - mime1.Dispose(); - stream.Close(); - stream.Dispose(); - if (buffer != null) - { - System.Buffers.ArrayPool.Shared.Return(buffer); - buffer = null; - } - } - - protected override void Header(byte[] data, int offset, int size) - { - - } - } - class HttpWebSocket : WebSocket - { - - public byte[] buffer = System.Buffers.ArrayPool.Shared.Rent(0x600); - Socket socket; - public HttpWebSocket(HttpMime mime, Socket stream) - { - this.mime = mime; - this.socket = stream; - SocketAsyncEventArgs eventArgs = new SocketAsyncEventArgs(); - eventArgs.SetBuffer(buffer); - eventArgs.Completed += Http; - if (!this.socket.ReceiveAsync(eventArgs)) - { - ProcessReceive(eventArgs); - } - } - HttpMime mime; - - private void Http(object sender, SocketAsyncEventArgs e) - { - switch (e.LastOperation) - { - case SocketAsyncOperation.Receive: - ProcessReceive(e); - break; - } - } - private void ProcessReceive(SocketAsyncEventArgs e) - { - if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) - { - try - { - this.mime.Write(buffer, e.Offset, e.BytesTransferred); - if (!socket.ReceiveAsync(e)) - { - ProcessReceive(e); - } - } - catch - { - this.Dispose(); - } - } - else - { - this.Dispose(); - - } - } - - public override void Receive(byte[] buffer, int offset, int size) - { - socket.Send(buffer, offset, size, SocketFlags.None); - } - protected override void Header(byte[] data, int offset, int size) - { - - } - public override void Dispose() - { - this.Dispose(); - try - { - socket.Shutdown(SocketShutdown.Both); - } - catch - { - - } - socket.Close(); - System.Buffers.ArrayPool.Shared.Return(buffer); - buffer = null; - } - } - + public class HttpMimeSocket : HttpMime { public override string Scheme => "http"; diff --git a/Mime/HttpMimeStream.cs b/Mime/HttpMimeStream.cs deleted file mode 100644 index 97ab37e..0000000 --- a/Mime/HttpMimeStream.cs +++ /dev/null @@ -1,218 +0,0 @@ -using System; -using System.Buffers; -using System.IO; -using System.Net; -using System.Net.Security; -using System.Net.Sockets; -using System.Security.Authentication; -using System.Security.Cryptography.X509Certificates; -using System.Threading; -using System.Threading.Tasks; -using System.Threading.Tasks.Sources; -using UMC.Net; - -namespace UMC.Host -{ - - class HttpMimeStream : System.IO.Stream, IValueTaskSource - { - private ManualResetValueTaskSourceCore _source = new ManualResetValueTaskSourceCore(); - - #region 实现接口,告诉调用者,任务是否已经完成,以及是否有结果,是否有异常等 - // 获取结果 - public int GetResult(short token) - { - return _source.GetResult(token); - } - - public ValueTaskSourceStatus GetStatus(short token) - { - return _source.GetStatus(token); ; - } - - // 实现延续 - public void OnCompleted(Action continuation, object state, short token, ValueTaskSourceOnCompletedFlags flags) - { - _source.OnCompleted(continuation, state, token, flags); - } - - #endregion - - - // 以及完成任务,并给出结果 - public void SetResult(int result) - { - _source.SetResult(result); - } - short _token = 0; - // 要执行的任务出现异常 - public void SetException(Exception exception) - { - _source.SetException(exception); - } - - - private int _disposed; - - - public override bool CanRead => true; - - public override bool CanSeek => false; - - public override bool CanWrite => true; - - public override long Length - { - get - { - throw new NotSupportedException(); - } - } - - public override long Position - { - get - { - throw new NotSupportedException(); - } - set - { - throw new NotSupportedException(); - } - } - HttpMime _mime; - public HttpMimeStream(HttpMime mime) - { - this._mime = mime; - } - - - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException(); - } - - public override int Read(byte[] buffer, int offset, int count) - { - return this.ReadAsync(buffer, offset, count).Result; - } - public void AppendData(byte[] buffer, int offset, int size) - { - if (_buffers.IsEmpty == false) - { - if (_buffers.Length >= size) - { - buffer.AsMemory(offset, size).CopyTo(_buffer); - _source.SetResult(size); - } - else - { - buffer.AsMemory(offset, _buffers.Length).CopyTo(_buffer); - int len = size - _buffers.Length; - if (len + _bufferSize > _buffer.Length) - { - var _bs = new byte[len + _bufferSize + 200]; - Array.Copy(_buffer, 0, _bs, 0, _bufferSize); - _buffer = _bs; - } - Array.Copy(buffer, offset + _buffer.Length, _buffer, _bufferSize, len); - - - _source.SetResult(_buffers.Length); - - } - } - } - - - public override void Write(byte[] buffer, int offset, int count) - { - _mime.Write(buffer, offset, count); - } - protected override void Dispose(bool disposing) - { - if (Interlocked.Exchange(ref _disposed, 1) != 0) - { - return; - } - base.Dispose(disposing); - } - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - { - Stream.ValidateBufferArguments(buffer, offset, count); - return ReadAsync(new Memory(buffer, offset, count), cancellationToken).AsTask(); - } - byte[] _buffer = Array.Empty(); - int _bufferSize = 0; - Memory _buffers; - //int _start = 0; - public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken) - { - if (_bufferSize == 0) - { - _token++; - _buffers = buffer; - _source.Reset(); - return new ValueTask(this, _token); - } - if (buffer.Length >= _bufferSize) - { - int len = _bufferSize; - _buffer.AsMemory(0, _bufferSize).CopyTo(buffer); - _bufferSize = 0; - - return new ValueTask(len); - } - else - { - _buffer.AsMemory(0, buffer.Length).CopyTo(buffer); - _bufferSize -= buffer.Length; - Array.Copy(_buffer, buffer.Length, _buffer, 0, _bufferSize); - - return new ValueTask(buffer.Length); - - - } - } - - public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - { - Stream.ValidateBufferArguments(buffer, offset, count); - _mime.Write(buffer, offset, count); - return Task.CompletedTask; - //NetworkStream - } - - public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken) - { - byte[] array = ArrayPool.Shared.Rent(buffer.Length); - try - { - buffer.CopyTo(array); - _mime.Write(array, 0, buffer.Length); - return ValueTask.CompletedTask; - } - finally - { - ArrayPool.Shared.Return(array); - } - - } - - public override void Flush() - { - } - - public override Task FlushAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - - public override void SetLength(long value) - { - throw new NotSupportedException(); - } - - } -} - diff --git a/Mime/HttpWebSocket.cs b/Mime/HttpWebSocket.cs index 61a2a52..ccac71a 100644 --- a/Mime/HttpWebSocket.cs +++ b/Mime/HttpWebSocket.cs @@ -1,14 +1,25 @@ -using System.Threading.Tasks; using System; +using System.Buffers; using System.IO; +using System.Net; +using System.Net.Security; +using System.Net.Sockets; +using System.Security.Authentication; +using System.Security.Cryptography.X509Certificates; using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Threading.Tasks.Sources; using UMC.Net; -using System.Net.WebSockets; namespace UMC.Host { - class HttpWebSocket2 + abstract class WebSocket : UMC.Net.MimeRequest, IDisposable { + public override bool IsWebSocket => true; + public abstract void Dispose(); + + internal static string GetSecWebSocketAcceptString(string secWebSocketKey) { @@ -79,7 +90,7 @@ namespace UMC.Host writer.Write($"Sec-WebSocket-Accept: {secWebSocketAcceptString}\r\n"); writer.Write($"Sec-WebSocket-Protocol: mqtt\r\n"); - writer.Write("Server: UMC.Proxy\r\n\r\n"); + writer.Write("Server: Apiumc\r\n\r\n"); writer.Flush(); writer.Dispose(); @@ -97,5 +108,83 @@ namespace UMC.Host } + + class HttpWebSocket : WebSocket + { + + public byte[] buffer = System.Buffers.ArrayPool.Shared.Rent(0x600); + Socket socket; + public HttpWebSocket(HttpMime mime, Socket stream) + { + this.mime = mime; + this.socket = stream; + SocketAsyncEventArgs eventArgs = new SocketAsyncEventArgs(); + eventArgs.SetBuffer(buffer); + eventArgs.Completed += Http; + if (!this.socket.ReceiveAsync(eventArgs)) + { + ProcessReceive(eventArgs); + } + } + HttpMime mime; + + private void Http(object sender, SocketAsyncEventArgs e) + { + switch (e.LastOperation) + { + case SocketAsyncOperation.Receive: + ProcessReceive(e); + break; + } + } + private void ProcessReceive(SocketAsyncEventArgs e) + { + if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) + { + try + { + this.mime.Write(buffer, e.Offset, e.BytesTransferred); + if (!socket.ReceiveAsync(e)) + { + ProcessReceive(e); + } + } + catch + { + this.Dispose(); + } + } + else + { + this.Dispose(); + + } + } + + public override void Receive(byte[] buffer, int offset, int size) + { + socket.Send(buffer, offset, size, SocketFlags.None); + } + protected override void Header(byte[] data, int offset, int size) + { + + } + public override void Dispose() + { + this.Dispose(); + try + { + socket.Shutdown(SocketShutdown.Both); + } + catch + { + + } + socket.Close(); + System.Buffers.ArrayPool.Shared.Return(buffer); + buffer = null; + } + } + } diff --git a/Mime/HttpsMimeSocket.cs b/Mime/HttpsMimeSocket.cs index 7b68d30..679e532 100644 --- a/Mime/HttpsMimeSocket.cs +++ b/Mime/HttpsMimeSocket.cs @@ -1,6 +1,5 @@ -using System.Net.WebSockets; -namespace UMC.Host; +namespace UMC.Host; using System; using System.Collections.Specialized; using System.IO; diff --git a/Mime/HttpsWebSocket.cs b/Mime/HttpsWebSocket.cs new file mode 100644 index 0000000..d3df7a4 --- /dev/null +++ b/Mime/HttpsWebSocket.cs @@ -0,0 +1,64 @@ +using System.Threading.Tasks; +using System; +using System.IO; +using System.Text; +using UMC.Net; +using System.Net.WebSockets; + +namespace UMC.Host +{ + + class HttpsWebSocket : WebSocket + { + public byte[] buffer = System.Buffers.ArrayPool.Shared.Rent(0x600); + System.IO.Stream stream; + public HttpsWebSocket(HttpMime mime, System.IO.Stream stream) + { + mime1 = mime; + this.stream = stream; + WebSocketRead(); + } + public override void Receive(byte[] buffer, int offset, int size) + { + stream.Write(buffer, offset, size); + + } + HttpMime mime1; + async void WebSocketRead() + { + int size = 0; + try + { + while ((size = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0) + { + mime1.Write(buffer, 0, size); + } + } + catch + { + this.Dispose(); + return; + } + this.Dispose(); + } + + + public override void Dispose() + { + mime1.Dispose(); + stream.Close(); + stream.Dispose(); + if (buffer != null) + { + System.Buffers.ArrayPool.Shared.Return(buffer); + buffer = null; + } + } + + protected override void Header(byte[] data, int offset, int size) + { + + } + } + } + diff --git a/Program.cs b/Program.cs index a7b9033..6a6748b 100644 --- a/Program.cs +++ b/Program.cs @@ -21,9 +21,6 @@ namespace UMC.Host public static void Main(string[] args) { - - // var ipHost = Dns.GetHostAddresses("blj.sau.edu.cn"); - AppDomain.CurrentDomain.UnhandledException += UnhandledException; var variable = System.Environment.GetEnvironmentVariable("UMC"); @@ -112,7 +109,7 @@ namespace UMC.Host public static bool IsRun() { - using (var pipeClient = new NamedPipeClientStream(".", $"APIUMC", PipeDirection.InOut)) + using (var pipeClient = new NamedPipeClientStream(".", "APIUMC", PipeDirection.InOut)) { try { @@ -257,6 +254,34 @@ namespace UMC.Host Console.WriteLine("Apiumc网关服务未开启,请开启。"); } break; + case "restart": + if (IsRun()) + { + Write(new string[] { "stop" }); + + Thread.Sleep(5000); + Excel("main"); + } + else + { + Excel("main"); + + } + if (OperatingSystem.IsWindows()) + { + Console.WriteLine(); + Console.Write("特别注意:"); + Console.ForegroundColor = ConsoleColor.DarkRed; + Console.WriteLine("需要退出程序时,请用[exit]指令退出当前程序"); + Console.WriteLine(); + Console.ResetColor(); + } + else + { + IsDo = false; + } + Write("info"); + break; case "start": if (IsRun() == false) { @@ -399,7 +424,6 @@ namespace UMC.Host size += l; if (b.Length == 0) { - Console.WriteLine("下载完成"); Console.WriteLine(); stream.Flush(); @@ -417,8 +441,9 @@ namespace UMC.Host Utility.Move(UMC.Data.Utility.MapPath("/cavif"), file); } } - catch//(Exception ex) + catch { + Console.SetCursorPosition(0, Console.CursorTop); Console.WriteLine($"图片组件下载失败,影响图片功能"); } File.Delete(downloadFile); @@ -426,8 +451,8 @@ namespace UMC.Host } else { - Console.Write($"正在下载图片组件{size * 100 / count:0}%"); Console.SetCursorPosition(0, Console.CursorTop); + Console.Write($"正在下载图片组件{size * 100 / count:0}%"); stream.Write(b, c, l); } @@ -487,8 +512,7 @@ namespace UMC.Host APIProxy.Version = p?.InformationalVersion ?? "1.0.0"; Register(); - // File.WriteAllText(UMC.Data.Utility.MapPath($".lock"), $"{{\"Id\":\"{System.Diagnostics.Process.GetCurrentProcess().Id}\"}}"); - // UMC.Data.HotCache.Namespace(typeof(UMC.Proxy.Entities.Cookie).Namespace); + UMC.Proxy.DataFactory.Instance(); UMC.Data.DataFactory.Instance(); diff --git a/Resources/dingtalk.html b/Resources/dingtalk.html index 07247ca..7e2d4a7 100644 --- a/Resources/dingtalk.html +++ b/Resources/dingtalk.html @@ -31,7 +31,7 @@ var qvalue = UMC.query(location.search.substring(1)); UMC.UI.On("User", function () { if (qvalue.oauth_callback) { - location.href = v.oauth_callback; + location.href = qvalue.oauth_callback; } else { location.reload(true); } @@ -55,7 +55,6 @@ 'code': info.code, 'appid': corpId, 'transfer': qvalue.transfer - }); } });