简化ssh实现(#28)
This commit is contained in:
parent
7d798ad010
commit
7f89f900a9
@ -1,7 +1,5 @@
|
|||||||
using FastGithub.DomainResolve;
|
using FastGithub.DomainResolve;
|
||||||
using Microsoft.AspNetCore.Connections;
|
using Microsoft.AspNetCore.Connections;
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using System.IO.Pipelines;
|
using System.IO.Pipelines;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
@ -38,99 +36,11 @@ namespace FastGithub.ReverseProxy
|
|||||||
var address = await this.domainResolver.ResolveAsync(GITHUB_COM, CancellationToken.None);
|
var address = await this.domainResolver.ResolveAsync(GITHUB_COM, CancellationToken.None);
|
||||||
using var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
using var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||||
await socket.ConnectAsync(new IPEndPoint(address, SSH_PORT));
|
await socket.ConnectAsync(new IPEndPoint(address, SSH_PORT));
|
||||||
|
var targetStream = new NetworkStream(socket, ownsSocket: false);
|
||||||
|
|
||||||
var upStream = new NetworkStream(socket, ownsSocket: false);
|
var task1 = targetStream.CopyToAsync(connection.Transport.Output);
|
||||||
var downStream = new DuplexStream(connection.Transport);
|
var task2 = connection.Transport.Input.CopyToAsync(targetStream);
|
||||||
|
|
||||||
var task1 = upStream.CopyToAsync(downStream);
|
|
||||||
var task2 = downStream.CopyToAsync(upStream);
|
|
||||||
await Task.WhenAny(task1, task2);
|
await Task.WhenAny(task1, task2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 双工数据流
|
|
||||||
/// </summary>
|
|
||||||
private class DuplexStream : Stream
|
|
||||||
{
|
|
||||||
private readonly Stream readStream;
|
|
||||||
private readonly Stream wirteStream;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 双工数据流
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="transport"></param>
|
|
||||||
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<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
return this.readStream.ReadAsync(buffer, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
return this.readStream.ReadAsync(buffer, offset, count, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void Write(ReadOnlySpan<byte> 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<byte> buffer, CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
await this.wirteStream.WriteAsync(buffer, cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user