Setup BT UDP SOCKS5 proxy on demand

pull/1857/head
myl7 2021-12-05 20:57:12 +08:00
parent 2806da3298
commit f2a66a68b3
No known key found for this signature in database
GPG Key ID: 04F1013B67177C88
1 changed files with 9 additions and 6 deletions

View File

@ -114,7 +114,10 @@ DHTSetup::setup(DownloadEngine* e, int family)
} }
uint16_t port; uint16_t port;
auto connection = make_unique<DHTConnectionSocksProxyImpl>(family); const std::string& proxyUri = e->getOption()->get(PREF_BT_UDP_SOCKS_PROXY);
auto connection = proxyUri.empty()
? make_unique<DHTConnectionImpl>(family)
: make_unique<DHTConnectionSocksProxyImpl>(family);
{ {
port = e->getBtRegistry()->getUdpPort(); port = e->getBtRegistry()->getUdpPort();
const std::string& addr = e->getOption()->get( const std::string& addr = e->getOption()->get(
@ -140,17 +143,17 @@ DHTSetup::setup(DownloadEngine* e, int family)
} }
A2_LOG_DEBUG(fmt("Initialized local node ID=%s", A2_LOG_DEBUG(fmt("Initialized local node ID=%s",
util::toHex(localNode->getID(), DHT_ID_LENGTH).c_str())); util::toHex(localNode->getID(), DHT_ID_LENGTH).c_str()));
{ if (!proxyUri.empty()) {
auto c = static_cast<DHTConnectionSocksProxyImpl*>(connection.get());
const std::string& user = const std::string& user =
e->getOption()->get(PREF_BT_UDP_SOCKS_PROXY_USER); e->getOption()->get(PREF_BT_UDP_SOCKS_PROXY_USER);
const std::string& passwd = const std::string& passwd =
e->getOption()->get(PREF_BT_UDP_SOCKS_PROXY_PASSWD); e->getOption()->get(PREF_BT_UDP_SOCKS_PROXY_PASSWD);
uri::UriStruct us; uri::UriStruct us;
uri::parse(us, e->getOption()->get(PREF_BT_UDP_SOCKS_PROXY)); uri::parse(us, proxyUri);
const std::string& host = us.host; const std::string& host = us.host;
uint16_t port = us.port; uint16_t port = us.port;
if (!connection->startProxy(host, port, user, passwd, if (!c->startProxy(host, port, user, passwd, localNode->getIPAddress(),
localNode->getIPAddress(),
localNode->getPort())) { localNode->getPort())) {
throw DL_ABORT_EX("Error occurred while connecting to SOCKS5 relay " throw DL_ABORT_EX("Error occurred while connecting to SOCKS5 relay "
"server for UDP proxy for DHT and UDP trackers"); "server for UDP proxy for DHT and UDP trackers");