summaryrefslogtreecommitdiffstats
blob: b5c5173c7f8ae9e168041d75bb1999a739441ed0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

/*
 * Copyright (c) 2015-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */


#pragma once

#include <map>
#include <string>
#include <utility>
#include <vector>

#include <Swiften/Base/API.h>
#include <Swiften/Elements/S5BProxyRequest.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamClientSession.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamProxyFinder.h>
#include <Swiften/JID/JID.h>

namespace Swift {
    class TimerFactory;
    class ConnectionFactory;
    class DomainNameResolver;
    class DomainNameResolveError;
    class IQRouter;

    /**
     *    - manages list of working S5B proxies
     *    - creates initial connections (for the candidates you provide)
     */
    class SWIFTEN_API SOCKS5BytestreamProxiesManager {
        public:
            SOCKS5BytestreamProxiesManager(ConnectionFactory*, TimerFactory*, DomainNameResolver*, IQRouter*, const JID&);
            ~SOCKS5BytestreamProxiesManager();

            void addS5BProxy(S5BProxyRequest::ref);

            /*
             * Returns a list of external S5B proxies. If the optinal return value is not initialized a discovery process has been started and
             * onDiscoveredProxiesChanged signal will be emitted when it is finished.
             */
            const boost::optional<std::vector<S5BProxyRequest::ref> >& getOrDiscoverS5BProxies();

            void connectToProxies(const std::string& sessionID);
            std::shared_ptr<SOCKS5BytestreamClientSession> getProxySessionAndCloseOthers(const JID& proxyJID, const std::string& sessionID);

            std::shared_ptr<SOCKS5BytestreamClientSession> createSOCKS5BytestreamClientSession(HostAddressPort addressPort, const std::string& destAddr);

        public:
            boost::signals2::signal<void ()> onDiscoveredProxiesChanged;

        private:
            void handleProxiesFound(std::vector<S5BProxyRequest::ref> proxyHosts);
            void handleNameLookupResult(const std::vector<HostAddress>&, boost::optional<DomainNameResolveError>, S5BProxyRequest::ref proxy);

            void queryForProxies();

            void handleProxySessionReady(const std::string& sessionID, const JID& jid, std::shared_ptr<SOCKS5BytestreamClientSession> session, bool error);
            void handleProxySessionFinished(const std::string& sessionID, const JID& jid, std::shared_ptr<SOCKS5BytestreamClientSession> session, boost::optional<FileTransferError> error);

        private:
            ConnectionFactory* connectionFactory_;
            TimerFactory* timerFactory_;
            DomainNameResolver* resolver_;
            IQRouter* iqRouter_;
            JID serviceRoot_;

            typedef std::vector<std::pair<JID, std::shared_ptr<SOCKS5BytestreamClientSession> > > ProxyJIDClientSessionVector;
            typedef std::map<std::string, ProxyJIDClientSessionVector> ProxySessionsMap;
            ProxySessionsMap proxySessions_;

            std::shared_ptr<SOCKS5BytestreamProxyFinder> proxyFinder_;

            boost::optional<std::vector<S5BProxyRequest::ref> > localS5BProxies_;
    };

}