From 7f89f900a9be78f4f97604d5906f142944fed76a Mon Sep 17 00:00:00 2001
From: xljiulang <366193849@qq.com>
Date: Fri, 13 Aug 2021 22:17:48 +0800
Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96ssh=E5=AE=9E=E7=8E=B0(#28)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.ReverseProxy/GithubSshHandler.cs | 96 +--------------------
1 file changed, 3 insertions(+), 93 deletions(-)
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);
- }
- }
}
}