From c0e0a7086902a9d5cfbe88c4aa220abdf2ee3dde Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 11 Apr 2014 21:13:09 +0200 Subject: [PATCH] socks5 initialisation works. doesn't get activate though --- .../conversations/xmpp/XmppConnection.java | 6 +- .../xmpp/jingle/JingleConnection.java | 62 +++++++++++++--- .../xmpp/jingle/JingleConnectionManager.java | 38 +++++----- .../xmpp/jingle/OnPrimaryCandidateFound.java | 7 ++ .../xmpp/jingle/OnPrimaryCanditateFound.java | 7 -- .../xmpp/jingle/SocksConnection.java | 70 +++++++++++++++++++ .../xmpp/jingle/stanzas/Content.java | 13 +++- .../xmpp/jingle/stanzas/JinglePacket.java | 7 ++ .../conversations/xmpp/stanzas/IqPacket.java | 8 +++ 9 files changed, 180 insertions(+), 38 deletions(-) create mode 100644 src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCandidateFound.java delete mode 100644 src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCanditateFound.java create mode 100644 src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java diff --git a/src/eu/siacs/conversations/xmpp/XmppConnection.java b/src/eu/siacs/conversations/xmpp/XmppConnection.java index c7bda9ca..0019094c 100644 --- a/src/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/eu/siacs/conversations/xmpp/XmppConnection.java @@ -748,8 +748,10 @@ public class XmppConnection implements Runnable { } public void sendIqPacket(IqPacket packet, OnIqPacketReceived callback) { - String id = nextRandomId(); - packet.setAttribute("id", id); + if (packet.getId()==null) { + String id = nextRandomId(); + packet.setAttribute("id", id); + } packet.setFrom(account.getFullJid()); this.sendPacket(packet, callback); } diff --git a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index e8f5aa22..f8083535 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.xmpp.jingle; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import android.util.Log; @@ -21,6 +22,7 @@ public class JingleConnection { public static final int STATUS_INITIATED = 0; public static final int STATUS_ACCEPTED = 1; + public static final int STATUS_TERMINATED = 2; public static final int STATUS_FAILED = 99; private int status = -1; @@ -29,13 +31,15 @@ public class JingleConnection { private Account account; private String initiator; private String responder; - private List canditates = new ArrayList(); + private List candidates = new ArrayList(); + private HashMap connections = new HashMap(); private OnIqPacketReceived responseListener = new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { if (packet.getType() == IqPacket.TYPE_ERROR) { + mXmppConnectionService.markMessage(message, Message.STATUS_SEND_FAILED); status = STATUS_FAILED; } } @@ -60,9 +64,16 @@ public class JingleConnection { } 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.account = message.getConversation().getAccount(); this.initiator = this.account.getFullJid(); - if (this.canditates.size() > 0) { + this.responder = this.message.getCounterpart(); + if (this.candidates.size() > 0) { this.sendInitRequest(); } else { - this.mJingleConnectionManager.getPrimaryCanditate(account, new OnPrimaryCanditateFound() { + this.mJingleConnectionManager.getPrimaryCandidate(account, new OnPrimaryCandidateFound() { @Override - public void onPrimaryCanditateFound(boolean success, Element canditate) { + public void onPrimaryCandidateFound(boolean success, Element canditate) { if (success) { - canditates.add(canditate); + candidates.add(canditate); } sendInitRequest(); } @@ -96,7 +108,7 @@ public class JingleConnection { content.setAttribute("creator", "initiator"); content.setAttribute("name", "a-file-offer"); content.offerFile(this.mXmppConnectionService.getFileBackend().getImageFile(message)); - content.setCanditates(this.canditates); + content.setCandidates(this.mJingleConnectionManager.nextRandomId(),this.candidates); packet.setContent(content); Log.d("xmppService",packet.toString()); account.getXmppConnection().sendIqPacket(packet, this.responseListener); @@ -111,5 +123,37 @@ public class JingleConnection { packet.setSessionId(this.sessionId); 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; + } } diff --git a/src/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java b/src/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java index b28f692a..7105774d 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java +++ b/src/eu/siacs/conversations/xmpp/jingle/JingleConnectionManager.java @@ -23,7 +23,7 @@ public class JingleConnectionManager { private List connections = new ArrayList(); // make // concurrent - private ConcurrentHashMap primaryCanditates = new ConcurrentHashMap(); + private ConcurrentHashMap primaryCandidates = new ConcurrentHashMap(); private SecureRandom random = new SecureRandom(); @@ -67,9 +67,9 @@ public class JingleConnectionManager { return this.xmppConnectionService; } - public void getPrimaryCanditate(Account account, - final OnPrimaryCanditateFound listener) { - if (!this.primaryCanditates.containsKey(account.getJid())) { + public void getPrimaryCandidate(Account account, + final OnPrimaryCandidateFound listener) { + if (!this.primaryCandidates.containsKey(account.getJid())) { String xmlns = "http://jabber.org/protocol/bytestreams"; final String proxy = account.getXmppConnection() .findDiscoItemByFeature(xmlns); @@ -90,34 +90,34 @@ public class JingleConnectionManager { if (streamhost != null) { Log.d("xmppService", "streamhost found " + streamhost.toString()); - Element canditate = new Element("canditate"); - canditate.setAttribute("cid", + Element candidate = new Element("candidate"); + candidate.setAttribute("cid", nextRandomId()); - canditate.setAttribute("host", + candidate.setAttribute("host", streamhost.getAttribute("host")); - canditate.setAttribute("port", + candidate.setAttribute("port", streamhost.getAttribute("port")); - canditate.setAttribute("type", "proxy"); - canditate.setAttribute("jid", proxy); - canditate + candidate.setAttribute("type", "proxy"); + candidate.setAttribute("jid", proxy); + candidate .setAttribute("priority", "655360"); - primaryCanditates.put(account.getJid(), - canditate); - listener.onPrimaryCanditateFound(true, - canditate); + primaryCandidates.put(account.getJid(), + candidate); + listener.onPrimaryCandidateFound(true, + candidate); } else { - listener.onPrimaryCanditateFound(false, + listener.onPrimaryCandidateFound(false, null); } } }); } else { - listener.onPrimaryCanditateFound(false, null); + listener.onPrimaryCandidateFound(false, null); } } else { - listener.onPrimaryCanditateFound(true, - this.primaryCanditates.get(account.getJid())); + listener.onPrimaryCandidateFound(true, + this.primaryCandidates.get(account.getJid())); } } diff --git a/src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCandidateFound.java b/src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCandidateFound.java new file mode 100644 index 00000000..9e00954f --- /dev/null +++ b/src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCandidateFound.java @@ -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); +} diff --git a/src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCanditateFound.java b/src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCanditateFound.java deleted file mode 100644 index 25aab118..00000000 --- a/src/eu/siacs/conversations/xmpp/jingle/OnPrimaryCanditateFound.java +++ /dev/null @@ -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); -} diff --git a/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java b/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java new file mode 100644 index 00000000..5c36d66f --- /dev/null +++ b/src/eu/siacs/conversations/xmpp/jingle/SocksConnection.java @@ -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; + } + } +} diff --git a/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java b/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java index 941e6559..5ea4619f 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java +++ b/src/eu/siacs/conversations/xmpp/jingle/stanzas/Content.java @@ -1,6 +1,7 @@ package eu.siacs.conversations.xmpp.jingle.stanzas; import java.io.File; +import java.util.ArrayList; import java.util.List; import eu.siacs.conversations.xml.Element; @@ -22,14 +23,24 @@ public class Content extends Element { file.addChild("name").setContent(actualFile.getName()); } - public void setCanditates(List canditates) { + public void setCandidates(String transportId, List canditates) { Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1"); if (transport==null) { transport = this.addChild("transport", "urn:xmpp:jingle:transports:s5b:1"); } + transport.setAttribute("sid", transportId); transport.clearChildren(); for(Element canditate : canditates) { transport.addChild(canditate); } } + + public List getCanditates() { + Element transport = this.findChild("transport", "urn:xmpp:jingle:transports:s5b:1"); + if (transport==null) { + return new ArrayList(); + } else { + return transport.getChildren(); + } + } } diff --git a/src/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java b/src/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java index bd912ea3..83f0aec6 100644 --- a/src/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java +++ b/src/eu/siacs/conversations/xmpp/jingle/stanzas/JinglePacket.java @@ -33,6 +33,13 @@ public class JinglePacket extends IqPacket { return this; } + public Content getJingleContent() { + if (this.content==null) { + this.content = new Content(); + } + return this.content; + } + public JinglePacket setReason(Reason reason) { this.reason = reason; return this; diff --git a/src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java b/src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java index 3ab3b6c3..9e288454 100644 --- a/src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java +++ b/src/eu/siacs/conversations/xmpp/stanzas/IqPacket.java @@ -63,5 +63,13 @@ public class IqPacket extends AbstractStanza { return 1000; } } + + public IqPacket generateRespone(int type) { + IqPacket packet = new IqPacket(type); + packet.setFrom(this.getTo()); + packet.setTo(this.getFrom()); + packet.setId(this.getId()); + return packet; + } }