zoukankan      html  css  js  c++  java
  • HTTP Tunneling (HTTP Proxy Socket Client)

    HTTP Tunneling (HTTP Proxy Socket Client)


    HTTP Tunneling

    HTTP is a text-based protocol to retreive Web pages through a Web browser. Mostly if you are on a LAN connection, you are behind a proxy server; this proxy server has one HTTP proxy running on some defined port. In your Internet Explorer's Connection option, you specify LAN settings as required. This proxy server is definitely running on a text-based protocol and you can only get HTTP-related data from the outside network, right!! Well, there is a small loophole from which you can go through HTTP and connect to the outside world and get any data you want in binary protocol, or even your own protocol. It's through HTTPS.

    HTTPS Explanation

    In HTTPS, data is transferred from browser to server and server to browser in a secure manner. It's a binary protocol; when it goes through a proxy, the proxy doesn't understand anything. The proxy just allows a binary stream to open and let both server and client exchange the data. Now, we can fool the proxy server and connect to any server and exchange data. The proxy server will think that we doing some secure HTTP session.

    For HTTPS, your browser connects to a proxy server and sends a command.

    1. CONNECT neurospeech.com:443 HTTP/1.0 <CR><LF>
    2. HOST neurospeech.com:443<CR><LF>
    3. [... other HTTP header lines ending with <CR><LF> if required]>
    4. <CR><LF> // Last Empty Line

    Then, the proxy server treats this as some HTTP Secure Session, and opens a binary stream to the required server and port as defined. If a connection established, the proxy server returns the following response:

    1. HTTP/1.0 200 Connection Established<CR><LF>
    2. [.... other HTTP header lines ending with <CR><LF>..
    3. ignore all of them]
    4. <CR><LF> // Last Empty Line

    Now, the browser is connected to the end server and can exchange data in both a binary and secure form.

    How to Do This

    Now, it's your program's turn to fool the proxy server and behave as Internet Explorer behaves for Secure HTTP.

    1. Connect to Proxy Server first.
    2. Issue CONNECT Host:Port HTTP/1.1<CR><LF>.
    3. Issue <CR><LF>.
    4. Wait for a line of response. If it contains HTTP/1.X 200 , the connection is successful.
    5. Read further lines of response until you receive an empty line.
    6. Now, you are connected to outside world through a proxy. Do any data exchange you want.

    Sample Source Code

    1. // You need to connect to mail.yahoo.com on port 25
    2. // Through a proxy on 192.0.1.1, on HTTP Proxy 4480
    3. // CSocketClient is Socket wrapping class
    4. // When you apply operator << on CString, it writes CString
    5. // To Socket ending with CRLF
    6. // When you apply operator >> on CString, it receives
    7. // a Line of response from socket until CRLF
    8.  
    9.  
    10. try
    11. {
    12. CString Request,Response;
    13. CSocketClient Client;
    14.  
    15. Client.ConnectTo("192.0.1.1",4480);
    16.  
    17. // Issue CONNECT Command
    18. Request = "CONNECT mail.yahoo.com:25 HTTP/1.0";
    19. Client<<Request;
    20.  
    21. // Issue empty line
    22. Request = "";
    23. Client<<Request;
    24.  
    25. // Receive Response From Server
    26. Client>>Response;
    27.  
    28. // Ignore HTTP Version
    29.  
    30. int n = Response.Find(' ');
    31. Response = Response.Mid(n+1);
    32.  
    33. // Http Response Must be 200 only
    34. if(Response.Left(3)!="200")
    35. {
    36. // Connection refused from HTTP Proxy Server
    37. AfxMessageBox(Response);
    38. }
    39.  
    40.  
    41. // Read Response Lines until you receive an empty line.
    42. do
    43. {
    44. Client>>Response;
    45. if (Response.IsEmpty())
    46. break;
    47. }while (true);
    48.  
    49.  
    50. // Coooooooool.... Now connected to mail.yahoo.com:25
    51. // Do further SMTP Protocol here..
    52.  
    53. }
    54. catch (CSocketException * pE)
    55. {
    56. pE->ReportError();
    57. }

    Library Source Code

    The Dns.h file contains all DNS-related source code. It uses other libraries, as SocketEx.h, SocketClient.h, and NeuroBuffer.h.

    CSocketEx

    Socket functions as a wrapper class. (CSocket is very heavy and unreliable if you don't have the exact idea of how it works.) All the functions are of same name as CSocket. You can use this class directly.

    CSocketClient

    Derived from CSocketEx and throws proper exceptions with details of Winsock errors. It defines two operators, >> and <<, for easy sending and receiving; it also changes network to host and host to network order of bytes if required.

    CHttpProxySocketClient

    Derived from CSocketClient, you can call the SetProxySettings(ProxyServer,Port) method and set proxy settings. Then, you can connect to the desired host and port as you need. The ConnectTo method is overridden and it automatically implements an HTTP proxy protocol and gives you a connection without any hassle.

    How to Use CHttpProxySocketClient

    1. // e.g. You need to connect to mail.yahoo.com on port 25
    2. // Through a proxy on 192.0.1.1, on HTTP Proxy 4480
    3. // CSocketClient is Socket wrapping class
    4. // When you apply operator << on CString, it writes CString
    5. // To Socket ending with CRLF
    6. // When you apply operator >> on CString, it receives
    7. // Line of response from socket until CRLF
    8. try
    9. {
    10. CHttpProxySocketClient Client;
    11.  
    12. Client.SetProxySettings("192.0.1.1",1979);
    13.  
    14. // Connect to server mail.yahoo.com on port 25
    15. Client.ConnectTo("mail.yahoo.com",25);
    16.  
    17. // You now have access to mail.yahoo.com on port 25
    18. // If you do not call SetProxySettings, then
    19. // you are connected to mail.yahoo.com directly if
    20. // you have direct access, so always use
    21. // CHttpProxySocketClient and no need to do any
    22. // extra coding.
    23.  
    24. }
    25. catch(CSocketException * pE) {
    26. pE->ReportError();
    27. }

    Note: I usually don't program in the form of .h and .cpp different files, because using them the next time somewhere else is a big problem because you must move both files here and there. So, I put all the code in my .h file only; I don't write to the .cpp file unless it's required. You need to copy only the SocketEx.h, SocketClient.h, and HttpProxySocket.h files into your project's directory, and add line

    1. #include "HttpProxySocket.h"

    after your

    1. #if !defined(.....

    and so forth code of your Visual Studio-generated file. If you put anything above this, you will get n number of errors.

    Downloads


    Download source - 17 Kb




  • 相关阅读:
    学习MSMQ笔记
    swfobject 2.0 使用(转)
    发现博客园的一个小问题
    4月10日
    NHibernate的一点思考
    最新手机号码正则表达式
    如何在页面完美显示版权符号(转)
    OpenGL由已知控制点绘制模拟曲面地形
    android自定义view[控件重用]时出现“No resource identifier found for attribute *** in package *** ”
    基于ARM的模拟器
  • 原文地址:https://www.cnblogs.com/lexus/p/2596836.html
Copyright © 2011-2022 走看看