2017-07-21 23:05:46 +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.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.jingle.transport.jingle_s5b;
|
2017-07-19 23:15:17 +02:00
|
|
|
|
2017-07-21 17:58:57 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.Socket;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.XMPPException;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Client;
|
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.Socks5ClientForInitiator;
|
2017-07-19 23:15:17 +02:00
|
|
|
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
|
2017-07-27 15:18:18 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.components.JingleSession;
|
|
|
|
import org.jivesoftware.smackx.jingle.components.JingleTransportCandidate;
|
2017-07-27 23:31:04 +02:00
|
|
|
import org.jivesoftware.smackx.jingle.transport.jingle_s5b.element.JingleS5BTransportCandidateElement;
|
2017-07-19 23:15:17 +02:00
|
|
|
|
|
|
|
/**
|
2017-07-21 23:05:46 +02:00
|
|
|
* Jingle SOCKS5Bytestream transport candidate.
|
2017-07-19 23:15:17 +02:00
|
|
|
*/
|
2017-07-21 23:51:06 +02:00
|
|
|
public class JingleS5BTransportCandidate extends JingleTransportCandidate<JingleS5BTransportCandidateElement> {
|
2017-07-19 23:15:17 +02:00
|
|
|
|
|
|
|
private final String candidateId;
|
|
|
|
private final Bytestream.StreamHost streamHost;
|
|
|
|
private final JingleS5BTransportCandidateElement.Type type;
|
|
|
|
|
2017-07-21 17:58:57 +02:00
|
|
|
private Socket socket;
|
|
|
|
|
2017-07-19 23:15:17 +02:00
|
|
|
public JingleS5BTransportCandidate(String candidateId,
|
|
|
|
Bytestream.StreamHost streamHost,
|
|
|
|
int priority,
|
|
|
|
JingleS5BTransportCandidateElement.Type type) {
|
|
|
|
this.candidateId = candidateId;
|
|
|
|
this.streamHost = streamHost;
|
|
|
|
this.type = type;
|
2017-07-21 17:58:57 +02:00
|
|
|
|
|
|
|
setPriority(priority);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JingleS5BTransportCandidate fromElement(JingleS5BTransportCandidateElement element) {
|
|
|
|
return new JingleS5BTransportCandidate(element.getCandidateId(), element.getStreamHost(), element.getPriority(), element.getType());
|
2017-07-19 23:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getCandidateId() {
|
|
|
|
return candidateId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bytestream.StreamHost getStreamHost() {
|
|
|
|
return streamHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
public JingleS5BTransportCandidateElement.Type getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2017-07-27 17:35:16 +02:00
|
|
|
@Override
|
2017-07-21 17:58:57 +02:00
|
|
|
public JingleS5BTransportCandidateElement getElement() {
|
|
|
|
return new JingleS5BTransportCandidateElement(
|
|
|
|
getCandidateId(), getStreamHost().getAddress(),
|
|
|
|
getStreamHost().getJID(), getStreamHost().getPort(),
|
|
|
|
getPriority(), getType());
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:29:27 +02:00
|
|
|
public JingleS5BTransportCandidate connect(int timeout) throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
|
2017-07-21 17:58:57 +02:00
|
|
|
Socks5Client client;
|
|
|
|
|
|
|
|
if (getParent().isPeersProposal()) {
|
|
|
|
client = new Socks5Client(getStreamHost(), ((JingleS5BTransport) getParent()).getDstAddr());
|
|
|
|
}
|
|
|
|
else {
|
2017-07-21 23:51:06 +02:00
|
|
|
JingleSession session = getParent().getParent().getParent();
|
2017-07-21 17:58:57 +02:00
|
|
|
client = new Socks5ClientForInitiator(getStreamHost(), ((JingleS5BTransport) getParent()).getDstAddr(),
|
|
|
|
session.getJingleManager().getConnection(), session.getSessionId(), session.getPeer());
|
|
|
|
}
|
|
|
|
|
|
|
|
this.socket = client.getSocket(timeout);
|
2017-07-21 18:29:27 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Socket getSocket() {
|
2017-07-21 17:58:57 +02:00
|
|
|
return socket;
|
2017-07-19 23:15:17 +02:00
|
|
|
}
|
|
|
|
}
|