summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/Network/SOCKS5ProxiedConnection.cpp')
-rw-r--r--Swiften/Network/SOCKS5ProxiedConnection.cpp108
1 files changed, 28 insertions, 80 deletions
diff --git a/Swiften/Network/SOCKS5ProxiedConnection.cpp b/Swiften/Network/SOCKS5ProxiedConnection.cpp
index 163e23a..3fd8184 100644
--- a/Swiften/Network/SOCKS5ProxiedConnection.cpp
+++ b/Swiften/Network/SOCKS5ProxiedConnection.cpp
@@ -5,4 +5,10 @@
*/
+/*
+ * Copyright (c) 2014 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
#include <Swiften/Network/SOCKS5ProxiedConnection.h>
@@ -19,57 +25,15 @@
using namespace Swift;
-SOCKS5ProxiedConnection::SOCKS5ProxiedConnection(ConnectionFactory* connectionFactory, const HostAddressPort& proxy) : connectionFactory_(connectionFactory), proxy_(proxy), server_(HostAddressPort(HostAddress("0.0.0.0"), 0)) {
- connected_ = false;
-}
-
-SOCKS5ProxiedConnection::~SOCKS5ProxiedConnection() {
- if (connection_) {
- connection_->onDataRead.disconnect(boost::bind(&SOCKS5ProxiedConnection::handleDataRead, shared_from_this(), _1));
- connection_->onDisconnected.disconnect(boost::bind(&SOCKS5ProxiedConnection::handleDisconnected, shared_from_this(), _1));
- }
-
- if (connected_) {
- std::cerr << "Warning: Connection was still established." << std::endl;
- }
-}
-
-void SOCKS5ProxiedConnection::connect(const HostAddressPort& server) {
- server_ = server;
- connection_ = connectionFactory_->createConnection();
- connection_->onConnectFinished.connect(boost::bind(&SOCKS5ProxiedConnection::handleConnectionConnectFinished, shared_from_this(), _1));
- connection_->onDataRead.connect(boost::bind(&SOCKS5ProxiedConnection::handleDataRead, shared_from_this(), _1));
- connection_->onDisconnected.connect(boost::bind(&SOCKS5ProxiedConnection::handleDisconnected, shared_from_this(), _1));
- SWIFT_LOG(debug) << "Trying to connect via proxy " << proxy_.getAddress().toString() << ":" << proxy_.getPort() << std::endl;
- SWIFT_LOG(debug) << "to server " << server.getAddress().toString() << ":" << server.getPort() << std::endl;
- connection_->connect(proxy_);
-}
-
-void SOCKS5ProxiedConnection::listen() {
- assert(false);
- connection_->listen();
-}
-
-void SOCKS5ProxiedConnection::disconnect() {
- connected_ = false;
- if (connection_) {
- connection_->disconnect();
- }
-}
-
-void SOCKS5ProxiedConnection::handleDisconnected(const boost::optional<Error>& error) {
- onDisconnected(error);
+SOCKS5ProxiedConnection::SOCKS5ProxiedConnection(
+ DomainNameResolver* resolver,
+ ConnectionFactory* connectionFactory,
+ TimerFactory* timerFactory,
+ const std::string& proxyHost,
+ int proxyPort) :
+ ProxiedConnection(resolver, connectionFactory, timerFactory, proxyHost, proxyPort),
+ proxyState_(Initial) {
}
-void SOCKS5ProxiedConnection::write(const SafeByteArray& data) {
- if (connection_) {
- connection_->write(data);
- }
-}
-
-void SOCKS5ProxiedConnection::handleConnectionConnectFinished(bool error) {
- connection_->onConnectFinished.disconnect(boost::bind(&SOCKS5ProxiedConnection::handleConnectionConnectFinished, shared_from_this(), _1));
- if (!error) {
- SWIFT_LOG(debug) << "Connection to proxy established, now connect to the server via it." << std::endl;
-
+void SOCKS5ProxiedConnection::initializeProxy() {
proxyState_ = ProxyAuthenticating;
SafeByteArray socksConnect;
@@ -80,16 +44,12 @@ void SOCKS5ProxiedConnection::handleConnectionConnectFinished(bool error) {
// buffer.push_back(0x02); // 0x02 == Username/Password
// rest see RFC 1928 (http://tools.ietf.org/html/rfc1928)
- connection_->write(socksConnect);
- }
- else {
- onConnectFinished(true);
- }
+ write(socksConnect);
}
-void SOCKS5ProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
+void SOCKS5ProxiedConnection::handleProxyInitializeData(boost::shared_ptr<SafeByteArray> data) {
SafeByteArray socksConnect;
- boost::asio::ip::address rawAddress = server_.getAddress().getRawAddress();
+ boost::asio::ip::address rawAddress = getServer().getAddress().getRawAddress();
assert(rawAddress.is_v4() || rawAddress.is_v6());
- if (!connected_) {
+
if (proxyState_ == ProxyAuthenticating) {
SWIFT_LOG(debug) << "ProxyAuthenticating response received, reply with the connect BYTEs" << std::endl;
@@ -113,10 +73,10 @@ void SOCKS5ProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> da
uc = rawAddress.to_v6().to_bytes()[s]; // the address.
}
- socksConnect.push_back(static_cast<char>(uc));
+ socksConnect.push_back(uc);
}
- socksConnect.push_back(static_cast<unsigned char> ((server_.getPort() >> 8) & 0xFF)); // highbyte of the port.
- socksConnect.push_back(static_cast<unsigned char> (server_.getPort() & 0xFF)); // lowbyte of the port.
- connection_->write(socksConnect);
+ socksConnect.push_back(static_cast<unsigned char> ((getServer().getPort() >> 8) & 0xFF)); // highbyte of the port.
+ socksConnect.push_back(static_cast<unsigned char> (getServer().getPort() & 0xFF)); // lowbyte of the port.
+ write(socksConnect);
return;
}
@@ -124,12 +84,13 @@ void SOCKS5ProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> da
std::cerr << "exception caught" << std::endl;
}
- connection_->write(socksConnect);
+ write(socksConnect);
break;
default:
- onConnectFinished(true);
+ setProxyInitializeFinished(true);
break;
}
return;
}
+ setProxyInitializeFinished(false);
}
else if (proxyState_ == ProxyConnecting) {
@@ -152,23 +113,10 @@ void SOCKS5ProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> da
if ((*data)[0] == 0x05 && (*data)[1] == 0x0) {
SWIFT_LOG(debug) << "Successfully connected the server via the proxy." << std::endl;
- connected_ = true;
- onConnectFinished(false);
- return;
+ setProxyInitializeFinished(true);
}
else {
std::cerr << "SOCKS Proxy returned an error: " << std::hex << (*data)[1] << std::endl;
+ setProxyInitializeFinished(false);
}
- return;
- }
- }
- else {
- onDataRead(data);
- return;
}
- disconnect();
- onConnectFinished(true);
-}
-
-HostAddressPort SOCKS5ProxiedConnection::getLocalAddress() const {
- return connection_->getLocalAddress();
}