1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-28 13:38:00 +01:00

SOCKS5 works again :)

This commit is contained in:
vanitasvitae 2017-06-28 11:40:27 +02:00
parent 3058ebe8b9
commit d537463a42
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 65 additions and 102 deletions

View file

@ -104,7 +104,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
return jutil.createErrorOutOfOrder(sessionAccept);
}
LOGGER.log(Level.INFO, "Session was accepted. Initiate Bytestream.");
state = State.active;
transportSession.processJingle(sessionAccept);
@ -112,7 +111,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
transportSession.initiateOutgoingSession(new JingleTransportInitiationCallback() {
@Override
public void onSessionInitiated(final BytestreamSession session) {
LOGGER.log(Level.INFO, "BytestreamSession initiated. Start transfer.");
sendingThread = new SendTask(session, source);
queued.add(threadPool.submit(sendingThread));
}
@ -128,8 +126,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
@Override
public IQ handleSessionTerminate(Jingle sessionTerminate) {
LOGGER.log(Level.INFO, "Received session-terminate: " + sessionTerminate.getReason().asEnum());
state = State.terminated;
return jutil.createAck(sessionTerminate);
}
@ -138,8 +134,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
public IQ handleTransportReplace(final Jingle transportReplace)
throws InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException {
LOGGER.log(Level.INFO, "Received transport-replace.");
final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection)
.getTransportManager(transportReplace);

View file

@ -45,7 +45,6 @@ public class ReceiveTask implements Runnable {
@Override
public void run() {
LOGGER.log(Level.INFO, "Start ReceiveTask");
JingleFileTransferChild transfer = (JingleFileTransferChild) fileTransfer.getJingleContentDescriptionChildren().get(0);
FileOutputStream outputStream = null;
InputStream inputStream;
@ -69,14 +68,12 @@ public class ReceiveTask implements Runnable {
}
outputStream.write(filebuf);
LOGGER.log(Level.INFO, "Received " + read + " bytes.");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Error while receiving data: ", e);
} finally {
try {
session.close();
LOGGER.log(Level.INFO, "Session closed.");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close InputStream.", e);
}

View file

@ -42,7 +42,6 @@ public class SendTask implements Runnable {
@Override
public void run() {
LOGGER.log(Level.INFO, "Start SendTask");
InputStream inputStream;
OutputStream outputStream;
@ -58,7 +57,7 @@ public class SendTask implements Runnable {
}
outputStream.write(filebuf);
LOGGER.log(Level.INFO, "Written " + r + " bytes.");
outputStream.flush();
}
catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not send file: " + e, e);
@ -66,7 +65,6 @@ public class SendTask implements Runnable {
finally {
try {
session.close();
LOGGER.log(Level.INFO, "Session closed.");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close session.", e);
}

View file

@ -239,7 +239,14 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
return IQ.createResultIQ(jingle);
}
/**
* Determine, which candidate (ours/theirs) is the nominated one.
* Connect to this candidate. If it is a proxy and it is ours, activate it and connect.
* If its a proxy and it is theirs, wait for activation.
* If it is not a proxy, just connect.
*/
private void connectIfReady() {
JingleContent content = jingleSession.getContents().get(0);
if (ourChoice == null || theirChoice == null) {
// Not yet ready.
LOGGER.log(Level.INFO, "Not ready.");
@ -249,102 +256,69 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
if (ourChoice == CANDIDATE_FAILURE && theirChoice == CANDIDATE_FAILURE) {
LOGGER.log(Level.INFO, "Failure.");
// TODO: Transport failed.
} else {
UsedCandidate nominated;
if (ourChoice != CANDIDATE_FAILURE && theirChoice != CANDIDATE_FAILURE) {
if (ourChoice.candidate.getPriority() > theirChoice.candidate.getPriority()) {
nominated = ourChoice;
} else if (ourChoice.candidate.getPriority() < theirChoice.candidate.getPriority()) {
nominated = theirChoice;
} else {
nominated = jingleSession.isInitiator() ? ourChoice : theirChoice;
}
} else if (ourChoice != CANDIDATE_FAILURE) {
return;
}
//Determine nominated candidate.
UsedCandidate nominated;
if (ourChoice != CANDIDATE_FAILURE && theirChoice != CANDIDATE_FAILURE) {
if (ourChoice.candidate.getPriority() > theirChoice.candidate.getPriority()) {
nominated = ourChoice;
} else {
} else if (ourChoice.candidate.getPriority() < theirChoice.candidate.getPriority()) {
nominated = theirChoice;
} else {
nominated = jingleSession.isInitiator() ? ourChoice : theirChoice;
}
} else if (ourChoice != CANDIDATE_FAILURE) {
nominated = ourChoice;
} else {
nominated = theirChoice;
}
boolean isExternalProxy = !nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid());
if (nominated == theirChoice) {
try {
nominated = connectToOurCandidate(nominated.candidate);
} catch (InterruptedException | IOException | XMPPException | SmackException | TimeoutException e) {
LOGGER.log(Level.INFO, "Could not connect to our candidate.");
return;
}
// Proxy. Needs activation.
//if (nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
if (nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
//Our proxy.
if (nominated == theirChoice) {
LOGGER.log(Level.INFO, "Our proxy.");
JingleContent content = jingleSession.getContents().get(0);
//Not a local proxy. Activate it.
if (!nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid())) {
Bytestream activateProxy = new Bytestream(ourProposal.getStreamId());
activateProxy.setToActivate(nominated.candidate.getJid());
activateProxy.setTo(nominated.candidate.getJid());
//Send proxy activation.
try {
jingleSession.getConnection().createStanzaCollectorAndSend(activateProxy).nextResultOrThrow();
//Connect
try {
nominated = connectToOurCandidate(nominated.candidate);
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket,
nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid()));
callback.onSessionInitiated(bs);
} catch (InterruptedException | TimeoutException | SmackException | IOException | XMPPException e) {
LOGGER.log(Level.WARNING, "Could not connect to our own proxy after activation.", e);
//TODO: ???
}
}
//Could not activate proxy. Send proxy-error
catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.WARNING, "Could not activate proxy at " + nominated.candidate.getJid()
+ ". Send proxy-error.", e);
Jingle proxyError = transportManager().createProxyError(
jingleSession.getRemote(), jingleSession.getInitiator(),
jingleSession.getSessionId(), content.getSenders(),
content.getCreator(), content.getName(), nominated.transport.getStreamId());
try {
jingleSession.getConnection().sendStanza(proxyError);
}
//Could not send proxy-error. WTF?
catch (SmackException.NotConnectedException | InterruptedException e1) {
LOGGER.log(Level.WARNING, "Could not send proxy-error.", e1);
}
callback.onException(e);
return;
}
}
//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();
LOGGER.log(Level.INFO, "Candidate-activate sent.");
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-activated", e);
return;
}
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, false);
callback.onSessionInitiated(bs);
if (isExternalProxy) {
Bytestream activate = new Bytestream(ourProposal.getStreamId());
activate.setMode(null);
activate.setType(IQ.Type.set);
activate.setTo(nominated.candidate.getJid());
activate.setToActivate(jingleSession.getRemote());
try {
jingleSession.getConnection().createStanzaCollectorAndSend(activate).nextResultOrThrow();
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.WARNING, "Could not activate proxy.", e);
return;
}
//Else wait for activation.
}
// Direct connection. Go ahead.
else {
Socks5BytestreamSession bs;
if (nominated == ourChoice) {
bs = new Socks5BytestreamSession(nominated.socket,
nominated.candidate.getJid().asBareJid().equals(jingleSession.getRemote().asBareJid()));
} else {
try {
nominated = connectToOurCandidate(theirChoice.candidate);
} catch (InterruptedException | IOException | XMPPException | SmackException | TimeoutException e) {
LOGGER.log(Level.SEVERE, "Failed to connect to our own StreamHost!");
callback.onException(e);
return;
}
bs = new Socks5BytestreamSession(nominated.socket,
nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid()));
}
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;
}
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !isExternalProxy);
callback.onSessionInitiated(bs);
}
//Our choice
else {
if (!nominated.candidate.getJid().asBareJid().equals(jingleSession.getRemote().asBareJid())) {
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !isExternalProxy);
callback.onSessionInitiated(bs);
}
}