AsyncProxySocketLayer.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*CAsyncProxySocketLayer by Tim Kosse ([email protected])
  2. Version 1.6 (2003-03-26)
  3. --------------------------------------------------------
  4. Introduction:
  5. -------------
  6. This class is layer class for CAsyncSocketEx. With this class you
  7. can connect through SOCKS4/5 and HTTP 1.1 proxies. This class works
  8. as semi-transparent layer between CAsyncSocketEx and the actual socket.
  9. This class is used in FileZilla, a powerful open-source FTP client.
  10. It can be found under http://sourceforge.net/projects/filezilla
  11. For more information about SOCKS4/5 goto
  12. http://www.socks.nec.com/socksprot.html
  13. For more information about HTTP 1.1 goto http://www.rfc-editor.org
  14. and search for RFC2616
  15. How to use?
  16. -----------
  17. You don't have to change much in you already existing code to use
  18. CAsyncProxySocketLayer.
  19. To use it, create an instance of CAsyncProxySocketLayer, call SetProxy
  20. and attach it to a CAsyncSocketEx instance.
  21. You have to process OnLayerCallback in you CAsyncSocketEx instance as it will
  22. receive all layer nofications.
  23. The following notifications are sent:
  24. //Error codes
  25. PROXYERROR_NOERROR 0
  26. PROXYERROR_NOCONN 1 //Can't connect to proxy server, use GetLastError for more information
  27. PROXYERROR_REQUESTFAILED 2 //Request failed, can't send data
  28. PROXYERROR_AUTHREQUIRED 3 //Authentication required
  29. PROXYERROR_AUTHTYPEUNKNOWN 4 //Authtype unknown or not supported
  30. PROXYERROR_AUTHFAILED 5 //Authentication failed
  31. PROXYERROR_AUTHNOLOGON 6
  32. PROXYERROR_CANTRESOLVEHOST 7
  33. //Status messages
  34. PROXYSTATUS_LISTENSOCKETCREATED 8 //Called when a listen socket was created successfully. Unlike the normal listen function,
  35. //a socksified socket has to connect to the proxy to negotiate the details with the server
  36. //on which the listen socket will be created
  37. //The two parameters will contain the ip and port of the listen socket on the server.
  38. If you want to use CAsyncProxySocketLayer to create a listen socket, you
  39. have to use this overloaded function:
  40. BOOL PrepareListen(unsigned long serverIp);
  41. serverIP is the IP of the server you are already connected
  42. through the SOCKS proxy. You can't use listen sockets over a
  43. SOCKS proxy without a primary connection. Listen sockets are only
  44. supported by SOCKS proxies, this won't work with HTTP proxies.
  45. When the listen socket is created successfully, the PROXYSTATUS_LISTENSOCKETCREATED
  46. notification is sent. The parameters will tell you the ip and the port of the listen socket.
  47. After it you have to handle the OnAccept message and accept the
  48. connection.
  49. Be carful when calling Accept: rConnected socket will NOT be filled! Instead use the instance which created the
  50. listen socket, it will handle the data connection.
  51. If you want to accept more than one connection, you have to create a listing socket for each of them!
  52. Description of important functions and their parameters:
  53. --------------------------------------------------------
  54. void SetProxy(int nProxyType);
  55. void SetProxy(int nProxyType, const char * ProxyHost, int nProxyPort);
  56. void SetProxy(int nProxyType, const char *, int nProxyPort, const char * ProxyUser, const char * ProxyPass);
  57. Call one of this functions to set the proxy type.
  58. Parametes:
  59. - nProxyType specifies the Proxy Type.
  60. - ProxyHost and nProxyPort specify the address of the proxy
  61. - ProxyUser and ProxyPass are only available for SOCKS5 proxies.
  62. supported proxy types:
  63. PROXYTYPE_NOPROXY
  64. PROXYTYPE_SOCKS4
  65. PROXYTYPE_SOCKS4A
  66. PROXYTYPE_SOCKS5
  67. PROXYTYPE_HTTP11
  68. There are also some other functions:
  69. GetProxyPeerName
  70. Like GetPeerName of CAsyncSocket, but returns the address of the
  71. server connected through the proxy. If using proxies, GetPeerName
  72. only returns the address of the proxy.
  73. int GetProxyType();
  74. Returns the used proxy
  75. const int GetLastProxyError() const;
  76. Returns the last proxy error
  77. License
  78. -------
  79. Feel free to use this class, as long as you don't claim that you wrote it
  80. and this copyright notice stays intact in the source files.
  81. If you use this class in commercial applications, please send a short message
  82. to [email protected]
  83. */
  84. //---------------------------------------------------------------------------
  85. #ifndef AsyncProxySocketLayerH
  86. #define AsyncProxySocketLayerH
  87. //---------------------------------------------------------------------------
  88. #include "AsyncSocketExLayer.h"
  89. //---------------------------------------------------------------------------
  90. class CAsyncProxySocketLayer : public CAsyncSocketExLayer
  91. {
  92. public:
  93. public:
  94. CAsyncProxySocketLayer();
  95. virtual ~CAsyncProxySocketLayer();
  96. public:
  97. virtual void Close();
  98. virtual BOOL Connect(LPCTSTR lpHostAddress, UINT nHostPort);
  99. virtual BOOL Connect(const SOCKADDR * lpSockAddr, int nSockAddrLen);
  100. virtual BOOL Listen(int nConnectionBacklog);
  101. void SetProxy(int nProxyType); // Only PROXYTYPE_NOPROXY
  102. void SetProxy(int nProxyType, const char * pProxyHost, int ProxyPort); // May not be PROXYTYPE_NOPROXY
  103. // Sets the proxy details.
  104. // nProxyType - Type of the proxy. May be PROXYTYPE_NONE, PROXYTYPE_SOCKS4, PROXYTYPE_SOCKS5 or PROXYTYPE_HTTP11
  105. // ProxyHost - The address of the proxy. Can be either IP or URL
  106. // ProxyPort - The port of the proxy
  107. // ProxyUser - the username for SOCKS5 proxies
  108. // ProxyPass - the password for SOCKS5 proxies
  109. void SetProxy(int nProxyType, const char * pProxyHost, int ProxyPort, const char * pProxyUser, const char * pProxyPass); // Only SOCKS5 and HTTP1.1 proxies
  110. // Prepare listen
  111. BOOL PrepareListen(unsigned long ip);
  112. int GetProxyType() const;
  113. // Returns the type of the proxy
  114. // Returns the address of the server behind the SOCKS proxy you are connected to
  115. virtual BOOL GetPeerName(CString & rPeerAddress, UINT & rPeerPort);
  116. virtual BOOL GetPeerName(SOCKADDR * lpSockAddr, int * lpSockAddrLen);
  117. protected:
  118. virtual BOOL Accept(CAsyncSocketEx & rConnectedSocket, SOCKADDR * lpSockAddr = NULL, int * lpSockAddrLen = NULL);
  119. virtual void OnReceive(int nErrorCode);
  120. virtual void OnConnect(int nErrorCode);
  121. virtual int Send(const void * lpBuf, int nBufLen, int nFlags = 0);
  122. virtual int Receive(void * lpBuf, int nBufLen, int nFlags = 0);
  123. private:
  124. void Reset();
  125. void ClearBuffer(); // Clears the receive buffer
  126. char *m_pRecvBuffer; // The receive buffer
  127. int m_nRecvBufferLen; // Length of the RecvBuffer
  128. int m_nRecvBufferPos; // Position within the receive buffer
  129. char *m_pStrBuffer; // Recvbuffer needed by HTTP1.1 proxy
  130. int m_nProxyOpState; // State of an operation
  131. int m_nProxyOpID; // Currently active operation (0 if none)
  132. int m_nProxyPeerPort; // Port of the server you are connected to, retrieve via GetPeerName
  133. ULONG m_nProxyPeerIp; // IP of the server you are connected to, retrieve via GetPeerName
  134. typedef struct
  135. {
  136. int nProxyType;
  137. char * pProxyHost;
  138. int nProxyPort;
  139. char * pProxyUser;
  140. char * pProxyPass;
  141. BOOL bUseLogon;
  142. } t_proxydata; // This structure will be used to hold the proxy details
  143. t_proxydata m_ProxyData; // Structure to hold the data set by SetProxy
  144. char * m_pProxyPeerHost; // The host connected to
  145. };
  146. //---------------------------------------------------------------------------
  147. // Errorcodes
  148. #define PROXYERROR_NOERROR 0
  149. #define PROXYERROR_NOCONN 1 //Can't connect to proxy server, use GetLastError for more information
  150. #define PROXYERROR_REQUESTFAILED 2 //Request failed, can't send data
  151. #define PROXYERROR_AUTHREQUIRED 3 //Authentication required
  152. #define PROXYERROR_AUTHTYPEUNKNOWN 4 //Authtype unknown or not supported
  153. #define PROXYERROR_AUTHFAILED 5 //Authentication failed
  154. #define PROXYERROR_AUTHNOLOGON 6
  155. #define PROXYERROR_CANTRESOLVEHOST 7
  156. //---------------------------------------------------------------------------
  157. // Status messages
  158. // Called when a listen socket was created successfully. Unlike the normal listen function,
  159. // a socksified socket has to connect to the proxy to negotiate the details with the server
  160. // on which the listen socket will be created
  161. // The two parameters will contain the ip and port of the listen socket on the server.
  162. #define PROXYSTATUS_LISTENSOCKETCREATED 8
  163. struct t_ListenSocketCreatedStruct
  164. {
  165. unsigned long ip;
  166. UINT nPort;
  167. };
  168. //---------------------------------------------------------------------------
  169. // Proxytypes
  170. #define PROXYTYPE_NOPROXY 0
  171. #define PROXYTYPE_SOCKS4 1
  172. #define PROXYTYPE_SOCKS4A 2
  173. #define PROXYTYPE_SOCKS5 3
  174. #define PROXYTYPE_HTTP11 4
  175. //---------------------------------------------------------------------------
  176. #define PROXYOP_CONNECT 1
  177. #define PROXYOP_LISTEN 2
  178. //---------------------------------------------------------------------------
  179. #endif // AsyncProxySocketLayerH