2017-06-08 01:59:38 +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-07 21:08:08 +02:00
|
|
|
package org.jivesoftware.smackx.jingle_s5b;
|
|
|
|
|
2017-06-08 01:59:38 +02:00
|
|
|
import java.io.OutputStream;
|
2017-06-08 17:32:37 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2017-06-08 14:59:49 +02:00
|
|
|
import java.util.List;
|
2017-06-08 01:59:38 +02:00
|
|
|
|
2017-06-08 14:59:49 +02:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
2017-06-07 21:08:08 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2017-06-08 14:59:49 +02:00
|
|
|
import org.jivesoftware.smack.XMPPException;
|
2017-06-08 17:32:37 +02:00
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2017-06-08 16:46:39 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager;
|
2017-06-08 14:59:49 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
|
2017-06-08 17:32:37 +02:00
|
|
|
import org.jivesoftware.smackx.hash.HashManager;
|
2017-06-08 01:59:38 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.AbstractJingleContentTransportManager;
|
|
|
|
import org.jivesoftware.smackx.jingle.JingleTransportInputStreamCallback;
|
2017-06-08 17:32:37 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.JingleTransportManager;
|
2017-06-08 01:59:38 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.element.Jingle;
|
|
|
|
import org.jivesoftware.smackx.jingle.provider.JingleContentTransportProvider;
|
|
|
|
import org.jivesoftware.smackx.jingle_s5b.elements.JingleSocks5BytestreamTransport;
|
2017-06-08 17:32:37 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_s5b.elements.JingleSocks5BytestreamTransportCandidate;
|
2017-06-08 01:59:38 +02:00
|
|
|
import org.jivesoftware.smackx.jingle_s5b.provider.JingleSocks5BytestreamTransportProvider;
|
2017-06-08 16:46:39 +02:00
|
|
|
import org.jxmpp.jid.Jid;
|
2017-06-07 21:08:08 +02:00
|
|
|
|
|
|
|
/**
|
2017-06-08 01:59:38 +02:00
|
|
|
* Manager for JingleSocks5BytestreamTransports.
|
2017-06-07 21:08:08 +02:00
|
|
|
*/
|
2017-06-08 01:59:38 +02:00
|
|
|
public final class JingleSocks5BytestreamTransportManager extends AbstractJingleContentTransportManager<JingleSocks5BytestreamTransport> {
|
2017-06-07 21:08:08 +02:00
|
|
|
|
|
|
|
private JingleSocks5BytestreamTransportManager(XMPPConnection connection) {
|
|
|
|
super(connection);
|
|
|
|
}
|
2017-06-08 01:59:38 +02:00
|
|
|
|
2017-06-08 16:46:39 +02:00
|
|
|
public List<Bytestream.StreamHost> getAvailableStreamHosts() throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
|
|
|
|
Socks5BytestreamManager s5m = Socks5BytestreamManager.getBytestreamManager(connection());
|
|
|
|
List<Jid> proxies = s5m.determineProxies();
|
|
|
|
return s5m.determineStreamHostInfos(proxies);
|
|
|
|
}
|
2017-06-08 14:59:49 +02:00
|
|
|
|
2017-06-08 16:46:39 +02:00
|
|
|
public List<Bytestream.StreamHost> getLocalStreamHosts() {
|
|
|
|
return Socks5BytestreamManager.getBytestreamManager(connection())
|
|
|
|
.getLocalStreamHost();
|
2017-06-08 14:59:49 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 01:59:38 +02:00
|
|
|
@Override
|
|
|
|
protected JingleContentTransportProvider<JingleSocks5BytestreamTransport> createJingleContentTransportProvider() {
|
|
|
|
return new JingleSocks5BytestreamTransportProvider();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getNamespace() {
|
|
|
|
return JingleSocks5BytestreamTransport.NAMESPACE_V1;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void acceptInputStream(Jingle jingle, JingleTransportInputStreamCallback callback) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public OutputStream createOutputStream(Jingle jingle) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-06-08 17:32:37 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public JingleSocks5BytestreamTransport createJingleContentTransport(Jid otherUser) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
|
|
|
|
JingleSocks5BytestreamTransport.Builder builder = JingleSocks5BytestreamTransport.getBuilder();
|
|
|
|
|
|
|
|
List<Bytestream.StreamHost> localStreams = getLocalStreamHosts();
|
|
|
|
List<Bytestream.StreamHost> availableStreams = getAvailableStreamHosts();
|
|
|
|
|
|
|
|
for (Bytestream.StreamHost host : localStreams) {
|
|
|
|
JingleSocks5BytestreamTransportCandidate candidate = new JingleSocks5BytestreamTransportCandidate(host, 100);
|
|
|
|
builder.addTransportCandidate(candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Bytestream.StreamHost host : availableStreams) {
|
|
|
|
JingleSocks5BytestreamTransportCandidate candidate = new JingleSocks5BytestreamTransportCandidate(host, 0);
|
|
|
|
builder.addTransportCandidate(candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.setMode(Bytestream.Mode.tcp);
|
|
|
|
String sid = JingleTransportManager.generateRandomId();
|
|
|
|
builder.setStreamId(sid);
|
|
|
|
String digestString =
|
|
|
|
sid +
|
|
|
|
connection().getUser().asFullJidIfPossible().toString() +
|
|
|
|
otherUser.asFullJidIfPossible().toString();
|
|
|
|
try {
|
|
|
|
builder.setDestinationAddress(
|
|
|
|
new String(HashManager.sha_1(digestString.getBytes(StringUtils.UTF8)), StringUtils.UTF8));
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.build();
|
|
|
|
}
|
2017-06-07 21:08:08 +02:00
|
|
|
}
|