001/** 002 * 003 * Copyright the original author or authors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.bytestreams.socks5; 018 019import java.io.DataInputStream; 020import java.io.IOException; 021 022import org.jivesoftware.smack.SmackException.SmackMessageException; 023import org.jivesoftware.smack.util.SHA1; 024 025import org.jxmpp.jid.Jid; 026 027/** 028 * A collection of utility methods for SOcKS5 messages. 029 * 030 * @author Henning Staib 031 */ 032public class Socks5Utils { 033 034 /** 035 * Returns a SHA-1 digest of the given parameters as specified in <a 036 * href="http://xmpp.org/extensions/xep-0065.html#impl-socks5">XEP-0065</a>. 037 * 038 * @param sessionID for the SOCKS5 Bytestream 039 * @param initiatorJID JID of the initiator of a SOCKS5 Bytestream 040 * @param targetJID JID of the target of a SOCKS5 Bytestream 041 * @return SHA-1 digest of the given parameters 042 */ 043 public static String createDigest(String sessionID, Jid initiatorJID, Jid targetJID) { 044 StringBuilder b = new StringBuilder(); 045 b.append(sessionID).append(initiatorJID).append(targetJID); 046 return SHA1.hex(b.toString()); 047 } 048 049 /** 050 * Reads a SOCKS5 message from the given InputStream. The message can either be a SOCKS5 request 051 * message or a SOCKS5 response message. 052 * <p> 053 * (see <a href="http://tools.ietf.org/html/rfc1928">RFC1928</a>) 054 * 055 * @param in the DataInputStream to read the message from 056 * @return the SOCKS5 message 057 * @throws IOException if a network error occurred 058 * @throws SmackMessageException if the SOCKS5 message contains an unsupported address type 059 */ 060 public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, SmackMessageException { 061 byte[] header = new byte[5]; 062 in.readFully(header, 0, 5); 063 064 if (header[3] != (byte) 0x03) { 065 throw new SmackMessageException("Unsupported SOCKS5 address type: " + header[3] + " (expected: 0x03)"); 066 } 067 068 int addressLength = header[4]; 069 070 byte[] response = new byte[7 + addressLength]; 071 System.arraycopy(header, 0, response, 0, header.length); 072 073 in.readFully(response, header.length, addressLength + 2); 074 075 return response; 076 } 077 078}