Package org.jivesoftware.smackx.bytestreams.socks5

Examples of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener


    @Test
    public void shouldSuccessfullyConnectThroughLocalSocks5Proxy() throws Exception {

        // start a local SOCKS5 proxy
        SmackConfiguration.setLocalSocks5ProxyPort(proxyPort);
        Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
        socks5Proxy.start();

        // test data
        final byte[] data = new byte[] { 1, 2, 3 };

        // create digest
        final String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);

        // allow connection of target with this digest
        socks5Proxy.addTransfer(digest);

        // build stream host information
        final StreamHost streamHost = new StreamHost(connection.getUser(),
                        socks5Proxy.getLocalAddresses().get(0));
        streamHost.setPort(socks5Proxy.getPort());

        // target connects to local SOCKS5 proxy
        Thread targetThread = new Thread() {

            @Override
            public void run() {
                try {
                    Socks5Client targetClient = new Socks5Client(streamHost, digest);
                    Socket socket = targetClient.getSocket(10000);
                    socket.getOutputStream().write(data);
                }
                catch (Exception e) {
                    fail(e.getMessage());
                }
            }

        };
        targetThread.start();

        Thread.sleep(200);

        // initiator connects
        Socks5ClientForInitiator socks5Client = new Socks5ClientForInitiator(streamHost, digest,
                        connection, sessionID, targetJID);

        Socket socket = socks5Client.getSocket(10000);

        // verify test data
        InputStream in = socket.getInputStream();
        for (int i = 0; i < data.length; i++) {
            assertEquals(data[i], in.read());
        }

        targetThread.join();

        protocol.verifyAll(); // assert no XMPP messages were sent

        socks5Proxy.removeTransfer(digest);
        socks5Proxy.stop();

    }
View Full Code Here


        // enable clients local SOCKS5 proxy on port 7778
        SmackConfiguration.setLocalSocks5ProxyEnabled(true);
        SmackConfiguration.setLocalSocks5ProxyPort(7778);

        // start a local SOCKS5 proxy
        Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
        socks5Proxy.start();
        assertTrue(socks5Proxy.isRunning());

        // 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(Socks5BytestreamManager.NAMESPACE);

        // return that SOCKS5 is supported if target is queried
        protocol.addResponse(discoverInfo, Verification.correspondingSenderReceiver,
                        Verification.requestTypeGET);

        // build discover items containing no proxy item
        DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer,
                        initiatorJID);

        // return the discover item if XMPP server is queried
        protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver,
                        Verification.requestTypeGET);

        // build used stream host response
        Bytestream streamHostUsedPacket = Socks5PacketUtils.createBytestreamResponse(targetJID,
                        initiatorJID);
        streamHostUsedPacket.setSessionID(sessionID);
        streamHostUsedPacket.setUsedHost(initiatorJID); // local proxy used

        // return used stream host info as response to the bytestream initiation
        protocol.addResponse(streamHostUsedPacket, new Verification<Bytestream, Bytestream>() {

            public void verify(Bytestream request, Bytestream response) {
                assertEquals(response.getSessionID(), request.getSessionID());
                assertEquals(2, request.getStreamHosts().size());
                StreamHost streamHost1 = (StreamHost) request.getStreamHosts().toArray()[0];
                assertEquals(response.getUsedHost().getJID(), streamHost1.getJID());
                StreamHost streamHost2 = (StreamHost) request.getStreamHosts().toArray()[1];
                assertEquals(response.getUsedHost().getJID(), streamHost2.getJID());
                assertEquals("localAddress", streamHost2.getAddress());
            }

        }, Verification.correspondingSenderReceiver, Verification.requestTypeSET);

        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);

        // connect to proxy as target
        socks5Proxy.addTransfer(digest);
        StreamHost streamHost = new StreamHost(targetJID, socks5Proxy.getLocalAddresses().get(0));
        streamHost.setPort(socks5Proxy.getPort());
        Socks5Client socks5Client = new Socks5Client(streamHost, digest);
        InputStream inputStream = socks5Client.getSocket(2000).getInputStream();

        // add another network address before establishing SOCKS5 Bytestream
        socks5Proxy.addLocalAddress("localAddress");

        // finally call the method that should be tested
        OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();

        // test the established bytestream
        byte[] data = new byte[] { 1, 2, 3 };
        outputStream.write(data);

        byte[] result = new byte[3];
        inputStream.read(result);

        assertArrayEquals(data, result);

        protocol.verifyAll();

        // reset proxy settings
        socks5Proxy.stop();
        socks5Proxy.removeLocalAddress("localAddress");
        SmackConfiguration.setLocalSocks5ProxyPort(7777);

    }
View Full Code Here

    public void shouldFailIfRequestHasNoStreamHosts() throws Exception {

        try {

            // build SOCKS5 Bytestream initialization request with no SOCKS5 proxies
            Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(
                            initiatorJID, targetJID, sessionID);

            // get SOCKS5 Bytestream manager for connection
            Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
View Full Code Here

    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
View Full Code Here

     */
    @Test
    public void shouldBlacklistInvalidProxyAfter2Failures() throws Exception {

        // build SOCKS5 Bytestream initialization request
        Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(
                        initiatorJID, targetJID, sessionID);
        bytestreamInitialization.addStreamHost("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 (XMPPException e) {
                assertTrue(e.getMessage().contains(
                                "Could not establish socket with any provided host"));
            }

            // verify targets response
            assertEquals(1, protocol.getRequests().size());
            Packet 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.toString(),
                            ((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);

View Full Code Here

        // disable blacklisting
        Socks5BytestreamRequest.setConnectFailureThreshold(0);

        // build SOCKS5 Bytestream initialization request
        Bytestream bytestreamInitialization = Socks5PacketUtils.createBytestreamInitiation(
                        initiatorJID, targetJID, sessionID);
        bytestreamInitialization.addStreamHost("invalid." + proxyJID, "127.0.0.2", 7778);

        // get SOCKS5 Bytestream manager for connection
        Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);

        // try to connect several times
View Full Code Here

        // 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
View Full Code Here

        // 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
View Full Code Here

     * @param to the target
     * @param sessionID the session ID
     * @return SOCKS5 Bytestream initialization request packet
     */
    public static Bytestream createBytestreamInitiation(String from, String to, String sessionID) {
        Bytestream bytestream = new Bytestream();
        bytestream.getPacketID();
        bytestream.setFrom(from);
        bytestream.setTo(to);
        bytestream.setSessionID(sessionID);
        bytestream.setType(IQ.Type.SET);
        return bytestream;
    }
View Full Code Here

     * @param from the target
     * @param to the initiator
     * @return response to a SOCKS5 Bytestream initialization request
     */
    public static Bytestream createBytestreamResponse(String from, String to) {
        Bytestream streamHostInfo = new Bytestream();
        streamHostInfo.getPacketID();
        streamHostInfo.setFrom(from);
        streamHostInfo.setTo(to);
        streamHostInfo.setType(IQ.Type.RESULT);
        return streamHostInfo;
    }
View Full Code Here

TOP

Related Classes of org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamListener

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.