| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 | /*CAsyncProxySocketLayer by Tim Kosse ([email protected])                 Version 1.6 (2003-03-26)--------------------------------------------------------Introduction:-------------This class is layer class for CAsyncSocketEx. With this class youcan connect through SOCKS4/5 and HTTP 1.1 proxies. This class worksas semi-transparent layer between CAsyncSocketEx and the actual socket.This class is used in FileZilla, a powerful open-source FTP client.It can be found under https://sourceforge.net/projects/filezilla/For more information about HTTP 1.1 goto https://tools.ietf.org/html/rfc2616How to use?-----------You don't have to change much in you already existing code to useCAsyncProxySocketLayer.To use it, create an instance of CAsyncProxySocketLayer, call SetProxyand attach it to a CAsyncSocketEx instance.You have to process OnLayerCallback in you CAsyncSocketEx instance as it willreceive all layer nofications.The following notifications are sent://Error codesPROXYERROR_NOERROR 0PROXYERROR_NOCONN 1 //Can't connect to proxy server, use GetLastError for more informationPROXYERROR_REQUESTFAILED 2 //Request failed, can't send dataPROXYERROR_AUTHREQUIRED 3 //Authentication requiredPROXYERROR_AUTHTYPEUNKNOWN 4 //Authtype unknown or not supportedPROXYERROR_AUTHFAILED 5  //Authentication failedPROXYERROR_AUTHNOLOGON 6PROXYERROR_CANTRESOLVEHOST 7//Status messagesPROXYSTATUS_LISTENSOCKETCREATED 8 //Called when a listen socket was created successfully. Unlike the normal listen function,                //a socksified socket has to connect to the proxy to negotiate the details with the server                //on which the listen socket will be created                //The two parameters will contain the ip and port of the listen socket on the server.Description of important functions and their parameters:--------------------------------------------------------void SetProxy(int nProxyType, const char * ProxyHost, int nProxyPort);void SetProxy(int nProxyType, const char *, int nProxyPort, const char * ProxyUser, const char * ProxyPass);Call one of this functions to set the proxy type.Parametes:- nProxyType specifies the Proxy Type.- ProxyHost and nProxyPort specify the address of the proxy- ProxyUser and ProxyPass are only available for SOCKS5 proxies.supported proxy types:PROXYTYPE_NOPROXYPROXYTYPE_SOCKS4PROXYTYPE_SOCKS4APROXYTYPE_SOCKS5PROXYTYPE_HTTP11There are also some other functions:GetProxyPeerNameLike GetPeerName of CAsyncSocket, but returns the address of theserver connected through the proxy.  If using proxies, GetPeerNameonly returns the address of the proxy.int GetProxyType();Returns the used proxyconst int GetLastProxyError() const;Returns the last proxy errorLicense-------Feel free to use this class, as long as you don't claim that you wrote itand this copyright notice stays intact in the source files.If you use this class in commercial applications, please send a short messageto [email protected]*///---------------------------------------------------------------------------#ifndef AsyncProxySocketLayerH#define AsyncProxySocketLayerH//---------------------------------------------------------------------------#include "AsyncSocketExLayer.h"//---------------------------------------------------------------------------class CAsyncProxySocketLayer : public CAsyncSocketExLayer{public:public:  CAsyncProxySocketLayer();  virtual ~CAsyncProxySocketLayer();public:  virtual void Close();  virtual BOOL Connect(LPCTSTR lpHostAddress, UINT nHostPort);  virtual BOOL Connect(const SOCKADDR * lpSockAddr, int nSockAddrLen);  virtual BOOL Listen(int nConnectionBacklog);  // Sets the proxy details.  // nProxyType - Type of the proxy. May be PROXYTYPE_NONE, PROXYTYPE_SOCKS4, PROXYTYPE_SOCKS5 or PROXYTYPE_HTTP11  // ProxyHost - The address of the proxy. Can be either IP or URL  // ProxyPort - The port of the proxy  // ProxyUser - the username for SOCKS5 proxies  // ProxyPass - the password for SOCKS5 proxies  void SetProxy(int nProxyType, const char * pProxyHost, int ProxyPort, bool bUseLogon, const char * pProxyUser, const char * pProxyPass);  // Returns the address of the server behind the SOCKS proxy you are connected to  virtual BOOL GetPeerName(CString & rPeerAddress, UINT & rPeerPort);  virtual BOOL GetPeerName(SOCKADDR * lpSockAddr, int * lpSockAddrLen);protected:  virtual BOOL Accept(CAsyncSocketEx & rConnectedSocket, SOCKADDR * lpSockAddr = NULL, int * lpSockAddrLen = NULL);  virtual void OnReceive(int nErrorCode);  virtual void OnConnect(int nErrorCode);  virtual int Send(const void * lpBuf, int nBufLen, int nFlags = 0);  virtual int Receive(void * lpBuf, int nBufLen, int nFlags = 0);private:  void Reset();  void ClearBuffer();    // Clears the receive buffer  void ConnectionEstablished();  void ConnectionFailed(int nErrorCode, char * Str = NULL);  char *m_pRecvBuffer;  // The receive buffer  int m_nRecvBufferLen;  // Length of the RecvBuffer  int m_nRecvBufferPos;  // Position within the receive buffer  char *m_pStrBuffer;    // Recvbuffer needed by HTTP1.1 proxy  int m_nProxyOpState;  // State of an operation  int m_nProxyOpID;    // Currently active operation (0 if none)  int m_nProxyPeerPort;  // Port of the server you are connected to, retrieve via GetPeerName  ULONG m_nProxyPeerIp;  // IP of the server you are connected to, retrieve via GetPeerName  typedef struct  {    int nProxyType;    char * pProxyHost;    int nProxyPort;    char * pProxyUser;    char * pProxyPass;    BOOL bUseLogon;  } t_proxydata; // This structure will be used to hold the proxy details  t_proxydata m_ProxyData; // Structure to hold the data set by SetProxy  char * m_pProxyPeerHost; // The host connected to};//---------------------------------------------------------------------------// Errorcodes#define PROXYERROR_NOERROR 0#define PROXYERROR_NOCONN 1 //Can't connect to proxy server, use GetLastError for more information#define PROXYERROR_REQUESTFAILED 2 //Request failed, can't send data#define PROXYERROR_AUTHREQUIRED 3 //Authentication required#define PROXYERROR_AUTHTYPEUNKNOWN 4 //Authtype unknown or not supported#define PROXYERROR_AUTHFAILED 5  //Authentication failed#define PROXYERROR_AUTHNOLOGON 6#define PROXYERROR_CANTRESOLVEHOST 7//---------------------------------------------------------------------------// Status messages// Called when a listen socket was created successfully. Unlike the normal listen function,// a socksified socket has to connect to the proxy to negotiate the details with the server// on which the listen socket will be created// The two parameters will contain the ip and port of the listen socket on the server.#define PROXYSTATUS_LISTENSOCKETCREATED 8struct t_ListenSocketCreatedStruct{  unsigned long ip;  UINT nPort;};//---------------------------------------------------------------------------// Proxytypes#define PROXYTYPE_NOPROXY 0#define PROXYTYPE_SOCKS4 1#define PROXYTYPE_SOCKS4A 2#define PROXYTYPE_SOCKS5 3#define PROXYTYPE_HTTP11 4//---------------------------------------------------------------------------#define PROXYOP_CONNECT 1#define PROXYOP_LISTEN 2//---------------------------------------------------------------------------#endif // AsyncProxySocketLayerH
 |