1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-23 20:42:06 +01:00

So close to working socks...

This commit is contained in:
vanitasvitae 2017-06-13 21:59:40 +02:00
parent 1cd59370e6
commit 7320d01997
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
7 changed files with 372 additions and 219 deletions

View file

@ -63,6 +63,7 @@ public class JingleFileTransferSession extends AbstractJingleSession {
private JingleContent receivedContent; private JingleContent receivedContent;
private final FullJid remote; private final FullJid remote;
private final String sessionId; private final String sessionId;
private final JingleContent.Creator role;
/** /**
* Send a file. * Send a file.
@ -74,6 +75,7 @@ public class JingleFileTransferSession extends AbstractJingleSession {
this.remote = recipient; this.remote = recipient;
this.source = send; this.source = send;
this.sessionId = StringUtils.randomString(24); this.sessionId = StringUtils.randomString(24);
this.role = JingleContent.Creator.initiator;
JingleFileTransferChild fileTransferChild = fileElementFromFile(send); JingleFileTransferChild fileTransferChild = fileElementFromFile(send);
JingleContent.Builder cb = JingleContent.getBuilder(); JingleContent.Builder cb = JingleContent.getBuilder();
@ -106,7 +108,7 @@ public class JingleFileTransferSession extends AbstractJingleSession {
*/ */
public JingleFileTransferSession(XMPPConnection connection, Jingle initiate) { public JingleFileTransferSession(XMPPConnection connection, Jingle initiate) {
super(connection); super(connection);
LOGGER.log(Level.INFO, "Incoming session!"); this.role = JingleContent.Creator.responder;
this.sessionId = initiate.getSessionId(); this.sessionId = initiate.getSessionId();
this.remote = initiate.getInitiator(); this.remote = initiate.getInitiator();
this.source = null; this.source = null;
@ -142,6 +144,25 @@ public class JingleFileTransferSession extends AbstractJingleSession {
return IQ.createResultIQ(jingle); return IQ.createResultIQ(jingle);
} }
@Override
public JingleManager.FullJidAndSessionId getFullJidAndSessionId() {
return parent.getFullJidAndSessionId();
}
@Override
public JingleContent getReceivedContent() {
return parent.getReceivedContent();
}
@Override
public JingleContent getProposedContent() {
return parent.getProposedContent();
}
@Override
public JingleContent.Creator getRole() {
return parent.getRole();
}
} }
private static class OutgoingAccepted extends AbstractJingleSession { private static class OutgoingAccepted extends AbstractJingleSession {
@ -160,10 +181,28 @@ public class JingleFileTransferSession extends AbstractJingleSession {
} }
JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this); JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this);
transportHandler.prepareOutgoingSession(parent.getFullJidAndSessionId(), parent.proposedContent);
parent.addTransportInfoListener(transportHandler); parent.addTransportInfoListener(transportHandler);
transportHandler.establishOutgoingSession(parent.getFullJidAndSessionId(), parent.receivedContent, transportHandler.establishOutgoingSession(parent.outgoingFileTransferSessionEstablishedCallback);
parent.proposedContent, parent.outgoingFileTransferSessionEstablishedCallback); }
@Override
public JingleManager.FullJidAndSessionId getFullJidAndSessionId() {
return parent.getFullJidAndSessionId();
}
@Override
public JingleContent getReceivedContent() {
return parent.getReceivedContent();
}
@Override
public JingleContent getProposedContent() {
return parent.getProposedContent();
}
@Override
public JingleContent.Creator getRole() {
return parent.getRole();
} }
} }
@ -215,12 +254,34 @@ public class JingleFileTransferSession extends AbstractJingleSession {
JingleFileTransferManager.getInstanceFor(connection).notifyIncomingJingleFileTransferListeners(file, callback); JingleFileTransferManager.getInstanceFor(connection).notifyIncomingJingleFileTransferListeners(file, callback);
return IQ.createResultIQ(initiate); return IQ.createResultIQ(initiate);
} }
@Override
public JingleManager.FullJidAndSessionId getFullJidAndSessionId() {
return parent.getFullJidAndSessionId();
}
@Override
public JingleContent getReceivedContent() {
return parent.getReceivedContent();
}
@Override
public JingleContent getProposedContent() {
return parent.getProposedContent();
}
@Override
public JingleContent.Creator getRole() {
return parent.getRole();
}
} }
private static class IncomingAccepted extends AbstractJingleSession { private static class IncomingAccepted extends AbstractJingleSession {
private final JingleFileTransferSession parent;
public IncomingAccepted(XMPPConnection connection, JingleFileTransferSession parent) { public IncomingAccepted(XMPPConnection connection, JingleFileTransferSession parent) {
super(connection); super(connection);
this.parent = parent;
AbstractJingleTransportManager<?> tm = null; AbstractJingleTransportManager<?> tm = null;
try { try {
tm = JingleTransportManager.getJingleContentTransportManager( tm = JingleTransportManager.getJingleContentTransportManager(
@ -231,8 +292,27 @@ public class JingleFileTransferSession extends AbstractJingleSession {
JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this); JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this);
parent.addTransportInfoListener(transportHandler); parent.addTransportInfoListener(transportHandler);
transportHandler.establishIncomingSession(parent.getFullJidAndSessionId(), parent.receivedContent, transportHandler.establishIncomingSession(parent.incomingFileTransferSessionEstablishedCallback);
parent.proposedContent, parent.incomingFileTransferSessionEstablishedCallback); }
@Override
public JingleManager.FullJidAndSessionId getFullJidAndSessionId() {
return parent.getFullJidAndSessionId();
}
@Override
public JingleContent getReceivedContent() {
return parent.getReceivedContent();
}
@Override
public JingleContent getProposedContent() {
return parent.getProposedContent();
}
@Override
public JingleContent.Creator getRole() {
return parent.getRole();
} }
} }
@ -440,10 +520,36 @@ public class JingleFileTransferSession extends AbstractJingleSession {
} }
} }
@Override
public JingleManager.FullJidAndSessionId getFullJidAndSessionId() { public JingleManager.FullJidAndSessionId getFullJidAndSessionId() {
return new JingleManager.FullJidAndSessionId(remote, sessionId); return new JingleManager.FullJidAndSessionId(remote, sessionId);
} }
@Override
public JingleContent getProposedContent() {
return proposedContent;
}
public void setProposedContent(JingleContent proposedContent) {
this.proposedContent = proposedContent;
}
@Override
public JingleContent getReceivedContent() {
return receivedContent;
}
public void setReceivedContent(JingleContent receivedContent) {
this.receivedContent = receivedContent;
}
@Override
public JingleContent.Creator getRole() {
return role;
}
@Override @Override
protected IQ handleSessionInitiate(Jingle sessionInitiate) { protected IQ handleSessionInitiate(Jingle sessionInitiate) {
return state.handleSessionInitiate(sessionInitiate); return state.handleSessionInitiate(sessionInitiate);

View file

@ -16,8 +16,6 @@
*/ */
package org.jivesoftware.smackx.jingle_ibb; package org.jivesoftware.smackx.jingle_ibb;
import java.lang.ref.WeakReference;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
@ -26,12 +24,10 @@ import org.jivesoftware.smackx.bytestreams.BytestreamRequest;
import org.jivesoftware.smackx.bytestreams.BytestreamSession; import org.jivesoftware.smackx.bytestreams.BytestreamSession;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager; import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager;
import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession; import org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleSessionHandler; import org.jivesoftware.smackx.jingle.JingleSessionHandler;
import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback; import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback;
import org.jivesoftware.smackx.jingle.JingleTransportHandler; import org.jivesoftware.smackx.jingle.JingleTransportHandler;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.exception.JingleTransportFailureException; import org.jivesoftware.smackx.jingle.exception.JingleTransportFailureException;
import org.jivesoftware.smackx.jingle_ibb.element.JingleIBBTransport; import org.jivesoftware.smackx.jingle_ibb.element.JingleIBBTransport;
@ -40,27 +36,20 @@ import org.jivesoftware.smackx.jingle_ibb.element.JingleIBBTransport;
*/ */
public class JingleIBBTransportHandler implements JingleTransportHandler<JingleIBBTransport> { public class JingleIBBTransportHandler implements JingleTransportHandler<JingleIBBTransport> {
private final WeakReference<JingleSessionHandler> jingleSessionHandler; private final JingleSessionHandler jingleSessionHandler;
public JingleIBBTransportHandler(JingleSessionHandler sessionHandler) { public JingleIBBTransportHandler(JingleSessionHandler sessionHandler) {
this.jingleSessionHandler = new WeakReference<>(sessionHandler); this.jingleSessionHandler = sessionHandler;
} }
@Override @Override
public void prepareOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent content) { public void establishOutgoingSession(JingleTransportEstablishedCallback callback) {
// Nothing to do
}
@Override
public void establishOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId,
JingleContent receivedContent,
JingleContent proposedContent,
JingleTransportEstablishedCallback callback) {
InBandBytestreamSession session; InBandBytestreamSession session;
try { try {
session = InBandBytestreamManager.getByteStreamManager(getConnection()) session = InBandBytestreamManager.getByteStreamManager(getConnection())
.establishSession(fullJidAndSessionId.getFullJid(), fullJidAndSessionId.getSessionId()); .establishSession(jingleSessionHandler.getFullJidAndSessionId().getFullJid(), jingleSessionHandler.getFullJidAndSessionId().getSessionId());
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) { } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) {
callback.onSessionFailure(new JingleTransportFailureException(e)); callback.onSessionFailure(new JingleTransportFailureException(e));
return; return;
@ -70,15 +59,12 @@ public class JingleIBBTransportHandler implements JingleTransportHandler<JingleI
} }
@Override @Override
public void establishIncomingSession(final JingleManager.FullJidAndSessionId fullJidAndSessionId, public void establishIncomingSession(final JingleTransportEstablishedCallback callback) {
JingleContent receivedContent,
JingleContent proposedContent,
final JingleTransportEstablishedCallback callback) {
InBandBytestreamManager.getByteStreamManager(getConnection()).addIncomingBytestreamListener(new BytestreamListener() { InBandBytestreamManager.getByteStreamManager(getConnection()).addIncomingBytestreamListener(new BytestreamListener() {
@Override @Override
public void incomingBytestreamRequest(BytestreamRequest request) { public void incomingBytestreamRequest(BytestreamRequest request) {
if (request.getFrom().asFullJidIfPossible().equals(fullJidAndSessionId.getFullJid()) if (request.getFrom().asFullJidIfPossible().equals(jingleSessionHandler.getFullJidAndSessionId().getFullJid())
&& request.getSessionID().equals(fullJidAndSessionId.getSessionId())) { && request.getSessionID().equals(jingleSessionHandler.getFullJidAndSessionId().getSessionId())) {
BytestreamSession session; BytestreamSession session;
try { try {
@ -96,12 +82,11 @@ public class JingleIBBTransportHandler implements JingleTransportHandler<JingleI
@Override @Override
public XMPPConnection getConnection() { public XMPPConnection getConnection() {
JingleSessionHandler sessionHandler = jingleSessionHandler.get(); return jingleSessionHandler.getConnection();
return sessionHandler != null ? sessionHandler.getConnection() : null;
} }
@Override @Override
public void onTransportInfoReceived(Jingle transportInfo) { public boolean onTransportInfoReceived(Jingle transportInfo) {
return false;
} }
} }

View file

@ -1,23 +1,6 @@
/**
*
* Copyright © 2017 Paul Schaub
*
* 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.jingle_s5b; package org.jivesoftware.smackx.jingle_s5b;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.Socket; import java.net.Socket;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.logging.Level; import java.util.logging.Level;
@ -31,97 +14,79 @@ import org.jivesoftware.smackx.bytestreams.socks5.Socks5Client;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy; import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Utils; import org.jivesoftware.smackx.bytestreams.socks5.Socks5Utils;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream; import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle.JingleManager;
import org.jivesoftware.smackx.jingle.JingleSessionHandler; import org.jivesoftware.smackx.jingle.JingleSessionHandler;
import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback; import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback;
import org.jivesoftware.smackx.jingle.JingleTransportHandler; import org.jivesoftware.smackx.jingle.JingleTransportHandler;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleAction; import org.jivesoftware.smackx.jingle.element.JingleAction;
import org.jivesoftware.smackx.jingle.element.JingleContent; import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate; import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;
import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransport; import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransport;
import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransportCandidate; import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransportCandidate;
import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransportInfo; import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransportInfo;
/** /**
* JingleTransportHandler for Socks5Bytestreams. * Implementation of a Jingle SOCKS5 Transport handler.
*/ */
public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS5BTransport> { public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS5BTransport> {
private static final Logger LOGGER = Logger.getLogger(JingleS5BTransportHandler.class.getName()); private static final Logger LOGGER = Logger.getLogger(JingleS5BTransportHandler.class.getName());
private final WeakReference<JingleSessionHandler> sessionHandler; private JingleS5BTransportHandlerInterface state;
private final JingleSessionHandler sessionHandler;
private JingleManager.FullJidAndSessionId fullJidAndSessionId;
private JingleContent receivedContent;
private JingleContent proposedContent;
private JingleTransportEstablishedCallback callback; private JingleTransportEstablishedCallback callback;
private JingleS5BTransport myTransport;
private JingleS5BTransportCandidate receivedCandidateUsed = null; private boolean localCandidateError = false;
private JingleS5BTransportCandidate selectedCandidateUsed = null; private boolean remoteCandidateError = false;
private Socket connectedSocket;
private Socket connectedSocket = null; private JingleS5BTransportCandidate usedCandidate;
public JingleS5BTransportHandler(JingleSessionHandler sessionHandler) { public JingleS5BTransportHandler(JingleSessionHandler sessionHandler) {
this.sessionHandler = new WeakReference<>(sessionHandler); this.sessionHandler = sessionHandler;
} }
@Override @Override
public void prepareOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent content) { public boolean onTransportInfoReceived(Jingle transportInfo) {
myTransport = (JingleS5BTransport) content.getJingleTransports().get(0); boolean handled = state.onTransportInfoReceived(transportInfo);
Socks5Proxy.getSocks5Proxy().addLocalAddress(myTransport.getDestinationAddress()); if (handled) {
return true;
}
return false;
} }
@Override @Override
public void establishOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, public void establishOutgoingSession(JingleTransportEstablishedCallback callback) {
JingleContent receivedContent, establishSession(callback);
JingleContent proposedContent,
JingleTransportEstablishedCallback callback) {
establishSession(fullJidAndSessionId, receivedContent, proposedContent, callback);
} }
@Override @Override
public void establishIncomingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, public void establishIncomingSession(JingleTransportEstablishedCallback callback) {
JingleContent receivedContent, establishSession(callback);
JingleContent proposedContent,
JingleTransportEstablishedCallback callback) {
establishSession(fullJidAndSessionId, receivedContent, proposedContent, callback);
} }
void establishSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, void establishSession(JingleTransportEstablishedCallback callback) {
JingleContent receivedContent,
JingleContent proposedContent,
JingleTransportEstablishedCallback callback) {
this.fullJidAndSessionId = fullJidAndSessionId;
this.receivedContent = receivedContent;
this.proposedContent = proposedContent;
this.callback = callback; this.callback = callback;
JingleS5BTransport transport = (JingleS5BTransport) sessionHandler.getReceivedContent().getJingleTransports().get(0);
JingleContentTransport hopefullyS5BTransport = receivedContent.getJingleTransports().get(0);
if (!hopefullyS5BTransport.getNamespace().equals(JingleS5BTransport.NAMESPACE_V1)) {
throw new IllegalArgumentException("Transport must be a JingleS5BTransport.");
}
JingleS5BTransport transport = (JingleS5BTransport) hopefullyS5BTransport;
Socks5Proxy.getSocks5Proxy().addLocalAddress(Socks5Utils.createDigest( Socks5Proxy.getSocks5Proxy().addLocalAddress(Socks5Utils.createDigest(
fullJidAndSessionId.getSessionId(), //SessionID sessionHandler.getFullJidAndSessionId().getSessionId(), //SessionID
getConnection().getUser().asFullJidIfPossible(), //Us getConnection().getUser().asFullJidIfPossible(), //Us
fullJidAndSessionId.getFullJid())); //Them sessionHandler.getFullJidAndSessionId().getFullJid())); //Them
JingleS5BTransportCandidate connectedCandidate = null;
JingleS5BTransportCandidate usedCandidate = null;
for (JingleContentTransportCandidate c : transport.getCandidates()) { for (JingleContentTransportCandidate c : transport.getCandidates()) {
usedCandidate = (JingleS5BTransportCandidate) c; JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c;
Bytestream.StreamHost streamHost = usedCandidate.getStreamHost(); Bytestream.StreamHost streamHost = candidate.getStreamHost();
String address = streamHost.getAddress() + ":" + streamHost.getPort(); String address = streamHost.getAddress() + ":" + streamHost.getPort();
LOGGER.log(Level.INFO, "Connect to " + address);
// establish socket // establish socket
try { try {
final Socks5Client socks5Client = new Socks5Client(streamHost, transport.getDestinationAddress()); final Socks5Client socks5Client = new Socks5Client(streamHost, transport.getDestinationAddress());
connectedSocket = socks5Client.getSocket(10 * 1000); connectedSocket = socks5Client.getSocket(10 * 1000);
selectedCandidateUsed = usedCandidate; connectedCandidate = candidate;
break; break;
} }
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) { catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
@ -129,136 +94,220 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
} }
} }
if (connectedSocket != null) { // Send candidate-used
//Send candidate-used if (connectedCandidate != null) {
Jingle jingle = createCandidateUsed(usedCandidate, fullJidAndSessionId, receivedContent); Jingle.Builder jb = Jingle.getBuilder();
jb.setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId())
.setAction(JingleAction.transport_info);
JingleContent.Builder cb = JingleContent.getBuilder();
cb.setSenders(sessionHandler.getProposedContent().getSenders())
.setCreator(sessionHandler.getProposedContent().getCreator())
.setName(sessionHandler.getProposedContent().getName());
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
tb.setStreamId(((JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0)).getStreamId())
.setCandidateUsed(connectedCandidate.getCandidateId());
Jingle jingle = jb.addJingleContent(cb.addTransport(tb.build()).build()).build();
jingle.setTo(sessionHandler.getFullJidAndSessionId().getFullJid());
jingle.setFrom(getConnection().getUser());
this.usedCandidate = connectedCandidate;
try { try {
getConnection().sendStanza(jingle); getConnection().sendStanza(jingle);
} catch (SmackException.NotConnectedException | InterruptedException e) { } catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-used stanza: " + e, e); LOGGER.log(Level.SEVERE, "Could not send candidate-used transport-info: " + e, e);
} }
} else { } else {
}
}
void activateProxy() {
//Activate proxy
if (selectedCandidateUsed.getType() == JingleS5BTransportCandidate.Type.proxy) {
Bytestream bytestream = new Bytestream(fullJidAndSessionId.getSessionId());
bytestream.setToActivate(fullJidAndSessionId.getFullJid());
bytestream.setTo(fullJidAndSessionId.getFullJid());
try {
getConnection().sendStanza(bytestream);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not activate proxy: " + e, e);
}
}
Jingle activate = createCandidateActive(selectedCandidateUsed, fullJidAndSessionId, receivedContent);
try {
getConnection().sendStanza(activate);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send transport-activated: " + e, e);
}
callback.onSessionEstablished(new Socks5BytestreamSession(connectedSocket, false));
}
Jingle createCandidateUsed(JingleS5BTransportCandidate usedCandidate, JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent receivedContent) {
Jingle.Builder jb = Jingle.getBuilder(); Jingle.Builder jb = Jingle.getBuilder();
jb.setSessionId(fullJidAndSessionId.getSessionId()) jb.setAction(JingleAction.transport_info)
.setAction(JingleAction.transport_info) .setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId());
.setInitiator(getConnection().getUser());
JingleContent.Builder cb = JingleContent.getBuilder(); JingleContent.Builder cb = JingleContent.getBuilder();
cb.setName(receivedContent.getName()) cb.setName(sessionHandler.getProposedContent().getName())
.setCreator(receivedContent.getCreator()) .setCreator(sessionHandler.getProposedContent().getCreator())
.setSenders(receivedContent.getSenders()); .setSenders(sessionHandler.getProposedContent().getSenders());
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder(); JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
tb.setStreamId(((JingleS5BTransport) receivedContent.getJingleTransports().get(0)).getStreamId()); tb.setCandidateError()
tb.setCandidateUsed(usedCandidate.getCandidateId()); .setStreamId(((JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0))
.getStreamId());
cb.addTransport(tb.build()); cb.addTransport(tb.build());
jb.addJingleContent(cb.build()); jb.addJingleContent(cb.build());
Jingle jingle = jb.build(); Jingle jingle = jb.build();
jingle.setFrom(getConnection().getUser()); jingle.setFrom(getConnection().getUser());
jingle.setTo(fullJidAndSessionId.getFullJid()); jingle.setTo(sessionHandler.getFullJidAndSessionId().getFullJid());
return jingle;
try {
getConnection().sendStanza(jingle);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send candidate-error transport-info: " + e, e);
}
} }
Jingle createCandidateActive(JingleS5BTransportCandidate usedCandidate, JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent receivedContent) { this.state = new FreshEstablishing(this);
Jingle.Builder jb = Jingle.getBuilder();
jb.setAction(JingleAction.transport_info)
.setSessionId(fullJidAndSessionId.getSessionId())
.setInitiator(fullJidAndSessionId.getFullJid());
JingleContent.Builder cb = JingleContent.getBuilder();
cb.setCreator(JingleContent.Creator.initiator)
.setName(receivedContent.getName());
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
tb.setStreamId(((JingleS5BTransport) receivedContent.getJingleTransports().get(0)).getStreamId());
tb.setCandidateActivated(usedCandidate.getCandidateId());
cb.addTransport(tb.build());
jb.addJingleContent(cb.build());
Jingle activate = jb.build();
activate.setTo(fullJidAndSessionId.getFullJid());
activate.setFrom(getConnection().getUser());
return activate;
} }
@Override @Override
public XMPPConnection getConnection() { public XMPPConnection getConnection() {
JingleSessionHandler handler = sessionHandler.get(); return sessionHandler.getConnection();
return handler != null ? handler.getConnection() : null; }
private interface JingleS5BTransportHandlerInterface {
boolean onTransportInfoReceived(Jingle transportInfo);
}
private class FreshEstablishing implements JingleS5BTransportHandlerInterface{
private final JingleS5BTransportHandler parent;
public FreshEstablishing(JingleS5BTransportHandler parent) {
this.parent = parent;
} }
@Override @Override
public void onTransportInfoReceived(Jingle jingle) { public boolean onTransportInfoReceived(Jingle transportInfo) {
if (jingle.getAction() != JingleAction.transport_info) { if (transportInfo.getAction() != JingleAction.transport_info) {
throw new IllegalArgumentException("Jingle Action must be transport-info."); throw new IllegalArgumentException("Jingle Action must be transport-info.");
} }
JingleContentTransport jingleTransport = jingle.getContents().get(0).getJingleTransports().get(0);
if (jingleTransport == null || !jingleTransport.getNamespace().equals(JingleS5BTransport.NAMESPACE_V1)) {
throw new IllegalArgumentException("Jingle must contain transport of type S5B.");
}
JingleS5BTransport transport = (JingleS5BTransport) jingleTransport; JingleContent content = transportInfo.getContents().get(0);
JingleS5BTransport transport = (JingleS5BTransport) content.getJingleTransports().get(0);
JingleS5BTransportInfo info = (JingleS5BTransportInfo) transport.getInfos().get(0); JingleS5BTransportInfo info = (JingleS5BTransportInfo) transport.getInfos().get(0);
if (info == null) { if (info == null) {
throw new IllegalArgumentException("Jingle must contain at least one JingleS5BTransportInfo."); throw new IllegalArgumentException("Jingle must contain at least one JingleS5BTransportInfo.");
} }
JingleS5BTransportCandidate finalCandidateUsed; switch (info.getElementName()) {
case JingleS5BTransportInfo.CandidateUsed.ELEMENT:
JingleS5BTransportInfo.CandidateUsed candidateUsed = (JingleS5BTransportInfo.CandidateUsed) info;
if (info.getElementName().equals(JingleS5BTransportInfo.CandidateUsed.ELEMENT)) { //do we know the used candidate?
String candidateUsedId = ((JingleS5BTransportInfo.CandidateUsed) info).getCandidateId(); JingleS5BTransportCandidate usedCandidate = ((JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0))
for (JingleContentTransportCandidate c : myTransport.getCandidates()) { .getCandidate(candidateUsed.getCandidateId());
if (((JingleS5BTransportCandidate) c).getCandidateId().equals(candidateUsedId)) {
receivedCandidateUsed = (JingleS5BTransportCandidate) c; if (usedCandidate == null) {
} //Error unknown candidate.
}
if (receivedCandidateUsed == null) {
//TODO: Unknown candidate.
} else { } else {
//We already have a remote candidate selected.
if (parent.usedCandidate == null ||
parent.usedCandidate.getPriority() < usedCandidate.getPriority() ||
(parent.usedCandidate.getPriority() == usedCandidate.getPriority() &&
parent.sessionHandler.getRole() == JingleContent.Creator.initiator)) {
parent.usedCandidate = usedCandidate;
Bytestream.StreamHost streamHost = usedCandidate.getStreamHost();
String address = streamHost.getAddress() + ":" + streamHost.getPort();
// establish socket
try {
final Socks5Client socks5Client = new Socks5Client(streamHost,
((JingleS5BTransport) sessionHandler.getReceivedContent().getJingleTransports().get(0))
.getDestinationAddress());
connectedSocket = socks5Client.getSocket(10 * 1000);
break;
}
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not connect to own proxy at " + address + ": " + e, e);
}
}
parent.state = new CandidateUsedReceived(parent);
}
break;
case JingleS5BTransportInfo.CandidateActivated.ELEMENT:
//Tie break
break;
case JingleS5BTransportInfo.CandidateError.ELEMENT:
parent.remoteCandidateError = true;
if (parent.localCandidateError) {
//Session transport-failed
} else {
state = new CandidateUsedReceived(parent);
}
break;
case JingleS5BTransportInfo.ProxyError.ELEMENT:
break;
}
return false;
}
}
private class CandidateUsedReceived implements JingleS5BTransportHandlerInterface {
private final JingleS5BTransportHandler parent;
public CandidateUsedReceived(JingleS5BTransportHandler parent) {
super();
this.parent = parent;
activateTransports();
}
void activateTransports() {
//If proxy...
if (parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
JingleS5BTransport transport = (JingleS5BTransport) parent.sessionHandler
.getProposedContent().getJingleTransports().get(0);
// ...and our candidate
if (transport.getCandidates().contains(parent.usedCandidate)) {
// activate proxy.
Bytestream activateProxy = new Bytestream(transport.getStreamId());
activateProxy.setToActivate(parent.usedCandidate.getJid());
activateProxy.setTo(parent.usedCandidate.getJid());
try {
getConnection().createStanzaCollectorAndSend(activateProxy).nextResultOrThrow();
//Send candidate-activate
Jingle.Builder jb = Jingle.getBuilder();
jb.setAction(JingleAction.transport_info)
.setSessionId(parent.sessionHandler.getFullJidAndSessionId().getSessionId())
.setInitiator(
parent.sessionHandler.getRole() == JingleContent.Creator.initiator ?
getConnection().getUser() : parent.sessionHandler.getFullJidAndSessionId().getFullJid());
JingleContent proposed = parent.sessionHandler.getProposedContent();
JingleContent.Builder cb = JingleContent.getBuilder();
cb.setName(proposed.getName())
.setCreator(proposed.getCreator())
.setSenders(proposed.getSenders());
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
tb.setCandidateActivated(parent.usedCandidate.getCandidateId())
.setStreamId(((JingleS5BTransport) proposed.getJingleTransports().get(0)).getStreamId());
cb.addTransport(tb.build());
jb.addJingleContent(cb.build());
Jingle j = jb.build();
j.setTo(parent.sessionHandler.getFullJidAndSessionId().getFullJid());
j.setFrom(getConnection().getUser());
getConnection().sendStanza(j);
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send candidate-active transport-info: " + e, e);
} }
} }
} }
void onCandidateUsed() { callback.onSessionEstablished(new Socks5BytestreamSession(parent.connectedSocket, parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.direct));
if (selectedCandidateUsed.getPriority() > receivedCandidateUsed.getPriority()) { }
activateProxy();
} else if (selectedCandidateUsed.getPriority() < receivedCandidateUsed.getPriority()) { @Override
//createCandidateActive() public boolean onTransportInfoReceived(Jingle transportInfo) {
} else { //== return false;
} }
} }
} }

View file

@ -70,6 +70,20 @@ public class JingleS5BTransport extends JingleContentTransport {
xml.attribute(ATTR_SID, streamId); xml.attribute(ATTR_SID, streamId);
} }
public boolean hasCandidate(String candidateId) {
return getCandidate(candidateId) != null;
}
public JingleS5BTransportCandidate getCandidate(String candidateId) {
for (JingleContentTransportCandidate c : candidates) {
JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c;
if (candidate.getCandidateId().equals(candidateId)) {
return candidate;
}
}
return null;
}
public static Builder getBuilder() { public static Builder getBuilder() {
return new Builder(); return new Builder();
} }

View file

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.jingle;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.jingle.element.Jingle; import org.jivesoftware.smackx.jingle.element.Jingle;
import org.jivesoftware.smackx.jingle.element.JingleContent;
public interface JingleSessionHandler { public interface JingleSessionHandler {
@ -30,4 +31,12 @@ public interface JingleSessionHandler {
void removeTransportInfoListener(JingleTransportInfoListener listener); void removeTransportInfoListener(JingleTransportInfoListener listener);
JingleManager.FullJidAndSessionId getFullJidAndSessionId();
JingleContent getReceivedContent();
JingleContent getProposedContent();
JingleContent.Creator getRole();
} }

View file

@ -17,7 +17,6 @@
package org.jivesoftware.smackx.jingle; package org.jivesoftware.smackx.jingle;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.jingle.element.JingleContent;
import org.jivesoftware.smackx.jingle.element.JingleContentTransport; import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
/** /**
@ -25,18 +24,9 @@ import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
*/ */
public interface JingleTransportHandler<D extends JingleContentTransport> extends JingleTransportInfoListener { public interface JingleTransportHandler<D extends JingleContentTransport> extends JingleTransportInfoListener {
void prepareOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, void establishOutgoingSession(JingleTransportEstablishedCallback callback);
JingleContent content);
void establishOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, void establishIncomingSession(JingleTransportEstablishedCallback callback);
JingleContent receivedContent,
JingleContent proposedContent,
JingleTransportEstablishedCallback callback);
void establishIncomingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId,
JingleContent receivedContent,
JingleContent proposedContent,
JingleTransportEstablishedCallback callback);
XMPPConnection getConnection(); XMPPConnection getConnection();
} }

View file

@ -23,5 +23,5 @@ import org.jivesoftware.smackx.jingle.element.Jingle;
*/ */
public interface JingleTransportInfoListener { public interface JingleTransportInfoListener {
void onTransportInfoReceived(Jingle transportInfo); boolean onTransportInfoReceived(Jingle transportInfo);
} }