public static ProxyCache add_ProxyCacheLogViewer(this ProxyCache proxyCache, Control control) { var cacheLog = control.add_TreeView() .hideSelection() .onDoubleClick <RequestResponseData>((requestResponseData) => requestResponseData.details()); proxyCache.ProxyCacheLogEntry = (logLine, requestResponseData) => { //cacheLog.insert_TreeNode(logLine, requestResponseData, 0).nodes()[0] if (proxyCache.ShowItemInLog(logLine, requestResponseData)) { if (requestResponseData.notNull()) { logLine = "{0} size: {1}".format(logLine, requestResponseData.Response_Bytes.notNull() ? requestResponseData.Response_Bytes.Length : -1); } cacheLog.add_Node(logLine, requestResponseData) .color(requestResponseData.isNull() ? Color.Gray : Color.DarkGreen) .selected(); } }; return(proxyCache); }
/*public static byte[] response_Bytes(this ProxyCache proxyCache, string key) * { * if (proxyCache.hasMapping(key)) * return proxyCache.requestMappings().value(key).Response_Bytes; * return null; * } * * public static string response_String(this ProxyCache proxyCache, string key) * { * if (proxyCache.hasMapping(key)) * return proxyCache.requestMappings().value(key).Response_String; * return null; * }*/ public static ProxyCache add_ToCache(this ProxyCache proxyCache, RequestResponseData requestResponseData) { var requestPostBytes = requestResponseData.Request_PostBytes; var cacheKey = proxyCache.cacheKey(requestResponseData); ProxyCache.Requests.Add(requestResponseData); ProxyCache.RequestCache.add(cacheKey, requestResponseData); return(proxyCache); }
public void setDefaultValues() { proxyCache = new ProxyCache(); CaptureRequests = true; this.clearPastRequests(); IPAddress ipAddress = IPAddress.Loopback; IPAddress.TryParse("127.0.0.1", out ipAddress); ListeningIPInterface = ipAddress; ListeningPort = 8081; }
public static Control add_ProxyCacheActionsPanel(this ProxyCache proxyCache, Control actionsPanel) { actionsPanel.add_Label("Settings") .append_CheckBox("cache Enabled", (value) => proxyCache.enabled(value)).@checked(proxyCache.enabled()) .append_Below_Link("show Cache entries", () => ProxyCache.RequestCache.details()) .append_Link("clear Cache", () => ProxyCache.RequestCache.Clear()) .append_Link("open a WebBrowser", () => "ProxyChache - test WebBrowser".popupWindow() .add_WebBrowser_Control() .add_NavigationBar() .open("http://www.google.com")); return(actionsPanel); }
public static RequestResponseData getMapping(this ProxyCache proxyCache, HttpWebRequest webRequest, byte[] requestPostBytes) { var cacheKey = proxyCache.ProxyCache_Brain.cacheKey(webRequest, requestPostBytes); var cacheObject = proxyCache.ProxyCache_Brain.getMapping(cacheKey); proxyCache.ProxyCacheLogEntry(cacheObject.isNull() ? "{0} (not in cache)".format(cacheKey) : "{0} (from cache)".format(cacheKey), cacheObject); if (cacheObject.notNull()) { cacheObject.CacheHits++; } return(cacheObject); }
public static RequestResponseData add(this ProxyCache proxyCache, HttpWebRequest webRequest, HttpWebResponse webResponse, byte[] requestPostBytes, byte[] responseBytes, string responseString) { var requestResponseData = new RequestResponseData() { WebRequest = webRequest, WebResponse = webResponse, Response_Bytes = responseBytes, Response_String = responseString }; if (requestPostBytes.notNull()) { requestResponseData.Request_PostBytes = requestPostBytes; requestResponseData.Request_PostString = requestPostBytes.ascii(); } proxyCache.add_ToCache(requestResponseData); return(requestResponseData); }
public static Control insert_Above_ProxyCacheActionsPanel(this Control control, ProxyCache proxyCache) { return(proxyCache.add_ProxyCacheActionsPanel(control.insert_Above(60, "actions"))); }
public static Control insert_Below_ProxyCacheActionsPanel(this Control control, ProxyCache proxyCache) { return(proxyCache.add_ProxyCacheActionsPanel(control.insert_Below(60, ""))); }
public static T add_ProxyCacheLogViewer <T>(this T control, ProxyCache proxyCache) where T : Control { proxyCache.add_ProxyCacheLogViewer(control); return(control); }
public static string cacheKey(this ProxyCache proxyCache, RequestResponseData requestResponseData) { return(proxyCache.ProxyCache_Brain.cacheKey(requestResponseData)); }
public static bool hasMapping(this ProxyCache proxyCache, HttpWebRequest webRequest, byte[] requestPostBytes) { var cacheKey = proxyCache.ProxyCache_Brain.cacheKey(webRequest, requestPostBytes); return(proxyCache.ProxyCache_Brain.hasMapping(cacheKey)); }
public static Dictionary <string, RequestResponseData> requestMappings(this ProxyCache proxyCache) { return(ProxyCache.RequestCache); }
public static ProxyCache enabled(this ProxyCache proxyCache, bool value) { proxyCache.CacheEnabled = value; return(proxyCache); }
public static bool enabled(this ProxyCache proxyCache) { return(proxyCache.CacheEnabled); }
public ProxyCache_Brain_DefaultMode(ProxyCache _proxyCache) { this.proxyCache = _proxyCache; }
private static void DoHttpProcessing(TcpClient client) { Stream clientStream = client.GetStream(); Stream outStream = clientStream; //use this stream for writing out - may change if we use ssl SslStream sslStream = new SslStream(clientStream); CacheEntry cacheEntry = null; MemoryStream cacheStream = null; StreamReader clientStreamReader = null; try { sslStream.AuthenticateAsServer(_certificate, false, SslProtocols.Tls, true); clientStreamReader = new StreamReader(sslStream); String httpCmd = clientStreamReader.ReadLine(); //now we need to decrypt https if (String.IsNullOrEmpty(httpCmd)) { clientStreamReader.Close(); clientStream.Close(); return; } //break up the line into three components String[] splitBuffer = httpCmd.Split(spaceSplit, 3); String method = splitBuffer[0]; String remoteUri = splitBuffer[1]; remoteUri = "https://" + remoteUri.Substring((6)); Version version = new Version(1, 0); HttpWebRequest webReq; HttpWebResponse response = null; if (!(splitBuffer[0].ToUpper() == "POST" || splitBuffer[0].ToUpper() == "GET")) { //Browser wants to create a secure tunnel //instead = we are going to perform a man in the middle "attack" //the user's browser should warn them of the certification errors however. //Please note: THIS IS ONLY FOR TESTING PURPOSES - you are responsible for the use of this code remoteUri = "https://" + splitBuffer[1]; while (!String.IsNullOrEmpty(clientStreamReader.ReadLine())) { ; } StreamWriter connectStreamWriter = new StreamWriter(clientStream); connectStreamWriter.WriteLine("HTTP/1.0 403 Forbidden"); connectStreamWriter.WriteLine(String.Format("Timestamp: {0}", DateTime.Now.ToString())); connectStreamWriter.WriteLine("Proxy-agent: matt-dot-net"); connectStreamWriter.WriteLine(); connectStreamWriter.Flush(); /* * * sslStream = new SslStream(clientStream, false); * try * { * sslStream.AuthenticateAsServer(_certificate, false, SslProtocols.Tls | SslProtocols.Ssl3 | SslProtocols.Ssl2, true); * } * catch (Exception) * { * sslStream.Close(); * clientStreamReader.Close(); * connectStreamWriter.Close(); * clientStream.Close(); * return; * } * * //HTTPS server created - we can now decrypt the client's traffic * clientStream = sslStream; * clientStreamReader = new StreamReader(sslStream); * outStream = sslStream; * //read the new http command. * httpCmd = clientStreamReader.ReadLine(); * if (String.IsNullOrEmpty(httpCmd)) * { * clientStreamReader.Close(); * clientStream.Close(); * sslStream.Close(); * return; * } * splitBuffer = httpCmd.Split(spaceSplit, 3); * method = splitBuffer[0]; * remoteUri = remoteUri + splitBuffer[1]; */ } //construct the web request that we are going to issue on behalf of the client. webReq = (HttpWebRequest)HttpWebRequest.Create(remoteUri); webReq.Method = method; webReq.ProtocolVersion = version; //read the request headers from the client and copy them to our request int contentLen = ReadRequestHeaders(clientStreamReader, webReq); webReq.Proxy = null; webReq.KeepAlive = false; webReq.AllowAutoRedirect = false; webReq.AutomaticDecompression = DecompressionMethods.None; if (Server.DumpHeaders) { Console.WriteLine(String.Format("{0} {1} HTTP/{2}", webReq.Method, webReq.RequestUri.AbsoluteUri, webReq.ProtocolVersion)); DumpHeaderCollectionToConsole(webReq.Headers); } //using the completed request, check our cache if (method.ToUpper() == "GET") { cacheEntry = ProxyCache.GetData(webReq); } else if (method.ToUpper() == "POST") { char[] postBuffer = new char[contentLen]; int bytesRead; int totalBytesRead = 0; StreamWriter sw = new StreamWriter(webReq.GetRequestStream()); while (totalBytesRead < contentLen && (bytesRead = clientStreamReader.ReadBlock(postBuffer, 0, contentLen)) > 0) { totalBytesRead += bytesRead; sw.Write(postBuffer, 0, bytesRead); if (ProxyServer.Server.DumpPostData) { Console.Write(postBuffer, 0, bytesRead); } } if (Server.DumpPostData) { Console.WriteLine(); Console.WriteLine(); } sw.Close(); } if (cacheEntry == null) { //Console.WriteLine(String.Format("ThreadID: {2} Requesting {0} on behalf of client {1}", webReq.RequestUri, client.Client.RemoteEndPoint.ToString(), Thread.CurrentThread.ManagedThreadId)); webReq.Timeout = 15000; try { response = (HttpWebResponse)webReq.GetResponse(); } catch (WebException webEx) { response = webEx.Response as HttpWebResponse; } if (response != null) { List <Tuple <String, String> > responseHeaders = ProcessResponse(response); StreamWriter myResponseWriter = new StreamWriter(outStream); Stream responseStream = response.GetResponseStream(); try { //send the response status and response headers WriteResponseStatus(response.StatusCode, response.StatusDescription, myResponseWriter); WriteResponseHeaders(myResponseWriter, responseHeaders); DateTime? expires = null; CacheEntry entry = null; Boolean canCache = (sslStream == null && ProxyCache.CanCache(response.Headers, ref expires)); if (canCache) { entry = ProxyCache.MakeEntry(webReq, response, responseHeaders, expires); if (response.ContentLength > 0) { cacheStream = new MemoryStream(entry.ResponseBytes); } } Byte[] buffer; if (response.ContentLength > 0) { buffer = new Byte[response.ContentLength]; } else { buffer = new Byte[BUFFER_SIZE]; } int bytesRead; while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0) { if (cacheStream != null) { cacheStream.Write(buffer, 0, bytesRead); } outStream.Write(buffer, 0, bytesRead); if (Server.DumpResponseData) { Console.Write(UTF8Encoding.UTF8.GetString(buffer, 0, bytesRead)); } } if (Server.DumpResponseData) { Console.WriteLine(); Console.WriteLine(); } responseStream.Close(); if (cacheStream != null) { cacheStream.Flush(); cacheStream.Close(); } outStream.Flush(); if (canCache) { ProxyCache.AddData(entry); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { responseStream.Close(); response.Close(); myResponseWriter.Close(); } } } else { //serve from cache StreamWriter myResponseWriter = new StreamWriter(outStream); try { WriteResponseStatus(cacheEntry.StatusCode, cacheEntry.StatusDescription, myResponseWriter); WriteResponseHeaders(myResponseWriter, cacheEntry.Headers); if (cacheEntry.ResponseBytes != null) { outStream.Write(cacheEntry.ResponseBytes, 0, cacheEntry.ResponseBytes.Length); if (ProxyServer.Server.DumpResponseData) { Console.Write(UTF8Encoding.UTF8.GetString(cacheEntry.ResponseBytes)); } } myResponseWriter.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { myResponseWriter.Close(); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (Server.DumpHeaders || Server.DumpPostData || Server.DumpResponseData) { //release the lock Monitor.Exit(_outputLockObj); } clientStreamReader.Close(); clientStream.Close(); if (sslStream != null) { sslStream.Close(); } outStream.Close(); if (cacheStream != null) { cacheStream.Close(); } } }