2017-06-11 02:52:34 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright © 2017 Paul Schaub
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-06-10 15:09:30 +02:00
|
|
|
package org.jivesoftware.smackx.jingle_s5b;
|
|
|
|
|
2017-06-10 18:27:41 +02:00
|
|
|
import java.io.IOException;
|
2017-06-10 15:09:30 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
2017-06-10 18:27:41 +02:00
|
|
|
import java.net.Socket;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2017-06-10 15:09:30 +02:00
|
|
|
|
2017-06-10 18:27:41 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2017-06-10 15:09:30 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2017-06-10 18:27:41 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2017-06-12 14:44:00 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession;
|
2017-06-10 18:27:41 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Client;
|
2017-06-10 21:45:00 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Utils;
|
2017-06-10 18:27:41 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
|
2017-06-11 02:52:34 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.JingleManager;
|
2017-06-10 15:09:30 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.JingleSessionHandler;
|
|
|
|
import org.jivesoftware.smackx.jingle.JingleTransportEstablishedCallback;
|
|
|
|
import org.jivesoftware.smackx.jingle.JingleTransportHandler;
|
2017-06-10 21:45:00 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleAction;
|
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContent;
|
2017-06-10 17:16:22 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentTransport;
|
2017-06-10 18:27:41 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.JingleContentTransportCandidate;
|
2017-06-10 15:09:30 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransport;
|
2017-06-10 18:27:41 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransportCandidate;
|
2017-06-10 21:45:00 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_s5b.elements.JingleS5BTransportInfo;
|
2017-06-10 15:09:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JingleTransportHandler for Socks5Bytestreams.
|
|
|
|
*/
|
|
|
|
public class JingleS5BTransportHandler implements JingleTransportHandler<JingleS5BTransport> {
|
2017-06-10 18:27:41 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(JingleS5BTransportHandler.class.getName());
|
2017-06-10 15:09:30 +02:00
|
|
|
|
|
|
|
private final WeakReference<JingleSessionHandler> sessionHandler;
|
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
private JingleManager.FullJidAndSessionId fullJidAndSessionId;
|
|
|
|
private JingleContent receivedContent;
|
|
|
|
private JingleContent proposedContent;
|
|
|
|
private JingleTransportEstablishedCallback callback;
|
2017-06-12 14:44:00 +02:00
|
|
|
private JingleS5BTransport myTransport;
|
|
|
|
|
2017-06-10 21:45:00 +02:00
|
|
|
private JingleS5BTransportCandidate receivedCandidateUsed = null;
|
2017-06-12 16:42:46 +02:00
|
|
|
private JingleS5BTransportCandidate selectedCandidateUsed = null;
|
2017-06-10 21:45:00 +02:00
|
|
|
|
|
|
|
private Socket connectedSocket = null;
|
|
|
|
|
2017-06-10 15:09:30 +02:00
|
|
|
public JingleS5BTransportHandler(JingleSessionHandler sessionHandler) {
|
|
|
|
this.sessionHandler = new WeakReference<>(sessionHandler);
|
|
|
|
}
|
|
|
|
|
2017-06-12 14:44:00 +02:00
|
|
|
@Override
|
|
|
|
public void prepareOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent content) {
|
|
|
|
myTransport = (JingleS5BTransport) content.getJingleTransports().get(0);
|
|
|
|
Socks5Proxy.getSocks5Proxy().addLocalAddress(myTransport.getDestinationAddress());
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:09:30 +02:00
|
|
|
@Override
|
2017-06-11 02:52:34 +02:00
|
|
|
public void establishOutgoingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId,
|
2017-06-12 14:44:00 +02:00
|
|
|
JingleContent receivedContent,
|
|
|
|
JingleContent proposedContent,
|
2017-06-11 02:52:34 +02:00
|
|
|
JingleTransportEstablishedCallback callback) {
|
2017-06-12 14:44:00 +02:00
|
|
|
establishSession(fullJidAndSessionId, receivedContent, proposedContent, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void establishIncomingSession(JingleManager.FullJidAndSessionId fullJidAndSessionId,
|
|
|
|
JingleContent receivedContent,
|
|
|
|
JingleContent proposedContent,
|
|
|
|
JingleTransportEstablishedCallback callback) {
|
|
|
|
establishSession(fullJidAndSessionId, receivedContent, proposedContent, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void establishSession(JingleManager.FullJidAndSessionId fullJidAndSessionId,
|
|
|
|
JingleContent receivedContent,
|
|
|
|
JingleContent proposedContent,
|
|
|
|
JingleTransportEstablishedCallback callback) {
|
2017-06-12 16:42:46 +02:00
|
|
|
this.fullJidAndSessionId = fullJidAndSessionId;
|
|
|
|
this.receivedContent = receivedContent;
|
|
|
|
this.proposedContent = proposedContent;
|
|
|
|
this.callback = callback;
|
2017-06-12 14:44:00 +02:00
|
|
|
|
|
|
|
JingleContentTransport hopefullyS5BTransport = receivedContent.getJingleTransports().get(0);
|
2017-06-10 17:46:57 +02:00
|
|
|
if (!hopefullyS5BTransport.getNamespace().equals(JingleS5BTransport.NAMESPACE_V1)) {
|
|
|
|
throw new IllegalArgumentException("Transport must be a JingleS5BTransport.");
|
|
|
|
}
|
2017-06-10 21:45:00 +02:00
|
|
|
|
2017-06-10 17:46:57 +02:00
|
|
|
JingleS5BTransport transport = (JingleS5BTransport) hopefullyS5BTransport;
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-10 21:45:00 +02:00
|
|
|
Socks5Proxy.getSocks5Proxy().addLocalAddress(Socks5Utils.createDigest(
|
2017-06-11 02:52:34 +02:00
|
|
|
fullJidAndSessionId.getSessionId(), //SessionID
|
|
|
|
getConnection().getUser().asFullJidIfPossible(), //Us
|
|
|
|
fullJidAndSessionId.getFullJid())); //Them
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-10 21:45:00 +02:00
|
|
|
JingleS5BTransportCandidate usedCandidate = null;
|
2017-06-10 18:27:41 +02:00
|
|
|
for (JingleContentTransportCandidate c : transport.getCandidates()) {
|
2017-06-10 21:45:00 +02:00
|
|
|
usedCandidate = (JingleS5BTransportCandidate) c;
|
|
|
|
Bytestream.StreamHost streamHost = usedCandidate.getStreamHost();
|
2017-06-10 18:27:41 +02:00
|
|
|
String address = streamHost.getAddress() + ":" + streamHost.getPort();
|
|
|
|
|
2017-06-12 14:44:00 +02:00
|
|
|
LOGGER.log(Level.INFO, "Connect to " + address);
|
2017-06-10 18:27:41 +02:00
|
|
|
// establish socket
|
|
|
|
try {
|
|
|
|
final Socks5Client socks5Client = new Socks5Client(streamHost, transport.getDestinationAddress());
|
2017-06-10 21:45:00 +02:00
|
|
|
connectedSocket = socks5Client.getSocket(10 * 1000);
|
2017-06-12 16:42:46 +02:00
|
|
|
selectedCandidateUsed = usedCandidate;
|
2017-06-10 18:27:41 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-06-10 21:45:00 +02:00
|
|
|
catch (TimeoutException | IOException | SmackException | XMPPException | InterruptedException e) {
|
2017-06-12 14:44:00 +02:00
|
|
|
LOGGER.log(Level.WARNING, "Could not connect to " + address + ": " + e, e);
|
2017-06-10 21:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-10 21:45:00 +02:00
|
|
|
if (connectedSocket != null) {
|
2017-06-12 16:42:46 +02:00
|
|
|
//Send candidate-used
|
|
|
|
Jingle jingle = createCandidateUsed(usedCandidate, fullJidAndSessionId, receivedContent);
|
2017-06-10 21:45:00 +02:00
|
|
|
try {
|
|
|
|
getConnection().sendStanza(jingle);
|
2017-06-11 02:52:34 +02:00
|
|
|
} catch (SmackException.NotConnectedException | InterruptedException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Could not send candidate-used stanza: " + e, e);
|
2017-06-10 18:27:41 +02:00
|
|
|
}
|
2017-06-12 16:42:46 +02:00
|
|
|
} else {
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2017-06-12 14:44:00 +02:00
|
|
|
}
|
2017-06-12 16:42:46 +02:00
|
|
|
}
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
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);
|
|
|
|
}
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
callback.onSessionEstablished(new Socks5BytestreamSession(connectedSocket, false));
|
|
|
|
}
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
Jingle createCandidateUsed(JingleS5BTransportCandidate usedCandidate, JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent receivedContent) {
|
|
|
|
Jingle.Builder jb = Jingle.getBuilder();
|
|
|
|
jb.setSessionId(fullJidAndSessionId.getSessionId())
|
|
|
|
.setAction(JingleAction.transport_info)
|
|
|
|
.setInitiator(getConnection().getUser());
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
JingleContent.Builder cb = JingleContent.getBuilder();
|
|
|
|
cb.setName(receivedContent.getName())
|
|
|
|
.setCreator(receivedContent.getCreator())
|
|
|
|
.setSenders(receivedContent.getSenders());
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
|
|
|
|
tb.setStreamId(((JingleS5BTransport) receivedContent.getJingleTransports().get(0)).getStreamId());
|
|
|
|
tb.setCandidateUsed(usedCandidate.getCandidateId());
|
|
|
|
cb.addTransport(tb.build());
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
jb.addJingleContent(cb.build());
|
2017-06-10 18:27:41 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
Jingle jingle = jb.build();
|
|
|
|
jingle.setFrom(getConnection().getUser());
|
|
|
|
jingle.setTo(fullJidAndSessionId.getFullJid());
|
|
|
|
return jingle;
|
|
|
|
}
|
2017-06-12 14:44:00 +02:00
|
|
|
|
2017-06-12 16:42:46 +02:00
|
|
|
Jingle createCandidateActive(JingleS5BTransportCandidate usedCandidate, JingleManager.FullJidAndSessionId fullJidAndSessionId, JingleContent receivedContent) {
|
|
|
|
Jingle.Builder jb = Jingle.getBuilder();
|
|
|
|
jb.setAction(JingleAction.transport_info)
|
|
|
|
.setSessionId(fullJidAndSessionId.getSessionId())
|
|
|
|
.setInitiator(fullJidAndSessionId.getFullJid());
|
|
|
|
|
|
|
|
JingleContent.Builder cb = JingleContent.getBuilder();
|
|
|
|
cb.setCreator(JingleContent.Creator.initiator)
|
|
|
|
.setName(receivedContent.getName());
|
|
|
|
|
|
|
|
JingleS5BTransport.Builder tb = JingleS5BTransport.getBuilder();
|
|
|
|
tb.setStreamId(((JingleS5BTransport) receivedContent.getJingleTransports().get(0)).getStreamId());
|
|
|
|
tb.setCandidateActivated(usedCandidate.getCandidateId());
|
|
|
|
|
|
|
|
cb.addTransport(tb.build());
|
|
|
|
jb.addJingleContent(cb.build());
|
|
|
|
|
|
|
|
Jingle activate = jb.build();
|
|
|
|
activate.setTo(fullJidAndSessionId.getFullJid());
|
|
|
|
activate.setFrom(getConnection().getUser());
|
|
|
|
|
|
|
|
return activate;
|
2017-06-10 15:09:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public XMPPConnection getConnection() {
|
|
|
|
JingleSessionHandler handler = sessionHandler.get();
|
|
|
|
return handler != null ? handler.getConnection() : null;
|
|
|
|
}
|
2017-06-10 21:45:00 +02:00
|
|
|
|
2017-06-12 14:44:00 +02:00
|
|
|
@Override
|
|
|
|
public void onTransportInfoReceived(Jingle jingle) {
|
|
|
|
if (jingle.getAction() != JingleAction.transport_info) {
|
|
|
|
throw new IllegalArgumentException("Jingle Action must be transport-info.");
|
|
|
|
}
|
|
|
|
JingleContentTransport jingleTransport = jingle.getContents().get(0).getJingleTransports().get(0);
|
|
|
|
if (jingleTransport == null || !jingleTransport.getNamespace().equals(JingleS5BTransport.NAMESPACE_V1)) {
|
|
|
|
throw new IllegalArgumentException("Jingle must contain transport of type S5B.");
|
|
|
|
}
|
|
|
|
|
|
|
|
JingleS5BTransport transport = (JingleS5BTransport) jingleTransport;
|
|
|
|
JingleS5BTransportInfo info = (JingleS5BTransportInfo) transport.getInfos().get(0);
|
|
|
|
if (info == null) {
|
|
|
|
throw new IllegalArgumentException("Jingle must contain at least one JingleS5BTransportInfo.");
|
|
|
|
}
|
2017-06-12 16:42:46 +02:00
|
|
|
|
|
|
|
JingleS5BTransportCandidate finalCandidateUsed;
|
|
|
|
|
2017-06-12 14:44:00 +02:00
|
|
|
if (info.getElementName().equals(JingleS5BTransportInfo.CandidateUsed.ELEMENT)) {
|
|
|
|
String candidateUsedId = ((JingleS5BTransportInfo.CandidateUsed) info).getCandidateId();
|
2017-06-12 16:42:46 +02:00
|
|
|
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 { //==
|
2017-06-12 14:44:00 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-10 15:09:30 +02:00
|
|
|
}
|