Show I2P peer addresses
[qBittorrent.git] / src / base / bittorrent / peerinfo.cpp
blob64fe1258b01a546e12893b6ba91a43d505c35bd3
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015-2023 Vladimir Golovnev <glassez@yandex.ru>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
29 #include "peerinfo.h"
31 #include <QBitArray>
33 #include "base/bittorrent/ltqbitarray.h"
34 #include "base/net/geoipmanager.h"
35 #include "base/unicodestrings.h"
36 #include "base/utils/bytearray.h"
37 #include "peeraddress.h"
39 using namespace BitTorrent;
41 PeerInfo::PeerInfo(const lt::peer_info &nativeInfo, const QBitArray &allPieces)
42 : m_nativeInfo(nativeInfo)
43 , m_relevance(calcRelevance(allPieces))
45 determineFlags();
48 bool PeerInfo::fromDHT() const
50 return static_cast<bool>(m_nativeInfo.source & lt::peer_info::dht);
53 bool PeerInfo::fromPeX() const
55 return static_cast<bool>(m_nativeInfo.source & lt::peer_info::pex);
58 bool PeerInfo::fromLSD() const
60 return static_cast<bool>(m_nativeInfo.source & lt::peer_info::lsd);
63 QString PeerInfo::country() const
65 if (m_country.isEmpty())
66 m_country = Net::GeoIPManager::instance()->lookup(address().ip);
67 return m_country;
70 bool PeerInfo::isInteresting() const
72 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::interesting);
75 bool PeerInfo::isChocked() const
77 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::choked);
80 bool PeerInfo::isRemoteInterested() const
82 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::remote_interested);
85 bool PeerInfo::isRemoteChocked() const
87 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::remote_choked);
90 bool PeerInfo::isSupportsExtensions() const
92 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::supports_extensions);
95 bool PeerInfo::isLocalConnection() const
97 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::local_connection);
100 bool PeerInfo::isHandshake() const
102 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::handshake);
105 bool PeerInfo::isConnecting() const
107 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::connecting);
110 bool PeerInfo::isOnParole() const
112 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::on_parole);
115 bool PeerInfo::isSeed() const
117 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::seed);
120 bool PeerInfo::optimisticUnchoke() const
122 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::optimistic_unchoke);
125 bool PeerInfo::isSnubbed() const
127 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::snubbed);
130 bool PeerInfo::isUploadOnly() const
132 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::upload_only);
135 bool PeerInfo::isEndgameMode() const
137 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::endgame_mode);
140 bool PeerInfo::isHolepunched() const
142 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::holepunched);
145 bool PeerInfo::useI2PSocket() const
147 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::i2p_socket);
150 bool PeerInfo::useUTPSocket() const
152 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::utp_socket);
155 bool PeerInfo::useSSLSocket() const
157 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::ssl_socket);
160 bool PeerInfo::isRC4Encrypted() const
162 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::rc4_encrypted);
165 bool PeerInfo::isPlaintextEncrypted() const
167 return static_cast<bool>(m_nativeInfo.flags & lt::peer_info::plaintext_encrypted);
170 PeerAddress PeerInfo::address() const
172 if (useI2PSocket())
173 return {};
175 // fast path for platforms which boost.asio internal struct maps to `sockaddr`
176 return {QHostAddress(m_nativeInfo.ip.data()), m_nativeInfo.ip.port()};
177 // slow path for the others
178 //return {QHostAddress(QString::fromStdString(m_nativeInfo.ip.address().to_string()))
179 // , m_nativeInfo.ip.port()};
182 QString PeerInfo::I2PAddress() const
184 if (!useI2PSocket())
185 return {};
187 #ifdef QBT_USES_LIBTORRENT2
188 if (m_I2PAddress.isEmpty())
190 const lt::sha256_hash destHash = m_nativeInfo.i2p_destination();
191 const QByteArray base32Dest = Utils::ByteArray::toBase32({destHash.data(), destHash.size()}).replace('=', "").toLower();
192 m_I2PAddress = QString::fromLatin1(base32Dest) + u".b32.i2p";
194 #endif
196 return m_I2PAddress;
199 QString PeerInfo::client() const
201 return QString::fromStdString(m_nativeInfo.client);
204 QString PeerInfo::peerIdClient() const
206 // when peer ID is not known yet it contains only zero bytes,
207 // do not create string in such case, return empty string instead
208 if (m_nativeInfo.pid.is_all_zeros())
209 return {};
211 QString result;
213 // interesting part of a typical peer ID is first 8 chars
214 for (int i = 0; i < 8; ++i)
216 const std::uint8_t c = m_nativeInfo.pid[i];
218 // ensure that the peer ID slice consists only of printable ASCII characters,
219 // this should filter out most of the improper IDs
220 if ((c < 32) || (c > 126))
221 return tr("Unknown");
223 result += QChar::fromLatin1(c);
226 return result;
229 qreal PeerInfo::progress() const
231 return m_nativeInfo.progress;
234 int PeerInfo::payloadUpSpeed() const
236 return m_nativeInfo.payload_up_speed;
239 int PeerInfo::payloadDownSpeed() const
241 return m_nativeInfo.payload_down_speed;
244 qlonglong PeerInfo::totalUpload() const
246 return m_nativeInfo.total_upload;
249 qlonglong PeerInfo::totalDownload() const
251 return m_nativeInfo.total_download;
254 QBitArray PeerInfo::pieces() const
256 return LT::toQBitArray(m_nativeInfo.pieces);
259 QString PeerInfo::connectionType() const
261 if (m_nativeInfo.flags & lt::peer_info::utp_socket)
262 return C_UTP;
264 return (m_nativeInfo.connection_type == lt::peer_info::standard_bittorrent)
265 ? u"BT"_qs
266 : u"Web"_qs;
269 qreal PeerInfo::calcRelevance(const QBitArray &allPieces) const
271 const int localMissing = allPieces.count(false);
272 if (localMissing <= 0)
273 return 0;
275 const QBitArray peerPieces = pieces();
276 const int remoteHaves = (peerPieces & (~allPieces)).count(true);
277 return static_cast<qreal>(remoteHaves) / localMissing;
280 qreal PeerInfo::relevance() const
282 return m_relevance;
285 void PeerInfo::determineFlags()
287 const auto updateFlags = [this](const QChar specifier, const QString &explanation)
289 m_flags += (specifier + u' ');
290 m_flagsDescription += u"%1 = %2\n"_qs.arg(specifier, explanation);
293 if (isInteresting())
295 if (isRemoteChocked())
297 // d = Your client wants to download, but peer doesn't want to send (interested and choked)
298 updateFlags(u'd', tr("Interested (local) and choked (peer)"));
300 else
302 // D = Currently downloading (interested and not choked)
303 updateFlags(u'D', tr("Interested (local) and unchoked (peer)"));
307 if (isRemoteInterested())
309 if (isChocked())
311 // u = Peer wants your client to upload, but your client doesn't want to (interested and choked)
312 updateFlags(u'u', tr("Interested (peer) and choked (local)"));
314 else
316 // U = Currently uploading (interested and not choked)
317 updateFlags(u'U', tr("Interested (peer) and unchoked (local)"));
321 // K = Peer is unchoking your client, but your client is not interested
322 if (!isRemoteChocked() && !isInteresting())
323 updateFlags(u'K', tr("Not interested (local) and unchoked (peer)"));
325 // ? = Your client unchoked the peer but the peer is not interested
326 if (!isChocked() && !isRemoteInterested())
327 updateFlags(u'?', tr("Not interested (peer) and unchoked (local)"));
329 // O = Optimistic unchoke
330 if (optimisticUnchoke())
331 updateFlags(u'O', tr("Optimistic unchoke"));
333 // S = Peer is snubbed
334 if (isSnubbed())
335 updateFlags(u'S', tr("Peer snubbed"));
337 // I = Peer is an incoming connection
338 if (!isLocalConnection())
339 updateFlags(u'I', tr("Incoming connection"));
341 // H = Peer was obtained through DHT
342 if (fromDHT())
343 updateFlags(u'H', tr("Peer from DHT"));
345 // X = Peer was included in peerlists obtained through Peer Exchange (PEX)
346 if (fromPeX())
347 updateFlags(u'X', tr("Peer from PEX"));
349 // L = Peer is local
350 if (fromLSD())
351 updateFlags(u'L', tr("Peer from LSD"));
353 // E = Peer is using Protocol Encryption (all traffic)
354 if (isRC4Encrypted())
355 updateFlags(u'E', tr("Encrypted traffic"));
357 // e = Peer is using Protocol Encryption (handshake)
358 if (isPlaintextEncrypted())
359 updateFlags(u'e', tr("Encrypted handshake"));
361 // P = Peer is using uTorrent uTP
362 if (useUTPSocket())
363 updateFlags(u'P', C_UTP);
365 m_flags.chop(1);
366 m_flagsDescription.chop(1);
369 QString PeerInfo::flags() const
371 return m_flags;
374 QString PeerInfo::flagsDescription() const
376 return m_flagsDescription;
379 int PeerInfo::downloadingPieceIndex() const
381 return static_cast<int>(m_nativeInfo.downloading_piece_index);