private void SetForwardedForHeader(SwitchboardContext context, SwitchboardRequest request) { string remoteAddress = context.InboundConnection.RemoteEndPoint.Address.ToString(); string currentForwardedFor = request.Headers["X-Forwarded-For"]; request.Headers["X-Forwarded-For"] = string.IsNullOrEmpty(currentForwardedFor) ? remoteAddress : currentForwardedFor + ", " + remoteAddress; }
public async Task <SwitchboardResponse> GetResponseAsync(SwitchboardContext context, SwitchboardRequest request) { var originalHost = request.Headers["Host"]; if (this.RewriteHost) { request.Headers["Host"] = this.backendUri.Host + (this.backendUri.IsDefaultPort ? string.Empty : ":" + this.backendUri.Port); } if (this.AddForwardedForHeader) { SetForwardedForHeader(context, request); } var sw = Stopwatch.StartNew(); IPAddress ip; if (this.backendUri.HostNameType == UriHostNameType.IPv4) { ip = IPAddress.Parse(this.backendUri.Host); } else { var ipAddresses = await Dns.GetHostAddressesAsync(this.backendUri.Host); ip = ipAddresses[0]; } var backendEp = new IPEndPoint(ip, this.backendUri.Port); Debug.WriteLine("{0}: Resolved upstream server to {1} in {2}ms, opening connection", context.InboundConnection.RemoteEndPoint, backendEp, sw.Elapsed.TotalMilliseconds); if (this.backendUri.Scheme != "https") { await context.OpenOutboundConnectionAsync(backendEp); } else { await context.OpenSecureOutboundConnectionAsync(backendEp, this.backendUri.Host); } Debug.WriteLine("{0}: Outbound connection established, sending request", context.InboundConnection.RemoteEndPoint); sw.Restart(); await context.OutboundConnection.WriteRequestAsync(request, cancellationTokenSource.Token); Debug.WriteLine("{0}: Handler sent request in {1}ms", context.InboundConnection.RemoteEndPoint, sw.Elapsed.TotalMilliseconds); var response = await context.OutboundConnection.ReadResponseAsync(); return(response); }
private async Task Run(CancellationToken ct) { while (!ct.IsCancellationRequested) { var client = await this.server.AcceptTcpClientAsync(); var inbound = await CreateInboundConnection(client); await inbound.OpenAsync(); Debug.WriteLine(string.Format("{0}: Connected", inbound.RemoteEndPoint)); var context = new SwitchboardContext(inbound); HandleSession(context); } }
public async Task<SwitchboardResponse> GetResponseAsync(SwitchboardContext context, SwitchboardRequest request) { var originalHost = request.Headers["Host"]; if (this.RewriteHost) request.Headers["Host"] = this.backendUri.Host + (this.backendUri.IsDefaultPort ? string.Empty : ":" + this.backendUri.Port); if (this.AddForwardedForHeader) SetForwardedForHeader(context, request); var sw = Stopwatch.StartNew(); IPAddress ip; if(this.backendUri.HostNameType == UriHostNameType.IPv4) { ip = IPAddress.Parse(this.backendUri.Host); } else { var ipAddresses = await Dns.GetHostAddressesAsync(this.backendUri.Host); ip = ipAddresses[0]; } var backendEp = new IPEndPoint(ip, this.backendUri.Port); Debug.WriteLine("{0}: Resolved upstream server to {1} in {2}ms, opening connection", context.InboundConnection.RemoteEndPoint, backendEp, sw.Elapsed.TotalMilliseconds); if (this.backendUri.Scheme != "https") await context.OpenOutboundConnectionAsync(backendEp); else await context.OpenSecureOutboundConnectionAsync(backendEp, this.backendUri.Host); Debug.WriteLine("{0}: Outbound connection established, sending request", context.InboundConnection.RemoteEndPoint); sw.Restart(); await context.OutboundConnection.WriteRequestAsync(request, cancellationTokenSource.Token); Debug.WriteLine("{0}: Handler sent request in {1}ms", context.InboundConnection.RemoteEndPoint, sw.Elapsed.TotalMilliseconds); var response = await context.OutboundConnection.ReadResponseAsync(); return response; }
private async void HandleSession(SwitchboardContext context) { try { Debug.WriteLine("{0}: Starting session", context.InboundConnection.RemoteEndPoint); do { var request = await context.InboundConnection.ReadRequestAsync().ConfigureAwait(false); if (request == null) return; Debug.WriteLine(string.Format("{0}: Got {1} request for {2}", context.InboundConnection.RemoteEndPoint, request.Method, request.RequestUri)); var response = await handler.GetResponseAsync(context, request).ConfigureAwait(false); Debug.WriteLine(string.Format("{0}: Got response from handler ({1})", context.InboundConnection.RemoteEndPoint, response.StatusCode)); await context.InboundConnection.WriteResponseAsync(response).ConfigureAwait(false); Debug.WriteLine(string.Format("{0}: Wrote response to client", context.InboundConnection.RemoteEndPoint)); if (context.OutboundConnection != null && !context.OutboundConnection.IsConnected) context.Close(); } while (context.InboundConnection.IsConnected); } catch (Exception exc) { Debug.WriteLine(string.Format("{0}: Error: {1}", context.InboundConnection.RemoteEndPoint, exc.Message)); context.Close(); Debug.WriteLine(string.Format("{0}: Closed context", context.InboundConnection.RemoteEndPoint, exc.Message)); } finally { context.Dispose(); } }