summaryrefslogtreecommitdiffstats
blob: 26ad8b58bcc9c6a226aea1119aded9623c892772 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

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


#include <Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.h>

#include <boost/smart_ptr/make_shared.hpp>
#include <boost/bind.hpp>

#include <Swiften/Base/foreach.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamClientSession.h>
#include <Swiften/Base/Log.h>
#include <Swiften/Network/DomainNameResolver.h>
#include <Swiften/Network/ConnectionFactory.h>
#include <Swiften/Network/TimerFactory.h>
#include <Swiften/Network/DomainNameAddressQuery.h>
#include <Swiften/Network/DomainNameResolveError.h>

namespace Swift {

SOCKS5BytestreamProxiesManager::SOCKS5BytestreamProxiesManager(ConnectionFactory *connFactory, TimerFactory *timeFactory, DomainNameResolver* resolver, IQRouter* iqRouter, const JID& serviceRoot) : connectionFactory_(connFactory), timerFactory_(timeFactory), resolver_(resolver), iqRouter_(iqRouter), serviceRoot_(serviceRoot) {

}

SOCKS5BytestreamProxiesManager::~SOCKS5BytestreamProxiesManager() {
	if (proxyFinder_) {
		proxyFinder_->stop();
	}
}

void SOCKS5BytestreamProxiesManager::addS5BProxy(S5BProxyRequest::ref proxy) {
	if (proxy) {
		SWIFT_LOG_ASSERT(HostAddress(proxy->getStreamHost().get().host).isValid(), warning) << std::endl;
		if (!localS5BProxies_) {
			localS5BProxies_ = std::vector<S5BProxyRequest::ref>();
		}
		localS5BProxies_->push_back(proxy);
		onDiscoveredProxiesChanged();
	}
}

const boost::optional<std::vector<S5BProxyRequest::ref> >& SOCKS5BytestreamProxiesManager::getOrDiscoverS5BProxies() {
	if (!localS5BProxies_ && !proxyFinder_) {
		queryForProxies();
	}
	return localS5BProxies_;
}

void SOCKS5BytestreamProxiesManager::connectToProxies(const std::string& sessionID) {
	SWIFT_LOG(debug) << "session ID: " << sessionID << std::endl;
	ProxyJIDClientSessionMap clientSessions;

	if (localS5BProxies_) {
		foreach(S5BProxyRequest::ref proxy, localS5BProxies_.get()) {
			boost::shared_ptr<Connection> conn = connectionFactory_->createConnection();

			HostAddressPort addressPort = HostAddressPort(proxy->getStreamHost().get().host, proxy->getStreamHost().get().port);
			SWIFT_LOG_ASSERT(addressPort.isValid(), warning) << std::endl;
			boost::shared_ptr<SOCKS5BytestreamClientSession> session = boost::make_shared<SOCKS5BytestreamClientSession>(conn, addressPort, sessionID, timerFactory_);
			clientSessions[proxy->getStreamHost().get().jid] = session;
			session->start();
		}
	}

	proxySessions_[sessionID] = clientSessions;
}

boost::shared_ptr<SOCKS5BytestreamClientSession> SOCKS5BytestreamProxiesManager::getProxySessionAndCloseOthers(const JID& proxyJID, const std::string& sessionID) {
	// checking parameters
	if (proxySessions_.find(sessionID) == proxySessions_.end()) {
		return boost::shared_ptr<SOCKS5BytestreamClientSession>();
	}
	if (proxySessions_[sessionID].find(proxyJID) == proxySessions_[sessionID].end()) {
		return boost::shared_ptr<SOCKS5BytestreamClientSession>();
	}

	// get active session
	boost::shared_ptr<SOCKS5BytestreamClientSession> activeSession = proxySessions_[sessionID][proxyJID];
	proxySessions_[sessionID].erase(proxyJID);

	// close other sessions
	foreach(const ProxyJIDClientSessionMap::value_type& myPair, proxySessions_[sessionID]) {
		myPair.second->stop();
	}

	proxySessions_.erase(sessionID);

	return activeSession;
}

boost::shared_ptr<SOCKS5BytestreamClientSession> SOCKS5BytestreamProxiesManager::createSOCKS5BytestreamClientSession(HostAddressPort addressPort, const std::string& destAddr) {
	SOCKS5BytestreamClientSession::ref connection = boost::make_shared<SOCKS5BytestreamClientSession>(connectionFactory_->createConnection(), addressPort, destAddr, timerFactory_);
	return connection;
}

void SOCKS5BytestreamProxiesManager::handleProxyFound(S5BProxyRequest::ref proxy) {
	if (proxy) {
		if (HostAddress(proxy->getStreamHost().get().host).isValid()) {
			addS5BProxy(proxy);
		}
		else {
			DomainNameAddressQuery::ref resolveRequest = resolver_->createAddressQuery(proxy->getStreamHost().get().host);
			resolveRequest->onResult.connect(boost::bind(&SOCKS5BytestreamProxiesManager::handleNameLookupResult, this, _1, _2, proxy));
			resolveRequest->run();
		}
	}
	else {
		onDiscoveredProxiesChanged();
	}
	proxyFinder_->stop();
	proxyFinder_.reset();
}

void SOCKS5BytestreamProxiesManager::handleNameLookupResult(const std::vector<HostAddress>& address, boost::optional<DomainNameResolveError> error, S5BProxyRequest::ref proxy) {
	if (error) {
		onDiscoveredProxiesChanged();
	}
	else {
		if (address.empty()) {
			SWIFT_LOG(warning) << "S5B proxy hostname does not resolve." << std::endl;
			onDiscoveredProxiesChanged();
		}
		else {
			S5BProxyRequest::StreamHost streamHost = proxy->getStreamHost().get();
			streamHost.host = address[0].toString();
			proxy->setStreamHost(streamHost);
			addS5BProxy(proxy);
		}
	}
}

void SOCKS5BytestreamProxiesManager::queryForProxies() {
	proxyFinder_ = boost::make_shared<SOCKS5BytestreamProxyFinder>(serviceRoot_, iqRouter_);

	proxyFinder_->onProxyFound.connect(boost::bind(&SOCKS5BytestreamProxiesManager::handleProxyFound, this, _1));
	proxyFinder_->start();
}

}