File indexing completed on 2025-01-05 04:37:21
0001 /* 0002 SPDX-FileCopyrightText: 2008 Joris Guisson <joris.guisson@gmail.com> 0003 SPDX-FileCopyrightText: 2008 Ivan Vasic <ivasic@gmail.com> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 #include "accessmanager.h" 0008 0009 #include <interfaces/blocklistinterface.h> 0010 #include <net/address.h> 0011 #include <peer/badpeerslist.h> 0012 #include <torrent/server.h> 0013 #include <tracker/tracker.h> 0014 0015 namespace bt 0016 { 0017 AccessManager::AccessManager() 0018 { 0019 banned = new BadPeersList(); 0020 addBlockList(banned); 0021 } 0022 0023 AccessManager::~AccessManager() 0024 { 0025 qDeleteAll(blocklists); 0026 } 0027 0028 AccessManager &AccessManager::instance() 0029 { 0030 static AccessManager inst; 0031 return inst; 0032 } 0033 0034 void AccessManager::addBlockList(BlockListInterface *bl) 0035 { 0036 blocklists.append(bl); 0037 } 0038 0039 void AccessManager::removeBlockList(BlockListInterface *bl) 0040 { 0041 blocklists.removeAll(bl); 0042 } 0043 0044 bool AccessManager::allowed(const net::Address &addr) const 0045 { 0046 // Don't connect to the custom IP sent to the tracker, this should be our own' 0047 if (isOurOwnAddress(addr)) 0048 return false; 0049 0050 for (const BlockListInterface *bl : std::as_const(blocklists)) { 0051 if (bl->blocked(addr)) 0052 return false; 0053 } 0054 return true; 0055 } 0056 0057 void AccessManager::banPeer(const QString &addr) 0058 { 0059 banned->addBadPeer(addr); 0060 } 0061 0062 void AccessManager::addExternalIP(const QString &addr) 0063 { 0064 external_addresses.append(addr); 0065 } 0066 0067 bool AccessManager::isOurOwnAddress(const net::Address &addr) const 0068 { 0069 Uint16 port = bt::Server::getPort(); 0070 if (!Tracker::getCustomIP().isEmpty() && net::Address(Tracker::getCustomIP(), port) == addr) 0071 return true; 0072 0073 for (const QString &ip : std::as_const(external_addresses)) { 0074 net::Address address(ip, port); 0075 if (address == addr) 0076 return true; 0077 } 0078 0079 return false; 0080 } 0081 0082 }