diff --git a/FastGithub.ReverseProxy/GithubSshHandler.cs b/FastGithub.ReverseProxy/GithubSshHandler.cs index 8445d17..338752a 100644 --- a/FastGithub.ReverseProxy/GithubSshHandler.cs +++ b/FastGithub.ReverseProxy/GithubSshHandler.cs @@ -1,7 +1,5 @@ using FastGithub.DomainResolve; using Microsoft.AspNetCore.Connections; -using System; -using System.IO; using System.IO.Pipelines; using System.Net; using System.Net.Sockets; @@ -38,99 +36,11 @@ namespace FastGithub.ReverseProxy var address = await this.domainResolver.ResolveAsync(GITHUB_COM, CancellationToken.None); using var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); await socket.ConnectAsync(new IPEndPoint(address, SSH_PORT)); + var targetStream = new NetworkStream(socket, ownsSocket: false); - var upStream = new NetworkStream(socket, ownsSocket: false); - var downStream = new DuplexStream(connection.Transport); - - var task1 = upStream.CopyToAsync(downStream); - var task2 = downStream.CopyToAsync(upStream); + var task1 = targetStream.CopyToAsync(connection.Transport.Output); + var task2 = connection.Transport.Input.CopyToAsync(targetStream); await Task.WhenAny(task1, task2); } - - /// - /// 双工数据流 - /// - private class DuplexStream : Stream - { - private readonly Stream readStream; - private readonly Stream wirteStream; - - /// - /// 双工数据流 - /// - /// - public DuplexStream(IDuplexPipe transport) - { - this.readStream = transport.Input.AsStream(); - this.wirteStream = transport.Output.AsStream(); - } - - public override bool CanRead => true; - - public override bool CanSeek => false; - - public override bool CanWrite => true; - - public override long Length => throw new NotSupportedException(); - - public override long Position - { - get => throw new NotSupportedException(); - set => throw new NotSupportedException(); - } - - public override void Flush() - { - this.wirteStream.Flush(); - } - - public override Task FlushAsync(CancellationToken cancellationToken) - { - return this.wirteStream.FlushAsync(cancellationToken); - } - - public override long Seek(long offset, SeekOrigin origin) - { - throw new NotSupportedException(); - } - - public override void SetLength(long value) - { - throw new NotSupportedException(); - } - - public override int Read(byte[] buffer, int offset, int count) - { - return this.readStream.Read(buffer, offset, count); - } - public override void Write(byte[] buffer, int offset, int count) - { - this.wirteStream.Write(buffer, offset, count); - } - public override ValueTask ReadAsync(Memory buffer, CancellationToken cancellationToken = default) - { - return this.readStream.ReadAsync(buffer, cancellationToken); - } - - public override Task ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - { - return this.readStream.ReadAsync(buffer, offset, count, cancellationToken); - } - - public override void Write(ReadOnlySpan buffer) - { - this.wirteStream.Write(buffer); - } - - public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) - { - return this.wirteStream.WriteAsync(buffer, offset, count, cancellationToken); - } - - public override async ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) - { - await this.wirteStream.WriteAsync(buffer, cancellationToken); - } - } } }