Note that there are some explanatory texts on larger screens.

plurals
  1. POC++/CLI HTTP Proxy problems
    primarykey
    data
    text
    <p>I'm trying(very hard) to make a small HTTP Proxy server which I can use to save all communications to a file. Seeing as I dont really have any experience in the area, I used a class from codeproject.com and some associated code to get started (It was made in the old CLI syntax, so I converted it). I couldn't get it working, so I added lots more code to make it work (threads etc), and now it sort of works. Basically, it recieves something from a client (I just configured Mozilla Firefox to route its connections through this proxy) and then routes it to google.com. After it sends Firefox's data to google, recieves a responce, and sends that to Firefox. This works fine, but then the proxy fails to recieve any data from Firefox. It just loops in the Sleep(50) section. Anyway, heres the code:</p> <p><strong>ProxyTest.cpp:</strong></p> <pre><code>#include "stdafx.h" #include "windows.h" #include "CHTTPProxy.h" public ref class ClientThread { public: System::Net::Sockets::TcpClient ^ pClient; CHttpProxy ^ pProxy; System::Int32 ^ pRecieveBufferSize; System::Threading::Thread ^ Thread; ClientThread(System::Net::Sockets::TcpClient ^ sClient, CHttpProxy ^ sProxy, System::Int32 ^ sRecieveBufferSize) { pClient = sClient; pProxy = sProxy; pRecieveBufferSize = sRecieveBufferSize; }; void StartReading() { Thread = gcnew System::Threading::Thread(gcnew System::Threading::ThreadStart(this,&amp;ClientThread::ThreadEntryPoint)); Thread-&gt;Start(); }; void ThreadEntryPoint() { char * bytess; bytess = new char[(int)pRecieveBufferSize]; memset(bytess, 0, (int)pRecieveBufferSize); array&lt;unsigned char&gt; ^ bytes = gcnew array&lt;unsigned char&gt;((int)pRecieveBufferSize); array&lt;unsigned char&gt; ^ sendbytes; do { if (pClient-&gt;GetStream()-&gt;DataAvailable) { try { do { Sleep(100); //Lets wait for whole packet to get cached (If it even does...) unsigned int k = pClient-&gt;GetStream()-&gt;Read(bytes, 0, (int)pRecieveBufferSize); //Read it for (unsigned int i=0; i&lt;(int)pRecieveBufferSize; i++) bytess[i] = bytes[i]; Console::WriteLine("Packet Received:\n"+gcnew System::String(bytess)); pProxy-&gt;SendToServer(bytes,pClient-&gt;GetStream()); //Now send it to google! } while (pClient-&gt;GetStream()-&gt;DataAvailable); } catch (Exception ^ e) { break; } } else { Sleep(50); if (!(pClient-&gt;Connected)) break; }; } while (pClient-&gt;GetStream()-&gt;CanRead); delete [] bytess; pClient-&gt;Close(); }; }; int main(array&lt;System::String ^&gt; ^args) { System::Collections::Generic::Stack&lt;ClientThread ^&gt; ^ Clients = gcnew System::Collections::Generic::Stack&lt;ClientThread ^&gt;(); System::Net::Sockets::TcpListener ^ pTcpListener = gcnew System::Net::Sockets::TcpListener(8080); pTcpListener-&gt;Start(); System::Net::Sockets::TcpClient ^ pTcpClient; while (1) { pTcpClient = pTcpListener-&gt;AcceptTcpClient(); //Wait for client ClientThread ^ Client = gcnew ClientThread(pTcpClient, gcnew CHttpProxy("www.google.com.au", 80), pTcpClient-&gt;ReceiveBufferSize); //Make a new object for this client Client-&gt;StartReading(); //Start the thread Clients-&gt;Push(Client); //Add it to the list }; pTcpListener-&gt;Stop(); return 0; } </code></pre> <p><strong>CHTTPProxy.h</strong>, from <a href="http://www.codeproject.com/KB/IP/howtoproxy.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/IP/howtoproxy.aspx</a> with a lot of modifications:</p> <pre><code>#using &lt;mscorlib.dll&gt; #using &lt;SYSTEM.DLL&gt; using namespace System; using System::Net::Sockets::TcpClient; using System::String; using System::Exception; using System::Net::Sockets::NetworkStream; #include &lt;stdio.h&gt; ref class CHttpProxy { public: CHttpProxy(System::String ^ szHost, int port); System::String ^ m_host; int m_port; void SendToServer(array&lt;unsigned char&gt; ^ Packet, System::Net::Sockets::NetworkStream ^ sendstr); }; CHttpProxy::CHttpProxy(System::String ^ szHost, int port) { m_host = gcnew System::String(szHost); m_port = port; } void CHttpProxy::SendToServer(array&lt;unsigned char&gt; ^ Packet, System::Net::Sockets::NetworkStream ^ sendstr) { TcpClient ^ tcpclnt = gcnew TcpClient(); try { tcpclnt-&gt;Connect(m_host,m_port); } catch (Exception ^ e ) { Console::WriteLine(e-&gt;ToString()); return; } // Send it if ( tcpclnt ) { NetworkStream ^ networkStream; networkStream = tcpclnt-&gt;GetStream(); int size = Packet-&gt;Length; networkStream-&gt;Write(Packet, 0, size); array&lt;unsigned char&gt; ^ bytes = gcnew array&lt;unsigned char&gt;(tcpclnt-&gt;ReceiveBufferSize); char * bytess = new char[tcpclnt-&gt;ReceiveBufferSize]; Sleep(500); //Wait for responce do { unsigned int k = networkStream-&gt;Read(bytes, 0, (int)tcpclnt-&gt;ReceiveBufferSize); //Read from google for(unsigned int i=0; i&lt;k; i++) { bytess[i] = bytes[i]; if (bytess[i] == 0) bytess[i] = ' '; //Dont terminate the string if (bytess[i] &lt; 8) bytess[i] = ' '; //Somethings making the computer beep, and its not 7?!?! }; Console::WriteLine("\n\nAbove packet sent to google. Google Packet Received:\n"+gcnew System::String(bytess)); sendstr-&gt;Write(bytes,0,k); //Send it to mozilla Console::WriteLine("\n\nAbove packet sent to client..."); //Sleep(1000); } while(networkStream-&gt;DataAvailable); delete [] bytess; } return; } </code></pre> <p>Any help would be much appreciated, I've tried for hours. (Sorry about the indents nobugz, its fixed now)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload