Improvements to Socks5

This commit is contained in:
vanitasvitae 2017-07-21 18:29:27 +02:00
parent c448d07234
commit 19d334d6bb
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 72 additions and 10 deletions

View File

@ -21,6 +21,10 @@ public abstract class Transport<D extends JingleContentTransportElement> {
private Transport peersProposal; private Transport peersProposal;
private boolean isPeersProposal; private boolean isPeersProposal;
public Transport(Content content) {
this.parent = content;
}
public abstract D getElement(); public abstract D getElement();
public void addCandidate(TransportCandidate<?> candidate) { public void addCandidate(TransportCandidate<?> candidate) {
@ -75,6 +79,10 @@ public abstract class Transport<D extends JingleContentTransportElement> {
return isPeersProposal; return isPeersProposal;
} }
public Transport<?> getPeersProposal() {
return peersProposal;
}
public abstract void handleTransportInfo(JingleContentTransportInfoElement info); public abstract void handleTransportInfo(JingleContentTransportInfoElement info);
public void setParent(Content parent) { public void setParent(Content parent) {

View File

@ -1,12 +1,20 @@
package org.jivesoftware.smackx.jingle3.transport.jingle_s5b; package org.jivesoftware.smackx.jingle3.transport.jingle_s5b;
import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy; import org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy;
import org.jivesoftware.smackx.bytestreams.socks5.Socks5Utils;
import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream; import org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream;
import org.jivesoftware.smackx.jingle3.element.JingleContentTransportInfoElement; import org.jivesoftware.smackx.jingle3.element.JingleContentTransportInfoElement;
import org.jivesoftware.smackx.jingle3.internal.Content;
import org.jivesoftware.smackx.jingle3.internal.Transport; import org.jivesoftware.smackx.jingle3.internal.Transport;
import org.jivesoftware.smackx.jingle3.internal.TransportCandidate; import org.jivesoftware.smackx.jingle3.internal.TransportCandidate;
import org.jivesoftware.smackx.jingle3.transport.BytestreamSessionEstablishedListener; import org.jivesoftware.smackx.jingle3.transport.BytestreamSessionEstablishedListener;
@ -29,12 +37,22 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
private String dstAddr; private String dstAddr;
private Bytestream.Mode mode; private Bytestream.Mode mode;
private JingleS5BTransport peersProposal; // PEERS candidate of OUR choice.
private JingleS5BTransportCandidate selectedCandidate;
private JingleS5BTransportCandidate ourChoice, theirChoice; /**
private JingleS5BTransportCandidate nominee; * Create fresh JingleS5BTransport.
* @param content parent content.
*/
public JingleS5BTransport(Content content) {
super(content);
this.sid = StringUtils.randomString(24);
this.dstAddr = Socks5Utils.createDigest(sid, content.getParent().getInitiator(), content.getParent().getResponder());
this.mode = Bytestream.Mode.tcp;
}
public JingleS5BTransport(String sid, String dstAddr, Bytestream.Mode mode, List<JingleS5BTransportCandidate> candidates) { public JingleS5BTransport(Content content, String sid, String dstAddr, Bytestream.Mode mode, List<JingleS5BTransportCandidate> candidates) {
super(content);
this.sid = sid; this.sid = sid;
this.dstAddr = dstAddr; this.dstAddr = dstAddr;
this.mode = mode; this.mode = mode;
@ -82,6 +100,16 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
} }
public void connectToCandidates(int timeout) {
for (TransportCandidate c : getCandidates()) {
try {
selectedCandidate = ((JingleS5BTransportCandidate) c).connect(timeout / getCandidates().size()); //TODO: Wise?
} catch (IOException | TimeoutException | InterruptedException | SmackException | XMPPException e) {
e.printStackTrace();
}
}
}
@Override @Override
public void handleTransportInfo(JingleContentTransportInfoElement info) { public void handleTransportInfo(JingleContentTransportInfoElement info) {
switch (info.getElementName()) { switch (info.getElementName()) {
@ -110,10 +138,24 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
private void handleCandidateUsed(JingleS5BTransportInfoElement info) { private void handleCandidateUsed(JingleS5BTransportInfoElement info) {
String candidateId = ((JingleS5BTransportInfoElement.CandidateUsed) info).getCandidateId(); String candidateId = ((JingleS5BTransportInfoElement.CandidateUsed) info).getCandidateId();
if (theirChoice == null) { JingleS5BTransport peers = (JingleS5BTransport) getPeersProposal();
/*
TODO: Booooooh illegal candidateId!! Go home!!!!11elf if (peers.getSelectedCandidate() != null) {
*/ //TODO: Alert! We already received one candidateUsed previously!
return;
}
Iterator<TransportCandidate<?>> ourCandidates = getCandidates().iterator();
while (ourCandidates.hasNext()) {
JingleS5BTransportCandidate candidate = (JingleS5BTransportCandidate) ourCandidates.next();
if (candidate.getCandidateId().equals(candidateId)) {
peers.setSelectedCandidate(candidate);
}
}
if (peers.getSelectedCandidate() == null) {
//TODO: Alert! Illegal candidateId!
} }
//connectIfReady(); //connectIfReady();
@ -126,7 +168,7 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
} }
private void handleCandidateError(JingleS5BTransportInfoElement info) { private void handleCandidateError(JingleS5BTransportInfoElement info) {
theirChoice = CANDIDATE_FAILURE; ((JingleS5BTransport) getPeersProposal()).setSelectedCandidate(CANDIDATE_FAILURE);
//connectIfReady(); //connectIfReady();
} }
@ -134,6 +176,14 @@ public class JingleS5BTransport extends Transport<JingleS5BTransportElement> {
//TODO //TODO
} }
public void setSelectedCandidate(JingleS5BTransportCandidate candidate) {
selectedCandidate = candidate;
}
public JingleS5BTransportCandidate getSelectedCandidate() {
return selectedCandidate;
}
/** /**
* Internal dummy candidate used to represent failure. * Internal dummy candidate used to represent failure.
* Kinda depressing, isn't it? * Kinda depressing, isn't it?

View File

@ -58,7 +58,7 @@ public class JingleS5BTransportCandidate extends TransportCandidate<JingleS5BTra
getPriority(), getType()); getPriority(), getType());
} }
public Socket connect(int timeout) throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException { public JingleS5BTransportCandidate connect(int timeout) throws InterruptedException, TimeoutException, SmackException, XMPPException, IOException {
Socks5Client client; Socks5Client client;
if (getParent().isPeersProposal()) { if (getParent().isPeersProposal()) {
@ -71,6 +71,10 @@ public class JingleS5BTransportCandidate extends TransportCandidate<JingleS5BTra
} }
this.socket = client.getSocket(timeout); this.socket = client.getSocket(timeout);
return this;
}
public Socket getSocket() {
return socket; return socket;
} }
} }