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 JingleContent proposedContent;
private JingleContent receivedContent;
private JingleTransportHandler<?> transportHandler;
private final FullJid remote;
private final String sessionId;
private final JingleContent.Creator role;
@ -99,6 +100,18 @@ public class JingleFileTransferSession extends AbstractJingleSession {
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);
}
@ -172,17 +185,7 @@ public class JingleFileTransferSession extends AbstractJingleSession {
super(connection);
this.parent = parent;
AbstractJingleTransportManager<?> tm;
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);
parent.transportHandler.establishOutgoingSession(parent.outgoingFileTransferSessionEstablishedCallback);
}
@Override
@ -279,10 +282,10 @@ public class JingleFileTransferSession extends AbstractJingleSession {
private static class IncomingAccepted extends AbstractJingleSession {
private final JingleFileTransferSession parent;
public IncomingAccepted(XMPPConnection connection, JingleFileTransferSession parent) {
IncomingAccepted(XMPPConnection connection, JingleFileTransferSession parent) {
super(connection);
this.parent = parent;
AbstractJingleTransportManager<?> tm = null;
AbstractJingleTransportManager<?> tm;
try {
tm = JingleTransportManager.getJingleContentTransportManager(
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.");
}
JingleTransportHandler<?> transportHandler = tm.createJingleTransportHandler(this);
parent.addTransportInfoListener(transportHandler);
transportHandler.establishIncomingSession(parent.incomingFileTransferSessionEstablishedCallback);
parent.transportHandler = tm.createJingleTransportHandler(this);
parent.addTransportInfoListener(parent.transportHandler);
parent.transportHandler.prepareSession();
parent.transportHandler.establishIncomingSession(parent.incomingFileTransferSessionEstablishedCallback);
}
@Override

View file

@ -42,6 +42,11 @@ public class JingleIBBTransportHandler implements JingleTransportHandler<JingleI
this.jingleSessionHandler = sessionHandler;
}
@Override
public void prepareSession() {
}
@Override
public void establishOutgoingSession(JingleTransportEstablishedCallback callback) {
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.Socks5Client;
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.jingle.JingleSessionHandler;
import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback;
@ -56,8 +55,15 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
return false;
}
@Override
public void prepareSession() {
JingleS5BTransport proposedTransport = (JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0);
Socks5Proxy.getSocks5Proxy().addTransfer(proposedTransport.getDestinationAddress());
}
@Override
public void establishOutgoingSession(JingleTransportEstablishedCallback callback) {
LOGGER.log(Level.INFO, "establish outgoing session");
establishSession(callback);
}
@ -66,28 +72,23 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
establishSession(callback);
}
void establishSession(JingleTransportEstablishedCallback callback) {
private void establishSession(JingleTransportEstablishedCallback callback) {
this.callback = callback;
JingleS5BTransport transport = (JingleS5BTransport) sessionHandler.getReceivedContent().getJingleTransports().get(0);
Socks5Proxy.getSocks5Proxy().addLocalAddress(Socks5Utils.createDigest(
sessionHandler.getFullJidAndSessionId().getSessionId(), //SessionID
getConnection().getUser().asFullJidIfPossible(), //Us
sessionHandler.getFullJidAndSessionId().getFullJid())); //Them
JingleS5BTransport receivedTransport = (JingleS5BTransport) sessionHandler.getReceivedContent().getJingleTransports().get(0);
JingleS5BTransportCandidate connectedCandidate = null;
for (JingleContentTransportCandidate c : transport.getCandidates()) {
for (JingleContentTransportCandidate c : receivedTransport.getCandidates()) {
JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) c;
Bytestream.StreamHost streamHost = candidate.getStreamHost();
String address = streamHost.getAddress() + ":" + streamHost.getPort();
// establish socket
try {
final Socks5Client socks5Client = new Socks5Client(streamHost, transport.getDestinationAddress());
final Socks5Client socks5Client = new Socks5Client(streamHost, receivedTransport.getDestinationAddress());
connectedSocket = socks5Client.getSocket(10 * 1000);
connectedCandidate = candidate;
LOGGER.log(Level.INFO, "Connected to "+address);
LOGGER.log(Level.INFO, "Connected to " + address + " using " + receivedTransport.getDestinationAddress());
break;
}
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
@ -97,6 +98,7 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
// Send candidate-used
if (connectedCandidate != null) {
LOGGER.log(Level.INFO, "Send candidate used");
Jingle.Builder jb = Jingle.getBuilder();
jb.setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId())
.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);
}
} else {
//Candidate error
LOGGER.log(Level.INFO, "Send candidate-error");
localCandidateError = true;
Jingle.Builder jb = Jingle.getBuilder();
jb.setAction(JingleAction.transport_info)
.setSessionId(sessionHandler.getFullJidAndSessionId().getSessionId());
@ -205,14 +210,18 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
parent.usedCandidate = usedCandidate;
Bytestream.StreamHost streamHost = usedCandidate.getStreamHost();
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
try {
final Socks5Client socks5Client = new Socks5Client(streamHost,
((JingleS5BTransport) sessionHandler.getProposedContent().getJingleTransports().get(0))
.getDestinationAddress());
final Socks5Client socks5Client = new Socks5Client(streamHost, digest);
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) {
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();
}
@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() {
//If proxy...
if (parent.usedCandidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
@ -263,12 +287,17 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
.getProposedContent().getJingleTransports().get(0);
// ...and our candidate
if (transport.getCandidates().contains(parent.usedCandidate)) {
if (!parent.usedCandidate.getJid().asFullJidIfPossible().equals(getConnection().getUser().asFullJidIfPossible())) {
// activate proxy.
Bytestream activateProxy = new Bytestream(transport.getStreamId());
activateProxy.setToActivate(parent.usedCandidate.getJid());
activateProxy.setTo(parent.usedCandidate.getJid());
try {
getConnection().createStanzaCollectorAndSend(activateProxy).nextResultOrThrow();
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not activate proxy server: " + e, e);
}
}
//Send candidate-activate
Jingle.Builder jb = Jingle.getBuilder();
@ -295,20 +324,16 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
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);
}
} catch (SmackException.NoResponseException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.SEVERE, "Could not send candidate-active transport-info: " + e, e);
}
}
}
LOGGER.log(Level.INFO, "Established connection with " + parent.usedCandidate.getHost() + " using " + Socks5Proxy.getSocks5Proxy().getLocalAddresses().get(0));
LOGGER.log(Level.INFO, "Established connection with " + parent.usedCandidate.getHost());
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
*/
protected void addTransfer(String digest) {
public void addTransfer(String digest) {
this.allowedConnections.add(digest);
}
@ -361,7 +361,7 @@ public final class Socks5Proxy {
*
* @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.connectionMap.remove(digest);
}

View file

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