Socks4ProxySocketConnection.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.proxy;

  18. import java.io.ByteArrayOutputStream;
  19. import java.io.DataInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.net.InetAddress;
  24. import java.net.InetSocketAddress;
  25. import java.net.Socket;
  26. import java.nio.charset.StandardCharsets;

  27. import org.jivesoftware.smack.util.OutputStreamUtil;

  28. /**
  29.  * Socket factory for socks4 proxy.
  30.  *
  31.  * @author Atul Aggarwal
  32.  */
  33. public class Socks4ProxySocketConnection implements ProxySocketConnection {
  34.     private final ProxyInfo proxy;

  35.     Socks4ProxySocketConnection(ProxyInfo proxy) {
  36.         this.proxy = proxy;
  37.     }

  38.     @Override
  39.     public void connect(Socket socket, String host, int port, int timeout)
  40.                     throws IOException {
  41.         String proxy_host = proxy.getProxyAddress();
  42.         int proxy_port = proxy.getProxyPort();
  43.         String user = proxy.getProxyUsername();

  44.         socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
  45.         InputStream in = socket.getInputStream();
  46.         DataInputStream dis = new DataInputStream(in);
  47.         OutputStream out = socket.getOutputStream();

  48.         ByteArrayOutputStream outBuf = new ByteArrayOutputStream();
  49.         byte[] inBuf;

  50.     /*
  51.     1) CONNECT

  52.     The client connects to the SOCKS server and sends a CONNECT request when
  53.     it wants to establish a connection to an application server. The client
  54.     includes in the request packet the IP address and the port number of the
  55.     destination host, and userid, in the following format.

  56.            +----+----+----+----+----+----+----+----+----+----+....+----+
  57.            | VN | CD | DSTPORT |      DSTIP        | USERID       |NULL|
  58.            +----+----+----+----+----+----+----+----+----+----+....+----+
  59.     # of bytes:   1    1      2              4           variable       1

  60.     VN is the SOCKS protocol version number and should be 4. CD is the
  61.     SOCKS command code and should be 1 for CONNECT request. NULL is a byte
  62.     of all zero bits.
  63.     */

  64.         outBuf.write(4);
  65.         outBuf.write(1);

  66.         outBuf.write(port >>> 8);
  67.         outBuf.write(port & 0xff);

  68.         InetAddress inetAddress = InetAddress.getByName(proxy_host);
  69.         byte[] byteAddress = inetAddress.getAddress();
  70.         outBuf.write(byteAddress);

  71.         if (user != null) {
  72.             byte[] userBytes = user.getBytes(StandardCharsets.UTF_8);
  73.             outBuf.write(userBytes);
  74.         }
  75.         outBuf.write(0);
  76.         OutputStreamUtil.writeResetAndFlush(outBuf, out);

  77.     /*
  78.     The SOCKS server checks to see whether such a request should be granted
  79.     based on any combination of source IP address, destination IP address,
  80.     destination port number, the userid, and information it may obtain by
  81.     consulting IDENT, cf. RFC 1413.  If the request is granted, the SOCKS
  82.     server makes a connection to the specified port of the destination host.
  83.     A reply packet is sent to the client when this connection is established,
  84.     or when the request is rejected or the operation fails.

  85.            +----+----+----+----+----+----+----+----+
  86.            | VN | CD | DSTPORT |      DSTIP        |
  87.            +----+----+----+----+----+----+----+----+
  88.     # of bytes:   1    1      2              4

  89.     VN is the version of the reply code and should be 0. CD is the result
  90.     code with one of the following values:

  91.     90: request granted
  92.     91: request rejected or failed
  93.     92: request rejected because SOCKS server cannot connect to
  94.     identd on the client
  95.     93: request rejected because the client program and identd
  96.     report different user-ids

  97.     The remaining fields are ignored.
  98.     */

  99.         inBuf = new byte[6];
  100.         dis.readFully(inBuf);
  101.         if (inBuf[0] != 0) {
  102.             throw new ProxyException(ProxyInfo.ProxyType.SOCKS4,
  103.                 "server returns VN " + inBuf[0]);
  104.         }
  105.         if (inBuf[1] != 90) {
  106.             String message = "ProxySOCKS4: server returns CD " + inBuf[1];
  107.             throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, message);
  108.         }
  109.         inBuf = new byte[2];
  110.         dis.readFully(inBuf);
  111.     }
  112. }