1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 17:19:39 +02:00
This commit is contained in:
vanitasvitae 2017-06-29 21:53:57 +02:00
parent a395b11ee9
commit 59a600a0b6
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
10 changed files with 217 additions and 33 deletions

View file

@ -17,6 +17,7 @@
package org.jivesoftware.smackx.jingle;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -35,6 +36,7 @@ import org.jxmpp.jid.FullJid;
public abstract class JingleSession implements JingleSessionHandler {
private static final Logger LOGGER = Logger.getLogger(JingleSession.class.getName());
protected HashSet<String> failedTransportMethods = new HashSet<>();
protected final FullJid local;
@ -46,7 +48,7 @@ public abstract class JingleSession implements JingleSessionHandler {
protected final List<JingleContent> contents = new ArrayList<>();
protected ExecutorService threadPool = Executors.newSingleThreadExecutor();
protected static ExecutorService threadPool = Executors.newSingleThreadExecutor();
protected ArrayList<Future<?>> queued = new ArrayList<>();
protected JingleTransportSession<?> transportSession;
@ -220,7 +222,7 @@ public abstract class JingleSession implements JingleSessionHandler {
return IQ.createResultIQ(securityInfo);
}
protected IQ handleTransportAccept(Jingle transportAccept) {
protected IQ handleTransportAccept(Jingle transportAccept) throws SmackException.NotConnectedException, InterruptedException {
return IQ.createResultIQ(transportAccept);
}
@ -236,4 +238,6 @@ public abstract class JingleSession implements JingleSessionHandler {
public abstract XMPPConnection getConnection();
public abstract void onTransportMethodFailed(String namespace);
}

View file

@ -18,6 +18,7 @@ package org.jivesoftware.smackx.jingle;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Manager;
@ -106,4 +107,26 @@ public final class JingleTransportMethodManager extends Manager {
return null;
}
public JingleTransportManager<?> getBestAvailableTransportManager(Set<String> except) {
JingleTransportManager<?> tm;
for (String ns : transportPreference) {
tm = getTransportManager(ns);
if (tm != null) {
if (except.contains(tm.getNamespace())) {
continue;
}
return tm;
}
}
for (String ns : transportManagers.keySet()) {
if (except.contains(ns)) {
continue;
}
return getTransportManager(ns);
}
return null;
}
}

View file

@ -46,7 +46,7 @@ import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTr
import org.jivesoftware.smackx.jingle.transports.jingle_s5b.elements.JingleS5BTransportInfo;
/**
* Created by vanitas on 26.06.17.
* Handler that handles Jingle Socks5Bytestream transports (XEP-0260).
*/
public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BTransport> {
private static final Logger LOGGER = Logger.getLogger(JingleS5BTransportSession.class.getName());
@ -78,9 +78,9 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
Socks5Utils.createDigest(sid, jingleSession.getLocal(), jingleSession.getRemote()));
//Local host
//for (Bytestream.StreamHost host : transportManager().getLocalStreamHosts()) {
// jb.addTransportCandidate(new JingleS5BTransportCandidate(host, 100));
//}
for (Bytestream.StreamHost host : transportManager().getLocalStreamHosts()) {
jb.addTransportCandidate(new JingleS5BTransportCandidate(host, 100, JingleS5BTransportCandidate.Type.direct));
}
List<Bytestream.StreamHost> remoteHosts;
try {
@ -91,7 +91,7 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
}
for (Bytestream.StreamHost host : remoteHosts) {
jb.addTransportCandidate(new JingleS5BTransportCandidate(host, 0));
jb.addTransportCandidate(new JingleS5BTransportCandidate(host, 0, JingleS5BTransportCandidate.Type.proxy));
}
return jb.build();
@ -222,7 +222,7 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
}
public IQ handleCandidateActivate(Jingle jingle) {
LOGGER.log(Level.INFO, "handleChandidateActivate");
LOGGER.log(Level.INFO, "handleCandidateActivate");
Socks5BytestreamSession bs = new Socks5BytestreamSession(ourChoice.socket,
ourChoice.candidate.getJid().asBareJid().equals(jingleSession.getRemote().asBareJid()));
callback.onSessionInitiated(bs);
@ -256,7 +256,7 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
if (ourChoice == CANDIDATE_FAILURE && theirChoice == CANDIDATE_FAILURE) {
LOGGER.log(Level.INFO, "Failure.");
// TODO: Transport failed.
jingleSession.onTransportMethodFailed(getNamespace());
return;
}
@ -278,19 +278,18 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
nominated = theirChoice;
}
boolean ourExternalProxy = !nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid());
boolean theirExternalProxy = !nominated.candidate.getJid().asBareJid().equals(jingleSession.getRemote().asBareJid());
if (nominated == theirChoice) {
LOGGER.log(Level.INFO, "Their choice, so our proposed candidate is used.");
boolean isProxy = nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy;
try {
nominated = connectToOurCandidate(nominated.candidate);
} catch (InterruptedException | IOException | XMPPException | SmackException | TimeoutException e) {
LOGGER.log(Level.INFO, "Could not connect to our candidate.", e);
//TODO: Proxy-Error
return;
}
if (ourExternalProxy) {
if (isProxy) {
LOGGER.log(Level.INFO, "Is external proxy. Activate it.");
Bytestream activate = new Bytestream(ourProposal.getStreamId());
activate.setMode(null);
@ -304,32 +303,33 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
LOGGER.log(Level.WARNING, "Could not activate proxy.", e);
return;
}
}
LOGGER.log(Level.INFO, "Send candidate-activate.");
Jingle candidateActivate = transportManager().createCandidateActivated(
jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(),
content.getSenders(), content.getCreator(), content.getName(), nominated.transport.getStreamId(),
nominated.candidate.getCandidateId());
try {
jingleSession.getConnection().createStanzaCollectorAndSend(candidateActivate)
.nextResultOrThrow();
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-activated", e);
return;
LOGGER.log(Level.INFO, "Send candidate-activate.");
Jingle candidateActivate = transportManager().createCandidateActivated(
jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(),
content.getSenders(), content.getCreator(), content.getName(), nominated.transport.getStreamId(),
nominated.candidate.getCandidateId());
try {
jingleSession.getConnection().createStanzaCollectorAndSend(candidateActivate)
.nextResultOrThrow();
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-activated", e);
return;
}
}
LOGGER.log(Level.INFO, "Start transmission.");
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !ourExternalProxy);
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !isProxy);
callback.onSessionInitiated(bs);
}
//Our choice
else {
LOGGER.log(Level.INFO, "Our choice, so their candidate was used.");
if (!theirExternalProxy) {
boolean isProxy = nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy;
if (!isProxy) {
LOGGER.log(Level.INFO, "Direct connection.");
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !ourExternalProxy);
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, true);
callback.onSessionInitiated(bs);
} else {
LOGGER.log(Level.INFO, "Our choice was their external proxy. wait for candidate-activate.");

View file

@ -66,8 +66,8 @@ public final class JingleS5BTransportCandidate extends JingleContentTransportCan
this.type = type;
}
public JingleS5BTransportCandidate(Bytestream.StreamHost streamHost, int priority) {
this(StringUtils.randomString(24), streamHost.getAddress(), streamHost.getJID(), streamHost.getPort(), priority, Type.proxy);
public JingleS5BTransportCandidate(Bytestream.StreamHost streamHost, int priority, Type type) {
this(StringUtils.randomString(24), streamHost.getAddress(), streamHost.getJID(), streamHost.getPort(), priority, type);
}
public enum Type {