Search in sources :

Example 11 with StreamHost

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

the class Socks5ClientTest method shouldCloseSocketIfServerDoesNotAcceptAuthenticationMethod.

/**
     * A SOCKS5 client MUST close connection if server doesn't accept any of the given
     * authentication methods. (See RFC1928 Section 3)
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldCloseSocketIfServerDoesNotAcceptAuthenticationMethod() throws Exception {
    // start thread to connect to SOCKS5 proxy
    Thread serverThread = new Thread() {

        @Override
        public void run() {
            StreamHost streamHost = new StreamHost(proxyJID, serverAddress, serverPort);
            Socks5Client socks5Client = new Socks5Client(streamHost, digest);
            try {
                socks5Client.getSocket(10000);
                fail("exception should be thrown");
            } catch (SmackException e) {
                assertTrue(e.getMessage().contains("SOCKS5 negotiation failed"));
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    serverThread.start();
    // accept connection form client
    Socket socket = serverSocket.accept();
    DataInputStream in = new DataInputStream(socket.getInputStream());
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    // validate authentication request
    // version
    assertEquals((byte) 0x05, (byte) in.read());
    // number of supported auth methods
    assertEquals((byte) 0x01, (byte) in.read());
    // no-authentication method
    assertEquals((byte) 0x00, (byte) in.read());
    // respond that no authentication method is accepted
    out.write(new byte[] { (byte) 0x05, (byte) 0xFF });
    out.flush();
    // wait for client to shutdown
    serverThread.join();
    // assert socket is closed
    assertEquals(-1, in.read());
}
Also used : DataOutputStream(java.io.DataOutputStream) SmackException(org.jivesoftware.smack.SmackException) DataInputStream(java.io.DataInputStream) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) SmackException(org.jivesoftware.smack.SmackException) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) Test(org.junit.Test)

Example 12 with StreamHost

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

the class Socks5BytestreamRequest method createUsedHostResponse.

/**
     * Returns the response to the SOCKS5 Bytestream request containing the SOCKS5 proxy used.
     * 
     * @param selectedHost the used SOCKS5 proxy
     * @return the response to the SOCKS5 Bytestream request
     */
private Bytestream createUsedHostResponse(StreamHost selectedHost) {
    Bytestream response = new Bytestream(this.bytestreamRequest.getSessionID());
    response.setTo(this.bytestreamRequest.getFrom());
    response.setType(IQ.Type.result);
    response.setStanzaId(this.bytestreamRequest.getStanzaId());
    response.setUsedHost(selectedHost.getJID());
    return response;
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)

Example 13 with StreamHost

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

the class Socks5BytestreamManager method establishSession.

/**
     * Establishes a SOCKS5 Bytestream with the given user using the given session ID and returns
     * the Socket to send/receive data to/from the user.
     * 
     * @param targetJID the JID of the user a SOCKS5 Bytestream should be established
     * @param sessionID the session ID for the SOCKS5 Bytestream request
     * @return the Socket to send/receive data to/from the user
     * @throws IOException if the bytestream could not be established
     * @throws InterruptedException if the current thread was interrupted while waiting
     * @throws NoResponseException 
     * @throws SmackException if the target does not support SOCKS5.
     * @throws XMPPException 
     */
@Override
public Socks5BytestreamSession establishSession(Jid targetJID, String sessionID) throws IOException, InterruptedException, NoResponseException, SmackException, XMPPException {
    XMPPConnection connection = connection();
    XMPPErrorException discoveryException = null;
    // check if target supports SOCKS5 Bytestream
    if (!supportsSocks5(targetJID)) {
        throw new FeatureNotSupportedException("SOCKS5 Bytestream", targetJID);
    }
    List<Jid> proxies = new ArrayList<>();
    // determine SOCKS5 proxies from XMPP-server
    try {
        proxies.addAll(determineProxies());
    } catch (XMPPErrorException e) {
        // don't abort here, just remember the exception thrown by determineProxies()
        // determineStreamHostInfos() will at least add the local Socks5 proxy (if enabled)
        discoveryException = e;
    }
    // determine address and port of each proxy
    List<StreamHost> streamHosts = determineStreamHostInfos(proxies);
    if (streamHosts.isEmpty()) {
        if (discoveryException != null) {
            throw discoveryException;
        } else {
            throw new SmackException("no SOCKS5 proxies available");
        }
    }
    // compute digest
    String digest = Socks5Utils.createDigest(sessionID, connection.getUser(), targetJID);
    // prioritize last working SOCKS5 proxy if exists
    if (this.proxyPrioritizationEnabled && this.lastWorkingProxy != null) {
        StreamHost selectedStreamHost = null;
        for (StreamHost streamHost : streamHosts) {
            if (streamHost.getJID().equals(this.lastWorkingProxy)) {
                selectedStreamHost = streamHost;
                break;
            }
        }
        if (selectedStreamHost != null) {
            streamHosts.remove(selectedStreamHost);
            streamHosts.add(0, selectedStreamHost);
        }
    }
    Socks5Proxy socks5Proxy = Socks5Proxy.getSocks5Proxy();
    try {
        // add transfer digest to local proxy to make transfer valid
        socks5Proxy.addTransfer(digest);
        // create initiation packet
        Bytestream initiation = createBytestreamInitiation(sessionID, targetJID, streamHosts);
        // send initiation packet
        Stanza response = connection.createStanzaCollectorAndSend(initiation).nextResultOrThrow(getTargetResponseTimeout());
        // extract used stream host from response
        StreamHostUsed streamHostUsed = ((Bytestream) response).getUsedHost();
        StreamHost usedStreamHost = initiation.getStreamHost(streamHostUsed.getJID());
        if (usedStreamHost == null) {
            throw new SmackException("Remote user responded with unknown host");
        }
        // build SOCKS5 client
        Socks5Client socks5Client = new Socks5ClientForInitiator(usedStreamHost, digest, connection, sessionID, targetJID);
        // establish connection to proxy
        Socket socket = socks5Client.getSocket(getProxyConnectionTimeout());
        // remember last working SOCKS5 proxy to prioritize it for next request
        this.lastWorkingProxy = usedStreamHost.getJID();
        // negotiation successful, return the output stream
        return new Socks5BytestreamSession(socket, usedStreamHost.getJID().equals(connection.getUser()));
    } catch (TimeoutException e) {
        throw new IOException("Timeout while connecting to SOCKS5 proxy");
    } finally {
        // remove transfer digest if output stream is returned or an exception
        // occurred
        socks5Proxy.removeTransfer(digest);
    }
}
Also used : StreamHostUsed(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHostUsed) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) Jid(org.jxmpp.jid.Jid) SmackException(org.jivesoftware.smack.SmackException) Stanza(org.jivesoftware.smack.packet.Stanza) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) IOException(java.io.IOException) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Example 14 with StreamHost

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

the class Socks5BytestreamManager method createBytestreamInitiation.

/**
     * Returns a SOCKS5 Bytestream initialization request stanza(/packet) with the given session ID
     * containing the given stream hosts for the given target JID.
     * 
     * @param sessionID the session ID for the SOCKS5 Bytestream
     * @param targetJID the target JID of SOCKS5 Bytestream request
     * @param streamHosts a list of SOCKS5 proxies the target should connect to
     * @return a SOCKS5 Bytestream initialization request packet
     */
private static Bytestream createBytestreamInitiation(String sessionID, Jid targetJID, List<StreamHost> streamHosts) {
    Bytestream initiation = new Bytestream(sessionID);
    // add all stream hosts
    for (StreamHost streamHost : streamHosts) {
        initiation.addStreamHost(streamHost);
    }
    initiation.setType(IQ.Type.set);
    initiation.setTo(targetJID);
    return initiation;
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)

Example 15 with StreamHost

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

the class Socks5BytestreamManager method determineStreamHostInfos.

/**
     * Returns a list of stream hosts containing the IP address an the port for the given list of
     * SOCKS5 proxy JIDs. The order of the returned list is the same as the given list of JIDs
     * excluding all SOCKS5 proxies who's network settings could not be determined. If a local
     * SOCKS5 proxy is running it will be the first item in the list returned.
     * 
     * @param proxies a list of SOCKS5 proxy JIDs
     * @return a list of stream hosts containing the IP address an the port
     */
private List<StreamHost> determineStreamHostInfos(List<Jid> proxies) {
    XMPPConnection connection = connection();
    List<StreamHost> streamHosts = new ArrayList<StreamHost>();
    // add local proxy on first position if exists
    List<StreamHost> localProxies = getLocalStreamHost();
    if (localProxies != null) {
        streamHosts.addAll(localProxies);
    }
    // query SOCKS5 proxies for network settings
    for (Jid proxy : proxies) {
        Bytestream streamHostRequest = createStreamHostRequest(proxy);
        try {
            Bytestream response = (Bytestream) connection.createStanzaCollectorAndSend(streamHostRequest).nextResultOrThrow();
            streamHosts.addAll(response.getStreamHosts());
        } catch (Exception e) {
            // blacklist errornous proxies
            this.proxyBlacklist.add(proxy);
        }
    }
    return streamHosts;
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Jid(org.jxmpp.jid.Jid) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) SmackException(org.jivesoftware.smack.SmackException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) TimeoutException(java.util.concurrent.TimeoutException) FeatureNotSupportedException(org.jivesoftware.smack.SmackException.FeatureNotSupportedException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)18 Test (org.junit.Test)13 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)12 SmackException (org.jivesoftware.smack.SmackException)10 Socket (java.net.Socket)8 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)5 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)5 DataInputStream (java.io.DataInputStream)4 DataOutputStream (java.io.DataOutputStream)4 IOException (java.io.IOException)4 ServerSocket (java.net.ServerSocket)4 XMPPException (org.jivesoftware.smack.XMPPException)4 ArrayList (java.util.ArrayList)3 TimeoutException (java.util.concurrent.TimeoutException)3 FeatureNotSupportedException (org.jivesoftware.smack.SmackException.FeatureNotSupportedException)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)3 IQ (org.jivesoftware.smack.packet.IQ)3 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)3