Search in sources :

Example 11 with Identity

use of org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity in project Smack by igniterealtime.

the class EntityCapsManager method updateLocalEntityCaps.

/**
     * Updates the local user Entity Caps information with the data provided
     *
     * If we are connected and there was already a presence send, another
     * presence is send to inform others about your new Entity Caps node string.
     *
     */
public void updateLocalEntityCaps() {
    XMPPConnection connection = connection();
    DiscoverInfo discoverInfo = new DiscoverInfo();
    discoverInfo.setType(IQ.Type.result);
    sdm.addDiscoverInfoTo(discoverInfo);
    // getLocalNodeVer() will return a result only after currentCapsVersion is set. Therefore
    // set it first and then call getLocalNodeVer()
    currentCapsVersion = generateVerificationString(discoverInfo);
    final String localNodeVer = getLocalNodeVer();
    discoverInfo.setNode(localNodeVer);
    addDiscoverInfoByNode(localNodeVer, discoverInfo);
    if (lastLocalCapsVersions.size() > 10) {
        CapsVersionAndHash oldCapsVersion = lastLocalCapsVersions.poll();
        sdm.removeNodeInformationProvider(entityNode + '#' + oldCapsVersion.version);
    }
    lastLocalCapsVersions.add(currentCapsVersion);
    if (connection != null)
        JID_TO_NODEVER_CACHE.put(connection.getUser(), new NodeVerHash(entityNode, currentCapsVersion));
    final List<Identity> identities = new LinkedList<Identity>(ServiceDiscoveryManager.getInstanceFor(connection).getIdentities());
    sdm.setNodeInformationProvider(localNodeVer, new AbstractNodeInformationProvider() {

        List<String> features = sdm.getFeatures();

        List<ExtensionElement> packetExtensions = sdm.getExtendedInfoAsList();

        @Override
        public List<String> getNodeFeatures() {
            return features;
        }

        @Override
        public List<Identity> getNodeIdentities() {
            return identities;
        }

        @Override
        public List<ExtensionElement> getNodePacketExtensions() {
            return packetExtensions;
        }
    });
    // to respect ConnectionConfiguration.isSendPresence()
    if (connection != null && connection.isAuthenticated() && presenceSend != null) {
        try {
            connection.sendStanza(presenceSend.cloneWithNewId());
        } catch (InterruptedException | NotConnectedException e) {
            LOGGER.log(Level.WARNING, "Could could not update presence with caps info", e);
        }
    }
}
Also used : DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) AbstractNodeInformationProvider(org.jivesoftware.smackx.disco.AbstractNodeInformationProvider) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XMPPConnection(org.jivesoftware.smack.XMPPConnection) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)

Example 12 with Identity

use of org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity 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 13 with Identity

use of org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldFailIfNoSocks5ProxyFound2.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should fail if no
 * proxy is a SOCKS5 proxy.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldFailIfNoSocks5ProxyFound2() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldFailIfNoSocks5ProxyFound2";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * create responses in the order they should be queried specified by the XEP-0065
     * specification
     */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items containing a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about NOT being a Socks5
    // proxy
    DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("noproxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the proxy identity if proxy is queried
    protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    SmackException e = assertThrows(SmackException.class, () -> {
        // start SOCKS5 Bytestream
        byteStreamManager.establishSession(targetJID, sessionID);
    });
    protocol.verifyAll();
    assertTrue(e.getMessage().contains("no SOCKS5 proxies available"));
}
Also used : Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) SmackException(org.jivesoftware.smack.SmackException) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Protocol(org.jivesoftware.util.Protocol) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) Test(org.junit.jupiter.api.Test)

Example 14 with Identity

use of org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity in project Smack by igniterealtime.

the class Socks5ByteStreamManagerTest method shouldNegotiateSocks5BytestreamAndTransferData.

/**
 * Invoking {@link Socks5BytestreamManager#establishSession(org.jxmpp.jid.Jid, String)} should successfully
 * negotiate and return a SOCKS5 Bytestream connection.
 *
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws XMPPException if an XMPP protocol error was received.
 * @throws IOException if an I/O error occurred.
 */
@Test
public void shouldNegotiateSocks5BytestreamAndTransferData() throws SmackException, InterruptedException, IOException, XMPPException {
    final Protocol protocol = new Protocol();
    final XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, initiatorJID);
    final String sessionID = "session_id_shouldNegotiateSocks5BytestreamAndTransferData";
    // get Socks5ByteStreamManager for connection
    Socks5BytestreamManager byteStreamManager = Socks5BytestreamManager.getBytestreamManager(connection);
    byteStreamManager.setAnnounceLocalStreamHost(false);
    /**
     * create responses in the order they should be queried specified by the XEP-0065
     * specification
     */
    // build discover info that supports the SOCKS5 feature
    DiscoverInfoBuilder discoverInfo = Socks5PacketUtils.createDiscoverInfo(targetJID, initiatorJID);
    discoverInfo.addFeature(Bytestream.NAMESPACE);
    // return that SOCKS5 is supported if target is queried
    protocol.addResponse(discoverInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover items containing a proxy item
    DiscoverItems discoverItems = Socks5PacketUtils.createDiscoverItems(xmppServer, initiatorJID);
    Item item = new Item(proxyJID);
    discoverItems.addItem(item);
    // return the proxy item if XMPP server is queried
    protocol.addResponse(discoverItems, Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build discover info for proxy containing information about being a SOCKS5 proxy
    DiscoverInfoBuilder proxyInfo = Socks5PacketUtils.createDiscoverInfo(proxyJID, initiatorJID);
    Identity identity = new Identity("proxy", proxyJID.toString(), "bytestreams");
    proxyInfo.addIdentity(identity);
    // return the socks5 bytestream proxy identity if proxy is queried
    protocol.addResponse(proxyInfo.build(), Verification.correspondingSenderReceiver, Verification.requestTypeGET);
    // build a socks5 stream host info containing the address and the port of the
    // proxy
    ServerSocket proxyServerSocket = NetworkUtil.getSocketOnLoopback();
    Bytestream streamHostInfo = Socks5PacketUtils.createBytestreamResponse(proxyJID, initiatorJID);
    streamHostInfo.addStreamHost(proxyJID, proxyAddress, proxyServerSocket.getLocalPort());
    // return stream host info if it is queried
    protocol.addResponse(streamHostInfo, 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, new Verification<Bytestream, Bytestream>() {

        @Override
        public void verify(Bytestream request, Bytestream response) {
            assertEquals(response.getSessionID(), request.getSessionID());
            assertEquals(1, request.getStreamHosts().size());
            StreamHost streamHost = (StreamHost) request.getStreamHosts().toArray()[0];
            assertEquals(response.getUsedHost().getJID(), streamHost.getJID());
        }
    }, 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);
    // start a local SOCKS5 proxy
    try (Socks5TestProxy socks5Proxy = new Socks5TestProxy(proxyServerSocket)) {
        // create digest to get the socket opened by target
        String digest = Socks5Utils.createDigest(sessionID, initiatorJID, targetJID);
        // finally call the method that should be tested
        OutputStream outputStream = byteStreamManager.establishSession(targetJID, sessionID).getOutputStream();
        // test the established bytestream
        InputStream inputStream = socks5Proxy.getSocket(digest).getInputStream();
        byte[] data = new byte[] { 1, 2, 3 };
        outputStream.write(data);
        byte[] result = new byte[3];
        inputStream.read(result);
        assertArrayEquals(data, result);
    }
    protocol.verifyAll();
}
Also used : InputStream(java.io.InputStream) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) OutputStream(java.io.OutputStream) ErrorIQ(org.jivesoftware.smack.packet.ErrorIQ) IQ(org.jivesoftware.smack.packet.IQ) DiscoverItems(org.jivesoftware.smackx.disco.packet.DiscoverItems) ServerSocket(java.net.ServerSocket) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Item(org.jivesoftware.smackx.disco.packet.DiscoverItems.Item) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) Protocol(org.jivesoftware.util.Protocol) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) StreamHost(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream.StreamHost) Test(org.junit.jupiter.api.Test)

Example 15 with Identity

use of org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity in project Smack by igniterealtime.

the class ConfigureFormTest method getConfigFormWithTimeout.

@Test
public void getConfigFormWithTimeout() throws XMPPException, InterruptedException, SmackException, IOException {
    ThreadedDummyConnection con = ThreadedDummyConnection.newInstance();
    PubSubManager mgr = new PubSubManager(con, PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    DiscoverInfoBuilder info = DiscoverInfo.builder("disco-result").ofType(IQ.Type.result).from(PubSubManagerTest.DUMMY_PUBSUB_SERVICE);
    Identity ident = new Identity("pubsub", null, "leaf");
    info.addIdentity(ident);
    DiscoverInfo discoverInfo = info.build();
    con.addIQReply(discoverInfo);
    Node node = mgr.getNode("princely_musings");
    SmackConfiguration.setDefaultReplyTimeout(100);
    con.setTimeout();
    assertThrows(SmackException.class, () -> {
        node.getNodeConfiguration();
    });
}
Also used : ThreadedDummyConnection(org.jivesoftware.smack.ThreadedDummyConnection) DiscoverInfo(org.jivesoftware.smackx.disco.packet.DiscoverInfo) DiscoverInfoBuilder(org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder) Identity(org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity) Test(org.junit.jupiter.api.Test)

Aggregations

Identity (org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity)15 DiscoverInfoBuilder (org.jivesoftware.smackx.disco.packet.DiscoverInfoBuilder)10 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)9 XMPPConnection (org.jivesoftware.smack.XMPPConnection)8 DiscoverItems (org.jivesoftware.smackx.disco.packet.DiscoverItems)8 Item (org.jivesoftware.smackx.disco.packet.DiscoverItems.Item)8 Test (org.junit.jupiter.api.Test)8 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)6 Protocol (org.jivesoftware.util.Protocol)6 ErrorIQ (org.jivesoftware.smack.packet.ErrorIQ)4 IQ (org.jivesoftware.smack.packet.IQ)4 SmackException (org.jivesoftware.smack.SmackException)3 ThreadedDummyConnection (org.jivesoftware.smack.ThreadedDummyConnection)3 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)3 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)3 MessageDigest (java.security.MessageDigest)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 StanzaError (org.jivesoftware.smack.packet.StanzaError)2