2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.bytestreams.socks5;
|
|
|
|
|
|
|
|
import java.io.DataInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-03-12 11:50:05 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2014-10-21 11:59:11 +02:00
|
|
|
import org.jivesoftware.smack.util.SHA1;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.Jid;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A collection of utility methods for SOcKS5 messages.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Henning Staib
|
|
|
|
*/
|
2017-07-03 10:35:46 +02:00
|
|
|
public class Socks5Utils {
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a SHA-1 digest of the given parameters as specified in <a
|
|
|
|
* href="http://xmpp.org/extensions/xep-0065.html#impl-socks5">XEP-0065</a>.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param sessionID for the SOCKS5 Bytestream
|
|
|
|
* @param initiatorJID JID of the initiator of a SOCKS5 Bytestream
|
|
|
|
* @param targetJID JID of the target of a SOCKS5 Bytestream
|
|
|
|
* @return SHA-1 digest of the given parameters
|
|
|
|
*/
|
2015-02-14 17:15:02 +01:00
|
|
|
public static String createDigest(String sessionID, Jid initiatorJID, Jid targetJID) {
|
2014-02-17 23:58:40 +01:00
|
|
|
StringBuilder b = new StringBuilder();
|
|
|
|
b.append(sessionID).append(initiatorJID).append(targetJID);
|
2014-10-21 11:59:11 +02:00
|
|
|
return SHA1.hex(b.toString());
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a SOCKS5 message from the given InputStream. The message can either be a SOCKS5 request
|
|
|
|
* message or a SOCKS5 response message.
|
|
|
|
* <p>
|
|
|
|
* (see <a href="http://tools.ietf.org/html/rfc1928">RFC1928</a>)
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @param in the DataInputStream to read the message from
|
|
|
|
* @return the SOCKS5 message
|
|
|
|
* @throws IOException if a network error occurred
|
2014-03-12 11:50:05 +01:00
|
|
|
* @throws SmackException if the SOCKS5 message contains an unsupported address type
|
2014-02-17 23:58:40 +01:00
|
|
|
*/
|
2014-03-12 11:50:05 +01:00
|
|
|
public static byte[] receiveSocks5Message(DataInputStream in) throws IOException, SmackException {
|
2014-02-17 23:58:40 +01:00
|
|
|
byte[] header = new byte[5];
|
|
|
|
in.readFully(header, 0, 5);
|
|
|
|
|
|
|
|
if (header[3] != (byte) 0x03) {
|
2016-11-05 18:24:07 +01:00
|
|
|
throw new SmackException("Unsupported SOCKS5 address type: " + header[3] + " (expected: 0x03)");
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int addressLength = header[4];
|
|
|
|
|
|
|
|
byte[] response = new byte[7 + addressLength];
|
|
|
|
System.arraycopy(header, 0, response, 0, header.length);
|
|
|
|
|
|
|
|
in.readFully(response, header.length, addressLength + 2);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|