Merge pull request #947 from kkartaltepe/bt-ext-hs

[WIP] Add option for bt extended handshake version/user agent string
pull/960/head
Tatsuhiro Tsujikawa 2017-07-12 21:13:34 +09:00 committed by GitHub
commit 1f187d1dec
10 changed files with 59 additions and 1 deletions

View File

@ -954,6 +954,15 @@ BitTorrent Specific Options
replaced by major, minor and patch version number respectively. For replaced by major, minor and patch version number respectively. For
instance, aria2 version 1.18.8 has prefix ID ``A2-1-18-8-``. instance, aria2 version 1.18.8 has prefix ID ``A2-1-18-8-``.
.. option:: --peer-agent=<PEER_AGENT>
Specify the string used during the bitorrent extended handshake
for the peer's client version.
Default: ``aria2/$MAJOR.$MINOR.$PATCH``, $MAJOR, $MINOR and $PATCH are
replaced by major, minor and patch version number respectively. For
instance, aria2 version 1.18.8 has peer agent ``aria2/1.18.8``.
.. option:: --seed-ratio=<RATIO> .. option:: --seed-ratio=<RATIO>
Specify share ratio. Seed completed torrents until share ratio reaches Specify share ratio. Seed completed torrents until share ratio reaches

View File

@ -166,6 +166,7 @@ Context::Context(bool standalone, int argc, char** argv, const KeyVals& options)
} }
#ifdef ENABLE_BITTORRENT #ifdef ENABLE_BITTORRENT
bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX)); bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX));
bittorrent::generateStaticPeerAgent(op->get(PREF_PEER_AGENT));
#endif // ENABLE_BITTORRENT #endif // ENABLE_BITTORRENT
LogFactory::setLogFile(op->get(PREF_LOG)); LogFactory::setLogFile(op->get(PREF_LOG));
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL)); LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));

View File

@ -199,7 +199,7 @@ void DefaultBtInteractive::addPortMessageToQueue()
void DefaultBtInteractive::addHandshakeExtendedMessageToQueue() void DefaultBtInteractive::addHandshakeExtendedMessageToQueue()
{ {
auto m = make_unique<HandshakeExtensionMessage>(); auto m = make_unique<HandshakeExtensionMessage>();
m->setClientVersion("aria2/" PACKAGE_VERSION); m->setClientVersion(bittorrent::getStaticPeerAgent());
m->setTCPPort(tcpPort_); m->setTCPPort(tcpPort_);
m->setExtensions(extensionMessageRegistry_->getExtensions()); m->setExtensions(extensionMessageRegistry_->getExtensions());
auto attrs = bittorrent::getTorrentAttrs(downloadContext_); auto attrs = bittorrent::getTorrentAttrs(downloadContext_);

View File

@ -1853,6 +1853,12 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
op->addTag(TAG_BITTORRENT); op->addTag(TAG_BITTORRENT);
handlers.push_back(op); handlers.push_back(op);
} }
{
OptionHandler* op(new DefaultOptionHandler(
PREF_PEER_AGENT, TEXT_PEER_AGENT, "aria2/" PACKAGE_VERSION));
op->addTag(TAG_BITTORRENT);
handlers.push_back(op);
}
{ {
OptionHandler* op(new FloatNumberOptionHandler( OptionHandler* op(new FloatNumberOptionHandler(
PREF_SEED_TIME, TEXT_SEED_TIME, NO_DEFAULT_VALUE, 0)); PREF_SEED_TIME, TEXT_SEED_TIME, NO_DEFAULT_VALUE, 0));

View File

@ -88,6 +88,7 @@ const char C_COMMENT_UTF8[] = "comment.utf-8";
const char C_CREATED_BY[] = "created by"; const char C_CREATED_BY[] = "created by";
const char DEFAULT_PEER_ID_PREFIX[] = "aria2-"; const char DEFAULT_PEER_ID_PREFIX[] = "aria2-";
const char DEFAULT_PEER_AGENT[] = "aria2/" PACKAGE_VERSION;
} // namespace } // namespace
const std::string MULTI("multi"); const std::string MULTI("multi");
@ -693,6 +694,7 @@ std::string generatePeerId(const std::string& peerIdPrefix)
namespace { namespace {
std::string peerId; std::string peerId;
std::string peerAgent;
} // namespace } // namespace
const std::string& generateStaticPeerId(const std::string& peerIdPrefix) const std::string& generateStaticPeerId(const std::string& peerIdPrefix)
@ -703,7 +705,16 @@ const std::string& generateStaticPeerId(const std::string& peerIdPrefix)
return peerId; return peerId;
} }
const std::string& generateStaticPeerAgent(const std::string& peerAgentNew)
{
if (peerAgent.empty()) {
peerAgent = peerAgentNew;
}
return peerAgent;
}
void setStaticPeerId(const std::string& newPeerId) { peerId = newPeerId; } void setStaticPeerId(const std::string& newPeerId) { peerId = newPeerId; }
void setStaticPeerAgent(const std::string& newPeerAgent) { peerAgent = newPeerAgent; }
// If PeerID is not generated, it is created with default peerIdPrefix // If PeerID is not generated, it is created with default peerIdPrefix
// (aria2-). // (aria2-).
@ -718,6 +729,16 @@ const unsigned char* getStaticPeerId()
} }
} }
// If PeerAgent is not generated, it is created with default agent
// aria2/PACKAGE_VERSION
const std::string& getStaticPeerAgent()
{
if (peerAgent.empty()) {
generateStaticPeerAgent(DEFAULT_PEER_AGENT);
}
return peerAgent;
}
uint8_t getId(const unsigned char* msg) { return msg[0]; } uint8_t getId(const unsigned char* msg) { return msg[0]; }
uint64_t getLLIntParam(const unsigned char* msg, size_t pos) uint64_t getLLIntParam(const unsigned char* msg, size_t pos)

View File

@ -134,15 +134,21 @@ std::string generatePeerId(const std::string& peerIdPrefix);
// uses generatePeerId(peerIdPrefix) to produce Peer ID. // uses generatePeerId(peerIdPrefix) to produce Peer ID.
const std::string& generateStaticPeerId(const std::string& peerIdPrefix); const std::string& generateStaticPeerId(const std::string& peerIdPrefix);
const std::string& generateStaticPeerAgent(const std::string& peerAgent);
// Returns Peer ID statically stored by generateStaticPeerId(). If // Returns Peer ID statically stored by generateStaticPeerId(). If
// Peer ID is not stored yet, this function calls // Peer ID is not stored yet, this function calls
// generateStaticPeerId("aria2-") // generateStaticPeerId("aria2-")
const unsigned char* getStaticPeerId(); const unsigned char* getStaticPeerId();
const std::string& getStaticPeerAgent();
// Set newPeerId as a static Peer ID. newPeerId must be 20-byte // Set newPeerId as a static Peer ID. newPeerId must be 20-byte
// length. // length.
void setStaticPeerId(const std::string& newPeerId); void setStaticPeerId(const std::string& newPeerId);
void setStaticPeerAgent(const std::string& newPeerAgent);
// Computes fast set index and returns them. // Computes fast set index and returns them.
std::vector<size_t> computeFastSet(const std::string& ipaddr, size_t numPieces, std::vector<size_t> computeFastSet(const std::string& ipaddr, size_t numPieces,
const unsigned char* infoHash, const unsigned char* infoHash,

View File

@ -482,6 +482,8 @@ PrefPtr PREF_SEED_RATIO = makePref("seed-ratio");
PrefPtr PREF_BT_KEEP_ALIVE_INTERVAL = makePref("bt-keep-alive-interval"); PrefPtr PREF_BT_KEEP_ALIVE_INTERVAL = makePref("bt-keep-alive-interval");
// values: a string, less than or equals to 20 bytes length // values: a string, less than or equals to 20 bytes length
PrefPtr PREF_PEER_ID_PREFIX = makePref("peer-id-prefix"); PrefPtr PREF_PEER_ID_PREFIX = makePref("peer-id-prefix");
// values: a string representing the extended BT handshake peer user agent
PrefPtr PREF_PEER_AGENT = makePref("peer-agent");
// values: true | false // values: true | false
PrefPtr PREF_ENABLE_PEER_EXCHANGE = makePref("enable-peer-exchange"); PrefPtr PREF_ENABLE_PEER_EXCHANGE = makePref("enable-peer-exchange");
// values: true | false // values: true | false

View File

@ -434,6 +434,8 @@ extern PrefPtr PREF_SEED_RATIO;
extern PrefPtr PREF_BT_KEEP_ALIVE_INTERVAL; extern PrefPtr PREF_BT_KEEP_ALIVE_INTERVAL;
// values: a string, less than or equals to 20 bytes length // values: a string, less than or equals to 20 bytes length
extern PrefPtr PREF_PEER_ID_PREFIX; extern PrefPtr PREF_PEER_ID_PREFIX;
// values: a string representing the extended BT handshake peer user agent
extern PrefPtr PREF_PEER_AGENT;
// values: true | false // values: true | false
extern PrefPtr PREF_ENABLE_PEER_EXCHANGE; extern PrefPtr PREF_ENABLE_PEER_EXCHANGE;
// values: true | false // values: true | false

View File

@ -331,6 +331,8 @@
" bytes are specified, only first 20 bytes are\n" \ " bytes are specified, only first 20 bytes are\n" \
" used. If less than 20 bytes are specified, random\n" \ " used. If less than 20 bytes are specified, random\n" \
" byte data are added to make its length 20 bytes.") " byte data are added to make its length 20 bytes.")
#define TEXT_PEER_AGENT \
_(" --peer-agent=PEER_AGENT Set client reported during Extended torrent handshakes")
#define TEXT_ENABLE_PEER_EXCHANGE \ #define TEXT_ENABLE_PEER_EXCHANGE \
_(" --enable-peer-exchange[=true|false] Enable Peer Exchange extension.") _(" --enable-peer-exchange[=true|false] Enable Peer Exchange extension.")
#define TEXT_ENABLE_DHT \ #define TEXT_ENABLE_DHT \

View File

@ -42,6 +42,7 @@ class BittorrentHelperTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testGetPieceLength); CPPUNIT_TEST(testGetPieceLength);
CPPUNIT_TEST(testGetInfoHashAsString); CPPUNIT_TEST(testGetInfoHashAsString);
CPPUNIT_TEST(testGetPeerId); CPPUNIT_TEST(testGetPeerId);
CPPUNIT_TEST(testGetPeerAgent);
CPPUNIT_TEST(testComputeFastSet); CPPUNIT_TEST(testComputeFastSet);
CPPUNIT_TEST(testGetFileEntries_multiFileUrlList); CPPUNIT_TEST(testGetFileEntries_multiFileUrlList);
CPPUNIT_TEST(testGetFileEntries_singleFileUrlList); CPPUNIT_TEST(testGetFileEntries_singleFileUrlList);
@ -100,6 +101,7 @@ public:
void testGetPieceLength(); void testGetPieceLength();
void testGetInfoHashAsString(); void testGetInfoHashAsString();
void testGetPeerId(); void testGetPeerId();
void testGetPeerAgent();
void testComputeFastSet(); void testComputeFastSet();
void testGetFileEntries_multiFileUrlList(); void testGetFileEntries_multiFileUrlList();
void testGetFileEntries_singleFileUrlList(); void testGetFileEntries_singleFileUrlList();
@ -325,6 +327,13 @@ void BittorrentHelperTest::testGetPeerId()
CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size()); CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
} }
void BittorrentHelperTest::testGetPeerAgent()
{
std::string peerAgent = generateStaticPeerAgent("aria2/-1.-1.-1");
CPPUNIT_ASSERT_EQUAL(std::string("aria2/-1.-1.-1"), peerAgent);
CPPUNIT_ASSERT_EQUAL(std::string("aria2/-1.-1.-1"), bittorrent::getStaticPeerAgent());
}
void BittorrentHelperTest::testComputeFastSet() void BittorrentHelperTest::testComputeFastSet()
{ {
std::string ipaddr = "192.168.0.1"; std::string ipaddr = "192.168.0.1";