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

More Progress

This commit is contained in:
vanitasvitae 2017-06-12 16:42:46 +02:00
parent 4feda2ffa9
commit 1cd59370e6
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 106 additions and 77 deletions

View file

@ -504,11 +504,6 @@ public class JingleFileTransferSession extends AbstractJingleSession {
return state.handleTransportAccept(transportAccept);
}
@Override
protected IQ handleTransportInfo(Jingle transportInfo) {
return state.handleTransportInfo(transportInfo);
}
@Override
protected IQ handleTransportReplace(Jingle transportReplace) {
return state.handleTransportReplace(transportReplace);

View file

@ -52,9 +52,14 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
private final WeakReference<JingleSessionHandler> sessionHandler;
private JingleManager.FullJidAndSessionId fullJidAndSessionId;
private JingleContent receivedContent;
private JingleContent proposedContent;
private JingleTransportEstablishedCallback callback;
private JingleS5BTransport myTransport;
private JingleS5BTransportCandidate receivedCandidateUsed = null;
private JingleS5BTransportCandidate selectedCandidateUsed = null;
private Socket connectedSocket = null;
@ -88,6 +93,10 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
JingleContent receivedContent,
JingleContent proposedContent,
JingleTransportEstablishedCallback callback) {
this.fullJidAndSessionId = fullJidAndSessionId;
this.receivedContent = receivedContent;
this.proposedContent = proposedContent;
this.callback = callback;
JingleContentTransport hopefullyS5BTransport = receivedContent.getJingleTransports().get(0);
if (!hopefullyS5BTransport.getNamespace().equals(JingleS5BTransport.NAMESPACE_V1)) {
@ -110,17 +119,10 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
LOGGER.log(Level.INFO, "Connect to " + address);
// establish socket
try {
// build SOCKS5 client
final Socks5Client socks5Client = new Socks5Client(streamHost, transport.getDestinationAddress());
// connect to SOCKS5 proxy with a timeout
connectedSocket = socks5Client.getSocket(10 * 1000);
// set selected host
selectedCandidateUsed = usedCandidate;
break;
}
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not connect to " + address + ": " + e, e);
@ -128,6 +130,42 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
}
if (connectedSocket != null) {
//Send candidate-used
Jingle jingle = createCandidateUsed(usedCandidate, fullJidAndSessionId, receivedContent);
try {
getConnection().sendStanza(jingle);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-used stanza: " + e, e);
}
} 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();
jb.setSessionId(fullJidAndSessionId.getSessionId())
.setAction(JingleAction.transport_info)
@ -148,34 +186,20 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
Jingle jingle = jb.build();
jingle.setFrom(getConnection().getUser());
jingle.setTo(fullJidAndSessionId.getFullJid());
try {
getConnection().sendStanza(jingle);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-used stanza: " + e, e);
return jingle;
}
//Activate proxy
if (usedCandidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
Bytestream bytestream = new Bytestream(fullJidAndSessionId.getSessionId());
bytestream.setToActivate(fullJidAndSessionId.getFullJid());
bytestream.setTo(usedCandidate.getJid());
try {
getConnection().sendStanza(bytestream);
} catch (SmackException.NotConnectedException | InterruptedException e) {
LOGGER.log(Level.WARNING, "Could not activate proxy: " + e, e);
}
}
jb = Jingle.getBuilder();
Jingle createCandidateActive(JingleS5BTransportCandidate usedCandidate, JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent receivedContent) {
Jingle.Builder jb = Jingle.getBuilder();
jb.setAction(JingleAction.transport_info)
.setSessionId(fullJidAndSessionId.getSessionId());
//TODO: setInitiator
.setSessionId(fullJidAndSessionId.getSessionId())
.setInitiator(fullJidAndSessionId.getFullJid());
cb = JingleContent.getBuilder();
JingleContent.Builder cb = JingleContent.getBuilder();
cb.setCreator(JingleContent.Creator.initiator)
.setName(receivedContent.getName());
tb = JingleS5BTransport.getBuilder();
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
tb.setStreamId(((JingleS5BTransport) receivedContent.getJingleTransports().get(0)).getStreamId());
tb.setCandidateActivated(usedCandidate.getCandidateId());
@ -186,16 +210,7 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
activate.setTo(fullJidAndSessionId.getFullJid());
activate.setFrom(getConnection().getUser());
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));
} else {
}
return activate;
}
@Override
@ -204,10 +219,6 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
return handler != null ? handler.getConnection() : null;
}
public void setReceivedCandidateUsed(JingleS5BTransportCandidate used) {
this.receivedCandidateUsed = used;
}
@Override
public void onTransportInfoReceived(Jingle jingle) {
if (jingle.getAction() != JingleAction.transport_info) {
@ -223,8 +234,31 @@ public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS
if (info == null) {
throw new IllegalArgumentException("Jingle must contain at least one JingleS5BTransportInfo.");
}
JingleS5BTransportCandidate finalCandidateUsed;
if (info.getElementName().equals(JingleS5BTransportInfo.CandidateUsed.ELEMENT)) {
String candidateUsedId = ((JingleS5BTransportInfo.CandidateUsed) info).getCandidateId();
for (JingleContentTransportCandidate c : myTransport.getCandidates()) {
if (((JingleS5BTransportCandidate) c).getCandidateId().equals(candidateUsedId)) {
receivedCandidateUsed = (JingleS5BTransportCandidate) c;
}
}
if (receivedCandidateUsed == null) {
//TODO: Unknown candidate.
} else {
}
}
}
void onCandidateUsed() {
if (selectedCandidateUsed.getPriority() > receivedCandidateUsed.getPriority()) {
activateProxy();
} else if (selectedCandidateUsed.getPriority() < receivedCandidateUsed.getPriority()) {
//createCandidateActive()
} else { //==
}
}
}