Прокси сервер:
Hi. I write http proxy using http listener. REquest from browser goes through HttpListener and then through php gate.
My Proxy Server:
namespace HtppListenerWebServer { class Program { static void Main(string[] args) { if (!HttpListener.IsSupported) { //Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); return; } HttpListener listener = new HttpListener(); listener.Prefixes.Add("http://*:8561/"); while (true) { listener.Start(); IAsyncResult result = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); Console.WriteLine("Waiting for request to be processed asyncronously."); result.AsyncWaitHandle.WaitOne(); Console.WriteLine("Request processed asyncronously."); } listener.Close(); } public static void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener)result.AsyncState; HttpListenerContext context = listener.EndGetContext(result); HttpListenerRequest request = context.Request; System.Collections.Specialized.NameValueCollection headersCollection = request.Headers; string headers = ""; foreach (string key in headersCollection.AllKeys) { string[] values = headersCollection.GetValues(key); headers += key + ": " + values[0] + "\r\n"; } headers = request.HttpMethod + " " + request.RawUrl + " HTTP/1.0\r\n" + headers + "\r\n"; HttpWebRequest werequest = WebRequest.Create("http://phpProxy1") as HttpWebRequest; werequest.Method = "POST"; werequest.ContentType = "application/x-www-form-urlencoded"; string post_data = "host=" + Program.EncodeTo64(request.UserHostName) + "&Data=" + Program.EncodeTo64(headers); byte[] byteArray = System.Text.ASCIIEncoding.UTF8.GetBytes(post_data); werequest.ContentLength = byteArray.Length; Stream dataStream = werequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)werequest.GetResponse(); using (Stream receiveStream = response.GetResponseStream()) { HttpListenerResponse responseOut = context.Response; responseOut.ContentLength64 = response.ContentLength >= 0 ?response.ContentLength : 0; int bytesCopied = CopyStream(receiveStream, responseOut.OutputStream); responseOut.OutputStream.Close(); Console.WriteLine("Copied {0} bytes", bytesCopied); } } static public string EncodeTo64(string toEncode) { byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode); string returnValue = System.Convert.ToBase64String(toEncodeAsBytes); return returnValue; } public static int CopyStream(Stream input, Stream output) { byte[] buffer = new byte[32768]; int bytesWritten = 0; while (true) { int read = input.Read(buffer, 0, buffer.Length); if (read <= 0) break; output.Write(buffer, 0, read); bytesWritten += read; } return bytesWritten; } } }
$host = base64_decode($_POST['host']); $Data = base64_decode($_POST['Data']); $stream = fopen('data://text/plain,' . $Data,'r'); $fsok = fsockopen($host, 80, $errno, $errstr, 20); $BuffLen = 4096; if($fsok) { while ($wbuffer = fread($stream , $BuffLen)) { fwrite($fsok, $wbuffer); } fflush($fsok); while ($rbuffer = fread($fsok, $BuffLen)) { echo $rbuffer; } } fflush($fsok); fclose($fsok);
Проблема в том, что юзая мой прокси, в браузере я получаю текстовый контент
HTTP/1.1 200 OK Date: Wed, 22 Feb 2012 11:22:41 GMT Server: Apache Set-Cookie: last_visit_time=1329909761; expires=Sat, 21-Feb-2015 11:22:41 GMT; path=/ Pragma: no-cache Cache-Control: no-cache, must-revalidate Expires: Sat, 22 Nov 2003 14:18:11 GMT X-robots-tag: noarchive Content-Length: 1578 Connection: close Content-Type: text/html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Documentation - System Favorites</title> <meta name="description" content="TrueCrypt - free open-source disk encryption - documentation - System Favorites"> <meta name="keywords" content="encryption, security"> <meta name="robots" content="noarchive"> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"> </head> <frameset rows="113,*" frameborder="no" border="1" framespacing="0" > <frame src="/navigation" name="frameTCDocsNavBar" scrolling="no" noresize > <frameset rows="*" cols="182,685,*" frameborder="no" border="1" bordercolor="#0099FF" > <frame src="/docs/toc" name="frameTCDocsTOC" scrolling="yes" noresize topmargin="0" leftmargin="0" marginheight="0" marginwidth="0"> <frame src="/docs/system-favorites" name="frameTCDocsMain" scrolling="yes" noresize marginheight="10" marginwidth="30"> <frame src="/docs/rightbar" name="frameTCDocsRightBar" scrolling="no" noresize marginheight="0" marginwidth="0"> </frameset> </frameset> </html>
Почему результат запроса отображается как текст ? Кто-нибудь может помочь?/frameset