Search in sources :

Example 31 with Bytestream

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

the class Socks5ByteStreamManagerTest method createResponses.

private void createResponses(Verification<Bytestream, Bytestream> streamHostUsedVerification) throws XmppStringprepException {
    // 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);
    discoverItems.addItem(new Item(JidCreate.from("proxy2.xmpp-server")));
    discoverItems.addItem(new Item(proxyJID));
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    /*
         * build discover info for proxy "proxy2.xmpp-server" containing information about being a
         * SOCKS5 proxy
         */
    DiscoverInfo proxyInfo1 = Socks5PacketUtils.createDiscoverInfo(JidCreate.from("proxy2.xmpp-server"), initiatorJID);
    Identity identity1 = new Identity("proxy", "proxy2.xmpp-server", "bytestreams");
    proxyInfo1.addIdentity(identity1);
    // return the SOCKS5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo1, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about being a SOCKS5 proxy
    DiscoverInfo proxyInfo2 = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity2 = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo2.addIdentity(identity2);
    // return the SOCKS5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo2, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    /*
         * build a SOCKS5 stream host info for "proxy2.xmpp-server" containing the address and the
         * port of the proxy
         */
    Bytestream streamHostInfo1 = Socks5PacketUtils.createBytestreamResponse(JidCreate.from("proxy2.xmpp-server"), initiatorJID);
    streamHostInfo1.addStreamHost(JidCreate.from("proxy2.xmpp-server"), proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo1, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a SOCKS5 stream host info containing the address and the port of the proxy
    Bytestream streamHostInfo2 = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo2.addStreamHost(proxyJID, proxyAddress, 7778);
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo2, 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, streamHostUsedVerification, 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);
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)

Example 32 with Bytestream

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

the class Socks5ByteStreamRequestTest method shouldNotTimeoutIfFirstSocks5ProxyDoesNotRespond.

/**
     * If the SOCKS5 Bytestream request contains multiple SOCKS5 proxies and the first one doesn't
     * respond, the connection attempt to this proxy should not consume the whole timeout for
     * connecting to the proxies.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldNotTimeoutIfFirstSocks5ProxyDoesNotRespond() throws Exception {
    // start a local SOCKS5 proxy
    Socks5TestProxy socks5Proxy = Socks5TestProxy.getProxy(7778);
    // create a fake SOCKS5 proxy that doesn't respond to a request
    ServerSocket serverSocket = new ServerSocket(7779);
    // build SOCKS5 Bytestream initialization request
    Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(initiatorJID, targetJID, sessionID);
    bytestreamInitialization.addStreamHost(proxyJID, proxyAddress, 7779);
    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);
    // set timeouts
    byteStreamRequest.setTotalConnectTimeout(2000);
    byteStreamRequest.setMinimumConnectTimeout(1000);
    // accept the stream (this is the call that is tested here)
    InputStream inputStream = byteStreamRequest.accept().getInputStream();
    // assert that client tries to connect to dumb SOCKS5 proxy
    Socket socket = serverSocket.accept();
    assertNotNull(socket);
    // 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());
    serverSocket.close();
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Stanza(org.jivesoftware.smack.packet.Stanza) ServerSocket(java.net.ServerSocket) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Aggregations

Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)28 Test (org.junit.Test)14 InputStream (java.io.InputStream)11 OutputStream (java.io.OutputStream)11 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)10 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)9 XMPPException (org.jivesoftware.smack.XMPPException)8 IQ (org.jivesoftware.smack.packet.IQ)8 IOException (java.io.IOException)7 SmackException (org.jivesoftware.smack.SmackException)7 Stanza (org.jivesoftware.smack.packet.Stanza)7 TimeoutException (java.util.concurrent.TimeoutException)6 XMPPConnection (org.jivesoftware.smack.XMPPConnection)6 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)6 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)6 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)5 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)5 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)5 Socket (java.net.Socket)4 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)4