socks5 initialisation works. doesn't get activate though

This commit is contained in:
Daniel Gultsch 2014-04-11 21:13:09 +02:00
parent 51100ab48b
commit c0e0a70869
9 changed files with 180 additions and 38 deletions

View file

@ -748,8 +748,10 @@ public class XmppConnection implements Runnable {
} }
public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) { public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) {
if (packet.getId()==null) {
String id = nextRandomId(); String id = nextRandomId();
packet.setAttribute("id", id); packet.setAttribute("id", id);
}
packet.setFrom(account.getFullJid()); packet.setFrom(account.getFullJid());
this.sendPacket(packet, callback); this.sendPacket(packet, callback);
} }

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.xmpp.jingle; package eu.siacs.conversations.xmpp.jingle;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import android.util.Log; import android.util.Log;
@ -21,6 +22,7 @@ public class JingleConnection {
public static final int STATUS_INITIATED = 0; public static final int STATUS_INITIATED = 0;
public static final int STATUS_ACCEPTED = 1; public static final int STATUS_ACCEPTED = 1;
public static final int STATUS_TERMINATED = 2;
public static final int STATUS_FAILED = 99; public static final int STATUS_FAILED = 99;
private int status = -1; private int status = -1;
@ -29,13 +31,15 @@ public class JingleConnection {
private Account account; private Account account;
private String initiator; private String initiator;
private String responder; private String responder;
private List<Element> canditates = new ArrayList<Element>(); private List<Element> candidates = new ArrayList<Element>();
private HashMap<String, SocksConnection> connections = new HashMap<String, SocksConnection>();
private OnIqPacketReceived responseListener = new OnIqPacketReceived() { private OnIqPacketReceived responseListener = new OnIqPacketReceived() {
@Override @Override
public void onIqPacketReceived(Account account, IqPacket packet) { public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) { if (packet.getType() == IqPacket.TYPE_ERROR) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED);
status = STATUS_FAILED; status = STATUS_FAILED;
} }
} }
@ -60,9 +64,16 @@ public class JingleConnection {
} }
public void deliverPacket(JinglePacket packet) { public void deliverPacket(JinglePacket packet) {
Log.d("xmppService","packet arrived in connection");
if (packet.isAction("")) {
if (packet.isAction("session-terminate")) {
if (status == STATUS_INITIATED) {
mXmppConnectionService.markMessage(message, Message.STATUS_SEND_REJECTED);
}
status = STATUS_TERMINATED;
} else if (packet.isAction("session-accept")) {
accept(packet);
} else {
Log.d("xmppService","packet arrived in connection. action was "+packet.getAction());
} }
} }
@ -70,15 +81,16 @@ public class JingleConnection {
this.message = message; this.message = message;
this.account = message.getConversation().getAccount(); this.account = message.getConversation().getAccount();
this.initiator = this.account.getFullJid(); this.initiator = this.account.getFullJid();
if (this.canditates.size() > 0) { this.responder = this.message.getCounterpart();
if (this.candidates.size() > 0) {
this.sendInitRequest(); this.sendInitRequest();
} else { } else {
this.mJingleConnectionManager.getPrimaryCanditate(account, new OnPrimaryCanditateFound() { this.mJingleConnectionManager.getPrimaryCandidate(account, new OnPrimaryCandidateFound() {
@Override @Override
public void onPrimaryCanditateFound(boolean success, Element canditate) { public void onPrimaryCandidateFound(boolean success, Element canditate) {
if (success) { if (success) {
canditates.add(canditate); candidates.add(canditate);
} }
sendInitRequest(); sendInitRequest();
} }
@ -96,7 +108,7 @@ public class JingleConnection {
content.setAttribute("creator", "initiator"); content.setAttribute("creator", "initiator");
content.setAttribute("name", "a-file-offer"); content.setAttribute("name", "a-file-offer");
content.offerFile(this.mXmppConnectionService.getFileBackend().getImageFile(message)); content.offerFile(this.mXmppConnectionService.getFileBackend().getImageFile(message));
content.setCanditates(this.canditates); content.setCandidates(this.mJingleConnectionManager.nextRandomId(),this.candidates);
packet.setContent(content); packet.setContent(content);
Log.d("xmppService",packet.toString()); Log.d("xmppService",packet.toString());
account.getXmppConnection().sendIqPacket(packet, this.responseListener); account.getXmppConnection().sendIqPacket(packet, this.responseListener);
@ -112,4 +124,36 @@ public class JingleConnection {
return packet; return packet;
} }
private void accept(JinglePacket packet) {
Log.d("xmppService","session-accept: "+packet.toString());
Content content = packet.getJingleContent();
this.candidates.addAll(content.getCanditates());
this.status = STATUS_ACCEPTED;
this.connectWithCandidates();
IqPacket response = packet.generateRespone(IqPacket.TYPE_RESULT);
Log.d("xmppService","response "+response.toString());
account.getXmppConnection().sendIqPacket(response, null);
}
private void connectWithCandidates() {
for(Element canditate : this.candidates) {
String host = canditate.getAttribute("host");
int port = Integer.parseInt(canditate.getAttribute("port"));
SocksConnection socksConnection = new SocksConnection(this, host, port);
socksConnection.connect();
this.connections.put(canditate.getAttribute("cid"), socksConnection);
}
}
private void sendCandidateUsed(String cid) {
}
public String getInitiator() {
return this.initiator;
}
public String getResponder() {
return this.responder;
}
} }

View file

@ -23,7 +23,7 @@ public class JingleConnectionManager {
private List<JingleConnection> connections = new ArrayList<JingleConnection>(); // make private List<JingleConnection> connections = new ArrayList<JingleConnection>(); // make
// concurrent // concurrent
private ConcurrentHashMap<String, Element> primaryCanditates = new ConcurrentHashMap<String, Element>(); private ConcurrentHashMap<String, Element> primaryCandidates = new ConcurrentHashMap<String, Element>();
private SecureRandom random = new SecureRandom(); private SecureRandom random = new SecureRandom();
@ -67,9 +67,9 @@ public class JingleConnectionManager {
return this.xmppConnectionService; return this.xmppConnectionService;
} }
public void getPrimaryCanditate(Account account, public void getPrimaryCandidate(Account account,
final OnPrimaryCanditateFound listener) { final OnPrimaryCandidateFound listener) {
if (!this.primaryCanditates.containsKey(account.getJid())) { if (!this.primaryCandidates.containsKey(account.getJid())) {
String xmlns = "http://jabber.org/protocol/bytestreams"; String xmlns = "http://jabber.org/protocol/bytestreams";
final String proxy = account.getXmppConnection() final String proxy = account.getXmppConnection()
.findDiscoItemByFeature(xmlns); .findDiscoItemByFeature(xmlns);
@ -90,34 +90,34 @@ public class JingleConnectionManager {
if (streamhost != null) { if (streamhost != null) {
Log.d("xmppService", "streamhost found " Log.d("xmppService", "streamhost found "
+ streamhost.toString()); + streamhost.toString());
Element canditate = new Element("canditate"); Element candidate = new Element("candidate");
canditate.setAttribute("cid", candidate.setAttribute("cid",
nextRandomId()); nextRandomId());
canditate.setAttribute("host", candidate.setAttribute("host",
streamhost.getAttribute("host")); streamhost.getAttribute("host"));
canditate.setAttribute("port", candidate.setAttribute("port",
streamhost.getAttribute("port")); streamhost.getAttribute("port"));
canditate.setAttribute("type", "proxy"); candidate.setAttribute("type", "proxy");
canditate.setAttribute("jid", proxy); candidate.setAttribute("jid", proxy);
canditate candidate
.setAttribute("priority", "655360"); .setAttribute("priority", "655360");
primaryCanditates.put(account.getJid(), primaryCandidates.put(account.getJid(),
canditate); candidate);
listener.onPrimaryCanditateFound(true, listener.onPrimaryCandidateFound(true,
canditate); candidate);
} else { } else {
listener.onPrimaryCanditateFound(false, listener.onPrimaryCandidateFound(false,
null); null);
} }
} }
}); });
} else { } else {
listener.onPrimaryCanditateFound(false, null); listener.onPrimaryCandidateFound(false, null);
} }
} else { } else {
listener.onPrimaryCanditateFound(true, listener.onPrimaryCandidateFound(true,
this.primaryCanditates.get(account.getJid())); this.primaryCandidates.get(account.getJid()));
} }
} }

View file

@ -0,0 +1,7 @@
package eu.siacs.conversations.xmpp.jingle;
import eu.siacs.conversations.xml.Element;
public interface OnPrimaryCandidateFound {
public void onPrimaryCandidateFound(boolean success, Element canditate);
}

View file

@ -1,7 +0,0 @@
package eu.siacs.conversations.xmpp.jingle;
import eu.siacs.conversations.xml.Element;
public interface OnPrimaryCanditateFound {
public void onPrimaryCanditateFound(boolean success, Element canditate);
}

View file

@ -0,0 +1,70 @@
package eu.siacs.conversations.xmpp.jingle;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import eu.siacs.conversations.utils.CryptoHelper;
import android.util.Log;
public class SocksConnection {
private JingleConnection jingleConnection;
private Socket socket;
private String host;
private int port;
private String destination;
public SocksConnection(JingleConnection jingleConnection, String host, int port) {
this.jingleConnection = jingleConnection;
this.host = host;
this.port = port;
try {
MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
StringBuilder destBuilder = new StringBuilder();
destBuilder.append(jingleConnection.getSessionId());
destBuilder.append(jingleConnection.getInitiator());
destBuilder.append(jingleConnection.getResponder());
mDigest.reset();
Log.d("xmppService","plain destination: "+destBuilder.toString());
this.destination = CryptoHelper.bytesToHex(mDigest.digest(destBuilder.toString().getBytes()));
Log.d("xmppService","host="+host+", port="+port+", destination: "+destination);
} catch (NoSuchAlgorithmException e) {
}
}
public boolean connect() {
try {
this.socket = new Socket(this.host, this.port);
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
byte[] login = {0x05, 0x01, 0x00};
byte[] expectedReply = {0x05,0x00};
byte[] reply = new byte[2];
os.write(login);
is.read(reply);
if (Arrays.equals(reply, expectedReply)) {
String connect = ""+'\u0005'+'\u0001'+'\u0000'+'\u0003'+'\u0028'+this.destination+'\u0000'+'\u0000';
os.write(connect.getBytes());
byte[] result = new byte[2];
is.read(result);
int status = result[0];
return (status==0);
} else {
socket.close();
return false;
}
} catch (UnknownHostException e) {
return false;
} catch (IOException e) {
return false;
}
}
}

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.xmpp.jingle.stanzas; package eu.siacs.conversations.xmpp.jingle.stanzas;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
@ -22,14 +23,24 @@ public class Content extends Element {
file.addChild("name").setContent(actualFile.getName()); file.addChild("name").setContent(actualFile.getName());
} }
public void setCanditates(List<Element> canditates) { public void setCandidates(String transportId, List<Element> canditates) {
Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1"); Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
if (transport==null) { if (transport==null) {
transport = this.addChild("transport", "urn:xmpp:jingle:transports:s5b:1"); transport = this.addChild("transport", "urn:xmpp:jingle:transports:s5b:1");
} }
transport.setAttribute("sid", transportId);
transport.clearChildren(); transport.clearChildren();
for(Element canditate : canditates) { for(Element canditate : canditates) {
transport.addChild(canditate); transport.addChild(canditate);
} }
} }
public List<Element> getCanditates() {
Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1");
if (transport==null) {
return new ArrayList<Element>();
} else {
return transport.getChildren();
}
}
} }

View file

@ -33,6 +33,13 @@ public class JinglePacket extends IqPacket {
return this; return this;
} }
public Content getJingleContent() {
if (this.content==null) {
this.content = new Content();
}
return this.content;
}
public JinglePacket setReason(Reason reason) { public JinglePacket setReason(Reason reason) {
this.reason = reason; this.reason = reason;
return this; return this;

View file

@ -64,4 +64,12 @@ public class IqPacket extends AbstractStanza {
} }
} }
public IqPacket generateRespone(int type) {
IqPacket packet = new IqPacket(type);
packet.setFrom(this.getTo());
packet.setTo(this.getFrom());
packet.setId(this.getId());
return packet;
}
} }