1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-27 14:32:06 +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); return jutil.createErrorOutOfOrder(sessionAccept);
} }
LOGGER.log(Level.INFO, "Session was accepted. Initiate Bytestream.");
state = State.active; state = State.active;
transportSession.processJingle(sessionAccept); transportSession.processJingle(sessionAccept);
@ -112,7 +111,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
transportSession.initiateOutgoingSession(new JingleTransportInitiationCallback() { transportSession.initiateOutgoingSession(new JingleTransportInitiationCallback() {
@Override @Override
public void onSessionInitiated(final BytestreamSession session) { public void onSessionInitiated(final BytestreamSession session) {
LOGGER.log(Level.INFO, "BytestreamSession initiated. Start transfer.");
sendingThread = new SendTask(session, source); sendingThread = new SendTask(session, source);
queued.add(threadPool.submit(sendingThread)); queued.add(threadPool.submit(sendingThread));
} }
@ -128,8 +126,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
@Override @Override
public IQ handleSessionTerminate(Jingle sessionTerminate) { public IQ handleSessionTerminate(Jingle sessionTerminate) {
LOGGER.log(Level.INFO, "Received session-terminate: " + sessionTerminate.getReason().asEnum());
state = State.terminated; state = State.terminated;
return jutil.createAck(sessionTerminate); return jutil.createAck(sessionTerminate);
} }
@ -138,8 +134,6 @@ public class OutgoingJingleFileOffer extends JingleFileTransferSession {
public IQ handleTransportReplace(final Jingle transportReplace) public IQ handleTransportReplace(final Jingle transportReplace)
throws InterruptedException, XMPPException.XMPPErrorException, throws InterruptedException, XMPPException.XMPPErrorException,
SmackException.NotConnectedException, SmackException.NoResponseException { SmackException.NotConnectedException, SmackException.NoResponseException {
LOGGER.log(Level.INFO, "Received transport-replace.");
final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection) final JingleTransportManager<?> replacementManager = JingleTransportMethodManager.getInstanceFor(connection)
.getTransportManager(transportReplace); .getTransportManager(transportReplace);

View file

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

View file

@ -42,7 +42,6 @@ public class SendTask implements Runnable {
@Override @Override
public void run() { public void run() {
LOGGER.log(Level.INFO, "Start SendTask");
InputStream inputStream; InputStream inputStream;
OutputStream outputStream; OutputStream outputStream;
@ -58,7 +57,7 @@ public class SendTask implements Runnable {
} }
outputStream.write(filebuf); outputStream.write(filebuf);
LOGGER.log(Level.INFO, "Written " + r + " bytes."); outputStream.flush();
} }
catch (IOException e) { catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not send file: " + e, e); LOGGER.log(Level.SEVERE, "Could not send file: " + e, e);
@ -66,7 +65,6 @@ public class SendTask implements Runnable {
finally { finally {
try { try {
session.close(); session.close();
LOGGER.log(Level.INFO, "Session closed.");
} catch (IOException e) { } catch (IOException e) {
LOGGER.log(Level.SEVERE, "Could not close session.", 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); 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() { private void connectIfReady() {
JingleContent content = jingleSession.getContents().get(0);
if (ourChoice == null || theirChoice == null) { if (ourChoice == null || theirChoice == null) {
// Not yet ready. // Not yet ready.
LOGGER.log(Level.INFO, "Not ready."); LOGGER.log(Level.INFO, "Not ready.");
@ -249,7 +256,10 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
if (ourChoice == CANDIDATE_FAILURE && theirChoice == CANDIDATE_FAILURE) { if (ourChoice == CANDIDATE_FAILURE && theirChoice == CANDIDATE_FAILURE) {
LOGGER.log(Level.INFO, "Failure."); LOGGER.log(Level.INFO, "Failure.");
// TODO: Transport failed. // TODO: Transport failed.
} else { return;
}
//Determine nominated candidate.
UsedCandidate nominated; UsedCandidate nominated;
if (ourChoice != CANDIDATE_FAILURE && theirChoice != CANDIDATE_FAILURE) { if (ourChoice != CANDIDATE_FAILURE && theirChoice != CANDIDATE_FAILURE) {
if (ourChoice.candidate.getPriority() > theirChoice.candidate.getPriority()) { if (ourChoice.candidate.getPriority() > theirChoice.candidate.getPriority()) {
@ -265,52 +275,31 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
nominated = theirChoice; nominated = theirChoice;
} }
// Proxy. Needs activation. boolean isExternalProxy = !nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid());
//if (nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
if (nominated.candidate.getType() == JingleS5BTransportCandidate.Type.proxy) {
//Our proxy.
if (nominated == theirChoice) { 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 { try {
nominated = connectToOurCandidate(nominated.candidate); nominated = connectToOurCandidate(nominated.candidate);
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, } catch (InterruptedException | IOException | XMPPException | SmackException | TimeoutException e) {
nominated.candidate.getJid().asBareJid().equals(jingleSession.getLocal().asBareJid())); LOGGER.log(Level.INFO, "Could not connect to our candidate.");
callback.onSessionInitiated(bs); return;
} 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 if (isExternalProxy) {
catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) { Bytestream activate = new Bytestream(ourProposal.getStreamId());
LOGGER.log(Level.WARNING, "Could not activate proxy at " + nominated.candidate.getJid() activate.setMode(null);
+ ". Send proxy-error.", e); activate.setType(IQ.Type.set);
Jingle proxyError = transportManager().createProxyError( activate.setTo(nominated.candidate.getJid());
jingleSession.getRemote(), jingleSession.getInitiator(), activate.setToActivate(jingleSession.getRemote());
jingleSession.getSessionId(), content.getSenders(),
content.getCreator(), content.getName(), nominated.transport.getStreamId());
try { try {
jingleSession.getConnection().sendStanza(proxyError); jingleSession.getConnection().createStanzaCollectorAndSend(activate).nextResultOrThrow();
} } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
//Could not send proxy-error. WTF? LOGGER.log(Level.WARNING, "Could not activate proxy.", e);
catch (SmackException.NotConnectedException | InterruptedException e1) {
LOGGER.log(Level.WARNING, "Could not send proxy-error.", e1);
}
callback.onException(e);
return; return;
} }
} }
//Send candidate-activate.
Jingle candidateActivate = transportManager().createCandidateActivated( Jingle candidateActivate = transportManager().createCandidateActivated(
jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(), jingleSession.getRemote(), jingleSession.getInitiator(), jingleSession.getSessionId(),
content.getSenders(), content.getCreator(), content.getName(), nominated.transport.getStreamId(), content.getSenders(), content.getCreator(), content.getName(), nominated.transport.getStreamId(),
@ -318,33 +307,18 @@ public class JingleS5BTransportSession extends JingleTransportSession<JingleS5BT
try { try {
jingleSession.getConnection().createStanzaCollectorAndSend(candidateActivate) jingleSession.getConnection().createStanzaCollectorAndSend(candidateActivate)
.nextResultOrThrow(); .nextResultOrThrow();
LOGGER.log(Level.INFO, "Candidate-activate sent.");
} catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) { } catch (InterruptedException | XMPPException.XMPPErrorException | SmackException.NotConnectedException | SmackException.NoResponseException e) {
LOGGER.log(Level.WARNING, "Could not send candidate-activated", e); LOGGER.log(Level.WARNING, "Could not send candidate-activated", e);
return; return;
} }
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, false);
Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !isExternalProxy);
callback.onSessionInitiated(bs); callback.onSessionInitiated(bs);
} }
//Else wait for activation. //Our choice
}
// Direct connection. Go ahead.
else { else {
Socks5BytestreamSession bs; if (!nominated.candidate.getJid().asBareJid().equals(jingleSession.getRemote().asBareJid())) {
if (nominated == ourChoice) { Socks5BytestreamSession bs = new Socks5BytestreamSession(nominated.socket, !isExternalProxy);
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()));
}
callback.onSessionInitiated(bs); callback.onSessionInitiated(bs);
} }
} }