AsyncProxySocketLayer.h 7.4 KB

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