2011-03-11 04:19:01 +00:00
|
|
|
/*
|
2014-01-18 20:56:57 +00:00
|
|
|
* This file Copyright (C) 2010-2014 Mnemosyne LLC
|
2011-03-11 04:19:01 +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.
|
2011-03-11 04:19:01 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-03-13 14:33:28 +00:00
|
|
|
#include <limits.h> /* USHRT_MAX */
|
2017-04-21 07:40:57 +00:00
|
|
|
#include <stdio.h> /* fprintf() */
|
|
|
|
#include <string.h> /* strchr(), memcmp(), memcpy() */
|
2011-03-13 14:33:28 +00:00
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
#include <event2/buffer.h>
|
|
|
|
#include <event2/http.h> /* for HTTP_OK */
|
|
|
|
|
2015-05-04 19:58:34 +00:00
|
|
|
#define __LIBTRANSMISSION_ANNOUNCER_MODULE__
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "announcer-common.h"
|
2013-01-25 23:34:20 +00:00
|
|
|
#include "log.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "net.h" /* tr_globalIPv6() */
|
2011-03-11 04:19:01 +00:00
|
|
|
#include "peer-mgr.h" /* pex */
|
|
|
|
#include "torrent.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "trevent.h" /* tr_runInEventThread() */
|
2011-03-13 14:33:28 +00:00
|
|
|
#include "utils.h"
|
2012-12-14 04:34:42 +00:00
|
|
|
#include "variant.h"
|
2017-04-21 07:40:57 +00:00
|
|
|
#include "web.h" /* tr_http_escape() */
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
#define dbgmsg(name, ...) \
|
2017-04-19 12:04:45 +00:00
|
|
|
do \
|
2013-01-25 23:34:20 +00:00
|
|
|
{ \
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_logGetDeepEnabled()) \
|
|
|
|
{ \
|
|
|
|
tr_logAddDeep(__FILE__, __LINE__, name, __VA_ARGS__); \
|
|
|
|
} \
|
2013-01-25 23:34:20 +00:00
|
|
|
} \
|
2017-04-19 12:04:45 +00:00
|
|
|
while (0)
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** ANNOUNCE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char const* get_event_string(tr_announce_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (req->partial_seed)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
if (req->event != TR_ANNOUNCE_EVENT_STOPPED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
return "paused";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_announce_event_get_string(req->event);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
2011-03-11 15:44:24 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* announce_url_new(tr_session const* session, tr_announce_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
|
|
|
unsigned char const* ipv6;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* buf = evbuffer_new();
|
|
|
|
char escaped_info_hash[SHA_DIGEST_LENGTH * 3 + 1];
|
|
|
|
|
|
|
|
tr_http_escape_sha1(escaped_info_hash, req->info_hash);
|
|
|
|
|
|
|
|
evbuffer_expand(buf, 1024);
|
|
|
|
|
|
|
|
evbuffer_add_printf(buf,
|
|
|
|
"%s"
|
|
|
|
"%c"
|
|
|
|
"info_hash=%s"
|
|
|
|
"&peer_id=%*.*s"
|
|
|
|
"&port=%d"
|
|
|
|
"&uploaded=%" PRIu64
|
|
|
|
"&downloaded=%" PRIu64
|
|
|
|
"&left=%" PRIu64
|
|
|
|
"&numwant=%d"
|
|
|
|
"&key=%x"
|
|
|
|
"&compact=1"
|
|
|
|
"&supportcrypto=1",
|
|
|
|
req->url,
|
|
|
|
strchr(req->url, '?') ? '&' : '?',
|
|
|
|
escaped_info_hash,
|
|
|
|
PEER_ID_LEN, PEER_ID_LEN, req->peer_id,
|
|
|
|
req->port,
|
|
|
|
req->up,
|
|
|
|
req->down,
|
|
|
|
req->leftUntilComplete,
|
|
|
|
req->numwant,
|
|
|
|
req->key);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (session->encryptionMode == TR_ENCRYPTION_REQUIRED)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&requirecrypto=1");
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (req->corrupt)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&corrupt=%" PRIu64, req->corrupt);
|
|
|
|
}
|
|
|
|
|
|
|
|
str = get_event_string(req);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (str && *str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&event=%s", str);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
str = req->tracker_id_str;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (str && *str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
evbuffer_add_printf(buf, "&trackerid=%s", str);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* There are two incompatible techniques for announcing an IPv6 address.
|
|
|
|
BEP-7 suggests adding an "ipv6=" parameter to the announce URL,
|
|
|
|
while OpenTracker requires that peers announce twice, once over IPv4
|
|
|
|
and once over IPv6.
|
|
|
|
|
|
|
|
To be safe, we should do both: add the "ipv6=" parameter and
|
|
|
|
announce twice. At any rate, we're already computing our IPv6
|
|
|
|
address (for the LTEP handshake), so this comes for free. */
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
ipv6 = tr_globalIPv6();
|
|
|
|
|
|
|
|
if (ipv6)
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
char ipv6_readable[INET6_ADDRSTRLEN];
|
2017-04-19 12:04:45 +00:00
|
|
|
evutil_inet_ntop(AF_INET6, ipv6, ipv6_readable, INET6_ADDRSTRLEN);
|
|
|
|
evbuffer_add_printf(buf, "&ipv6=");
|
|
|
|
tr_http_escape(buf, ipv6_readable, TR_BAD_SIZE, true);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return evbuffer_free_to_str(buf, NULL);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_pex* listToPex(tr_variant* peerList, size_t* setme_len)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t n;
|
2017-04-20 16:02:19 +00:00
|
|
|
size_t const len = tr_variantListSize(peerList);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_pex* pex = tr_new0(tr_pex, len);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (i = n = 0; i < len; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int64_t port;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* ip;
|
2011-03-11 04:19:01 +00:00
|
|
|
tr_address addr;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* peer = tr_variantListChild(peerList, i);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (peer == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tr_variantDictFindStr(peer, TR_KEY_ip, &ip, NULL))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tr_address_from_string(&addr, ip))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tr_variantDictFindInt(peer, TR_KEY_port, &port))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if ((port < 0) || (port > USHRT_MAX))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!tr_address_is_valid_for_peers(&addr, port))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
continue;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
pex[n].addr = addr;
|
2017-04-19 12:04:45 +00:00
|
|
|
pex[n].port = htons((uint16_t)port);
|
2011-03-11 04:19:01 +00:00
|
|
|
++n;
|
|
|
|
}
|
|
|
|
|
|
|
|
*setme_len = n;
|
|
|
|
return pex;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct announce_data
|
|
|
|
{
|
|
|
|
tr_announce_response response;
|
2013-09-08 17:08:18 +00:00
|
|
|
tr_announce_response_func response_func;
|
2017-04-19 12:04:45 +00:00
|
|
|
void* response_func_user_data;
|
2011-03-11 04:19:01 +00:00
|
|
|
char log_name[128];
|
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void on_announce_done_eventthread(void* vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct announce_data* data = vdata;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (data->response_func != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
data->response_func(&data->response, data->response_func_user_data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(data->response.pex6);
|
|
|
|
tr_free(data->response.pex);
|
|
|
|
tr_free(data->response.tracker_id_str);
|
|
|
|
tr_free(data->response.warning);
|
|
|
|
tr_free(data->response.errmsg);
|
|
|
|
tr_free(data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_announce_done(tr_session* session, bool did_connect, bool did_timeout, long response_code, void const* msg,
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t msglen, void* vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_announce_response* response;
|
|
|
|
struct announce_data* data = vdata;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
response = &data->response;
|
|
|
|
response->did_connect = did_connect;
|
|
|
|
response->did_timeout = did_timeout;
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(data->log_name, "Got announce response");
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response_code != HTTP_OK)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* fmt = _("Tracker gave HTTP response code %1$ld (%2$s)");
|
|
|
|
char const* response_str = tr_webGetResponseStr(response_code);
|
2017-04-19 12:04:45 +00:00
|
|
|
response->errmsg = tr_strdup_printf(fmt, response_code, response_str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant benc;
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const variant_loaded = !tr_variantFromBenc(&benc, msg, msglen);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_env_key_exists("TR_CURL_VERBOSE"))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!variant_loaded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", "Announce response was not in benc format\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-25 11:34:35 +00:00
|
|
|
size_t i, len;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* str = tr_variantToStr(&benc, TR_VARIANT_FMT_JSON, &len);
|
|
|
|
fprintf(stderr, "%s", "Announce response:\n< ");
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
fputc(str[i], stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
tr_free(str);
|
2011-07-30 22:40:15 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (variant_loaded && tr_variantIsDict(&benc))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int64_t i;
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* tmp;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
|
|
|
uint8_t const* raw;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(&benc, TR_KEY_failure_reason, &str, &len))
|
|
|
|
{
|
|
|
|
response->errmsg = tr_strndup(str, len);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(&benc, TR_KEY_warning_message, &str, &len))
|
|
|
|
{
|
|
|
|
response->warning = tr_strndup(str, len);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(&benc, TR_KEY_interval, &i))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
response->interval = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(&benc, TR_KEY_min_interval, &i))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
response->min_interval = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(&benc, TR_KEY_tracker_id, &str, &len))
|
|
|
|
{
|
|
|
|
response->tracker_id_str = tr_strndup(str, len);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(&benc, TR_KEY_complete, &i))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
response->seeders = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(&benc, TR_KEY_incomplete, &i))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
response->leechers = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(&benc, TR_KEY_downloaded, &i))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
response->downloads = i;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindRaw(&benc, TR_KEY_peers6, &raw, &len))
|
|
|
|
{
|
|
|
|
dbgmsg(data->log_name, "got a peers6 length of %zu", len);
|
|
|
|
response->pex6 = tr_peerMgrCompact6ToPex(raw, len, NULL, 0, &response->pex6_count);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindRaw(&benc, TR_KEY_peers, &raw, &len))
|
|
|
|
{
|
|
|
|
dbgmsg(data->log_name, "got a compact peers length of %zu", len);
|
|
|
|
response->pex = tr_peerMgrCompactToPex(raw, len, NULL, 0, &response->pex_count);
|
|
|
|
}
|
|
|
|
else if (tr_variantDictFindList(&benc, TR_KEY_peers, &tmp))
|
|
|
|
{
|
|
|
|
response->pex = listToPex(tmp, &response->pex_count);
|
|
|
|
dbgmsg(data->log_name, "got a peers list with %zu entries", response->pex_count);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (variant_loaded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variantFree(&benc);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(session, on_announce_done_eventthread, data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_tracker_http_announce(tr_session* session, tr_announce_request const* request, tr_announce_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* response_func_user_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct announce_data* d;
|
|
|
|
char* url = announce_url_new(session, request);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
d = tr_new0(struct announce_data, 1);
|
2011-10-14 00:27:14 +00:00
|
|
|
d->response.seeders = -1;
|
|
|
|
d->response.leechers = -1;
|
|
|
|
d->response.downloads = -1;
|
2011-03-11 04:19:01 +00:00
|
|
|
d->response_func = response_func;
|
|
|
|
d->response_func_user_data = response_func_user_data;
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(d->response.info_hash, request->info_hash, SHA_DIGEST_LENGTH);
|
|
|
|
tr_strlcpy(d->log_name, request->log_name, sizeof(d->log_name));
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(request->log_name, "Sending announce to libcurl: \"%s\"", url);
|
|
|
|
tr_webRun(session, url, on_announce_done, d);
|
2011-04-27 20:41:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(url);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
***** SCRAPE
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
struct scrape_data
|
|
|
|
{
|
|
|
|
tr_scrape_response response;
|
2013-09-08 17:08:18 +00:00
|
|
|
tr_scrape_response_func response_func;
|
2017-04-19 12:04:45 +00:00
|
|
|
void* response_func_user_data;
|
2011-03-11 04:19:01 +00:00
|
|
|
char log_name[128];
|
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void on_scrape_done_eventthread(void* vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct scrape_data* data = vdata;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (data->response_func != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
data->response_func(&data->response, data->response_func_user_data);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(data->response.errmsg);
|
|
|
|
tr_free(data->response.url);
|
|
|
|
tr_free(data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static void on_scrape_done(tr_session* session, bool did_connect, bool did_timeout, long response_code, void const* msg,
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t msglen, void* vdata)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_scrape_response* response;
|
|
|
|
struct scrape_data* data = vdata;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
response = &data->response;
|
|
|
|
response->did_connect = did_connect;
|
|
|
|
response->did_timeout = did_timeout;
|
2017-04-19 12:04:45 +00:00
|
|
|
dbgmsg(data->log_name, "Got scrape response for \"%s\"", response->url);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (response_code != HTTP_OK)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* fmt = _("Tracker gave HTTP response code %1$ld (%2$s)");
|
|
|
|
char const* response_str = tr_webGetResponseStr(response_code);
|
2017-04-19 12:04:45 +00:00
|
|
|
response->errmsg = tr_strdup_printf(fmt, response_code, response_str);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
tr_variant top;
|
2011-03-11 04:19:01 +00:00
|
|
|
int64_t intVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* files;
|
|
|
|
tr_variant* flags;
|
2012-12-14 04:34:42 +00:00
|
|
|
size_t len;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* str;
|
|
|
|
bool const variant_loaded = !tr_variantFromBenc(&top, msg, msglen);
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_env_key_exists("TR_CURL_VERBOSE"))
|
2011-09-24 05:54:47 +00:00
|
|
|
{
|
2012-12-14 04:34:42 +00:00
|
|
|
if (!variant_loaded)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s", "Scrape response was not in benc format\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-25 11:34:35 +00:00
|
|
|
size_t i, len;
|
2017-04-19 12:04:45 +00:00
|
|
|
char* str = tr_variantToStr(&top, TR_VARIANT_FMT_JSON, &len);
|
|
|
|
fprintf(stderr, "%s", "Scrape response:\n< ");
|
|
|
|
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
{
|
|
|
|
fputc(str[i], stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', stderr);
|
|
|
|
tr_free(str);
|
2011-09-24 05:54:47 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-03 16:44:07 +00:00
|
|
|
|
2012-12-14 04:34:42 +00:00
|
|
|
if (variant_loaded)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindStr(&top, TR_KEY_failure_reason, &str, &len))
|
|
|
|
{
|
|
|
|
response->errmsg = tr_strndup(str, len);
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(&top, TR_KEY_flags, &flags))
|
|
|
|
{
|
|
|
|
if (tr_variantDictFindInt(flags, TR_KEY_min_request_interval, &intVal))
|
|
|
|
{
|
2011-09-25 21:48:34 +00:00
|
|
|
response->min_request_interval = intVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindDict(&top, TR_KEY_files, &files))
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (;;)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int j;
|
2012-12-22 20:35:19 +00:00
|
|
|
tr_quark key;
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant* val;
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* get the next "file" */
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!tr_variantDictChild(files, i++, &key, &val))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
|
|
|
/* populate the corresponding row in our response array */
|
2017-04-19 12:04:45 +00:00
|
|
|
for (j = 0; j < response->row_count; ++j)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
struct tr_scrape_response_row* row = &response->rows[j];
|
|
|
|
|
|
|
|
if (memcmp(tr_quark_get_string(key, NULL), row->info_hash, SHA_DIGEST_LENGTH) == 0)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindInt(val, TR_KEY_complete, &intVal))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
row->seeders = intVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(val, TR_KEY_incomplete, &intVal))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
row->leechers = intVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(val, TR_KEY_downloaded, &intVal))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
row->downloads = intVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindInt(val, TR_KEY_downloaders, &intVal))
|
|
|
|
{
|
2011-03-11 04:19:01 +00:00
|
|
|
row->downloaders = intVal;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2011-03-11 04:19:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variantFree(&top);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_runInEventThread(session, on_scrape_done_eventthread, data);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static char* scrape_url_new(tr_scrape_request const* req)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char delimiter;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct evbuffer* buf = evbuffer_new();
|
|
|
|
|
|
|
|
evbuffer_add_printf(buf, "%s", req->url);
|
|
|
|
delimiter = strchr(req->url, '?') ? '&' : '?';
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (i = 0; i < req->info_hash_count; ++i)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
char str[SHA_DIGEST_LENGTH * 3 + 1];
|
|
|
|
tr_http_escape_sha1(str, req->info_hash[i]);
|
|
|
|
evbuffer_add_printf(buf, "%cinfo_hash=%s", delimiter, str);
|
2011-03-11 04:19:01 +00:00
|
|
|
delimiter = '&';
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return evbuffer_free_to_str(buf, NULL);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void tr_tracker_http_scrape(tr_session* session, tr_scrape_request const* request, tr_scrape_response_func response_func,
|
2017-04-19 12:04:45 +00:00
|
|
|
void* response_func_user_data)
|
2011-03-11 04:19:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
2017-04-19 12:04:45 +00:00
|
|
|
struct scrape_data* d;
|
|
|
|
char* url = scrape_url_new(request);
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
d = tr_new0(struct scrape_data, 1);
|
|
|
|
d->response.url = tr_strdup(request->url);
|
2011-03-11 04:19:01 +00:00
|
|
|
d->response_func = response_func;
|
|
|
|
d->response_func_user_data = response_func_user_data;
|
|
|
|
d->response.row_count = request->info_hash_count;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
for (i = 0; i < d->response.row_count; ++i)
|
2011-10-14 00:27:14 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
memcpy(d->response.rows[i].info_hash, request->info_hash[i], SHA_DIGEST_LENGTH);
|
2011-10-14 00:27:14 +00:00
|
|
|
d->response.rows[i].seeders = -1;
|
|
|
|
d->response.rows[i].leechers = -1;
|
|
|
|
d->response.rows[i].downloads = -1;
|
|
|
|
}
|
2011-03-11 04:19:01 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_strlcpy(d->log_name, request->log_name, sizeof(d->log_name));
|
|
|
|
|
|
|
|
dbgmsg(request->log_name, "Sending scrape to libcurl: \"%s\"", url);
|
|
|
|
tr_webRun(session, url, on_scrape_done, d);
|
2011-04-27 20:41:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(url);
|
2011-03-11 04:19:01 +00:00
|
|
|
}
|