2006-02-03 20:13:23 +01:00
|
|
|
/**
|
|
|
|
* All rights reserved. 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.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
2010-08-15 12:49:11 +02:00
|
|
|
package org.jivesoftware.smackx.socks5bytestream.packet;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2006-02-03 19:46:04 +01:00
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
|
|
import org.jivesoftware.smack.packet.PacketExtension;
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* A packet representing part of a SOCKS5 Bytestream negotiation.
|
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
|
|
|
public class Bytestream extends IQ {
|
|
|
|
|
|
|
|
private String sessionID;
|
|
|
|
|
2007-03-21 05:09:52 +01:00
|
|
|
private Mode mode = Mode.tcp;
|
2006-02-03 19:46:04 +01:00
|
|
|
|
2007-03-21 05:09:52 +01:00
|
|
|
private final List<StreamHost> streamHosts = new ArrayList<StreamHost>();
|
2006-02-03 19:46:04 +01:00
|
|
|
|
|
|
|
private StreamHostUsed usedHost;
|
|
|
|
|
|
|
|
private Activate toActivate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default constructor
|
|
|
|
*/
|
|
|
|
public Bytestream() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A constructor where the session ID can be specified.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @param SID The session ID related to the negotiation.
|
|
|
|
* @see #setSessionID(String)
|
|
|
|
*/
|
|
|
|
public Bytestream(final String SID) {
|
|
|
|
super();
|
|
|
|
setSessionID(SID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Set the session ID related to the bytestream. The session ID is a unique identifier used to
|
|
|
|
* differentiate between stream negotiations.
|
|
|
|
*
|
2007-03-21 05:09:52 +01:00
|
|
|
* @param sessionID the unique session ID that identifies the transfer.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public void setSessionID(final String sessionID) {
|
|
|
|
this.sessionID = sessionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the session ID related to the bytestream negotiation.
|
|
|
|
*
|
|
|
|
* @return Returns the session ID related to the bytestream negotiation.
|
2006-02-03 19:46:04 +01:00
|
|
|
* @see #setSessionID(String)
|
|
|
|
*/
|
|
|
|
public String getSessionID() {
|
|
|
|
return sessionID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Set the transport mode. This should be put in the initiation of the interaction.
|
|
|
|
*
|
2007-03-21 05:09:52 +01:00
|
|
|
* @param mode the transport mode, either UDP or TCP
|
2006-02-03 19:46:04 +01:00
|
|
|
* @see Mode
|
|
|
|
*/
|
|
|
|
public void setMode(final Mode mode) {
|
|
|
|
this.mode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the transport mode.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @return Returns the transport mode.
|
|
|
|
* @see #setMode(Mode)
|
|
|
|
*/
|
|
|
|
public Mode getMode() {
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Adds a potential stream host that the remote user can connect to to receive the file.
|
|
|
|
*
|
|
|
|
* @param JID The JID of the stream host.
|
2006-02-03 19:46:04 +01:00
|
|
|
* @param address The internet address of the stream host.
|
|
|
|
* @return The added stream host.
|
|
|
|
*/
|
|
|
|
public StreamHost addStreamHost(final String JID, final String address) {
|
|
|
|
return addStreamHost(JID, address, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Adds a potential stream host that the remote user can connect to to receive the file.
|
|
|
|
*
|
|
|
|
* @param JID The JID of the stream host.
|
2006-02-03 19:46:04 +01:00
|
|
|
* @param address The internet address of the stream host.
|
2010-08-15 12:49:11 +02:00
|
|
|
* @param port The port on which the remote host is seeking connections.
|
2006-02-03 19:46:04 +01:00
|
|
|
* @return The added stream host.
|
|
|
|
*/
|
2010-08-15 12:49:11 +02:00
|
|
|
public StreamHost addStreamHost(final String JID, final String address, final int port) {
|
2006-02-03 19:46:04 +01:00
|
|
|
StreamHost host = new StreamHost(JID, address);
|
|
|
|
host.setPort(port);
|
|
|
|
addStreamHost(host);
|
|
|
|
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Adds a potential stream host that the remote user can transfer the file through.
|
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @param host The potential stream host.
|
|
|
|
*/
|
|
|
|
public void addStreamHost(final StreamHost host) {
|
|
|
|
streamHosts.add(host);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of stream hosts contained in the packet.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @return Returns the list of stream hosts contained in the packet.
|
|
|
|
*/
|
2007-03-21 05:09:52 +01:00
|
|
|
public Collection<StreamHost> getStreamHosts() {
|
2006-02-03 19:46:04 +01:00
|
|
|
return Collections.unmodifiableCollection(streamHosts);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the stream host related to the given JID, or null if there is none.
|
|
|
|
*
|
|
|
|
* @param JID The JID of the desired stream host.
|
|
|
|
* @return Returns the stream host related to the given JID, or null if there is none.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public StreamHost getStreamHost(final String JID) {
|
2010-08-15 12:49:11 +02:00
|
|
|
if (JID == null) {
|
2007-03-21 05:09:52 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (StreamHost host : streamHosts) {
|
2006-02-03 19:46:04 +01:00
|
|
|
if (host.getJID().equals(JID)) {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the count of stream hosts contained in this packet.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @return Returns the count of stream hosts contained in this packet.
|
|
|
|
*/
|
|
|
|
public int countStreamHosts() {
|
|
|
|
return streamHosts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Upon connecting to the stream host the target of the stream replies to the initiator with the
|
|
|
|
* JID of the SOCKS5 host that they used.
|
|
|
|
*
|
|
|
|
* @param JID The JID of the used host.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public void setUsedHost(final String JID) {
|
|
|
|
this.usedHost = new StreamHostUsed(JID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the SOCKS5 host connected to by the remote user.
|
|
|
|
*
|
|
|
|
* @return Returns the SOCKS5 host connected to by the remote user.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public StreamHostUsed getUsedHost() {
|
|
|
|
return usedHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the activate element of the packet sent to the proxy host to verify the identity of
|
|
|
|
* the initiator and match them to the appropriate stream.
|
|
|
|
*
|
|
|
|
* @return Returns the activate element of the packet sent to the proxy host to verify the
|
|
|
|
* identity of the initiator and match them to the appropriate stream.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public Activate getToActivate() {
|
|
|
|
return toActivate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Upon the response from the target of the used host the activate packet is sent to the SOCKS5
|
|
|
|
* proxy. The proxy will activate the stream or return an error after verifying the identity of
|
|
|
|
* the initiator, using the activate packet.
|
|
|
|
*
|
|
|
|
* @param targetID The JID of the target of the file transfer.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public void setToActivate(final String targetID) {
|
|
|
|
this.toActivate = new Activate(targetID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getChildElementXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2006-02-03 19:46:04 +01:00
|
|
|
|
|
|
|
buf.append("<query xmlns=\"http://jabber.org/protocol/bytestreams\"");
|
|
|
|
if (this.getType().equals(IQ.Type.SET)) {
|
2010-08-15 12:49:11 +02:00
|
|
|
if (getSessionID() != null) {
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append(" sid=\"").append(getSessionID()).append("\"");
|
2010-08-15 12:49:11 +02:00
|
|
|
}
|
|
|
|
if (getMode() != null) {
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append(" mode = \"").append(getMode()).append("\"");
|
2010-08-15 12:49:11 +02:00
|
|
|
}
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append(">");
|
|
|
|
if (getToActivate() == null) {
|
2007-03-21 05:09:52 +01:00
|
|
|
for (StreamHost streamHost : getStreamHosts()) {
|
|
|
|
buf.append(streamHost.toXML());
|
|
|
|
}
|
2006-02-03 19:46:04 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
buf.append(getToActivate().toXML());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (this.getType().equals(IQ.Type.RESULT)) {
|
|
|
|
buf.append(">");
|
2010-08-15 12:49:11 +02:00
|
|
|
if (getUsedHost() != null) {
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append(getUsedHost().toXML());
|
2010-08-15 12:49:11 +02:00
|
|
|
}
|
2006-03-20 18:59:01 +01:00
|
|
|
// A result from the server can also contain stream hosts
|
|
|
|
else if (countStreamHosts() > 0) {
|
2007-03-21 05:09:52 +01:00
|
|
|
for (StreamHost host : streamHosts) {
|
|
|
|
buf.append(host.toXML());
|
2006-03-20 18:59:01 +01:00
|
|
|
}
|
|
|
|
}
|
2006-02-03 19:46:04 +01:00
|
|
|
}
|
2010-08-15 12:49:11 +02:00
|
|
|
else if (this.getType().equals(IQ.Type.GET)) {
|
|
|
|
return buf.append("/>").toString();
|
|
|
|
}
|
2006-02-03 19:46:04 +01:00
|
|
|
else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
buf.append("</query>");
|
|
|
|
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Packet extension that represents a potential SOCKS5 proxy for the file transfer. Stream hosts
|
|
|
|
* are forwarded to the target of the file transfer who then chooses and connects to one.
|
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
|
|
|
public static class StreamHost implements PacketExtension {
|
|
|
|
|
|
|
|
public static String NAMESPACE = "";
|
|
|
|
|
|
|
|
public static String ELEMENTNAME = "streamhost";
|
|
|
|
|
|
|
|
private final String JID;
|
|
|
|
|
|
|
|
private final String addy;
|
|
|
|
|
|
|
|
private int port = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
|
|
|
* @param JID The JID of the stream host.
|
2006-02-03 19:46:04 +01:00
|
|
|
* @param address The internet address of the stream host.
|
|
|
|
*/
|
|
|
|
public StreamHost(final String JID, final String address) {
|
|
|
|
this.JID = JID;
|
|
|
|
this.addy = address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the JID of the stream host.
|
|
|
|
*
|
|
|
|
* @return Returns the JID of the stream host.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public String getJID() {
|
|
|
|
return JID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the internet address of the stream host.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @return Returns the internet address of the stream host.
|
|
|
|
*/
|
|
|
|
public String getAddress() {
|
|
|
|
return addy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the port of the stream host.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
|
|
|
* @param port The port on which the potential stream host would accept the connection.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public void setPort(final int port) {
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the port on which the potential stream host would accept the connection.
|
|
|
|
*
|
|
|
|
* @return Returns the port on which the potential stream host would accept the connection.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public int getPort() {
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNamespace() {
|
|
|
|
return NAMESPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getElementName() {
|
|
|
|
return ELEMENTNAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2006-02-03 19:46:04 +01:00
|
|
|
|
|
|
|
buf.append("<").append(getElementName()).append(" ");
|
|
|
|
buf.append("jid=\"").append(getJID()).append("\" ");
|
|
|
|
buf.append("host=\"").append(getAddress()).append("\" ");
|
2010-08-15 12:49:11 +02:00
|
|
|
if (getPort() != 0) {
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append("port=\"").append(getPort()).append("\"");
|
2010-08-15 12:49:11 +02:00
|
|
|
}
|
|
|
|
else {
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append("zeroconf=\"_jabber.bytestreams\"");
|
2010-08-15 12:49:11 +02:00
|
|
|
}
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append("/>");
|
|
|
|
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* After selected a SOCKS5 stream host and successfully connecting, the target of the file
|
|
|
|
* transfer returns a byte stream packet with the stream host used extension.
|
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
|
|
|
public static class StreamHostUsed implements PacketExtension {
|
|
|
|
|
|
|
|
public String NAMESPACE = "";
|
|
|
|
|
|
|
|
public static String ELEMENTNAME = "streamhost-used";
|
|
|
|
|
|
|
|
private final String JID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
|
|
|
* @param JID The JID of the selected stream host.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public StreamHostUsed(final String JID) {
|
|
|
|
this.JID = JID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* Returns the JID of the selected stream host.
|
|
|
|
*
|
|
|
|
* @return Returns the JID of the selected stream host.
|
2006-02-03 19:46:04 +01:00
|
|
|
*/
|
|
|
|
public String getJID() {
|
|
|
|
return JID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNamespace() {
|
|
|
|
return NAMESPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getElementName() {
|
|
|
|
return ELEMENTNAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append("<").append(getElementName()).append(" ");
|
|
|
|
buf.append("jid=\"").append(getJID()).append("\" ");
|
|
|
|
buf.append("/>");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-08-15 12:49:11 +02:00
|
|
|
* The packet sent by the stream initiator to the stream proxy to activate the connection.
|
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
|
|
|
public static class Activate implements PacketExtension {
|
|
|
|
|
|
|
|
public String NAMESPACE = "";
|
|
|
|
|
|
|
|
public static String ELEMENTNAME = "activate";
|
|
|
|
|
|
|
|
private final String target;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor specifying the target of the stream.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @param target The target of the stream.
|
|
|
|
*/
|
|
|
|
public Activate(final String target) {
|
|
|
|
this.target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the target of the activation.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @return Returns the target of the activation.
|
|
|
|
*/
|
|
|
|
public String getTarget() {
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getNamespace() {
|
|
|
|
return NAMESPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getElementName() {
|
|
|
|
return ELEMENTNAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toXML() {
|
2006-07-18 07:14:33 +02:00
|
|
|
StringBuilder buf = new StringBuilder();
|
2006-02-03 19:46:04 +01:00
|
|
|
buf.append("<").append(getElementName()).append(">");
|
|
|
|
buf.append(getTarget());
|
|
|
|
buf.append("</").append(getElementName()).append(">");
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The stream can be either a TCP stream or a UDP stream.
|
2010-08-15 12:49:11 +02:00
|
|
|
*
|
2006-02-03 19:46:04 +01:00
|
|
|
* @author Alexander Wenckus
|
|
|
|
*/
|
2007-03-21 05:09:52 +01:00
|
|
|
public enum Mode {
|
2006-02-03 19:46:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A TCP based stream.
|
|
|
|
*/
|
2007-03-21 05:09:52 +01:00
|
|
|
tcp,
|
2006-02-03 19:46:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A UDP based stream.
|
|
|
|
*/
|
2007-03-21 05:09:52 +01:00
|
|
|
udp;
|
2006-02-03 19:46:04 +01:00
|
|
|
|
2007-03-21 05:09:52 +01:00
|
|
|
public static Mode fromName(String name) {
|
|
|
|
Mode mode;
|
|
|
|
try {
|
|
|
|
mode = Mode.valueOf(name);
|
|
|
|
}
|
2010-08-15 12:49:11 +02:00
|
|
|
catch (Exception ex) {
|
2007-03-21 05:09:52 +01:00
|
|
|
mode = tcp;
|
|
|
|
}
|
2006-02-03 19:46:04 +01:00
|
|
|
|
2007-03-21 05:09:52 +01:00
|
|
|
return mode;
|
2006-02-03 19:46:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|