Search in sources :

Example 11 with Socks5BytestreamManager

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

the class Socks5ByteStreamRequestTest method shouldNotBlacklistInvalidProxy.

/**
     * Target should not not blacklist any SOCKS5 proxies regardless of failing connections.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNotBlacklistInvalidProxy() throws Exception {
    // disable blacklisting
    Socks5BytestreamRequest.setConnectFailureThreshold(0);
    // 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 < 10; 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());
    }
    // enable blacklisting
    Socks5BytestreamRequest.setConnectFailureThreshold(2);
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Example 12 with Socks5BytestreamManager

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

the class Socks5ByteStreamRequestTest method shouldFailIfRequestHasInvalidStreamHosts.

/**
     * Accepting a SOCKS5 Bytestream request should fail if target is not able to connect to any of
     * the provided SOCKS5 proxies.
     * 
     * @throws Exception
     */
@Test
public void shouldFailIfRequestHasInvalidStreamHosts() throws Exception {
    try {
        // build SOCKS5 Bytestream initialization request
        Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
        // add proxy that is not running
        bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7778);
        // 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)
        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());
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Stanza(org.jivesoftware.smack.packet.Stanza) IQ(org.jivesoftware.smack.packet.IQ) Test(org.junit.Test)

Example 13 with Socks5BytestreamManager

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

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

Example 15 with Socks5BytestreamManager

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

the class Socks5ByteStreamManagerTest method shouldNegotiateSocks5BytestreamAndTransferData.

/**
     * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should successfully
     * negotiate and return a SOCKS5 Bytestream connection.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNegotiateSocks5BytestreamAndTransferData() throws Exception {
    // disable clients local SOCKS5 proxy
    Socks5Proxy.setLocalSocks5ProxyEnabled(false);
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    /**
         * create responses in the order they should be queried specified by the XEP-0065
         * specification
         */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfo discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo, 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
    DiscoverInfo 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, 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
    Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID, initiatorJID);
    streamHostUsedPacket.setSessionID(sessionID);
    streamHostUsedPacket.setUsedHost(proxyJID);
    // return used stream host info as response to the bytestream initiation
    protocol.addResponse(streamHostUsedPacket, new Verification<Bytestream, Bytestream>() {

        @Override
        public void verify(Bytestream request, Bytestream response) {
            assertEquals(response.getSessionID(), request.getSessionID());
            assertEquals(1, request.getStreamHosts().size());
            StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
            assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
        }
    }, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    // build response to proxy activation
    IQ activationResponse = Socks5PacketUtils.createActivationConfirmation(proxyJID, initiatorJID);
    // return proxy activation response if proxy should be activated
    protocol.addResponse(activationResponse, new Verification<Bytestream, IQ>() {

        @Override
        public void verify(Bytestream request, IQ response) {
            assertEquals(targetJID, request.getToActivate().getTarget());
        }
    }, Verification.correspondingSenderReceiver, Verification.requestTypeSET);
    // start a local SOCKS5 proxy
    Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7778);
    socks5Proxy.start();
    // create digest to get the socket opened by target
    String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
    // finally call the method that should be tested
    OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
    // test the established bytestream
    InputStream inputStream = socks5Proxy.getSocket(digest).getInputStream();
    byte[] data = new byte[] { 1, 2, 3 };
    outputStream.write(data);
    byte[] result = new byte[3];
    inputStream.read(result);
    assertArrayEquals(data, result);
    protocol.verifyAll();
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Test(org.junit.Test)

Aggregations

Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)13 Test (org.junit.Test)13 InputStream (java.io.InputStream)10 OutputStream (java.io.OutputStream)10 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)7 XMPPException (org.jivesoftware.smack.XMPPException)6 IQ (org.jivesoftware.smack.packet.IQ)6 Stanza (org.jivesoftware.smack.packet.Stanza)6 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)5 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)5 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)5 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)4 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)4 IOException (java.io.IOException)3 ConnectException (java.net.ConnectException)3 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 TimeoutException (java.util.concurrent.TimeoutException)3 SmackException (org.jivesoftware.smack.SmackException)3 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3