File indexing completed on 2025-01-05 04:37:17

0001 /*
0002     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "serverinterface.h"
0008 #include <QHostAddress>
0009 #include <mse/encryptedpacketsocket.h>
0010 #include <mse/encryptedserverauthenticate.h>
0011 #include <peer/accessmanager.h>
0012 #include <peer/authenticationmonitor.h>
0013 #include <peer/peermanager.h>
0014 #include <peer/serverauthenticate.h>
0015 #include <torrent/torrent.h>
0016 #include <util/functions.h>
0017 #include <util/log.h>
0018 #include <util/sha1hash.h>
0019 
0020 namespace bt
0021 {
0022 QList<PeerManager *> ServerInterface::peer_managers;
0023 bool ServerInterface::encryption = false;
0024 bool ServerInterface::allow_unencrypted = true;
0025 Uint16 ServerInterface::port = 6881;
0026 bool ServerInterface::utp_enabled = false;
0027 bool ServerInterface::only_use_utp = false;
0028 TransportProtocol ServerInterface::primary_transport_protocol = TCP;
0029 
0030 ServerInterface::ServerInterface(QObject *parent)
0031     : QObject(parent)
0032 {
0033 }
0034 
0035 ServerInterface::~ServerInterface()
0036 {
0037 }
0038 
0039 void ServerInterface::addPeerManager(PeerManager *pman)
0040 {
0041     peer_managers.append(pman);
0042 }
0043 
0044 void ServerInterface::removePeerManager(PeerManager *pman)
0045 {
0046     peer_managers.removeAll(pman);
0047 }
0048 
0049 PeerManager *ServerInterface::findPeerManager(const bt::SHA1Hash &hash)
0050 {
0051     QList<PeerManager *>::iterator i = peer_managers.begin();
0052     while (i != peer_managers.end()) {
0053         PeerManager *pm = *i;
0054         if (pm && pm->getTorrent().getInfoHash() == hash) {
0055             if (!pm->isStarted())
0056                 return nullptr;
0057             else
0058                 return pm;
0059         }
0060         ++i;
0061     }
0062     return nullptr;
0063 }
0064 
0065 bool ServerInterface::findInfoHash(const bt::SHA1Hash &skey, SHA1Hash &info_hash)
0066 {
0067     Uint8 buf[24];
0068     memcpy(buf, "req2", 4);
0069     QList<PeerManager *>::iterator i = peer_managers.begin();
0070     while (i != peer_managers.end()) {
0071         PeerManager *pm = *i;
0072         memcpy(buf + 4, pm->getTorrent().getInfoHash().getData(), 20);
0073         if (SHA1Hash::generate(buf, 24) == skey) {
0074             info_hash = pm->getTorrent().getInfoHash();
0075             return true;
0076         }
0077         ++i;
0078     }
0079     return false;
0080 }
0081 
0082 void ServerInterface::disableEncryption()
0083 {
0084     encryption = false;
0085 }
0086 
0087 void ServerInterface::enableEncryption(bool unencrypted_allowed)
0088 {
0089     encryption = true;
0090     allow_unencrypted = unencrypted_allowed;
0091 }
0092 
0093 QStringList ServerInterface::bindAddresses()
0094 {
0095     QString iface = NetworkInterface();
0096     QStringList ips = NetworkInterfaceIPAddresses(iface);
0097     if (ips.count() == 0) {
0098         // Interface does not exist, so add any addresses
0099         ips << QHostAddress(QHostAddress::AnyIPv6).toString() << QHostAddress(QHostAddress::Any).toString();
0100     }
0101 
0102     return ips;
0103 }
0104 
0105 void ServerInterface::newConnection(mse::EncryptedPacketSocket::Ptr s)
0106 {
0107     if (peer_managers.count() == 0) {
0108         s->close();
0109     } else {
0110         if (!AccessManager::instance().allowed(s->getRemoteAddress())) {
0111             Out(SYS_CON | LOG_DEBUG) << "A client with a blocked IP address (" << s->getRemoteIPAddress() << ") tried to connect !" << endl;
0112             return;
0113         }
0114 
0115         // Not enough free file descriptors
0116         if (!OpenFileAllowed())
0117             return;
0118 
0119         ServerAuthenticate *auth = nullptr;
0120 
0121         if (encryption)
0122             auth = new mse::EncryptedServerAuthenticate(s);
0123         else
0124             auth = new ServerAuthenticate(s);
0125 
0126         AuthenticationMonitor::instance().add(auth);
0127     }
0128 }
0129 
0130 void ServerInterface::setPrimaryTransportProtocol(TransportProtocol proto)
0131 {
0132     primary_transport_protocol = proto;
0133 }
0134 
0135 void ServerInterface::setUtpEnabled(bool on, bool only_utp)
0136 {
0137     utp_enabled = on;
0138     only_use_utp = only_utp;
0139 }
0140 
0141 }
0142 
0143 #include "moc_serverinterface.cpp"