github ssh转发功能
This commit is contained in:
		
							parent
							
								
									5c7145c056
								
							
						
					
					
						commit
						9da7f18b9d
					
				
							
								
								
									
										136
									
								
								FastGithub.ReverseProxy/GitubSshHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										136
									
								
								FastGithub.ReverseProxy/GitubSshHandler.cs
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,136 @@
 | 
				
			|||||||
 | 
					using FastGithub.DomainResolve;
 | 
				
			||||||
 | 
					using Microsoft.AspNetCore.Connections;
 | 
				
			||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using System.IO;
 | 
				
			||||||
 | 
					using System.IO.Pipelines;
 | 
				
			||||||
 | 
					using System.Net;
 | 
				
			||||||
 | 
					using System.Net.Sockets;
 | 
				
			||||||
 | 
					using System.Threading;
 | 
				
			||||||
 | 
					using System.Threading.Tasks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					namespace FastGithub.ReverseProxy
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    /// <summary>
 | 
				
			||||||
 | 
					    /// github的ssl处理者
 | 
				
			||||||
 | 
					    /// </summary>
 | 
				
			||||||
 | 
					    sealed class GitubSshHandler : ConnectionHandler
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        private const int SSH_PORT = 22;
 | 
				
			||||||
 | 
					        private const string GITHUB_COM = "github.com";
 | 
				
			||||||
 | 
					        private readonly IDomainResolver domainResolver;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// github的ssl处理者
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="domainResolver"></param>
 | 
				
			||||||
 | 
					        public GitubSshHandler(IDomainResolver domainResolver)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            this.domainResolver = domainResolver;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// ssh连接后
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="connection"></param>
 | 
				
			||||||
 | 
					        /// <returns></returns>
 | 
				
			||||||
 | 
					        public override async Task OnConnectedAsync(ConnectionContext connection)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            var address = await this.domainResolver.ResolveAsync(GITHUB_COM, CancellationToken.None);
 | 
				
			||||||
 | 
					            var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
 | 
				
			||||||
 | 
					            await socket.ConnectAsync(new IPEndPoint(address, SSH_PORT));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            using var upStream = new NetworkStream(socket, ownsSocket: true);
 | 
				
			||||||
 | 
					            var downStream = new SshStream(connection.Transport);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var task1 = upStream.CopyToAsync(downStream);
 | 
				
			||||||
 | 
					            var task2 = downStream.CopyToAsync(upStream);
 | 
				
			||||||
 | 
					            await Task.WhenAny(task1, task2);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 表示Ssh的流
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        private class SshStream : Stream
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            private readonly Stream readStream;
 | 
				
			||||||
 | 
					            private readonly Stream wirteStream;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            /// <summary>
 | 
				
			||||||
 | 
					            /// Ssh的流
 | 
				
			||||||
 | 
					            /// </summary>
 | 
				
			||||||
 | 
					            /// <param name="transport"></param>
 | 
				
			||||||
 | 
					            public SshStream(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);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -1,5 +1,6 @@
 | 
				
			|||||||
using FastGithub.Configuration;
 | 
					using FastGithub.Configuration;
 | 
				
			||||||
using FastGithub.ReverseProxy;
 | 
					using FastGithub.ReverseProxy;
 | 
				
			||||||
 | 
					using Microsoft.AspNetCore.Connections;
 | 
				
			||||||
using Microsoft.AspNetCore.Hosting;
 | 
					using Microsoft.AspNetCore.Hosting;
 | 
				
			||||||
using Microsoft.AspNetCore.Server.Kestrel.Core;
 | 
					using Microsoft.AspNetCore.Server.Kestrel.Core;
 | 
				
			||||||
using Microsoft.Extensions.DependencyInjection;
 | 
					using Microsoft.Extensions.DependencyInjection;
 | 
				
			||||||
@ -69,5 +70,29 @@ namespace FastGithub
 | 
				
			|||||||
                https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
 | 
					                https.ServerCertificateSelector = (ctx, domain) => certService.GetOrCreateServerCert(domain);
 | 
				
			||||||
            }));
 | 
					            }));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// <summary>
 | 
				
			||||||
 | 
					        /// 监听github的ssh的代理
 | 
				
			||||||
 | 
					        /// </summary>
 | 
				
			||||||
 | 
					        /// <param name="kestrel"></param>
 | 
				
			||||||
 | 
					        public static void ListenGithubSshProxy(this KestrelServerOptions kestrel)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            const int SSH_PORT = 22;
 | 
				
			||||||
 | 
					            if (OperatingSystem.IsWindows())
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                TcpTable.KillPortOwner(SSH_PORT);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (LocalMachine.CanListenTcp(SSH_PORT) == false)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                var loggerFactory = kestrel.ApplicationServices.GetRequiredService<ILoggerFactory>();
 | 
				
			||||||
 | 
					                var logger = loggerFactory.CreateLogger($"{nameof(FastGithub)}.{nameof(ReverseProxy)}");
 | 
				
			||||||
 | 
					                logger.LogWarning($"由于tcp端口{SSH_PORT}已经被其它进程占用,github的ssh代理功能将受限");
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            else
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                kestrel.Listen(IPAddress.Any, SSH_PORT, listen => listen.UseConnectionHandler<GitubSshHandler>());
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -49,6 +49,7 @@ namespace FastGithub
 | 
				
			|||||||
                    webBuilder.UseKestrel(kestrel =>
 | 
					                    webBuilder.UseKestrel(kestrel =>
 | 
				
			||||||
                    {
 | 
					                    {
 | 
				
			||||||
                        kestrel.Limits.MaxRequestBodySize = null;
 | 
					                        kestrel.Limits.MaxRequestBodySize = null;
 | 
				
			||||||
 | 
					                        kestrel.ListenGithubSshProxy();
 | 
				
			||||||
                        kestrel.ListenHttpReverseProxy();
 | 
					                        kestrel.ListenHttpReverseProxy();
 | 
				
			||||||
                        kestrel.ListenHttpsReverseProxy();
 | 
					                        kestrel.ListenHttpsReverseProxy();
 | 
				
			||||||
                    });
 | 
					                    });
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user