From 1ff690e9a04cc5a1e7d5fef382c396a6f490836d Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sat, 15 Feb 2014 16:51:00 -0200 Subject: [PATCH] use the same proxy settings (like Tor socks5) for libtorrent as well. DHT still won't work. --- libtorrent/src/udp_socket.cpp | 6 ++-- src/init.cpp | 4 ++- src/twister.cpp | 68 ++++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 23 deletions(-) diff --git a/libtorrent/src/udp_socket.cpp b/libtorrent/src/udp_socket.cpp index 8890c684..4df007cd 100644 --- a/libtorrent/src/udp_socket.cpp +++ b/libtorrent/src/udp_socket.cpp @@ -813,8 +813,10 @@ void udp_socket::on_name_lookup(error_code const& e, tcp::resolver::iterator i) void udp_socket::on_timeout() { #if defined TORRENT_DEBUG || TORRENT_RELEASE_ASSERTS - TORRENT_ASSERT(m_outstanding_timeout > 0); - --m_outstanding_timeout; + if(m_outstanding_timeout > 0) { // without this check it breaks using socks5 + TORRENT_ASSERT(m_outstanding_timeout > 0); + --m_outstanding_timeout; + } #endif TORRENT_ASSERT(m_outstanding_ops > 0); --m_outstanding_ops; diff --git a/src/init.cpp b/src/init.cpp index a4105abf..e3966ab1 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -607,8 +607,10 @@ bool AppInit2(boost::thread_group& threadGroup) if (!addrProxy.IsValid()) return InitError(strprintf(_("Invalid -proxy address: '%s'"), mapArgs["-proxy"].c_str())); - if (!IsLimited(NET_IPV4)) + if (!IsLimited(NET_IPV4)) { SetProxy(NET_IPV4, addrProxy, nSocksVersion); + SetReachable(NET_IPV4); + } if (nSocksVersion > 4) { #ifdef USE_IPV6 if (!IsLimited(NET_IPV6)) diff --git a/src/twister.cpp b/src/twister.cpp index c8b57e50..1140bb15 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -38,6 +38,7 @@ twister::twister() using namespace libtorrent; static session *ses = NULL; +static bool m_usingProxy; static int num_outstanding_resume_data; static CCriticalSection cs_dhtgetMap; @@ -216,14 +217,27 @@ void ThreadWaitExtIP() int listen_port = GetListenPort() + LIBTORRENT_PORT_OFFSET; std::string bind_to_interface = ""; + proxyType proxyInfoOut; + m_usingProxy = GetProxy(NET_IPV4, proxyInfoOut); - printf("Creating new libtorrent session ext_ip=%s port=%d\n", ipStr.c_str(), listen_port); + printf("Creating new libtorrent session ext_ip=%s port=%d proxy=%s\n", + ipStr.c_str(), !m_usingProxy ? listen_port : 0, + m_usingProxy ? proxyInfoOut.first.ToStringIPPort().c_str() : ""); ses = new session(*m_swarmDb, fingerprint("TW", LIBTORRENT_VERSION_MAJOR, LIBTORRENT_VERSION_MINOR, 0, 0) , session::add_default_plugins , alert::dht_notification , ipStr.size() ? ipStr.c_str() : NULL - , std::make_pair(listen_port, listen_port)); + , !m_usingProxy ? std::make_pair(listen_port, listen_port) : std::make_pair(0, 0) ); + + if( m_usingProxy ) { + proxy_settings proxy; + proxy.hostname = proxyInfoOut.first.ToStringIP(); + proxy.port = proxyInfoOut.first.GetPort(); + proxy.type = HaveNameProxy() ? proxy_settings::socks5 : + proxy_settings::socks4; + ses->set_proxy(proxy); + } // session will be paused until we have an up-to-date blockchain ses->pause(); @@ -237,32 +251,39 @@ void ThreadWaitExtIP() ses->load_state(e); } - ses->start_upnp(); - ses->start_natpmp(); - - ses->listen_on(std::make_pair(listen_port, listen_port) - , ec, bind_to_interface.c_str()); - if (ec) - { + if( !m_usingProxy ) { + ses->start_upnp(); + ses->start_natpmp(); + + ses->listen_on(std::make_pair(listen_port, listen_port) + , ec, bind_to_interface.c_str()); + if (ec) + { fprintf(stderr, "failed to listen%s%s on ports %d-%d: %s\n" , bind_to_interface.empty() ? "" : " on ", bind_to_interface.c_str() , listen_port, listen_port+1, ec.message().c_str()); + } + + dht_settings dhts; + // settings to test local connections + //dhts.restrict_routing_ips = false; + //dhts.restrict_search_ips = false; + ses->set_dht_settings(dhts); + ses->start_dht(); } - - dht_settings dhts; - // settings to test local connections - dhts.restrict_routing_ips = false; - dhts.restrict_search_ips = false; - ses->set_dht_settings(dhts); - ses->start_dht(); - + session_settings settings; // settings to test local connections settings.allow_multiple_connections_per_ip = true; //settings.enable_outgoing_utp = false; // (false to see connections in netstat) //settings.dht_announce_interval = 60; // test //settings.min_announce_interval = 60; // test - settings.anonymous_mode = false; // (false => send peer_id, avoid connecting to itself) + if( !m_usingProxy ) { + settings.anonymous_mode = false; // (false => send peer_id, avoid connecting to itself) + } else { + settings.anonymous_mode = true; + settings.force_proxy = true; // DHT won't work + } // disable read cache => there is still some bug due to twister piece size changes settings.use_read_cache = false; settings.cache_size = 0; @@ -392,7 +413,8 @@ void ThreadMaintainDHTNodes() int totalNodesCandidates = (int)(vNodesSize + vAddr.size()); if( ((!dht_nodes && totalNodesCandidates) || (dht_nodes < 5 && totalNodesCandidates > 10)) && - totalNodesCandidates != lastTotalNodesCandidates ) { + !m_usingProxy && + totalNodesCandidates != lastTotalNodesCandidates) { lastTotalNodesCandidates = totalNodesCandidates; printf("ThreadMaintainDHTNodes: too few dht_nodes, trying to add some...\n"); BOOST_FOREACH(const CAddress &a, vAddr) { @@ -682,7 +704,13 @@ void stopSessionTorrent() printf("\nsaving session state\n"); entry session_state; - ses->save_state(session_state); + ses->save_state(session_state, + session::save_settings | + session::save_dht_settings | + session::save_dht_state | + session::save_encryption_settings | + session::save_as_map | + session::save_feeds); std::vector out; bencode(std::back_inserter(out), session_state);