2008-04-25 18:35:48 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2008-2014 Mnemosyne LLC
|
2008-04-24 01:42:53 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2008-04-24 01:42:53 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-09-19 20:41:35 +00:00
|
|
|
#include <algorithm>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <cstring>
|
2021-09-27 16:27:02 +00:00
|
|
|
#include <set>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <string>
|
2021-12-15 21:25:42 +00:00
|
|
|
#include <string_view>
|
2011-03-16 18:04:23 +00:00
|
|
|
|
2014-07-04 00:00:07 +00:00
|
|
|
#ifdef _WIN32
|
2019-06-22 13:02:17 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <wincrypt.h>
|
2010-06-25 20:36:10 +00:00
|
|
|
#endif
|
2010-01-04 20:06:39 +00:00
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
#include <curl/curl.h>
|
2010-12-20 02:07:51 +00:00
|
|
|
|
|
|
|
#include <event2/buffer.h>
|
2008-04-24 01:42:53 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
2019-06-22 13:02:17 +00:00
|
|
|
#include "crypto-utils.h"
|
2014-07-08 00:08:43 +00:00
|
|
|
#include "file.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2010-03-06 15:05:05 +00:00
|
|
|
#include "net.h" /* tr_address */
|
2013-04-13 20:25:28 +00:00
|
|
|
#include "torrent.h"
|
2010-03-06 15:05:05 +00:00
|
|
|
#include "platform.h" /* mutex */
|
2009-07-01 14:58:57 +00:00
|
|
|
#include "session.h"
|
2017-06-08 07:24:12 +00:00
|
|
|
#include "tr-assert.h"
|
2020-08-18 11:09:30 +00:00
|
|
|
#include "tr-macros.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "utils.h"
|
2010-03-06 15:05:05 +00:00
|
|
|
#include "version.h" /* User-Agent */
|
2008-04-24 01:42:53 +00:00
|
|
|
#include "web.h"
|
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
using namespace std::literals;
|
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#if LIBCURL_VERSION_NUM >= 0x070F06 /* CURLOPT_SOCKOPT* was added in 7.15.6 */
|
2017-04-19 12:04:45 +00:00
|
|
|
#define USE_LIBCURL_SOCKOPT
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
|
|
|
|
2021-10-21 00:39:05 +00:00
|
|
|
static auto constexpr ThreadfuncMaxSleepMsec = int{ 200 };
|
2008-10-17 20:57:54 +00:00
|
|
|
|
2017-05-22 20:12:57 +00:00
|
|
|
#define dbgmsg(...) tr_logAddDeepNamed("web", __VA_ARGS__)
|
2008-04-25 02:57:33 +00:00
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2008-04-24 01:42:53 +00:00
|
|
|
struct tr_web_task
|
|
|
|
{
|
2021-11-15 01:17:03 +00:00
|
|
|
std::string cookies;
|
|
|
|
std::string range;
|
|
|
|
std::string url;
|
|
|
|
|
|
|
|
CURL* curl_easy = nullptr;
|
|
|
|
evbuffer* freebuf = nullptr;
|
|
|
|
evbuffer* response = nullptr;
|
|
|
|
tr_session* session = nullptr;
|
|
|
|
tr_web_done_func done_func = nullptr;
|
|
|
|
tr_web_task* next = nullptr;
|
|
|
|
void* done_func_user_data = nullptr;
|
|
|
|
|
|
|
|
long code = 0;
|
|
|
|
long timeout_secs = 0;
|
|
|
|
|
|
|
|
int torrentId = 0;
|
|
|
|
|
|
|
|
bool did_connect = false;
|
|
|
|
bool did_timeout = false;
|
2008-04-24 01:42:53 +00:00
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void task_free(struct tr_web_task* task)
|
2009-12-14 12:54:30 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (task->freebuf != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_free(task->freebuf);
|
|
|
|
}
|
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
delete task;
|
2009-12-14 12:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2011-06-19 18:18:48 +00:00
|
|
|
struct tr_web
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool curl_verbose;
|
|
|
|
bool curl_ssl_verify;
|
|
|
|
char* curl_ca_bundle;
|
|
|
|
int close_mode;
|
2021-11-20 21:20:45 +00:00
|
|
|
|
|
|
|
std::recursive_mutex web_tasks_mutex;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_web_task* tasks;
|
2021-11-20 21:20:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
char* cookie_filename;
|
2021-09-27 16:27:02 +00:00
|
|
|
std::set<CURL*> paused_easy_handles;
|
2011-06-19 18:18:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static size_t writeFunc(void* ptr, size_t size, size_t nmemb, void* vtask)
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const byteCount = size * nmemb;
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* task = static_cast<struct tr_web_task*>(vtask);
|
2013-04-13 20:56:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* webseed downloads should be speed limited */
|
|
|
|
if (task->torrentId != -1)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2020-11-09 03:31:02 +00:00
|
|
|
tr_torrent const* const tor = tr_torrentFindFromId(task->session, task->torrentId);
|
2013-04-13 20:56:24 +00:00
|
|
|
|
2021-10-09 12:52:09 +00:00
|
|
|
if (tor != nullptr && tor->bandwidth->clamp(TR_DOWN, nmemb) == 0)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2021-09-27 16:27:02 +00:00
|
|
|
task->session->web->paused_easy_handles.insert(task->curl_easy);
|
2017-04-19 12:04:45 +00:00
|
|
|
return CURL_WRITEFUNC_PAUSE;
|
2013-04-13 20:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
evbuffer_add(task->response, ptr, byteCount);
|
|
|
|
dbgmsg("wrote %zu bytes to task %p's buffer", byteCount, (void*)task);
|
|
|
|
return byteCount;
|
2008-04-25 18:35:48 +00:00
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#ifdef USE_LIBCURL_SOCKOPT
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static int sockoptfunction(void* vtask, curl_socket_t fd, curlsocktype /*purpose*/)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* task = static_cast<struct tr_web_task*>(vtask);
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* announce and scrape requests have tiny payloads. */
|
2021-12-17 05:47:51 +00:00
|
|
|
if (auto const is_scrape = tr_strvContains(task->url, "scrape"sv), is_announce = tr_strvContains(task->url, "announce"sv);
|
|
|
|
is_scrape || is_announce)
|
2009-12-02 05:30:46 +00:00
|
|
|
{
|
2021-12-17 05:47:51 +00:00
|
|
|
int const sndbuf = is_scrape ? 4096 : 1024;
|
|
|
|
int const rcvbuf = is_scrape ? 4096 : 3072;
|
2020-09-12 13:17:18 +00:00
|
|
|
/* ignore the sockopt() return values -- these are suggestions
|
|
|
|
rather than hard requirements & it's OK for them to fail */
|
2021-09-12 17:41:49 +00:00
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char const*>(&sndbuf), sizeof(sndbuf));
|
|
|
|
(void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char const*>(&rcvbuf), sizeof(rcvbuf));
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
2009-12-28 23:25:50 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* return nonzero if this function encountered an error */
|
|
|
|
return 0;
|
2009-12-02 05:30:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
2009-12-02 05:30:46 +00:00
|
|
|
|
2021-10-24 16:41:54 +00:00
|
|
|
static CURLcode ssl_context_func(CURL* /*curl*/, void* ssl_ctx, void* /*user_data*/)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
tr_x509_store_t const cert_store = tr_ssl_get_x509_store(ssl_ctx);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cert_store == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
curl_version_info_data const* const curl_ver = curl_version_info(CURLVERSION_NOW);
|
|
|
|
if (curl_ver->age >= 0 && strncmp(curl_ver->ssl_version, "Schannel", 8) == 0)
|
|
|
|
{
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static LPCWSTR const sys_store_names[] = {
|
2019-06-22 13:02:17 +00:00
|
|
|
L"CA",
|
2021-08-15 09:41:48 +00:00
|
|
|
L"ROOT",
|
2019-06-22 13:02:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t i = 0; i < TR_N_ELEMENTS(sys_store_names); ++i)
|
|
|
|
{
|
|
|
|
HCERTSTORE const sys_cert_store = CertOpenSystemStoreW(0, sys_store_names[i]);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (sys_cert_store == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
PCCERT_CONTEXT sys_cert = nullptr;
|
2019-06-22 13:02:17 +00:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
sys_cert = CertFindCertificateInStore(sys_cert_store, X509_ASN_ENCODING, 0, CERT_FIND_ANY, nullptr, sys_cert);
|
|
|
|
if (sys_cert == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_cert_t const cert = tr_x509_cert_new(sys_cert->pbCertEncoded, sys_cert->cbCertEncoded);
|
2021-09-15 00:18:09 +00:00
|
|
|
if (cert == nullptr)
|
2019-06-22 13:02:17 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_x509_store_add(cert_store, cert);
|
|
|
|
tr_x509_cert_free(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
CertCloseStore(sys_cert_store, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static long getTimeoutFromURL(struct tr_web_task const* task)
|
2009-12-13 17:54:01 +00:00
|
|
|
{
|
2021-12-17 05:47:51 +00:00
|
|
|
if (auto const* const session = task->session; session == nullptr || session->isClosed)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
return 20L;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
if (tr_strvContains(task->url, "scrape"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
return 30L;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
if (tr_strvContains(task->url, "announce"sv))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
return 90L;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-08-07 02:17:29 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
return 240L;
|
2009-12-13 17:54:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static CURL* createEasy(tr_session* s, struct tr_web* web, struct tr_web_task* task)
|
2008-04-25 18:35:48 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
CURL* const e = curl_easy_init();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2019-03-17 05:00:15 +00:00
|
|
|
task->curl_easy = e;
|
2017-04-19 12:04:45 +00:00
|
|
|
task->timeout_secs = getTimeoutFromURL(task);
|
|
|
|
|
|
|
|
curl_easy_setopt(e, CURLOPT_AUTOREFERER, 1L);
|
2018-01-30 21:56:24 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_ENCODING, "");
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_FOLLOWLOCATION, 1L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_MAXREDIRS, -1L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_NOSIGNAL, 1L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_PRIVATE, task);
|
|
|
|
|
2010-04-15 15:32:36 +00:00
|
|
|
#ifdef USE_LIBCURL_SOCKOPT
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_SOCKOPTFUNCTION, sockoptfunction);
|
|
|
|
curl_easy_setopt(e, CURLOPT_SOCKOPTDATA, task);
|
2010-04-15 15:32:36 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (web->curl_ssl_verify)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (web->curl_ca_bundle != nullptr)
|
2018-04-25 08:21:50 +00:00
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_CAINFO, web->curl_ca_bundle);
|
|
|
|
}
|
2019-06-22 13:02:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
curl_easy_setopt(e, CURLOPT_SSL_CTX_FUNCTION, ssl_context_func);
|
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_SSL_VERIFYHOST, 0L);
|
|
|
|
curl_easy_setopt(e, CURLOPT_SSL_VERIFYPEER, 0L);
|
2012-03-04 13:21:42 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_TIMEOUT, task->timeout_secs);
|
2021-11-15 01:17:03 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_URL, task->url.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_USERAGENT, TR_NAME "/" SHORT_VERSION_STRING);
|
|
|
|
curl_easy_setopt(e, CURLOPT_VERBOSE, (long)(web->curl_verbose ? 1 : 0));
|
|
|
|
curl_easy_setopt(e, CURLOPT_WRITEDATA, task);
|
|
|
|
curl_easy_setopt(e, CURLOPT_WRITEFUNCTION, writeFunc);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto is_default_value = bool{};
|
|
|
|
tr_address const* addr = tr_sessionGetPublicAddress(s, TR_AF_INET, &is_default_value);
|
|
|
|
if (addr != nullptr && !is_default_value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_INTERFACE, tr_address_to_string(addr));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2021-10-15 17:27:12 +00:00
|
|
|
|
|
|
|
addr = tr_sessionGetPublicAddress(s, TR_AF_INET6, &is_default_value);
|
|
|
|
if (addr != nullptr && !is_default_value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_INTERFACE, tr_address_to_string(addr));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
if (!std::empty(task->cookies))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_COOKIE, task->cookies.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (web->cookie_filename != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-26 00:14:25 +00:00
|
|
|
(void)curl_easy_setopt(e, CURLOPT_COOKIEFILE, web->cookie_filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-05-22 19:02:07 +00:00
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
if (!std::empty(task->range))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-11-15 01:17:03 +00:00
|
|
|
curl_easy_setopt(e, CURLOPT_RANGE, task->range.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
/* don't bother asking the server to compress webseed fragments */
|
|
|
|
curl_easy_setopt(e, CURLOPT_ENCODING, "identity");
|
2011-12-14 05:58:23 +00:00
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return e;
|
2008-04-24 01:42:53 +00:00
|
|
|
}
|
|
|
|
|
2008-10-15 16:43:51 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
2008-04-27 18:27:32 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void task_finish_func(void* vtask)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* task = static_cast<struct tr_web_task*>(vtask);
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg("finished web task %p; got %ld", (void*)task, task->code);
|
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (task->done_func != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-11-15 18:10:18 +00:00
|
|
|
auto const sv = std::string_view{ reinterpret_cast<char const*>(evbuffer_pullup(task->response, -1)),
|
|
|
|
evbuffer_get_length(task->response) };
|
|
|
|
(*task->done_func)(task->session, task->did_connect, task->did_timeout, task->code, sv, task->done_func_user_data);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
task_free(task);
|
2008-10-15 16:43:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_webThreadFunc(void* vsession);
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
static struct tr_web_task* tr_webRunImpl(
|
|
|
|
tr_session* session,
|
|
|
|
int torrentId,
|
2021-11-15 01:17:03 +00:00
|
|
|
std::string_view url,
|
|
|
|
std::string_view range,
|
|
|
|
std::string_view cookies,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_web_done_func done_func,
|
|
|
|
void* done_func_user_data,
|
2019-02-10 11:05:16 +00:00
|
|
|
struct evbuffer* buffer)
|
2008-10-15 16:43:51 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
struct tr_web_task* task = nullptr;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2021-11-18 18:19:55 +00:00
|
|
|
if (!session->isClosing())
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session->web == nullptr)
|
2013-02-15 01:52:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_threadNew(tr_webThreadFunc, session);
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
while (session->web == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_wait_msec(20);
|
|
|
|
}
|
2013-02-15 01:52:47 +00:00
|
|
|
}
|
2015-01-02 11:15:31 +00:00
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
task = new tr_web_task{};
|
2017-04-19 12:04:45 +00:00
|
|
|
task->session = session;
|
|
|
|
task->torrentId = torrentId;
|
2021-11-15 01:17:03 +00:00
|
|
|
task->url = url;
|
|
|
|
task->range = range;
|
|
|
|
task->cookies = cookies;
|
2017-04-19 12:04:45 +00:00
|
|
|
task->done_func = done_func;
|
|
|
|
task->done_func_user_data = done_func_user_data;
|
2021-09-15 00:18:09 +00:00
|
|
|
task->response = buffer != nullptr ? buffer : evbuffer_new();
|
|
|
|
task->freebuf = buffer != nullptr ? nullptr : task->response;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = std::unique_lock(session->web->web_tasks_mutex);
|
2017-04-19 12:04:45 +00:00
|
|
|
task->next = session->web->tasks;
|
|
|
|
session->web->tasks = task;
|
2008-08-06 23:33:29 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return task;
|
2008-05-06 15:52:57 +00:00
|
|
|
}
|
2008-04-24 19:38:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
struct tr_web_task* tr_webRunWithCookies(
|
|
|
|
tr_session* session,
|
2021-11-15 01:17:03 +00:00
|
|
|
std::string_view url,
|
|
|
|
std::string_view cookies,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_web_done_func done_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* done_func_user_data)
|
2013-04-13 20:25:28 +00:00
|
|
|
{
|
2021-11-15 01:17:03 +00:00
|
|
|
return tr_webRunImpl(session, -1, url, {}, cookies, done_func, done_func_user_data, nullptr);
|
2013-04-13 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 01:17:03 +00:00
|
|
|
struct tr_web_task* tr_webRun(tr_session* session, std::string_view url, tr_web_done_func done_func, void* done_func_user_data)
|
2013-04-13 20:25:28 +00:00
|
|
|
{
|
2021-11-15 01:17:03 +00:00
|
|
|
return tr_webRunWithCookies(session, url, {}, done_func, done_func_user_data);
|
2013-04-13 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
struct tr_web_task* tr_webRunWebseed(
|
|
|
|
tr_torrent* tor,
|
2021-11-15 01:17:03 +00:00
|
|
|
std::string_view url,
|
|
|
|
std::string_view range,
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_web_done_func done_func,
|
|
|
|
void* done_func_user_data,
|
|
|
|
struct evbuffer* buffer)
|
2013-04-13 20:25:28 +00:00
|
|
|
{
|
2021-11-15 01:17:03 +00:00
|
|
|
return tr_webRunImpl(tor->session, tr_torrentId(tor), url, range, {}, done_func, done_func_user_data, buffer);
|
2013-04-13 20:25:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void tr_webThreadFunc(void* vsession)
|
2008-04-24 01:42:53 +00:00
|
|
|
{
|
2021-09-12 17:41:49 +00:00
|
|
|
auto* session = static_cast<tr_session*>(vsession);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* try to enable ssl for https support; but if that fails,
|
|
|
|
* try a plain vanilla init */
|
2017-04-30 16:25:26 +00:00
|
|
|
if (curl_global_init(CURL_GLOBAL_SSL) != CURLE_OK)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
curl_global_init(0);
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:27:02 +00:00
|
|
|
auto* web = new tr_web{};
|
2017-04-19 12:04:45 +00:00
|
|
|
web->close_mode = ~0;
|
2021-09-15 00:18:09 +00:00
|
|
|
web->tasks = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
web->curl_verbose = tr_env_key_exists("TR_CURL_VERBOSE");
|
2017-07-16 17:28:13 +00:00
|
|
|
web->curl_ssl_verify = !tr_env_key_exists("TR_CURL_SSL_NO_VERIFY");
|
2021-09-15 00:18:09 +00:00
|
|
|
web->curl_ca_bundle = tr_env_get_string("CURL_CA_BUNDLE", nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (web->curl_ssl_verify)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logAddNamedInfo(
|
|
|
|
"web",
|
|
|
|
"will verify tracker certs using envvar CURL_CA_BUNDLE: %s",
|
2021-09-15 00:18:09 +00:00
|
|
|
web->curl_ca_bundle == nullptr ? "none" : web->curl_ca_bundle);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_logAddNamedInfo("web", "NB: this only works if you built against libcurl with openssl or gnutls, NOT nss");
|
|
|
|
tr_logAddNamedInfo("web", "NB: invalid certs will show up as 'Could not connect to tracker' like many other errors");
|
|
|
|
}
|
|
|
|
|
2022-01-02 17:51:59 +00:00
|
|
|
auto const str = tr_strvPath(session->config_dir, "cookies.txt");
|
2021-11-13 00:10:04 +00:00
|
|
|
if (tr_sys_path_exists(str.c_str(), nullptr))
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2021-11-13 00:10:04 +00:00
|
|
|
web->cookie_filename = tr_strvDup(str);
|
2012-03-04 13:21:42 +00:00
|
|
|
}
|
2011-04-27 17:02:18 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto* const multi = curl_multi_init();
|
2017-04-19 12:04:45 +00:00
|
|
|
session->web = web;
|
2009-12-11 15:41:34 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto repeats = uint32_t{};
|
2017-04-19 12:04:45 +00:00
|
|
|
for (;;)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (web->close_mode == TR_WEB_CLOSE_NOW)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2021-09-15 00:18:09 +00:00
|
|
|
if (web->close_mode == TR_WEB_CLOSE_WHEN_IDLE && web->tasks == nullptr)
|
2013-04-13 20:56:24 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-04-28 00:06:22 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* add tasks from the queue */
|
|
|
|
{
|
2021-11-20 21:20:45 +00:00
|
|
|
auto const lock = std::unique_lock(web->web_tasks_mutex);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
while (web->tasks != nullptr)
|
|
|
|
{
|
|
|
|
/* pop the task */
|
|
|
|
struct tr_web_task* task = web->tasks;
|
|
|
|
web->tasks = task->next;
|
|
|
|
task->next = nullptr;
|
2013-04-13 20:56:24 +00:00
|
|
|
|
2021-11-20 21:20:45 +00:00
|
|
|
dbgmsg("adding task to curl: [%s]", task->url.c_str());
|
|
|
|
curl_multi_add_handle(multi, createEasy(session, web, task));
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-09-27 16:27:02 +00:00
|
|
|
/* resume any paused curl handles.
|
|
|
|
swap paused_easy_handles to prevent oscillation
|
|
|
|
between writeFunc this while loop */
|
|
|
|
auto paused = decltype(web->paused_easy_handles){};
|
|
|
|
std::swap(paused, web->paused_easy_handles);
|
|
|
|
std::for_each(std::begin(paused), std::end(paused), [](auto* curl) { curl_easy_pause(curl, CURLPAUSE_CONT); });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* maybe wait a little while before calling curl_multi_perform() */
|
2021-10-15 17:27:12 +00:00
|
|
|
auto msec = long{};
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_multi_timeout(multi, &msec);
|
|
|
|
|
|
|
|
if (msec < 0)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
msec = ThreadfuncMaxSleepMsec;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-24 23:59:52 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (session->isClosed)
|
|
|
|
{
|
2017-04-21 07:40:57 +00:00
|
|
|
msec = 100; /* on shutdown, call perform() more frequently */
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (msec > 0)
|
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
if (msec > ThreadfuncMaxSleepMsec)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-21 00:39:05 +00:00
|
|
|
msec = ThreadfuncMaxSleepMsec;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto numfds = int{};
|
2021-09-15 00:18:09 +00:00
|
|
|
curl_multi_wait(multi, nullptr, 0, msec, &numfds);
|
2020-07-29 02:38:27 +00:00
|
|
|
if (!numfds)
|
|
|
|
{
|
|
|
|
repeats++;
|
|
|
|
if (repeats > 1)
|
|
|
|
{
|
|
|
|
/* curl_multi_wait() returns immediately if there are
|
|
|
|
* no fds to wait for, so we need an explicit wait here
|
|
|
|
* to emulate select() behavior */
|
2021-10-21 00:39:05 +00:00
|
|
|
tr_wait_msec(std::min(msec, ThreadfuncMaxSleepMsec / 2L));
|
2020-07-29 02:38:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
repeats = 0;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-01-22 03:39:21 +00:00
|
|
|
|
2017-04-21 07:40:57 +00:00
|
|
|
/* call curl_multi_perform() */
|
2021-10-15 17:27:12 +00:00
|
|
|
auto mcode = CURLMcode{};
|
|
|
|
auto unused = int{};
|
2017-04-19 12:04:45 +00:00
|
|
|
do
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mcode = curl_multi_perform(multi, &unused);
|
2021-08-15 09:41:48 +00:00
|
|
|
} while (mcode == CURLM_CALL_MULTI_PERFORM);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
/* pump completed tasks from the multi */
|
2021-10-15 17:27:12 +00:00
|
|
|
CURLMsg* msg = nullptr;
|
2021-09-15 00:18:09 +00:00
|
|
|
while ((msg = curl_multi_info_read(multi, &unused)) != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (msg->msg == CURLMSG_DONE && msg->easy_handle != nullptr)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2021-10-15 17:27:12 +00:00
|
|
|
CURL* const e = msg->easy_handle;
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
struct tr_web_task* task = nullptr;
|
|
|
|
curl_easy_getinfo(e, CURLINFO_PRIVATE, (void*)&task);
|
2017-06-08 07:24:12 +00:00
|
|
|
TR_ASSERT(e == task->curl_easy);
|
2017-06-13 02:24:09 +00:00
|
|
|
|
2021-10-15 17:27:12 +00:00
|
|
|
auto req_bytes_sent = long{};
|
|
|
|
auto total_time = double{};
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_getinfo(e, CURLINFO_RESPONSE_CODE, &task->code);
|
|
|
|
curl_easy_getinfo(e, CURLINFO_REQUEST_SIZE, &req_bytes_sent);
|
|
|
|
curl_easy_getinfo(e, CURLINFO_TOTAL_TIME, &total_time);
|
|
|
|
task->did_connect = task->code > 0 || req_bytes_sent > 0;
|
2017-04-30 16:25:26 +00:00
|
|
|
task->did_timeout = task->code == 0 && total_time >= task->timeout_secs;
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_multi_remove_handle(multi, e);
|
2021-09-27 16:27:02 +00:00
|
|
|
web->paused_easy_handles.erase(e);
|
2017-04-19 12:04:45 +00:00
|
|
|
curl_easy_cleanup(e);
|
|
|
|
tr_runInEventThread(task->session, task_finish_func, task);
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-24 01:42:53 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Discard any remaining tasks.
|
|
|
|
* This is rare, but can happen on shutdown with unresponsive trackers. */
|
2021-09-15 00:18:09 +00:00
|
|
|
while (web->tasks != nullptr)
|
2013-01-24 23:59:52 +00:00
|
|
|
{
|
2020-08-11 18:11:55 +00:00
|
|
|
struct tr_web_task* task = web->tasks;
|
2017-04-19 12:04:45 +00:00
|
|
|
web->tasks = task->next;
|
2021-11-15 01:17:03 +00:00
|
|
|
dbgmsg("Discarding task \"%s\"", task->url.c_str());
|
2017-04-19 12:04:45 +00:00
|
|
|
task_free(task);
|
2011-02-21 01:13:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* cleanup */
|
|
|
|
curl_multi_cleanup(multi);
|
|
|
|
tr_free(web->curl_ca_bundle);
|
|
|
|
tr_free(web->cookie_filename);
|
2021-09-27 16:27:02 +00:00
|
|
|
delete web;
|
2021-09-15 00:18:09 +00:00
|
|
|
session->web = nullptr;
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_webClose(tr_session* session, tr_web_close_mode close_mode)
|
2008-04-25 19:46:36 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
if (session->web != nullptr)
|
2010-03-06 15:05:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
session->web->close_mode = close_mode;
|
2010-03-06 15:05:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (close_mode == TR_WEB_CLOSE_NOW)
|
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
while (session->web != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_wait_msec(100);
|
|
|
|
}
|
|
|
|
}
|
2010-03-06 15:05:05 +00:00
|
|
|
}
|
2008-04-25 19:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 23:41:26 +00:00
|
|
|
long tr_webGetTaskResponseCode(struct tr_web_task* task)
|
|
|
|
{
|
|
|
|
long code = 0;
|
|
|
|
curl_easy_getinfo(task->curl_easy, CURLINFO_RESPONSE_CODE, &code);
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
char const* tr_webGetTaskRealUrl(struct tr_web_task* task)
|
2011-07-10 15:24:51 +00:00
|
|
|
{
|
2021-09-15 00:18:09 +00:00
|
|
|
char* url = nullptr;
|
2020-08-27 23:41:26 +00:00
|
|
|
curl_easy_getinfo(task->curl_easy, CURLINFO_EFFECTIVE_URL, &url);
|
|
|
|
return url;
|
2011-07-10 15:24:51 +00:00
|
|
|
}
|