public async Task <LastPipeState> Run(ProxyMediatorHandler handler, SessionContext context) { if (context.CurrentHostAddress == null || !Equals(context.Header.Host, context.CurrentHostAddress)) { return(LastPipeState.NewHostConnectionRequired); } var header = context.Header; if (context.CurrentExternalProxy != null) { header.AppendContextLine(context.CurrentExternalProxy.AuthHeaderLine); await HttpProxyProcessor.ForwardHeader(header, context.HostStream); } using var tunnel = new TcpTwoWayTunnel(); var task = tunnel.Run(context.ClientStream, context.HostStream); if (context.CurrentExternalProxy == null) { await SendConnectionEstablised(context.ClientStream); } await task; return(LastPipeState.GameOver); }
public ProxyMediatorBackgroundService(ProxyMediatorHandler handler, ILogger <ProxyMediatorBackgroundService> logger, StageFunc onStageFunc, IHostApplicationLifetime appLifetime) : base(appLifetime) { _handler = handler; _logger = logger; _onStageFunc = onStageFunc; }
public async Task <LastPipeState> Run(ProxyMediatorHandler handler, SessionContext context) { if (IsNewHostRequired(context)) { return(LastPipeState.NewHostConnectionRequired); } using (OneWayTunnel(context.HostStream, context.ClientStream)) { var buffer = new byte[BufferSize]; try { int bytesRead; do { context.Header = await GetHeader(context.Header, context.ClientStream); if (context.Header == null) { return(LastPipeState.GameOver); } if (IsNewHostRequired(context)) { return(LastPipeState.NewHostConnectionRequired); } var header = context.Header; if (context.CurrentExternalProxy != null) { header.AppendContextLine(context.CurrentExternalProxy.AuthHeaderLine); } bytesRead = await ForwardHeader(header, context.HostStream); if (HasBody(header)) { bytesRead = await ForwardBody(context.ClientStream, context.HostStream, context.Header.ContentLength, buffer); } context.Header = null; } while (bytesRead > 0); } catch (ObjectDisposedException) { return(LastPipeState.NewHostConnectionRequired); } } return(LastPipeState.GameOver); }
public static IServiceCollection AddProxyMediator(this IServiceCollection svc, IPEndPoint ipEndPoint, Func <SessionContext, ProxyMediatorHandler, Task <ExternalProxy> > onBeforeConnect ) { var handler = new ProxyMediatorHandler(new SessionContextPool(), ipEndPoint); svc.AddSingleton(_ => handler); svc.AddSingleton(_ => new StageFunc() { OnBeforeConnect = onBeforeConnect }); svc.AddHostedService <ProxyMediatorBackgroundService>(); return(svc); }
public async Task <LastPipeState> Run(ProxyMediatorHandler handler, SessionContext context) { context.RemoveHost(); if (context.StageFunc?.OnBeforeConnect != null) { context.CurrentExternalProxy = await context.StageFunc.OnBeforeConnect(context, handler); } if (context.CurrentExternalProxy == null) { context.AddHost(await Connect(context.Header.Host.Hostname, context.Header.Host.Port)); } else { context.AddHost(await Connect(context.CurrentExternalProxy.Address, context.CurrentExternalProxy.Port)); } context.CurrentHostAddress = context.Header.Host; return(LastPipeState.NewHostConnected); }
public Task <LastPipeState> Run(ProxyMediatorHandler handler, SessionContext context) { return(Task.FromResult(context.Header.Verb == "CONNECT" ? LastPipeState.HttpsTunnelRequired : LastPipeState.HttpProxyRequired)); }
public ProxyServerConfig(ProxyMediatorHandler handler) { Handler = handler; }
public async Task <LastPipeState> Run(ProxyMediatorHandler handler, SessionContext context) { context.Header = await HttpHeaderStream.GetHeader(context.ClientStream); return(context.Header != null ? LastPipeState.Initialized : LastPipeState.GameOver); }