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

Should work, but doesnt

This commit is contained in:
vanitasvitae 2017-06-14 17:56:46 +02:00
parent 7a7b45ad9b
commit f3c1eebd05
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
5 changed files with 113 additions and 76 deletions

View file

@ -61,6 +61,7 @@ public class JingleFileTransferSession extends AbstractJingleSession {
private File target; private File target;
private JingleContent proposedContent; private JingleContent proposedContent;
private JingleContent receivedContent; private JingleContent receivedContent;
private JingleTransportHandler<?> transportHandler;
private final FullJid remote; private final FullJid remote;
private final String sessionId; private final String sessionId;
private final JingleContent.Creator role; private final JingleContent.Creator role;
@ -99,6 +100,18 @@ public class JingleFileTransferSession extends AbstractJingleSession {
return; return;
} }
AbstractJingleTransportManager<?> tm;
try {
tm = JingleTransportManager.getJingleContentTransportManager(connection,
proposedContent.getJingleTransports().get(0));
} catch (UnsupportedJingleTransportException e) {
throw new AssertionError("Since we initiated the transport, we MUST know the transport method.");
}
transportHandler = tm.createJingleTransportHandler(this);
addTransportInfoListener(transportHandler);
transportHandler.prepareSession();
this.state = new OutgoingInitiated(connection, this); this.state = new OutgoingInitiated(connection, this);
} }
@ -172,17 +185,7 @@ public class JingleFileTransferSession extends AbstractJingleSession {
super(connection); super(connection);
this.parent = parent; this.parent = parent;
AbstractJingleTransportManager<?> tm; parent.transportHandler.establishOutgoingSession(parent.outgoingFileTransferSessionEstablishedCallback);
try {
tm = JingleTransportManager.getJingleContentTransportManager(connection,
parent.proposedContent.getJingleTransports().get(0));
} catch (UnsupportedJingleTransportException e) {
throw new AssertionError("Since we initiated the transport, we MUST know the transport method.");
}
JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this);
parent.addTransportInfoListener(transportHandler);
transportHandler.establishOutgoingSession(parent.outgoingFileTransferSessionEstablishedCallback);
} }
@Override @Override
@ -279,10 +282,10 @@ public class JingleFileTransferSession extends AbstractJingleSession {
private static class IncomingAccepted extends AbstractJingleSession { private static class IncomingAccepted extends AbstractJingleSession {
private final JingleFileTransferSession parent; private final JingleFileTransferSession parent;
public IncomingAccepted(XMPPConnection connection, JingleFileTransferSession parent) { IncomingAccepted(XMPPConnection connection, JingleFileTransferSession parent) {
super(connection); super(connection);
this.parent = parent; this.parent = parent;
AbstractJingleTransportManager<?> tm = null; AbstractJingleTransportManager<?> tm;
try { try {
tm = JingleTransportManager.getJingleContentTransportManager( tm = JingleTransportManager.getJingleContentTransportManager(
connection, parent.proposedContent.getJingleTransports().get(0)); connection, parent.proposedContent.getJingleTransports().get(0));
@ -290,9 +293,11 @@ public class JingleFileTransferSession extends AbstractJingleSession {
throw new AssertionError("Since we accepted the transfer, we MUST know the transport method."); throw new AssertionError("Since we accepted the transfer, we MUST know the transport method.");
} }
JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this); parent.transportHandler = tm.createJingleTransportHandler(this);
parent.addTransportInfoListener(transportHandler);
transportHandler.establishIncomingSession(parent.incomingFileTransferSessionEstablishedCallback); parent.addTransportInfoListener(parent.transportHandler);
parent.transportHandler.prepareSession();
parent.transportHandler.establishIncomingSession(parent.incomingFileTransferSessionEstablishedCallback);
} }
@Override @Override

View file

@ -42,6 +42,11 @@ public class JingleIBBTransportHandler implements JingleTransportHandler<JingleI
this.jingleSessionHandler = sessionHandler; this.jingleSessionHandler = sessionHandler;
} }
@Override
public void prepareSession() {
}
@Override @Override
public void establishOutgoingSession(JingleTransportEstablishedCallback callback) { public void establishOutgoingSession(JingleTransportEstablishedCallback callback) {
InBandBytestreamSession session; InBandBytestreamSession session;

View file

@ -12,7 +12,6 @@ import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession; import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Client; 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.packet.Bytestream; import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle.JingleSessionHandler; import org.jivesoftware.smackx.jingle.JingleSessionHandler;
import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback; import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback;
@ -56,8 +55,15 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
return false; return false;
} }
@Override
public void prepareSession() {
JingleS5BTransport proposedTransport = (JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0);
Socks5Proxy.getSocks5Proxy().addTransfer(proposedTransport.getDestinationAddress());
}
@Override @Override
public void establishOutgoingSession(JingleTransportEstablishedCallback callback) { public void establishOutgoingSession(JingleTransportEstablishedCallback callback) {
LOGGER.log(Level.INFO, "establish outgoing session");
establishSession(callback); establishSession(callback);
} }
@ -66,28 +72,23 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
establishSession(callback); establishSession(callback);
} }
void establishSession(JingleTransportEstablishedCallback callback) { private void establishSession(JingleTransportEstablishedCallback callback) {
this.callback = callback; this.callback = callback;
JingleS5BTransport transport = (JingleS5BTransport) sessionHandler.getReceivedContent().getJingleTransports().get(0); JingleS5BTransport receivedTransport = (JingleS5BTransport) sessionHandler.getReceivedContent().getJingleTransports().get(0);
Socks5Proxy.getSocks5Proxy().addLocalAddress(Socks5Utils.createDigest(
sessionHandler.getFullJidAndSessionId().getSessionId(), //SessionID
getConnection().getUser().asFullJidIfPossible(), //Us
sessionHandler.getFullJidAndSessionId().getFullJid())); //Them
JingleS5BTransportCandidate connectedCandidate = null; JingleS5BTransportCandidate connectedCandidate = null;
for (JingleContentTransportCandidate c : transport.getCandidates()) { for (JingleContentTransportCandidate c : receivedTransport.getCandidates()) {
JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c; JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c;
Bytestream.StreamHost streamHost = candidate.getStreamHost(); Bytestream.StreamHost streamHost = candidate.getStreamHost();
String address = streamHost.getAddress() + ":" + streamHost.getPort(); String address = streamHost.getAddress() + ":" + streamHost.getPort();
// establish socket // establish socket
try { try {
final Socks5Client socks5Client = new Socks5Client(streamHost, transport.getDestinationAddress()); final Socks5Client socks5Client = new Socks5Client(streamHost, receivedTransport.getDestinationAddress());
connectedSocket = socks5Client.getSocket(10 * 1000); connectedSocket = socks5Client.getSocket(10 * 1000);
connectedCandidate = candidate; connectedCandidate = candidate;
LOGGER.log(Level.INFO, "Connected to "+address); LOGGER.log(Level.INFO, "Connected to " + address + " using " + receivedTransport.getDestinationAddress());
break; break;
} }
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) { catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
@ -97,6 +98,7 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
// Send candidate-used // Send candidate-used
if (connectedCandidate != null) { if (connectedCandidate != null) {
LOGGER.log(Level.INFO, "Send candidate used");
Jingle.Builder jb = Jingle.getBuilder(); Jingle.Builder jb = Jingle.getBuilder();
jb.setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId()) jb.setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId())
.setAction(JingleAction.transport_info); .setAction(JingleAction.transport_info);
@ -122,6 +124,9 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
LOGGER.log(Level.SEVERE, "Could not send candidate-used transport-info: " + e, e); LOGGER.log(Level.SEVERE, "Could not send candidate-used transport-info: " + e, e);
} }
} else { } else {
//Candidate error
LOGGER.log(Level.INFO, "Send candidate-error");
localCandidateError = true;
Jingle.Builder jb = Jingle.getBuilder(); Jingle.Builder jb = Jingle.getBuilder();
jb.setAction(JingleAction.transport_info) jb.setAction(JingleAction.transport_info)
.setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId()); .setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId());
@ -205,14 +210,18 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
parent.usedCandidate = usedCandidate; parent.usedCandidate = usedCandidate;
Bytestream.StreamHost streamHost = usedCandidate.getStreamHost(); Bytestream.StreamHost streamHost = usedCandidate.getStreamHost();
String address = streamHost.getAddress() + ":" + streamHost.getPort(); String address = streamHost.getAddress() + ":" + streamHost.getPort();
String digest;
if (parent.sessionHandler.getProposedContent().getJingleTransports().get(0).getCandidates().contains(parent.usedCandidate)) {
digest = ((JingleS5BTransport) parent.sessionHandler.getProposedContent().getJingleTransports().get(0)).getDestinationAddress();
} else {
digest = ((JingleS5BTransport) parent.sessionHandler.getReceivedContent().getJingleTransports().get(0)).getDestinationAddress();
}
// establish socket // establish socket
try { try {
final Socks5Client socks5Client = new Socks5Client(streamHost, final Socks5Client socks5Client = new Socks5Client(streamHost, digest);
((JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0))
.getDestinationAddress());
connectedSocket = socks5Client.getSocket(10 * 1000); connectedSocket = socks5Client.getSocket(10 * 1000);
LOGGER.log(Level.INFO, "Connected to " + address); LOGGER.log(Level.INFO, "Connected to " + address + " using " + digest);
} }
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) { catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not connect to own proxy at " + address + ": " + e, e); LOGGER.log(Level.WARNING, "Could not connect to own proxy at " + address + ": " + e, e);
@ -256,6 +265,21 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
activateTransports(); activateTransports();
} }
@Override
public boolean onTransportInfoReceived(Jingle transportInfo) {
JingleContent content = transportInfo.getContents().get(0);
JingleS5BTransport transport = (JingleS5BTransport) content.getJingleTransports().get(0);
JingleS5BTransportInfo info = (JingleS5BTransportInfo) transport.getInfos().get(0);
if (info.getElementName().equals(JingleS5BTransportInfo.CandidateActivated.ELEMENT)) {
//TODO: Check if same candidate
LOGGER.log(Level.INFO, "Established connection with " + parent.usedCandidate.getHost());
callback.onSessionEstablished(new Socks5BytestreamSession(parent.connectedSocket, parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.direct));
return true;
}
return false;
}
void activateTransports() { void activateTransports() {
//If proxy... //If proxy...
if (parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.proxy) { if (parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
@ -263,52 +287,53 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
.getProposedContent().getJingleTransports().get(0); .getProposedContent().getJingleTransports().get(0);
// ...and our candidate // ...and our candidate
if (transport.getCandidates().contains(parent.usedCandidate)) { if (transport.getCandidates().contains(parent.usedCandidate)) {
// activate proxy. if (!parent.usedCandidate.getJid().asFullJidIfPossible().equals(getConnection().getUser().asFullJidIfPossible())) {
Bytestream activateProxy = new Bytestream(transport.getStreamId()); // activate proxy.
activateProxy.setToActivate(parent.usedCandidate.getJid()); Bytestream activateProxy = new Bytestream(transport.getStreamId());
activateProxy.setTo(parent.usedCandidate.getJid()); activateProxy.setToActivate(parent.usedCandidate.getJid());
try { activateProxy.setTo(parent.usedCandidate.getJid());
getConnection().createStanzaCollectorAndSend(activateProxy).nextResultOrThrow(); try {
getConnection().createStanzaCollectorAndSend(activateProxy).nextResultOrThrow();
//Send candidate-activate } catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) {
Jingle.Builder jb = Jingle.getBuilder(); LOGGER.log(Level.SEVERE, "Could not activate proxy server: " + e, e);
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);
} }
//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());
try {
getConnection().sendStanza(j);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send candidate-activated : " + e, e);
}
LOGGER.log(Level.INFO, "Established connection with " + parent.usedCandidate.getHost());
callback.onSessionEstablished(new Socks5BytestreamSession(parent.connectedSocket, parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.direct));
} }
} }
LOGGER.log(Level.INFO, "Established connection with " + parent.usedCandidate.getHost() + " using " + Socks5Proxy.getSocks5Proxy().getLocalAddresses().get(0));
callback.onSessionEstablished(new Socks5BytestreamSession(parent.connectedSocket, parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.direct));
}
@Override
public boolean onTransportInfoReceived(Jingle transportInfo) {
return false;
} }
} }
} }

View file

@ -348,7 +348,7 @@ public final class Socks5Proxy {
* *
* @param digest to be added to the list of allowed transfers * @param digest to be added to the list of allowed transfers
*/ */
protected void addTransfer(String digest) { public void addTransfer(String digest) {
this.allowedConnections.add(digest); this.allowedConnections.add(digest);
} }
@ -361,7 +361,7 @@ public final class Socks5Proxy {
* *
* @param digest to be removed from the list of allowed transfers * @param digest to be removed from the list of allowed transfers
*/ */
protected void removeTransfer(String digest) { public void removeTransfer(String digest) {
this.allowedConnections.remove(digest); this.allowedConnections.remove(digest);
this.connectionMap.remove(digest); this.connectionMap.remove(digest);
} }

View file

@ -24,6 +24,8 @@ import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
*/ */
public interface JingleTransportHandler<D extends JingleContentTransport> extends JingleTransportInfoListener { public interface JingleTransportHandler<D extends JingleContentTransport> extends JingleTransportInfoListener {
void prepareSession();
void establishOutgoingSession(JingleTransportEstablishedCallback callback); void establishOutgoingSession(JingleTransportEstablishedCallback callback);
void establishIncomingSession(JingleTransportEstablishedCallback callback); void establishIncomingSession(JingleTransportEstablishedCallback callback);