private async void Run(CancellationToken ct) { while (!ct.IsCancellationRequested) { //Log.Info("{0} [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[", _server.LocalEndpoint); var client = await _server.AcceptTcpClientAsync(); //Log.Info("{0}: ...................................................", client.Client.RemoteEndPoint); var inbound = await CreateInboundConnection(client); //if (client.Connected && client.Available > 0 && inbound.IsConnected) //{ Log.Debug("{0}: Connected", inbound.RemoteEndPoint); await inbound.OpenAsync(ct); //} var context = new MiddlemanContext(inbound); HandleSession(context); //Log.Debug("{0}: Connected", inbound.RemoteEndPoint); //Log.Info("]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]"); } }
private void SetForwardedForHeader(MiddlemanContext context, MiddlemanRequest request) { var remoteAddress = context.InboundConnection.RemoteEndPoint.Address.ToString(); var currentForwardedFor = request.Headers["X-Forwarded-For"]; request.Headers["X-Forwarded-For"] = string.IsNullOrEmpty(currentForwardedFor) ? remoteAddress : currentForwardedFor + ", " + remoteAddress; }
public async Task<MiddlemanResponse> GetResponseAsync(MiddlemanContext context, MiddlemanRequest request) { if (request.Headers.AllKeys.Any(h => h.Equals("VsDebuggerCausalityData", StringComparison.InvariantCultureIgnoreCase))) { request.Headers.Remove("VsDebuggerCausalityData"); } //if (request.Headers.AllKeys.Any(h => h.Equals("SOAPAction", StringComparison.InvariantCultureIgnoreCase))) //{ // request.Headers.Remove("SOAPAction"); //} if (request.Headers.AllKeys.Any( h => h.Equals("Accept-Encoding", StringComparison.InvariantCultureIgnoreCase) && request.Headers[h].Contains("/"))) { string[] parts = request.Headers["Accept-Encoding"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(s => !s.Contains("/")).ToArray(); Log.Debug("Fixed bad [Accept-Encoding] header"); if (parts.Length > 0) { request.Headers["Accept-Encoding"] = string.Join(",", parts); } else { request.Headers.Remove("Accept-Encoding"); } } if (RewriteHost) { request.Headers["Host"] = _backendUri.Host + (_backendUri.IsDefaultPort ? string.Empty : ":" + _backendUri.Port); } if (AddForwardedForHeader) { SetForwardedForHeader(context, request); } if (RemoveExpectHeader && request.Headers.AllKeys.Any( h => h.Equals(HttpRequestHeader.Expect.ToString(), StringComparison.InvariantCultureIgnoreCase))) { request.Headers.Remove(HttpRequestHeader.Expect); } //if (request.Headers.AllKeys.Any(h => h.Equals(HttpRequestHeader.Range.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.Range); //if (request.Headers.AllKeys.Any(h => h.Replace("-","").Equals(HttpRequestHeader.IfRange.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfRange); //if (request.Headers.AllKeys.Any(h => h.Replace("-", "").Equals(HttpRequestHeader.IfModifiedSince.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfModifiedSince); //if (request.Headers.AllKeys.Any(h => h.Replace("-", "").Equals(HttpRequestHeader.IfMatch.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfMatch); //if (request.Headers.AllKeys.Any(h => h.Replace("-", "").Equals(HttpRequestHeader.IfNoneMatch.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfNoneMatch); var sw = Stopwatch.StartNew(); IPAddress ip; if (_backendUri.HostNameType == UriHostNameType.IPv4) { ip = IPAddress.Parse(_backendUri.Host); } else { var ipAddresses = await Dns.GetHostAddressesAsync(_backendUri.Host); ip = ipAddresses.FirstOrDefault(o => !o.ToString().Contains(":")); } var backendEp = new IPEndPoint(ip, _backendUri.Port); Log.Debug("{0}: Resolved upstream server to {1} in {2}ms, opening connection", context.InboundConnection.RemoteEndPoint, backendEp, sw.Elapsed.TotalMilliseconds); if (_backendUri.Scheme != "https") await context.OpenOutboundConnectionAsync(backendEp); else await context.OpenSecureOutboundConnectionAsync(backendEp, _backendUri.Host); Log.Debug("{0}: Outbound connection established, sending request", context.InboundConnection.RemoteEndPoint); sw.Restart(); await context.OutboundConnection.WriteRequestAsync(request); Log.Debug("{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(MiddlemanContext context) { try { Log.Info("{0}:{1}:{2} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", context.ContextId, context.InboundConnection.RemoteEndPoint.Address.ToString(), context.InboundConnection.RemoteEndPoint.Port.ToString()); Log.Debug("{0}: Starting session", context.InboundConnection.RemoteEndPoint); int count = 1; do { if (count > 1) { //Log.Info("{0}:{1}:{2} >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", context.ContextId, context.InboundConnection.RemoteEndPoint.Address.ToString(), context.InboundConnection.RemoteEndPoint.Port.ToString()); Log.Info("{0}:{1}:{2} connection alive <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<", context.ContextId, context.InboundConnection.RemoteEndPoint.Address.ToString(), context.InboundConnection.RemoteEndPoint.Port.ToString()); } var request = await context.InboundConnection.ReadRequestAsync().ConfigureAwait(true); if (request != null) { Log.Info("{0}: Got {1} request {3} for {2}", context.InboundConnection.RemoteEndPoint, request.Method, request.RequestUri, count); var response = await _handler.GetResponseAsync(context, request).ConfigureAwait(false); Log.Info("{0}: Got response from handler ({1})", context.InboundConnection.RemoteEndPoint, response.StatusCode); await context.InboundConnection.WriteResponseAsync(response).ConfigureAwait(false); Log.Info("{0}: Wrote response to client", context.InboundConnection.RemoteEndPoint); //bool closeConnection = response.Headers.AllKeys.Any(h => h.Equals("Connection", StringComparison.InvariantCultureIgnoreCase) && response.Headers[h].Equals("Close", StringComparison.InvariantCultureIgnoreCase)); if (context.InboundConnection.MustClose || (context.OutboundConnection != null && !context.OutboundConnection.IsConnected)) //if ((context.OutboundConnection != null && !context.OutboundConnection.IsConnected)) { //if (context.InboundConnection != null) //{ // context.InboundConnection.Close(); //} Log.Info("Closing context!"); context.Close(); //context.Dispose(); } count++; } else { Log.Info("Closing context!"); context.Close(); } } while (context.InboundConnection.IsConnected); Log.Info("{0}:{1}:{2} connection closed <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<", context.ContextId, context.InboundConnection.RemoteEndPoint.Address.ToString(), context.InboundConnection.RemoteEndPoint.Port.ToString()); } catch (Exception exc) { Log.ErrorException(string.Format("{0}: HandleSession Error.", context.InboundConnection.RemoteEndPoint), exc); context.Close(); } finally { Log.Info("Closing context!"); context.Close(); //context.Dispose(); } }
public async Task <MiddlemanResponse> GetResponseAsync(MiddlemanContext context, MiddlemanRequest request) { if (request.Headers.AllKeys.Any(h => h.Equals("VsDebuggerCausalityData", StringComparison.InvariantCultureIgnoreCase))) { request.Headers.Remove("VsDebuggerCausalityData"); } //if (request.Headers.AllKeys.Any(h => h.Equals("SOAPAction", StringComparison.InvariantCultureIgnoreCase))) //{ // request.Headers.Remove("SOAPAction"); //} if (request.Headers.AllKeys.Any( h => h.Equals("Accept-Encoding", StringComparison.InvariantCultureIgnoreCase) && request.Headers[h].Contains("/"))) { string[] parts = request.Headers["Accept-Encoding"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(s => !s.Contains("/")).ToArray(); Log.Debug("Fixed bad [Accept-Encoding] header"); if (parts.Length > 0) { request.Headers["Accept-Encoding"] = string.Join(",", parts); } else { request.Headers.Remove("Accept-Encoding"); } } if (request.Headers.AllKeys.Any( h => h.Equals("Accept-Encoding", StringComparison.InvariantCultureIgnoreCase) && (request.Headers[h].Contains("gzip") || request.Headers[h].Contains("deflate") || request.Headers[h].Contains("sdch")))) { string acceptEncodingHeader = (request.Headers["Accept-Encoding"] ?? "").Trim(); Log.Debug("Disabled gzip compression as it is not currently supported."); if (acceptEncodingHeader.Length > 0) { acceptEncodingHeader = acceptEncodingHeader.Replace("gzip", "gzip;q=0").Replace("deflate", "deflate;q=0").Replace("sdch", "sdch;q=0").Replace(";;", ";"); request.Headers["Accept-Encoding"] = acceptEncodingHeader; } else { acceptEncodingHeader = "gzip;q=0,deflate;q=0,sdch;q=0"; request.Headers["Accept-Encoding"] = acceptEncodingHeader; } } if (RewriteHost) { request.Headers["Host"] = _backendUri.Host + (_backendUri.IsDefaultPort ? string.Empty : ":" + _backendUri.Port); } if (AddForwardedForHeader) { SetForwardedForHeader(context, request); } if (RemoveExpectHeader && request.Headers.AllKeys.Any( h => h.Equals(HttpRequestHeader.Expect.ToString(), StringComparison.InvariantCultureIgnoreCase))) { request.Headers.Remove(HttpRequestHeader.Expect); } //if (request.Headers.AllKeys.Any(h => h.Equals(HttpRequestHeader.Range.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.Range); //if (request.Headers.AllKeys.Any(h => h.Replace("-","").Equals(HttpRequestHeader.IfRange.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfRange); //if (request.Headers.AllKeys.Any(h => h.Replace("-", "").Equals(HttpRequestHeader.IfModifiedSince.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfModifiedSince); //if (request.Headers.AllKeys.Any(h => h.Replace("-", "").Equals(HttpRequestHeader.IfMatch.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfMatch); //if (request.Headers.AllKeys.Any(h => h.Replace("-", "").Equals(HttpRequestHeader.IfNoneMatch.ToString(), StringComparison.InvariantCultureIgnoreCase))) // request.Headers.Remove(HttpRequestHeader.IfNoneMatch); var sw = Stopwatch.StartNew(); IPAddress ip; if (_backendUri.HostNameType == UriHostNameType.IPv4) { ip = IPAddress.Parse(_backendUri.Host); } else { var ipAddresses = await Dns.GetHostAddressesAsync(_backendUri.Host); ip = ipAddresses.FirstOrDefault(o => !o.ToString().Contains(":")); } var backendEp = new IPEndPoint(ip, _backendUri.Port); Log.Debug("{0}: Resolved upstream server to {1} in {2}ms, opening connection", context.InboundConnection.RemoteEndPoint, backendEp, sw.Elapsed.TotalMilliseconds); if (_backendUri.Scheme != "https") { await context.OpenOutboundConnectionAsync(backendEp); } else { await context.OpenSecureOutboundConnectionAsync(backendEp, _backendUri.Host); } Log.Debug("{0}: Outbound connection established, sending request", context.InboundConnection.RemoteEndPoint); sw.Restart(); await context.OutboundConnection.WriteRequestAsync(request); Log.Debug("{0}: Handler sent request in {1}ms", context.InboundConnection.RemoteEndPoint, sw.Elapsed.TotalMilliseconds); var response = await context.OutboundConnection.ReadResponseAsync(); return(response); }