summaryrefslogtreecommitdiffstats
blob: 0b94763411a133f3d4d228a954c92572a4f758a7 (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
/*
 * Copyright (c) 2011 Tobias Markmann
 * Licensed under the simplified BSD license.
 * See Documentation/Licenses/BSD-simplified.txt for more information.
 */

#include <Swiften/FileTransfer/SOCKS5BytestreamProxiesManager.h>

#include <boost/smart_ptr/make_shared.hpp>

#include <Swiften/Base/foreach.h>
#include <Swiften/FileTransfer/SOCKS5BytestreamClientSession.h>
#include <Swiften/Base/Log.h>

namespace Swift {

SOCKS5BytestreamProxiesManager::SOCKS5BytestreamProxiesManager(ConnectionFactory *connFactory, TimerFactory *timeFactory) : connectionFactory(connFactory), timerFactory(timeFactory) {

}

void SOCKS5BytestreamProxiesManager::addS5BProxy(S5BProxyRequest::ref proxy) {
	localS5BProxies.push_back(proxy);
}

const std::vector<S5BProxyRequest::ref>& SOCKS5BytestreamProxiesManager::getS5BProxies() const {
	return localS5BProxies;
}

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

	foreach(S5BProxyRequest::ref proxy, localS5BProxies) {
		boost::shared_ptr<Connection> conn = connectionFactory->createConnection();

		boost::shared_ptr<SOCKS5BytestreamClientSession> session = boost::make_shared<SOCKS5BytestreamClientSession>(conn, proxy->getStreamHost().get().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;
}

}