AsyncProxySocketLayer.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Description of important functions and their parameters:
  39. --------------------------------------------------------
  40. void SetProxy(int nProxyType, const char * ProxyHost, int nProxyPort);
  41. void SetProxy(int nProxyType, const char *, int nProxyPort, const char * ProxyUser, const char * ProxyPass);
  42. Call one of this functions to set the proxy type.
  43. Parametes:
  44. - nProxyType specifies the Proxy Type.
  45. - ProxyHost and nProxyPort specify the address of the proxy
  46. - ProxyUser and ProxyPass are only available for SOCKS5 proxies.
  47. supported proxy types:
  48. PROXYTYPE_NOPROXY
  49. PROXYTYPE_SOCKS4
  50. PROXYTYPE_SOCKS4A
  51. PROXYTYPE_SOCKS5
  52. PROXYTYPE_HTTP11
  53. There are also some other functions:
  54. GetProxyPeerName
  55. Like GetPeerName of CAsyncSocket, but returns the address of the
  56. server connected through the proxy. If using proxies, GetPeerName
  57. only returns the address of the proxy.
  58. int GetProxyType();
  59. Returns the used proxy
  60. const int GetLastProxyError() const;
  61. Returns the last proxy error
  62. License
  63. -------
  64. Feel free to use this class, as long as you don't claim that you wrote it
  65. and this copyright notice stays intact in the source files.
  66. If you use this class in commercial applications, please send a short message
  67. to [email protected]
  68. */
  69. //---------------------------------------------------------------------------
  70. #ifndef AsyncProxySocketLayerH
  71. #define AsyncProxySocketLayerH
  72. //---------------------------------------------------------------------------
  73. #include "AsyncSocketExLayer.h"
  74. //---------------------------------------------------------------------------
  75. class CAsyncProxySocketLayer : public CAsyncSocketExLayer
  76. {
  77. public:
  78. public:
  79. CAsyncProxySocketLayer();
  80. virtual ~CAsyncProxySocketLayer();
  81. public:
  82. virtual void Close();
  83. virtual BOOL Connect(LPCTSTR lpHostAddress, UINT nHostPort);
  84. virtual BOOL Connect(const SOCKADDR * lpSockAddr, int nSockAddrLen);
  85. virtual BOOL Listen(int nConnectionBacklog);
  86. // Sets the proxy details.
  87. // nProxyType - Type of the proxy. May be PROXYTYPE_NONE, PROXYTYPE_SOCKS4, PROXYTYPE_SOCKS5 or PROXYTYPE_HTTP11
  88. // ProxyHost - The address of the proxy. Can be either IP or URL
  89. // ProxyPort - The port of the proxy
  90. // ProxyUser - the username for SOCKS5 proxies
  91. // ProxyPass - the password for SOCKS5 proxies
  92. void SetProxy(int nProxyType, const char * pProxyHost, int ProxyPort, bool bUseLogon, const char * pProxyUser, const char * pProxyPass);
  93. // Returns the address of the server behind the SOCKS proxy you are connected to
  94. virtual BOOL GetPeerName(CString & rPeerAddress, UINT & rPeerPort);
  95. virtual BOOL GetPeerName(SOCKADDR * lpSockAddr, int * lpSockAddrLen);
  96. protected:
  97. virtual BOOL Accept(CAsyncSocketEx & rConnectedSocket, SOCKADDR * lpSockAddr = NULL, int * lpSockAddrLen = NULL);
  98. virtual void OnReceive(int nErrorCode);
  99. virtual void OnConnect(int nErrorCode);
  100. virtual int Send(const void * lpBuf, int nBufLen, int nFlags = 0);
  101. virtual int Receive(void * lpBuf, int nBufLen, int nFlags = 0);
  102. private:
  103. void Reset();
  104. void ClearBuffer(); // Clears the receive buffer
  105. void ConnectionEstablished();
  106. char *m_pRecvBuffer; // The receive buffer
  107. int m_nRecvBufferLen; // Length of the RecvBuffer
  108. int m_nRecvBufferPos; // Position within the receive buffer
  109. char *m_pStrBuffer; // Recvbuffer needed by HTTP1.1 proxy
  110. int m_nProxyOpState; // State of an operation
  111. int m_nProxyOpID; // Currently active operation (0 if none)
  112. int m_nProxyPeerPort; // Port of the server you are connected to, retrieve via GetPeerName
  113. ULONG m_nProxyPeerIp; // IP of the server you are connected to, retrieve via GetPeerName
  114. typedef struct
  115. {
  116. int nProxyType;
  117. char * pProxyHost;
  118. int nProxyPort;
  119. char * pProxyUser;
  120. char * pProxyPass;
  121. BOOL bUseLogon;
  122. } t_proxydata; // This structure will be used to hold the proxy details
  123. t_proxydata m_ProxyData; // Structure to hold the data set by SetProxy
  124. char * m_pProxyPeerHost; // The host connected to
  125. };
  126. //---------------------------------------------------------------------------
  127. // Errorcodes
  128. #define PROXYERROR_NOERROR 0
  129. #define PROXYERROR_NOCONN 1 //Can't connect to proxy server, use GetLastError for more information
  130. #define PROXYERROR_REQUESTFAILED 2 //Request failed, can't send data
  131. #define PROXYERROR_AUTHREQUIRED 3 //Authentication required
  132. #define PROXYERROR_AUTHTYPEUNKNOWN 4 //Authtype unknown or not supported
  133. #define PROXYERROR_AUTHFAILED 5 //Authentication failed
  134. #define PROXYERROR_AUTHNOLOGON 6
  135. #define PROXYERROR_CANTRESOLVEHOST 7
  136. //---------------------------------------------------------------------------
  137. // Status messages
  138. // Called when a listen socket was created successfully. Unlike the normal listen function,
  139. // a socksified socket has to connect to the proxy to negotiate the details with the server
  140. // on which the listen socket will be created
  141. // The two parameters will contain the ip and port of the listen socket on the server.
  142. #define PROXYSTATUS_LISTENSOCKETCREATED 8
  143. struct t_ListenSocketCreatedStruct
  144. {
  145. unsigned long ip;
  146. UINT nPort;
  147. };
  148. //---------------------------------------------------------------------------
  149. // Proxytypes
  150. #define PROXYTYPE_NOPROXY 0
  151. #define PROXYTYPE_SOCKS4 1
  152. #define PROXYTYPE_SOCKS4A 2
  153. #define PROXYTYPE_SOCKS5 3
  154. #define PROXYTYPE_HTTP11 4
  155. //---------------------------------------------------------------------------
  156. #define PROXYOP_CONNECT 1
  157. #define PROXYOP_LISTEN 2
  158. //---------------------------------------------------------------------------
  159. #endif // AsyncProxySocketLayerH