From f36ff9640d365cbab7838250ce00ce04de1b9d11 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Tue, 3 Sep 2019 19:56:00 +0200 Subject: also reply with direct connections on response --- src/main/java/de/pixart/messenger/Config.java | 1 + .../messenger/xmpp/jingle/JingleConnection.java | 31 ++++++++++++++++------ .../xmpp/jingle/JingleSocks5Transport.java | 21 ++++++++------- 3 files changed, 35 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/main/java/de/pixart/messenger/Config.java b/src/main/java/de/pixart/messenger/Config.java index d6063ab19..134bd3dc0 100644 --- a/src/main/java/de/pixart/messenger/Config.java +++ b/src/main/java/de/pixart/messenger/Config.java @@ -118,6 +118,7 @@ public final class Config { public static final int ExportLogs_Minute = 0; //Time - minutes: valid values from 0 to 59 public static final boolean DISABLE_PROXY_LOOKUP = false; //useful to debug ibb + public static final boolean USE_DIRECT_JINGLE_CANDIDATES = false; public static final boolean DISABLE_HTTP_UPLOAD = false; public static final boolean EXTENDED_SM_LOGGING = false; // log stanza counts public static final boolean BACKGROUND_STANZA_LOGGING = false; //log all stanzas that were received while the app is in background diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java index 00acb6ca3..baae498e3 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleConnection.java @@ -177,7 +177,8 @@ public class JingleConnection implements Transferable { @Override public void failed() { - Log.d(Config.LOGTAG, "proxy activation failed"); + Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": proxy activation failed"); + //TODO: when initiating send fallback to ibb } }; @@ -309,12 +310,7 @@ public class JingleConnection implements Transferable { if (this.initialTransport == Transport.IBB) { this.sendInitRequest(); } else { - final List directCandidates = DirectConnectionUtils.getLocalCandidates(account.getJid()); - for (JingleCandidate directCandidate : directCandidates) { - final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, directCandidate); - connections.put(directCandidate.getCid(), socksConnection); - candidates.add(directCandidate); - } + gatherAndConnectDirectCandidates(); this.mJingleConnectionManager.getPrimaryCandidate(account, (success, candidate) -> { if (success) { final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, candidate); @@ -343,7 +339,24 @@ public class JingleConnection implements Transferable { } }); } + } + private void gatherAndConnectDirectCandidates() { + final List directCandidates; + if (Config.USE_DIRECT_JINGLE_CANDIDATES) { + if (account.isOnion() || mXmppConnectionService.useTorToConnect()) { + directCandidates = Collections.emptyList(); + } else { + directCandidates = DirectConnectionUtils.getLocalCandidates(account.getJid()); + } + } else { + directCandidates = Collections.emptyList(); + } + for (JingleCandidate directCandidate : directCandidates) { + final JingleSocks5Transport socksConnection = new JingleSocks5Transport(this, directCandidate); + connections.put(directCandidate.getCid(), socksConnection); + candidates.add(directCandidate); + } } private void upgradeNamespace() { @@ -578,6 +591,7 @@ public class JingleConnection implements Transferable { } private void sendAcceptSocks() { + gatherAndConnectDirectCandidates(); this.mJingleConnectionManager.getPrimaryCandidate(this.account, (success, candidate) -> { final JinglePacket packet = bootstrapPacket("session-accept"); final Content content = new Content(contentCreator, contentName); @@ -608,7 +622,7 @@ public class JingleConnection implements Transferable { } }); } else { - Log.d(Config.LOGTAG, "did not find a primary candidate for ourself"); + Log.d(Config.LOGTAG, "did not find a primary candidate for ourselves"); content.socks5transport().setChildren(getCandidatesAsElements()); packet.setContent(content); sendJinglePacket(packet); @@ -770,6 +784,7 @@ public class JingleConnection implements Transferable { if (response.getType() != IqPacket.TYPE.RESULT) { Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": " + response.toString()); onProxyActivated.failed(); + //TODO send proxy-error } else { onProxyActivated.success(); sendProxyActivated(connection.getCandidate().getCid()); diff --git a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java index f1b527b99..027395a5a 100644 --- a/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java +++ b/src/main/java/de/pixart/messenger/xmpp/jingle/JingleSocks5Transport.java @@ -94,15 +94,15 @@ public class JingleSocks5Transport extends JingleTransport { private void acceptIncomingSocketConnection(Socket socket) throws IOException { Log.d(Config.LOGTAG, "accepted connection from " + socket.getInetAddress().getHostAddress()); - byte[] authBegin = new byte[2]; - InputStream inputStream = socket.getInputStream(); - OutputStream outputStream = socket.getOutputStream(); + final byte[] authBegin = new byte[2]; + final InputStream inputStream = socket.getInputStream(); + final OutputStream outputStream = socket.getOutputStream(); inputStream.read(authBegin); if (authBegin[0] != 0x5) { socket.close(); } - short methodCount = authBegin[1]; - byte[] methods = new byte[methodCount]; + final short methodCount = authBegin[1]; + final byte[] methods = new byte[methodCount]; inputStream.read(methods); if (SocksSocketFactory.contains((byte) 0x00, methods)) { outputStream.write(new byte[]{0x05, 0x00}); @@ -113,18 +113,18 @@ public class JingleSocks5Transport extends JingleTransport { inputStream.read(connectCommand); if (connectCommand[0] == 0x05 && connectCommand[1] == 0x01 && connectCommand[3] == 0x03) { int destinationCount = inputStream.read(); - byte[] destination = new byte[destinationCount]; + final byte[] destination = new byte[destinationCount]; inputStream.read(destination); - int port = inputStream.read(); + final int port = inputStream.read(); final String receivedDestination = new String(destination); - Log.d(Config.LOGTAG, "received destination " + receivedDestination + ":" + port + " - expected " + this.destination); final ByteBuffer response = ByteBuffer.allocate(7 + destination.length); final byte[] responseHeader; final boolean success; - if (receivedDestination.equals(this.destination)) { + if (receivedDestination.equals(this.destination) && this.socket == null) { responseHeader = new byte[]{0x05, 0x00, 0x00, 0x03}; success = true; } else { + Log.d(Config.LOGTAG, connection.getAccount().getJid().asBareJid() + ": destination mismatch. received " + receivedDestination + " (expected " + this.destination + ")"); responseHeader = new byte[]{0x05, 0x04, 0x00, 0x03}; success = false; } @@ -139,6 +139,7 @@ public class JingleSocks5Transport extends JingleTransport { this.inputStream = inputStream; this.outputStream = outputStream; this.isEstablished = true; + FileBackend.close(serverSocket); } } else { socket.close(); @@ -154,7 +155,7 @@ public class JingleSocks5Transport extends JingleTransport { } else { socket = new Socket(); SocketAddress address = new InetSocketAddress(candidate.getHost(), candidate.getPort()); - socket.connect(address, Config.SOCKET_TIMEOUT * 1000); + socket.connect(address, 5000); } inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); -- cgit v1.2.3