Search in sources :

Example 1 with Bytestream

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

the class BytestreamsProvider method parse.

@Override
public Bytestream parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException {
    boolean done = false;
    Bytestream toReturn = new Bytestream();
    String id = parser.getAttributeValue("", "sid");
    String mode = parser.getAttributeValue("", "mode");
    // streamhost
    Jid JID = null;
    String host = null;
    String port = null;
    int eventType;
    String elementName;
    while (!done) {
        eventType = parser.next();
        elementName = parser.getName();
        if (eventType == XmlPullParser.START_TAG) {
            if (elementName.equals(Bytestream.StreamHost.ELEMENTNAME)) {
                JID = ParserUtils.getJidAttribute(parser);
                host = parser.getAttributeValue("", "host");
                port = parser.getAttributeValue("", "port");
            } else if (elementName.equals(Bytestream.StreamHostUsed.ELEMENTNAME)) {
                toReturn.setUsedHost(ParserUtils.getJidAttribute(parser));
            } else if (elementName.equals(Bytestream.Activate.ELEMENTNAME)) {
                toReturn.setToActivate(ParserUtils.getJidAttribute(parser));
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (elementName.equals("streamhost")) {
                if (port == null) {
                    toReturn.addStreamHost(JID, host);
                } else {
                    toReturn.addStreamHost(JID, host, Integer.parseInt(port));
                }
                JID = null;
                host = null;
                port = null;
            } else if (elementName.equals("query")) {
                done = true;
            }
        }
    }
    if (mode == null) {
        toReturn.setMode(Mode.tcp);
    } else {
        toReturn.setMode((Bytestream.Mode.fromName(mode)));
    }
    toReturn.setSessionID(id);
    return toReturn;
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Jid(org.jxmpp.jid.Jid)

Example 2 with Bytestream

use of org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream in project ecf by eclipse.

the class InitiationListener method processRequest.

private void processRequest(Packet packet) {
    Bytestream byteStreamRequest = (Bytestream) packet;
    // ignore request if in ignore list
    if (this.manager.getIgnoredBytestreamRequests().remove(byteStreamRequest.getSessionID())) {
        return;
    }
    // build bytestream request from packet
    Socks5BytestreamRequest request = new Socks5BytestreamRequest(this.manager, byteStreamRequest);
    // notify listeners for bytestream initiation from a specific user
    BytestreamListener userListener = this.manager.getUserListener(byteStreamRequest.getFrom());
    if (userListener != null) {
        userListener.incomingBytestreamRequest(request);
    } else if (!this.manager.getAllRequestListeners().isEmpty()) {
        /*
             * if there is no user specific listener inform listeners for all initiation requests
             */
        for (BytestreamListener listener : this.manager.getAllRequestListeners()) {
            listener.incomingBytestreamRequest(request);
        }
    } else {
        /*
             * if there is no listener for this initiation request, reply with reject message
             */
        this.manager.replyRejectPacket(byteStreamRequest);
    }
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) BytestreamListener(org.jivesoftware.smackx.bytestreams.BytestreamListener)

Example 3 with Bytestream

use of org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream in project ecf by eclipse.

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 XMPPException if the user doesn't support or accept SOCKS5 Bytestreams, if no Socks5
 *         Proxy could be found, if the user couldn't connect to any of the SOCKS5 Proxies
 * @throws IOException if the bytestream could not be established
 * @throws InterruptedException if the current thread was interrupted while waiting
 */
public Socks5BytestreamSession establishSession(String targetJID, String sessionID) throws XMPPException, IOException, InterruptedException {
    XMPPException discoveryException = null;
    // check if target supports SOCKS5 Bytestream
    if (!supportsSocks5(targetJID)) {
        throw new XMPPException(targetJID + " doesn't support SOCKS5 Bytestream");
    }
    List<String> proxies = new ArrayList<String>();
    // determine SOCKS5 proxies from XMPP-server
    try {
        proxies.addAll(determineProxies());
    } catch (XMPPException 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()) {
        throw discoveryException != null ? discoveryException : new XMPPException("no SOCKS5 proxies available");
    }
    // compute digest
    String digest = Socks5Utils.createDigest(sessionID, this.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
        Packet response = SyncPacketSend.getReply(this.connection, initiation, getTargetResponseTimeout());
        // extract used stream host from response
        StreamHostUsed streamHostUsed = ((Bytestream) response).getUsedHost();
        StreamHost usedStreamHost = initiation.getStreamHost(streamHostUsed.getJID());
        if (usedStreamHost == null) {
            throw new XMPPException("Remote user responded with unknown host");
        }
        // build SOCKS5 client
        Socks5Client socks5Client = new Socks5ClientForInitiator(usedStreamHost, digest, this.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(this.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) Packet(org.jivesoftware.smack.packet.Packet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPException(org.jivesoftware.smack.XMPPException) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Socket(java.net.Socket) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with Bytestream

use of org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream in project ecf by eclipse.

the class Socks5BytestreamManager method createStreamHostRequest.

/**
 * Returns a IQ packet to query a SOCKS5 proxy its network settings.
 *
 * @param proxy the proxy to query
 * @return IQ packet to query a SOCKS5 proxy its network settings
 */
private Bytestream createStreamHostRequest(String proxy) {
    Bytestream request = new Bytestream();
    request.setType(IQ.Type.GET);
    request.setTo(proxy);
    return request;
}
Also used : Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)

Example 5 with Bytestream

use of org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream in project ecf by eclipse.

the class Socks5BytestreamManager method createBytestreamInitiation.

/**
 * Returns a SOCKS5 Bytestream initialization request 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 Bytestream createBytestreamInitiation(String sessionID, String 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)

Aggregations

Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)42 XMPPConnection (org.jivesoftware.smack.XMPPConnection)21 StreamHost (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost)14 Protocol (org.jivesoftware.util.Protocol)14 Test (org.junit.jupiter.api.Test)14 InputStream (java.io.InputStream)11 OutputStream (java.io.OutputStream)11 XMPPException (org.jivesoftware.smack.XMPPException)11 IQ (org.jivesoftware.smack.packet.IQ)10 IOException (java.io.IOException)9 TimeoutException (java.util.concurrent.TimeoutException)9 Stanza (org.jivesoftware.smack.packet.Stanza)7 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)7 Socket (java.net.Socket)6 SmackException (org.jivesoftware.smack.SmackException)6 Socks5BytestreamSession (org.jivesoftware.smackx.bytestreams.socks5.Socks5BytestreamSession)6 Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)6 DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)6 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)6 ArrayList (java.util.ArrayList)5