PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/jivesoftware/smack/proxy/Socks5ProxySocketFactory.java

https://bitbucket.org/Trugath/xabber
Java | 358 lines | 187 code | 37 blank | 134 comment | 8 complexity | 0a75573c66ed0b3c3fb89c3ad462ba0b MD5 | raw file
  1. package org.jivesoftware.smack.proxy;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.InetAddress;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. import javax.net.SocketFactory;
  9. /**
  10. * Socket factory for Socks5 proxy
  11. *
  12. * @author Atul Aggarwal
  13. */
  14. public class Socks5ProxySocketFactory
  15. extends SocketFactory
  16. {
  17. private ProxyInfo proxy;
  18. public Socks5ProxySocketFactory(ProxyInfo proxy)
  19. {
  20. this.proxy = proxy;
  21. }
  22. public Socket createSocket(String host, int port)
  23. throws IOException, UnknownHostException
  24. {
  25. return socks5ProxifiedSocket(host,port);
  26. }
  27. public Socket createSocket(String host ,int port, InetAddress localHost,
  28. int localPort)
  29. throws IOException, UnknownHostException
  30. {
  31. return socks5ProxifiedSocket(host,port);
  32. }
  33. public Socket createSocket(InetAddress host, int port)
  34. throws IOException
  35. {
  36. return socks5ProxifiedSocket(host.getHostAddress(),port);
  37. }
  38. public Socket createSocket( InetAddress address, int port,
  39. InetAddress localAddress, int localPort)
  40. throws IOException
  41. {
  42. return socks5ProxifiedSocket(address.getHostAddress(),port);
  43. }
  44. private Socket socks5ProxifiedSocket(String host, int port)
  45. throws IOException
  46. {
  47. Socket socket = null;
  48. InputStream in = null;
  49. OutputStream out = null;
  50. String proxy_host = proxy.getProxyAddress();
  51. int proxy_port = proxy.getProxyPort();
  52. String user = proxy.getProxyUsername();
  53. String passwd = proxy.getProxyPassword();
  54. try
  55. {
  56. socket=new Socket(proxy_host, proxy_port);
  57. in=socket.getInputStream();
  58. out=socket.getOutputStream();
  59. socket.setTcpNoDelay(true);
  60. byte[] buf=new byte[1024];
  61. int index=0;
  62. /*
  63. +----+----------+----------+
  64. |VER | NMETHODS | METHODS |
  65. +----+----------+----------+
  66. | 1 | 1 | 1 to 255 |
  67. +----+----------+----------+
  68. The VER field is set to X'05' for this version of the protocol. The
  69. NMETHODS field contains the number of method identifier octets that
  70. appear in the METHODS field.
  71. The values currently defined for METHOD are:
  72. o X'00' NO AUTHENTICATION REQUIRED
  73. o X'01' GSSAPI
  74. o X'02' USERNAME/PASSWORD
  75. o X'03' to X'7F' IANA ASSIGNED
  76. o X'80' to X'FE' RESERVED FOR PRIVATE METHODS
  77. o X'FF' NO ACCEPTABLE METHODS
  78. */
  79. buf[index++]=5;
  80. buf[index++]=2;
  81. buf[index++]=0; // NO AUTHENTICATION REQUIRED
  82. buf[index++]=2; // USERNAME/PASSWORD
  83. out.write(buf, 0, index);
  84. /*
  85. The server selects from one of the methods given in METHODS, and
  86. sends a METHOD selection message:
  87. +----+--------+
  88. |VER | METHOD |
  89. +----+--------+
  90. | 1 | 1 |
  91. +----+--------+
  92. */
  93. //in.read(buf, 0, 2);
  94. fill(in, buf, 2);
  95. boolean check=false;
  96. switch((buf[1])&0xff)
  97. {
  98. case 0: // NO AUTHENTICATION REQUIRED
  99. check=true;
  100. break;
  101. case 2: // USERNAME/PASSWORD
  102. if(user==null || passwd==null)
  103. {
  104. break;
  105. }
  106. /*
  107. Once the SOCKS V5 server has started, and the client has selected the
  108. Username/Password Authentication protocol, the Username/Password
  109. subnegotiation begins. This begins with the client producing a
  110. Username/Password request:
  111. +----+------+----------+------+----------+
  112. |VER | ULEN | UNAME | PLEN | PASSWD |
  113. +----+------+----------+------+----------+
  114. | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
  115. +----+------+----------+------+----------+
  116. The VER field contains the current version of the subnegotiation,
  117. which is X'01'. The ULEN field contains the length of the UNAME field
  118. that follows. The UNAME field contains the username as known to the
  119. source operating system. The PLEN field contains the length of the
  120. PASSWD field that follows. The PASSWD field contains the password
  121. association with the given UNAME.
  122. */
  123. index=0;
  124. buf[index++]=1;
  125. buf[index++]=(byte)(user.length());
  126. System.arraycopy(user.getBytes(), 0, buf, index,
  127. user.length());
  128. index+=user.length();
  129. buf[index++]=(byte)(passwd.length());
  130. System.arraycopy(passwd.getBytes(), 0, buf, index,
  131. passwd.length());
  132. index+=passwd.length();
  133. out.write(buf, 0, index);
  134. /*
  135. The server verifies the supplied UNAME and PASSWD, and sends the
  136. following response:
  137. +----+--------+
  138. |VER | STATUS |
  139. +----+--------+
  140. | 1 | 1 |
  141. +----+--------+
  142. A STATUS field of X'00' indicates success. If the server returns a
  143. `failure' (STATUS value other than X'00') status, it MUST close the
  144. connection.
  145. */
  146. //in.read(buf, 0, 2);
  147. fill(in, buf, 2);
  148. if(buf[1]==0)
  149. {
  150. check=true;
  151. }
  152. break;
  153. default:
  154. }
  155. if(!check)
  156. {
  157. try
  158. {
  159. socket.close();
  160. }
  161. catch(Exception eee)
  162. {
  163. }
  164. throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
  165. "fail in SOCKS5 proxy");
  166. }
  167. /*
  168. The SOCKS request is formed as follows:
  169. +----+-----+-------+------+----------+----------+
  170. |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
  171. +----+-----+-------+------+----------+----------+
  172. | 1 | 1 | X'00' | 1 | Variable | 2 |
  173. +----+-----+-------+------+----------+----------+
  174. Where:
  175. o VER protocol version: X'05'
  176. o CMD
  177. o CONNECT X'01'
  178. o BIND X'02'
  179. o UDP ASSOCIATE X'03'
  180. o RSV RESERVED
  181. o ATYP address type of following address
  182. o IP V4 address: X'01'
  183. o DOMAINNAME: X'03'
  184. o IP V6 address: X'04'
  185. o DST.ADDR desired destination address
  186. o DST.PORT desired destination port in network octet
  187. order
  188. */
  189. index=0;
  190. buf[index++]=5;
  191. buf[index++]=1; // CONNECT
  192. buf[index++]=0;
  193. byte[] hostb=host.getBytes();
  194. int len=hostb.length;
  195. buf[index++]=3; // DOMAINNAME
  196. buf[index++]=(byte)(len);
  197. System.arraycopy(hostb, 0, buf, index, len);
  198. index+=len;
  199. buf[index++]=(byte)(port>>>8);
  200. buf[index++]=(byte)(port&0xff);
  201. out.write(buf, 0, index);
  202. /*
  203. The SOCKS request information is sent by the client as soon as it has
  204. established a connection to the SOCKS server, and completed the
  205. authentication negotiations. The server evaluates the request, and
  206. returns a reply formed as follows:
  207. +----+-----+-------+------+----------+----------+
  208. |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
  209. +----+-----+-------+------+----------+----------+
  210. | 1 | 1 | X'00' | 1 | Variable | 2 |
  211. +----+-----+-------+------+----------+----------+
  212. Where:
  213. o VER protocol version: X'05'
  214. o REP Reply field:
  215. o X'00' succeeded
  216. o X'01' general SOCKS server failure
  217. o X'02' connection not allowed by ruleset
  218. o X'03' Network unreachable
  219. o X'04' Host unreachable
  220. o X'05' Connection refused
  221. o X'06' TTL expired
  222. o X'07' Command not supported
  223. o X'08' Address type not supported
  224. o X'09' to X'FF' unassigned
  225. o RSV RESERVED
  226. o ATYP address type of following address
  227. o IP V4 address: X'01'
  228. o DOMAINNAME: X'03'
  229. o IP V6 address: X'04'
  230. o BND.ADDR server bound address
  231. o BND.PORT server bound port in network octet order
  232. */
  233. //in.read(buf, 0, 4);
  234. fill(in, buf, 4);
  235. if(buf[1]!=0)
  236. {
  237. try
  238. {
  239. socket.close();
  240. }
  241. catch(Exception eee)
  242. {
  243. }
  244. throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
  245. "server returns "+buf[1]);
  246. }
  247. switch(buf[3]&0xff)
  248. {
  249. case 1:
  250. //in.read(buf, 0, 6);
  251. fill(in, buf, 6);
  252. break;
  253. case 3:
  254. //in.read(buf, 0, 1);
  255. fill(in, buf, 1);
  256. //in.read(buf, 0, buf[0]+2);
  257. fill(in, buf, (buf[0]&0xff)+2);
  258. break;
  259. case 4:
  260. //in.read(buf, 0, 18);
  261. fill(in, buf, 18);
  262. break;
  263. default:
  264. }
  265. return socket;
  266. }
  267. catch(RuntimeException e)
  268. {
  269. throw e;
  270. }
  271. catch(Exception e)
  272. {
  273. try
  274. {
  275. if(socket!=null)
  276. {
  277. socket.close();
  278. }
  279. }
  280. catch(Exception eee)
  281. {
  282. }
  283. String message="ProxySOCKS5: "+e.toString();
  284. if(e instanceof Throwable)
  285. {
  286. throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,message,
  287. (Throwable)e);
  288. }
  289. throw new IOException(message);
  290. }
  291. }
  292. private void fill(InputStream in, byte[] buf, int len)
  293. throws IOException
  294. {
  295. int s=0;
  296. while(s<len)
  297. {
  298. int i=in.read(buf, s, len-s);
  299. if(i<=0)
  300. {
  301. throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "stream " +
  302. "is closed");
  303. }
  304. s+=i;
  305. }
  306. }
  307. }