Search in sources :

Example 6 with Socks5BytestreamRequest

use of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest 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 7 with Socks5BytestreamRequest

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

the class InitiationListener method processRequest.

private void processRequest(Stanza packet) throws NotConnectedException, InterruptedException {
    Bytestream byteStreamRequest = (Bytestream) packet;
    StreamNegotiator.signal(byteStreamRequest.getFrom().toString() + '\t' + byteStreamRequest.getSessionID(), byteStreamRequest);
    // ignore request if in ignore list
    if (this.manager.getIgnoredBytestreamRequests().remove(byteStreamRequest.getSessionID())) {
        return;
    }
    // build bytestream request from packet
    Socks5BytestreamRequest request = new Socks5BytestreamRequest(this.manager, byteStreamRequest);
    // notify listeners for bytestream initiation from a specific user
    BytestreamListener userListener = this.manager.getUserListener(byteStreamRequest.getFrom());
    if (userListener != null) {
        userListener.incomingBytestreamRequest(request);
    } else if (!this.manager.getAllRequestListeners().isEmpty()) {
        /*
             * if there is no user specific listener inform listeners for all initiation requests
             */
        for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
            listener.incomingBytestreamRequest(request);
        }
    } else {
        /*
             * if there is no listener for this initiation request, reply with reject message
             */
        this.manager.replyRejectPacket(byteStreamRequest);
    }
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) BytestreamListener(org.jivesoftware.smackx.bytestreams.BytestreamListener)

Example 8 with Socks5BytestreamRequest

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

the class Socks5TransferNegotiator method negotiateIncomingStream.

@Override
InputStream negotiateIncomingStream(Stanza streamInitiation) throws InterruptedException, SmackException, XMPPErrorException {
    // build SOCKS5 Bytestream request
    Socks5BytestreamRequest request = new ByteStreamRequest(this.manager, (Bytestream) streamInitiation);
    // always accept the request
    Socks5BytestreamSession session = request.accept();
    // test input stream
    try {
        PushbackInputStream stream = new PushbackInputStream(session.getInputStream());
        int firstByte = stream.read();
        stream.unread(firstByte);
        return stream;
    } catch (IOException e) {
        throw new SmackException("Error establishing input stream", e);
    }
}
Also used : Socks5BytestreamRequest(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest) PushbackInputStream(java.io.PushbackInputStream) SmackException(org.jivesoftware.smack.SmackException) IOException(java.io.IOException) Socks5BytestreamSession(org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)

Example 9 with Socks5BytestreamRequest

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

the class Socks5ByteStreamRequestTest method shouldAcceptSocks5BytestreamRequestAndReceiveData.

/**
     * Accepting the SOCKS5 Bytestream request should be successfully.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldAcceptSocks5BytestreamRequestAndReceiveData() throws Exception {
    // start a local SOCKS5 proxy
    Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7778);
    // build SOCKS5 Bytestream initialization request
    Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
    bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7778);
    // create test data for stream
    byte[] data = new byte[] { 1, 2, 3 };
    // get SOCKS5 Bytestream manager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    // build SOCKS5 Bytestream request with the bytestream initialization
    Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
    // accept the stream (this is the call that is tested here)
    InputStream inputStream = byteStreamRequest.accept().getInputStream();
    // create digest to get the socket opened by target
    String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
    // test stream by sending some data
    OutputStream outputStream = socks5Proxy.getSocket(digest).getOutputStream();
    outputStream.write(data);
    // verify that data is transferred correctly
    byte[] result = new byte[3];
    inputStream.read(result);
    assertArrayEquals(data, result);
    // verify targets response
    assertEquals(1, protocol.getRequests().size());
    Stanza targetResponse = protocol.getRequests().remove(0);
    assertEquals(Bytestream.class, targetResponse.getClass());
    assertEquals(initiatorJID, targetResponse.getTo());
    assertEquals(IQ.Type.result, ((Bytestream) targetResponse).getType());
    assertEquals(proxyJID, ((Bytestream) targetResponse).getUsedHost().getJID());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 10 with Socks5BytestreamRequest

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

the class Socks5ByteStreamRequestTest method shouldBlacklistInvalidProxyAfter2Failures.

/**
     * Target should not try to connect to SOCKS5 proxies that already failed twice.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldBlacklistInvalidProxyAfter2Failures() throws Exception {
    // build SOCKS5 Bytestream initialization request
    Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
    bytestreamInitialization.addStreamHost(JidCreate.from("invalid." + proxyJID), "127.0.0.2", 7778);
    // get SOCKS5 Bytestream manager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    // try to connect several times
    for (int i = 0; i < 2; i++) {
        try {
            // build SOCKS5 Bytestream request with the bytestream initialization
            Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
            // set timeouts
            byteStreamRequest.setTotalConnectTimeout(600);
            byteStreamRequest.setMinimumConnectTimeout(300);
            // accept the stream (this is the call that is tested here)
            byteStreamRequest.accept();
            fail("exception should be thrown");
        } catch (XMPPErrorException e) {
            assertTrue(e.getXMPPError().getDescriptiveText("en").contains("Could not establish socket with any provided host"));
        }
        // verify targets response
        assertEquals(1, protocol.getRequests().size());
        Stanza targetResponse = protocol.getRequests().remove(0);
        assertTrue(IQ.class.isInstance(targetResponse));
        assertEquals(initiatorJID, targetResponse.getTo());
        assertEquals(IQ.Type.error, ((IQ) targetResponse).getType());
        assertEquals(XMPPError.Condition.item_not_found, ((IQ) targetResponse).getError().getCondition());
    }
    // create test data for stream
    byte[] data = new byte[] { 1, 2, 3 };
    Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7779);
    assertTrue(socks5Proxy.isRunning());
    // add a valid SOCKS5 proxy
    bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7779);
    // build SOCKS5 Bytestream request with the bytestream initialization
    Socks5BytestreamRequest byteStreamRequest = new Socks5BytestreamRequest(byteStreamManager, bytestreamInitialization);
    // set timeouts
    byteStreamRequest.setTotalConnectTimeout(600);
    byteStreamRequest.setMinimumConnectTimeout(300);
    // accept the stream (this is the call that is tested here)
    InputStream inputStream = byteStreamRequest.accept().getInputStream();
    // create digest to get the socket opened by target
    String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
    // test stream by sending some data
    OutputStream outputStream = socks5Proxy.getSocket(digest).getOutputStream();
    outputStream.write(data);
    // verify that data is transferred correctly
    byte[] result = new byte[3];
    inputStream.read(result);
    assertArrayEquals(data, result);
    // verify targets response
    assertEquals(1, protocol.getRequests().size());
    Stanza targetResponse = protocol.getRequests().remove(0);
    assertEquals(Bytestream.class, targetResponse.getClass());
    assertEquals(initiatorJID, targetResponse.getTo());
    assertEquals(IQ.Type.result, ((Bytestream) targetResponse).getType());
    assertEquals(proxyJID, ((Bytestream) targetResponse).getUsedHost().getJID());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) InputStream(java.io.InputStream) Stanza(org.jivesoftware.smack.packet.Stanza) OutputStream(java.io.OutputStream) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Aggregations

Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 Stanza (org.jivesoftware.smack.packet.Stanza)6 Test (org.junit.Test)6 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)4 IQ (org.jivesoftware.smack.packet.IQ)4 Socks5BytestreamRequest (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamRequest)4 Socks5BytestreamSession (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)4 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 TimeoutException (java.util.concurrent.TimeoutException)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 XMPPException (org.jivesoftware.smack.XMPPException)3 Socks5BytestreamListener (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener)3 Socks5BytestreamManager (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamManager)3 IOException (java.io.IOException)1 PushbackInputStream (java.io.PushbackInputStream)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 FutureTask (java.util.concurrent.FutureTask)1