forked from shiftwinting/FastGithub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SshReverseProxyHandler.cs
44 lines (40 loc) · 1.52 KB
/
SshReverseProxyHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using FastGithub.DomainResolve;
using Microsoft.AspNetCore.Connections;
using System.IO.Pipelines;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
namespace FastGithub.HttpServer
{
/// <summary>
/// github的ssh代理处理者
/// </summary>
sealed class SshReverseProxyHandler : ConnectionHandler
{
private readonly IDomainResolver domainResolver;
private readonly DnsEndPoint sshOverHttpsEndPoint = new("ssh.github.com", 443);
/// <summary>
/// github的ssh代理处理者
/// </summary>
/// <param name="domainResolver"></param>
public SshReverseProxyHandler(IDomainResolver domainResolver)
{
this.domainResolver = domainResolver;
}
/// <summary>
/// ssh连接后
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task OnConnectedAsync(ConnectionContext context)
{
var address = await this.domainResolver.ResolveAnyAsync(this.sshOverHttpsEndPoint);
using var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(address, this.sshOverHttpsEndPoint.Port);
var targetStream = new NetworkStream(socket, ownsSocket: false);
var task1 = targetStream.CopyToAsync(context.Transport.Output);
var task2 = context.Transport.Input.CopyToAsync(targetStream);
await Task.WhenAny(task1, task2);
}
}
}