Download source code - 2.1 Kb
Some time we need to establish TCP connection through proxy server, where our client host is behind proxy or firewall and deny the TCP connection to outside world. It just allows only HTTP connection. HTTP Tunnel is the method from which we can go through HTTP and connect to outside world as a TCP connection.
When we want create HTTP connection through proxy server, the client sends a request to the proxy server using HTTP protocol CONNECT method that it wants to establish a connection to some host (Destination) on some port. Now proxy server if allows tries to establish a connection to destination host by sending a request on behalf of client and receives the response which in turns send it back to client. If the negative response is received from destination it sends negative HTTP response to the client and closes the connection. And if the positive response is received, it sends a positive HTTP response to the client and the connection is considered to be established. Now the proxy server is acting as a Tunnel and dose not care what data is transferring between client and destination. Due to lack of time I have not tested it 100%, if you find any bug please email me so that I can rectify it.
HTTP protocol specifies the CONNECT method through which we can create a connection between client and destination. The client will first opens a connection with the proxy server. And after that it must send a CONNECT request in the form of HTTP request with some required parameter.
CONNECT <Destination ip>:<Destination Port> HTTP/1.0\nProxy-Authorization:Basic <\n><\n>
The proxy server process the request and tries to make the connection to
After that proxy server will send HTTP response, If it positive response that is with CODE=200 then it is considered that connection is established. And if response dose not include code=200 then connection is closed. Here user id and password should be in the form of base64 encoding, I have written another class to convert Proxy user id and password in base64 encoding format.
Usage of Code
There are two class file in this application httptunnel.cs and Base64Encoder.cs. you just need to add these classes in your application and call a method createHttpTunnel which will accept 5 parameter.
(string proxyipadd,string port,string struid,string strpwd,string destip,string destport)
proxyipadd – Proxy Ip.
port - Proxy Port.
struid - Proxy Uid.
strpwd - Proxy Pwd.
destip - Destination IP(e.g. 184.108.40.206).
destport - Destination Port..
Suppose you need to connect to 220.127.116.11 on port 25 where your proxy IP is 192.168.0.101 and proxy port is 808. you just need to create an object of HTTP_TUNNEL class nad call method createHttpTunnel.
HTTP_TUNNEL objHTTP_TUNNEL = new HTTP_TUNNEL();
Socket s = objHTTP_TUNNEL. createHttpTunnel("192.168.0.101", "808", "proxyid", "proxypwd", "18.104.22.168", "25");
In my next article we will discuss how to establish TCP connection using SOCKs proxy.
Dotnet developer working with software Development Company in India.
Achieved Master of Computer Application and Bachelor degree in commerce from Mumbai University, India.