Package org.jivesoftware.smackx.bytestreams.socks5

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


     * @throws Exception should not happen
     */
    @Test
    public void shouldRespondWithErrorIfNoSupportedAuthenticationMethod() throws Exception {
        SmackConfiguration.setLocalSocks5ProxyPort(7890);
        Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
        proxy.start();

        Socket socket = new Socket(proxy.getLocalAddresses().get(0), proxy.getPort());

        OutputStream out = socket.getOutputStream();

        // request username/password-authentication
        out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x02 });

        InputStream in = socket.getInputStream();

        assertEquals((byte) 0x05, (byte) in.read());
        assertEquals((byte) 0xFF, (byte) in.read());

        assertEquals(-1, in.read());

        proxy.stop();

    }
View Full Code Here


     * @throws Exception should not happen
     */
    @Test
    public void shouldRespondWithErrorIfConnectionIsNotAllowed() throws Exception {
        SmackConfiguration.setLocalSocks5ProxyPort(7890);
        Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
        proxy.start();

        Socket socket = new Socket(proxy.getLocalAddresses().get(0), proxy.getPort());

        OutputStream out = socket.getOutputStream();
        out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00 });

        InputStream in = socket.getInputStream();

        assertEquals((byte) 0x05, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read());

        // send valid SOCKS5 message
        out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01,
                        (byte) 0xAA, (byte) 0x00, (byte) 0x00 });

        // verify error message
        assertEquals((byte) 0x05, (byte) in.read());
        assertFalse((byte) 0x00 == (byte) in.read()); // something other than 0 == success
        assertEquals((byte) 0x00, (byte) in.read());
        assertEquals((byte) 0x03, (byte) in.read());
        assertEquals((byte) 0x01, (byte) in.read());
        assertEquals((byte) 0xAA, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read());

        assertEquals(-1, in.read());

        proxy.stop();

    }
View Full Code Here

     * @throws Exception should not happen
     */
    @Test
    public void shouldSuccessfullyEstablishConnection() throws Exception {
        SmackConfiguration.setLocalSocks5ProxyPort(7890);
        Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
        proxy.start();

        assertTrue(proxy.isRunning());

        String digest = new String(new byte[] { (byte) 0xAA });

        // add digest to allow connection
        proxy.addTransfer(digest);

        Socket socket = new Socket(proxy.getLocalAddresses().get(0), proxy.getPort());

        OutputStream out = socket.getOutputStream();
        out.write(new byte[] { (byte) 0x05, (byte) 0x01, (byte) 0x00 });

        InputStream in = socket.getInputStream();

        assertEquals((byte) 0x05, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read());

        // send valid SOCKS5 message
        out.write(new byte[] { (byte) 0x05, (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x01,
                        (byte) 0xAA, (byte) 0x00, (byte) 0x00 });

        // verify response
        assertEquals((byte) 0x05, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read()); // success
        assertEquals((byte) 0x00, (byte) in.read());
        assertEquals((byte) 0x03, (byte) in.read());
        assertEquals((byte) 0x01, (byte) in.read());
        assertEquals((byte) 0xAA, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read());
        assertEquals((byte) 0x00, (byte) in.read());

        Thread.sleep(200);

        Socket remoteSocket = proxy.getSocket(digest);

        // remove digest
        proxy.removeTransfer(digest);

        // test stream
        OutputStream remoteOut = remoteSocket.getOutputStream();
        byte[] data = new byte[] { 1, 2, 3, 4, 5 };
        remoteOut.write(data);
        remoteOut.flush();

        for (int i = 0; i < data.length; i++) {
            assertEquals(data[i], in.read());
        }

        remoteSocket.close();

        assertEquals(-1, in.read());

        proxy.stop();

    }
View Full Code Here

     */
    @After
    public void cleanup() {
        SmackConfiguration.setLocalSocks5ProxyEnabled(true);
        SmackConfiguration.setLocalSocks5ProxyPort(7777);
        Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
        try {
            String address = InetAddress.getLocalHost().getHostAddress();
            List<String> addresses = new ArrayList<String>();
            addresses.add(address);
            socks5Proxy.replaceLocalAddresses(addresses);
        }
        catch (UnknownHostException e) {
            // ignore
        }

        socks5Proxy.stop();
    }
View Full Code Here

        return stunResults;
    }

    synchronized public void setPublicIP(InetAddress privateIP,
        InetAddress publicIP) {
        Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();

        if (recentPublicIPs.containsKey(privateIP)) {
            proxy.removeLocalAddress(recentPublicIPs.get(privateIP)
                .getHostAddress());

            recentPublicIPs.remove(privateIP);
        }
View Full Code Here

                /*
                 * If a public IP was retrieved, add it to the local addresses
                 * of the Socks5 proxy for external clients to be able to
                 * connect to me.
                 */
                Socks5Proxy proxy = Socks5Proxy.getSocks5Proxy();
                if (ip != null) {
                    // the local IP matches the retrieved IP address, so it is
                    // already the public IP. Store this fact for later.
                    if (ip.equals(localAddress)) {
                        aLocalIPisPublicIP = true;
                        return;
                    }

                    synchronized (stunResults) {
                        stunResults.add(di);
                    }

                    // Add public IP if its not already in the list
                    if (!proxy.getLocalAddresses()
                        .contains(ip.getHostAddress())) {
                        setPublicIP(localAddress, ip);
                        log.debug("Added WAN-IP: " + ip.getHostAddress()
                            + " (through " + localAddress.getHostAddress()
                            + ")");
View Full Code Here

         * TODO Fix in Smack: Either always start proxy when enabled in the
         * smack configuration or never start it automatically. Currently it
         * only starts after initiation the singleton on first access.
         */

        Socks5Proxy proxy = NetworkingUtils.getSocks5ProxySafe();

        if (proxyEnabled != SmackConfiguration.isLocalSocks5ProxyEnabled()) {
            settingsChanged = true;
            SmackConfiguration.setLocalSocks5ProxyEnabled(proxyEnabled);
        }

        // Note: The proxy gets restarted on port change, too.
        if (proxyPort != SmackConfiguration.getLocalSocks5ProxyPort()) {
            settingsChanged = true;
            portChanged = true;
            SmackConfiguration.setLocalSocks5ProxyPort(proxyPort);
        }

        // Get & set all (useful) internal and external IP addresses as
        // potential connect addresses
        if (proxyEnabled) {

            try {
                List<InetAddress> myAdresses = NetworkingUtils
                    .getAllNonLoopbackLocalIPAdresses(true);

                if (!myAdresses.isEmpty()) {
                    // convert to List of Strings
                    List<String> myAdressesStr = new LinkedList<String>();
                    for (InetAddress ip : myAdresses)
                        myAdressesStr.add(ip.getHostAddress());
                    proxy.replaceLocalAddresses(myAdressesStr);
                }

                // Perform public IP detection concurrently
                // Dont perform if we know a local IP is the WAN IP
                if (stunHelper != null && !stunHelper.isLocalIPthePublicIP())
                    stunHelper.startWANIPDetection(stunServer, stunPort, false);

            } catch (Exception e) {
                log.debug("Error while retrieving IP addresses", e);
            }
        }

        if (settingsChanged || proxy.isRunning() != proxyEnabled) {
            StringBuilder sb = new StringBuilder();
            if (settingsChanged)
                sb.append("Socks5Proxy properties changed. ");

            if (proxy.isRunning()) {
                proxy.stop();
                sb.append("Socks5Proxy stopped. ");
            }
            if (proxyEnabled) {
                proxy.start();
                sb.append("Socks5Proxy started on port " + proxy.getPort()
                    + "...");
            }
            log.debug(sb);

            if (upnpManager != null) {
View Full Code Here

    @Test
    public void shouldFailIfTargetIsNotConnectedToLocalSocks5Proxy() throws Exception {

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

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

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

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

        try {
            socks5Client.getSocket(10000);

            fail("exception should be thrown");
        }
        catch (XMPPException e) {
            assertTrue(e.getMessage().contains("target is not connected to SOCKS5 proxy"));
            protocol.verifyAll(); // assert no XMPP messages were sent
        }

        socks5Proxy.stop();

    }
View Full Code Here

    @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

TOP

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

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.