Socks5Utils.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.smackx.bytestreams.socks5;

  18. import java.io.DataInputStream;
  19. import java.io.IOException;

  20. import org.jivesoftware.smack.SmackException.SmackMessageException;
  21. import org.jivesoftware.smack.util.SHA1;

  22. import org.jxmpp.jid.Jid;

  23. /**
  24.  * A collection of utility methods for SOcKS5 messages.
  25.  *
  26.  * @author Henning Staib
  27.  */
  28. public class Socks5Utils {

  29.     /**
  30.      * Returns a SHA-1 digest of the given parameters as specified in <a
  31.      * href="http://xmpp.org/extensions/xep-0065.html#impl-socks5">XEP-0065</a>.
  32.      *
  33.      * @param sessionID for the SOCKS5 Bytestream
  34.      * @param initiatorJID JID of the initiator of a SOCKS5 Bytestream
  35.      * @param targetJID JID of the target of a SOCKS5 Bytestream
  36.      * @return SHA-1 digest of the given parameters
  37.      */
  38.     public static String createDigest(String sessionID, Jid initiatorJID, Jid targetJID) {
  39.         StringBuilder b = new StringBuilder();
  40.         b.append(sessionID).append(initiatorJID).append(targetJID);
  41.         return SHA1.hex(b.toString());
  42.     }

  43.     /**
  44.      * Reads a SOCKS5 message from the given InputStream. The message can either be a SOCKS5 request
  45.      * message or a SOCKS5 response message.
  46.      * <p>
  47.      * (see <a href="http://tools.ietf.org/html/rfc1928">RFC1928</a>)
  48.      *
  49.      * @param in the DataInputStream to read the message from
  50.      * @return the SOCKS5 message
  51.      * @throws IOException if a network error occurred
  52.      * @throws SmackMessageException if the SOCKS5 message contains an unsupported address type
  53.      */
  54.     public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, SmackMessageException {
  55.         byte[] header = new byte[5];
  56.         in.readFully(header, 0, 5);

  57.         if (header[3] != (byte) 0x03) {
  58.             throw new SmackMessageException("Unsupported SOCKS5 address type: " + header[3] + " (expected: 0x03)");
  59.         }

  60.         int addressLength = header[4];

  61.         byte[] response = new byte[7 + addressLength];
  62.         System.arraycopy(header, 0, response, 0, header.length);

  63.         in.readFully(response, header.length, addressLength + 2);

  64.         return response;
  65.     }

  66. }