sockspacket.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:7k
源码类别:

模拟服务器

开发平台:

C/C++

  1. // SocksPacket.h: interface for the CSocksPacket class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_SOCKSPACKET_H__04B153A0_80D4_442C_ACCE_0FA5C8ECF577__INCLUDED_)
  5. #define AFX_SOCKSPACKET_H__04B153A0_80D4_442C_ACCE_0FA5C8ECF577__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include "BufSocket.h" //需要CBufSocket类
  10. //常量定义
  11. #undef IN
  12. #define IN
  13. #undef OUT
  14. #define OUT
  15. static const BYTE BYTE_NULL = 0x00; // 空字节
  16. static const WORD WORD_NULL = 0x0000; // 空字
  17. static const DWORD UNKNOWN_IP = 0x10000000; // 特殊的IP地址,指明要SOCKS4A PROXY解析域名 0.0.0.1
  18. static const BYTE AUTH_OK = 0x00; // Username/Passwd验证OK
  19. static const DWORD SOP_SEND_TIMEOUT= 120*1000; //WSA_INFINITE;
  20. static const DWORD SOP_RECV_TIMEOUT= 120*1000; //WSA_INFINITE;
  21. // SOCKS的类型
  22. const int SOCKS_TYPE_4 = 0x01;
  23. const int SOCKS_TYPE_4A = 0x02;
  24. const int SOCKS_TYPE_5 = 0x03; 
  25. // SOCKS的版本号
  26. const BYTE VER_SOCKS4 = 0x04;
  27. const BYTE VER_SOCKS4A = 0x04;
  28. const BYTE VER_SOCKS5 = 0x05;
  29. const BYTE VER_SOCKSUNKNOWN= 0xFF;
  30. //所支持的命令
  31. const BYTE CMD_CONNECT = 0x01;
  32. const BYTE CMD_BIND = 0x02;
  33. const BYTE CMD_UDP = 0x03; // (SOCKS5 ONLY)
  34. const BYTE CMD_UNKNOWN = 0xFF;
  35. /* SOCKS4(A)错误(返回值) */
  36. const BYTE REP4_SUCCESS = 90;
  37. const BYTE REP4_RESULT = 90;
  38. const BYTE REP4_FAIL = 91;
  39. const BYTE REP4_NOIDENTD = 92;
  40. const BYTE REP4_BADID = 93;
  41. /* SOCKS5错误(返回值) */
  42. const BYTE REP5_SUCCESS = 0x00;
  43. const BYTE REP5_RESULT = 0x00;
  44. const BYTE REP5_FAIL = 0x01;
  45. const BYTE REP5_NOTALLOW = 0x02;
  46. const BYTE REP5_NETUNREACH = 0x03;
  47. const BYTE REP5_HOSTUNREACH= 0x04;
  48. const BYTE REP5_CONNREF = 0x05;
  49. const BYTE REP5_TTLEXP = 0x06;
  50. const BYTE REP5_BADCMD = 0x07;
  51. const BYTE REP5_BADADDR = 0x08;
  52. // 验证方式(SOCKS5 ONLY)
  53. const BYTE AUTH_NONE = 0x00;
  54. const BYTE AUTH_GSSAPI = 0x01;
  55. const BYTE AUTH_PASSWD = 0x02;
  56. const BYTE AUTH_CHAP = 0x03;
  57. const BYTE AUTH_UNKNOWN = 0xFF;
  58. // 地址方式(SOCKS5 ONLY)
  59. const BYTE ATYP_IPV4ADDR = 0x01;
  60. const BYTE ATYP_HOSTNAME = 0x03;
  61. const BYTE ATYP_IPV6ADDR = 0x04;
  62. // 包的类型
  63. const int PACKET_SOCKS4REQ = 0x01;
  64. const int PACKET_SOCKS4AREQ = 0x02;
  65. const int PACKET_SOCKS5AUTHREQ = 0x03;
  66. const int PACKET_SOCKS5AUTHPASSWDREQ = 0x04;
  67. const int PACKET_SOCKS5REQ = 0x05;
  68. const int PACKET_SOCKS4REP = 0x21;
  69. const int PACKET_SOCKS4AREP = 0x22;
  70. const int PACKET_SOCKS5AUTHREP = 0x23;
  71. const int PACKET_SOCKS5AUTHPASSWDREP = 0x24;
  72. const int PACKET_SOCKS5REP = 0x25;
  73. const int PACKET_SOCKS5UDP = 0x41;
  74. #pragma pack(push, 1)
  75. ///////////////////////////////////////////////////////
  76. //
  77. // SOCK4 Proxy
  78. //
  79. //////////////////////////////////////////////////////
  80. typedef struct _tagSocks4Req
  81. {
  82. BYTE byVer; // 版本号 0x04
  83. BYTE byCmd; // 命令
  84. WORD wDestPort; // 目标端口
  85. DWORD dwDestIP; // 目标IP地址
  86. LPCTSTR pszUserID; // 用户ID
  87. BYTE byNULL; // 0x00
  88. } SOCKS4REQ,*PSOCKS4REQ;
  89. typedef struct _tagSock4Rep
  90. {
  91. BYTE byVer;
  92. BYTE byRep; // 0x90-0x93
  93. WORD wDestPort;
  94. DWORD dwDestIP;
  95. } SOCK4REP,SOCKS4AREP,*PSOCK4REP,*PSOCK4AREP;
  96. ///////////////////////////////////////////////////////
  97. //
  98. // SOCK4A Proxy
  99. //
  100. //////////////////////////////////////////////////////
  101. typedef struct _tagSocks4AReq
  102. {
  103. BYTE byVer; // 版本号 0x04
  104. BYTE byCmd; // 命令
  105. WORD wDestPort; // 目标端口
  106. DWORD dwDestIP; // 目标机器IP地址 0x000000XX
  107. LPCTSTR pszUserID; // 用户ID
  108. BYTE byNULL1; // 分隔用
  109. LPCTSTR pszDestHostName;// 目标机器域名
  110. BYTE byNULL2; // 结束0x00
  111. } SOCKS4AREQ,*PSOCKS4AREQ;
  112. ///////////////////////////////////////////////////////
  113. //
  114. // SOCK5 Proxy
  115. //
  116. //////////////////////////////////////////////////////
  117. // Auth-Request
  118. typedef struct _tagSocks5AuthReq
  119. {
  120. BYTE byVer;
  121. BYTE byNAuth;
  122. LPCTSTR pszAuth;
  123. } SOCKS5AUTHREQ,*PSOCKS5AUTHREQ;
  124. // Auth-Reply
  125. typedef struct _tagSocks5AuthRep
  126. {
  127. BYTE byVer;
  128. BYTE byAuth;
  129. } SOCKS5AUTHREP,*PSOCKS5AUTHREP;
  130. // USERNAME/PASSWORD验证 - Request
  131. typedef struct _tagSocks5AuthPasswdReq
  132. {
  133. BYTE byVer;
  134. BYTE byUserNameLen;
  135. LPCTSTR pszUserName;
  136. BYTE byPasswdLen;
  137. LPCTSTR pszPasswd;
  138. } SOCKS5AUTHPASSWDREQ,*PSOCKS5AUTHPASSWDREQ;
  139. // USERNAME/PASSWORD验证 - Reply
  140. typedef struct _tagSocks5AuthPasswdRep
  141. {
  142. BYTE byVer;
  143. BYTE byStatus;
  144. } SOCKS5AUTHPASSWDREP,*PSOCKS5AUTHPASSWDREP;
  145.  
  146. // Request
  147. typedef struct _tagSocks5Req
  148. {
  149. BYTE byVer;
  150. BYTE byCmd;
  151. BYTE byRsv;
  152. BYTE byAtyp;
  153. LPCTSTR pszDestAddr;
  154. WORD wDestPort;
  155. } SOCKS5REQ,*PSOCKS5REQ;
  156. // Reply
  157. typedef struct _tagSocks5Rep
  158. {
  159. BYTE byVer;
  160. BYTE byRep;
  161. BYTE byRsv;
  162. BYTE byAtyp;
  163. LPSTR pszBindAddr;
  164. WORD wBindPort;
  165. } SOCKS5REP,*PSOCKS5REP;
  166. // UDP (Socks5 ONLY)
  167. typedef struct _tagSocks5UDP
  168. {
  169. WORD wRsv;
  170. BYTE byFrag;
  171. BYTE byAtyp;
  172. LPSTR pszDestAddr;
  173. WORD wDestPort;
  174. LPSTR pszData;
  175. } SOCKS5UDP,*PSOCKS5UDP;
  176. // 其他定义
  177. // Request-Packet (TCP ONLY)
  178. typedef union _tagSocksReqPacket
  179. {
  180. SOCKS4REQ socks4Req;
  181. SOCKS4AREQ socks4aReq;
  182. SOCKS5AUTHREQ socks5AuthReq;
  183. SOCKS5AUTHPASSWDREQ socks5AuthPasswdReq;
  184. SOCKS5REQ socks5Req;
  185. } SOCKSREQPACKET,*PSOCKSREQPACKET;
  186. // Reply-Packet (TCP ONLY)
  187. typedef union _tagSocksRepPacket
  188. {
  189. SOCK4REP socks4Rep;
  190. SOCKS4AREP socks4aRep;
  191. SOCKS5AUTHREP socks5AuthRep;
  192. SOCKS5AUTHPASSWDREP socks5AuthPasswdRep;
  193. SOCKS5REP socks5Rep;
  194. } SOCKSREPPACKET,*PSOCKSREPPACKET;
  195. // Request-Reply UDP Packet (UDP ONLY)
  196. typedef union _tagSocksUDPPacket
  197. {
  198. SOCKS5UDP socks5UDP;
  199. } SOCKSUDPPACKET,*PSOCKSUDPPACKET;
  200. #pragma pack(pop)
  201. class CSocksPacket  
  202. {
  203. public:
  204. CSocksPacket( CBufSocket* pBufSocket );
  205. virtual ~CSocksPacket();
  206. public:
  207. BOOL SendSocks4Req(BYTE byCmd,WORD wDestPort,DWORD dwDestIP,const char * pszUserID  = NULL ,DWORD dwTimeout  = SOP_SEND_TIMEOUT );
  208. BOOL SendSocks4aReq(BYTE byCmd,WORD wDestPort,const char* pszDestHostName,const char * pszUserID  = NULL ,DWORD dwTimeout  = SOP_SEND_TIMEOUT );
  209. BOOL SendSocks5AuthReq(BYTE byNAuth,const char* pszAuth,DWORD dwTimeout  = SOP_SEND_TIMEOUT );
  210. BOOL SendSocks5AuthPasswdReq(const char* pszUserName,const char * pszPassword,DWORD dwTimeout  = SOP_SEND_TIMEOUT );
  211. BOOL SendSocks5Req(BYTE byCmd,BYTE byAtyp,const char * pszDestAddr,WORD wDestPort,DWORD dwTimeout  = SOP_SEND_TIMEOUT );
  212. BOOL RecvPacket(OUT PSOCKSREPPACKET pPacket,int nPacketType,DWORD dwTimeout = SOP_RECV_TIMEOUT);
  213. BOOL IsSocksOK( PSOCKSREPPACKET pPacket,int nPacketType );
  214. BOOL SendPacket(PSOCKSREQPACKET pPacket,int nPacketType,DWORD dwTimeout = SOP_SEND_TIMEOUT);
  215. // ADD IN 1.1 <
  216. BOOL RecvPacket(int nPacketType,DWORD dwTimeout = SOP_RECV_TIMEOUT);
  217. BOOL IsSocksOK();
  218. // ADD IN 1.1 >
  219. private:
  220. BOOL Pack(OUT char *pszDest,PSOCKSREQPACKET pPacket,int nPacketType);
  221. int PacketSize(PSOCKSREQPACKET pPacket,int nPacketType);
  222. private:
  223. CBufSocket* m_pBufSocket;
  224. // ADD IN 1.1 <
  225. SOCKSREPPACKET m_stRepPacket;
  226. int m_nRepPacketType;
  227. // ADD IN 1.1 >
  228. };
  229. #endif // !defined(AFX_SOCKSPACKET_H__04B153A0_80D4_442C_ACCE_0FA5C8ECF577__INCLUDED_)