Search in sources :

Example 1 with Socks5BytestreamManager

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testBiDirectionalSocks5BytestreamWithRemoteSocks5Proxy.

/**
 * Socks5 bytestream should be successfully established using a Socks5 proxy provided by the
 * XMPP server. The established connection should transfer data bidirectional if the Socks5
 * proxy supports it.
 * <p>
 * Support for bidirectional Socks5 bytestream:
 * <ul>
 * <li>Openfire (3.6.4 and below) - no</li>
 * <li>ejabberd (2.0.5 and higher) - yes</li>
 * </ul>
 * <p>
 * This test will fail if the XMPP server doesn't provide any Socks5 proxies or the Socks5 proxy
 * only allows Socks5 bytestreams in the context of a file transfer (like Openfire in default
 * configuration, see xmpp.proxy.transfer.required flag).
 *
 * @throws Exception if no Socks5 proxies found or proxy is unwilling to activate Socks5
 *         bytestream
 */
public void testBiDirectionalSocks5BytestreamWithRemoteSocks5Proxy() throws Exception {
    XMPPConnection initiatorConnection = getConnection(0);
    // disable local socks5 proxy
    SmackConfiguration.setLocalSocks5ProxyEnabled(false);
    Socks5Proxy.getSocks5Proxy().stop();
    assertFalse(Socks5Proxy.getSocks5Proxy().isRunning());
    XMPPConnection targetConnection = getConnection(1);
    // test data
    final byte[] data = new byte[] { 1, 2, 3 };
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
    Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {

        public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
            try {
                Socks5BytestreamSession session = request.accept();
                OutputStream outputStream = session.getOutputStream();
                outputStream.write(data);
                outputStream.flush();
                InputStream inputStream = session.getInputStream();
                byte[] receivedData = new byte[3];
                inputStream.read(receivedData);
                queue.put(receivedData);
                session.close();
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
    Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
    assertTrue(session.isMediated());
    // verify stream
    final byte[] receivedData = new byte[3];
    final InputStream inputStream = session.getInputStream();
    FutureTask<Integer> futureTask = new FutureTask<Integer>(new Callable<Integer>() {

        public Integer call() throws Exception {
            return inputStream.read(receivedData);
        }
    });
    Thread executor = new Thread(futureTask);
    executor.start();
    try {
        futureTask.get(2000, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        // reset default configuration
        SmackConfiguration.setLocalSocks5ProxyEnabled(true);
        Socks5Proxy.getSocks5Proxy().start();
        fail("Couldn't send data from target to inititator");
    }
    assertEquals("sent data not equal to received data", data, receivedData);
    OutputStream outputStream = session.getOutputStream();
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
    session.close();
    // reset default configuration
    SmackConfiguration.setLocalSocks5ProxyEnabled(true);
    Socks5Proxy.getSocks5Proxy().start();
}
Also used : Socks5BytestreamManager(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager) Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) TimeoutException(java.util.concurrent.TimeoutException) XMPPException(org.jivesoftware.smack.XMPPException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession) Socks5BytestreamListener(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener) FutureTask(java.util.concurrent.FutureTask) SynchronousQueue(java.util.concurrent.SynchronousQueue) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with Socks5BytestreamManager

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testSocks5BytestreamWithRemoteSocks5Proxy.

/**
 * Socks5 bytestream should be successfully established using a Socks5 proxy provided by the
 * XMPP server.
 * <p>
 * This test will fail if the XMPP server doesn't provide any Socks5 proxies or the Socks5 proxy
 * only allows Socks5 bytestreams in the context of a file transfer (like Openfire in default
 * configuration, see xmpp.proxy.transfer.required flag).
 *
 * @throws Exception if no Socks5 proxies found or proxy is unwilling to activate Socks5
 *         bytestream
 */
public void testSocks5BytestreamWithRemoteSocks5Proxy() throws Exception {
    // disable local socks5 proxy
    SmackConfiguration.setLocalSocks5ProxyEnabled(false);
    Socks5Proxy.getSocks5Proxy().stop();
    assertFalse(Socks5Proxy.getSocks5Proxy().isRunning());
    XMPPConnection initiatorConnection = getConnection(0);
    XMPPConnection targetConnection = getConnection(1);
    // test data
    final byte[] data = new byte[] { 1, 2, 3 };
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
    Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {

        public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
            InputStream inputStream;
            try {
                Socks5BytestreamSession session = request.accept();
                inputStream = session.getInputStream();
                byte[] receivedData = new byte[3];
                inputStream.read(receivedData);
                queue.put(receivedData);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
    Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
    OutputStream outputStream = session.getOutputStream();
    assertTrue(session.isMediated());
    // verify stream
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
    // reset default configuration
    SmackConfiguration.setLocalSocks5ProxyEnabled(true);
    Socks5Proxy.getSocks5Proxy().start();
}
Also used : Socks5BytestreamManager(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager) Socks5BytestreamListener(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener) Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) InputStream(java.io.InputStream) SynchronousQueue(java.util.concurrent.SynchronousQueue) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) TimeoutException(java.util.concurrent.TimeoutException) XMPPException(org.jivesoftware.smack.XMPPException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)

Example 3 with Socks5BytestreamManager

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testSocks5BytestreamWithLocalSocks5Proxy.

/**
 * Socks5 bytestream should be successfully established using the local Socks5 proxy.
 *
 * @throws Exception should not happen
 */
public void testSocks5BytestreamWithLocalSocks5Proxy() throws Exception {
    // setup port for local socks5 proxy
    SmackConfiguration.setLocalSocks5ProxyEnabled(true);
    SmackConfiguration.setLocalSocks5ProxyPort(7778);
    Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
    socks5Proxy.start();
    assertTrue(socks5Proxy.isRunning());
    XMPPConnection initiatorConnection = getConnection(0);
    XMPPConnection targetConnection = getConnection(1);
    // test data
    final byte[] data = new byte[] { 1, 2, 3 };
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    Socks5BytestreamManager targetByteStreamManager = Socks5BytestreamManager.getBytestreamManager(targetConnection);
    Socks5BytestreamListener incomingByteStreamListener = new Socks5BytestreamListener() {

        public void incomingBytestreamRequest(Socks5BytestreamRequest request) {
            InputStream inputStream;
            try {
                Socks5BytestreamSession session = request.accept();
                inputStream = session.getInputStream();
                byte[] receivedData = new byte[3];
                inputStream.read(receivedData);
                queue.put(receivedData);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    Socks5BytestreamManager initiatorByteStreamManager = Socks5BytestreamManager.getBytestreamManager(initiatorConnection);
    Socks5BytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
    OutputStream outputStream = session.getOutputStream();
    assertTrue(session.isDirect());
    // verify stream
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
    // reset default configuration
    SmackConfiguration.setLocalSocks5ProxyPort(7777);
}
Also used : Socks5BytestreamManager(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager) Socks5BytestreamListener(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener) Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) Socks5Proxy(org.jivesoftware.smackx.bytestreams.socks5.Socks5Proxy) InputStream(java.io.InputStream) SynchronousQueue(java.util.concurrent.SynchronousQueue) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) TimeoutException(java.util.concurrent.TimeoutException) XMPPException(org.jivesoftware.smack.XMPPException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)

Example 4 with Socks5BytestreamManager

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager in project Smack by igniterealtime.

the class JingleS5BTransportManager method queryAvailableStreamHosts.

private List<Bytestream.StreamHost> queryAvailableStreamHosts() throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
    Socks5BytestreamManager s5m = Socks5BytestreamManager.getBytestreamManager(getConnection());
    List<Jid> proxies = s5m.determineProxies();
    return determineStreamHostInfo(proxies);
}
Also used : Socks5BytestreamManager(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager) FullJid(org.jxmpp.jid.FullJid) Jid(org.jxmpp.jid.Jid)

Example 5 with Socks5BytestreamManager

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfTargetUsesInvalidSocks5Proxy.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if the
 * proxy used by target is invalid.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfTargetUsesInvalidSocks5Proxy() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldFailIfTargetUsesInvalidSocks5Proxy";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    // TODO: It appears that it is not required to disable the local stream host for this unit test.
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * create responses in the order they should be queried specified by the XEP-0065
     * specification
     */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items containing a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about being a SOCKS5 proxy
    DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the socks5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build used stream host response with unknown proxy
    Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
    streamHostUsedPacket.setSessionID(sessionID);
    streamHostUsedPacket.setUsedHost(JidCreate.from("invalid.proxy"));
    // return used stream host info as response to the bytestream initiation
    protocol.addResponse(streamHostUsedPacket, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    SmackException e = assertThrows(SmackException.class, () -> {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
    });
    protocol.verifyAll();
    assertTrue(e.getMessage().contains("Remote user responded with unknown host"));
}
Also used : Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) Test(org.junit.jupiter.api.Test)

Aggregations

XMPPConnection (org.jivesoftware.smack.XMPPConnection)16 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)13 Protocol (org.jivesoftware.util.Protocol)13 Test (org.junit.jupiter.api.Test)13 InputStream (java.io.InputStream)10 OutputStream (java.io.OutputStream)10 IQ (org.jivesoftware.smack.packet.IQ)6 Stanza (org.jivesoftware.smack.packet.Stanza)6 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)5 DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)5 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)5 Socks5BytestreamManager (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager)4 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)4 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 TimeoutException (java.util.concurrent.TimeoutException)3 XMPPException (org.jivesoftware.smack.XMPPException)3 Socks5BytestreamListener (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener)3 Socks5BytestreamRequest (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest)3 Socks5BytestreamSession (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)3