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

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #include "serverauthenticate.h"
0007 #include "peerid.h"
0008 #include "peermanager.h"
0009 #include <mse/encryptedpacketsocket.h>
0010 #include <peer/accessmanager.h>
0011 #include <torrent/globals.h>
0012 #include <torrent/server.h>
0013 #include <torrent/torrent.h>
0014 #include <util/log.h>
0015 #include <util/sha1hash.h>
0016 
0017 namespace bt
0018 {
0019 bool ServerAuthenticate::s_firewalled = true;
0020 
0021 ServerAuthenticate::ServerAuthenticate(mse::EncryptedPacketSocket::Ptr sock)
0022     : AuthenticateBase(sock)
0023 {
0024 }
0025 
0026 ServerAuthenticate::~ServerAuthenticate()
0027 {
0028 }
0029 
0030 void ServerAuthenticate::onFinish(bool succes)
0031 {
0032     Out(SYS_CON | LOG_NOTICE) << "Authentication(S) to " << sock->getRemoteIPAddress() << " : " << (succes ? "ok" : "failure") << endl;
0033     finished = true;
0034     setFirewalled(false);
0035 
0036     if (!succes)
0037         sock.clear();
0038 
0039     timer.stop();
0040 }
0041 
0042 void ServerAuthenticate::handshakeReceived(bool full)
0043 {
0044     Uint8 *hs = handshake;
0045     AccessManager &aman = AccessManager::instance();
0046 
0047     if (!aman.allowed(sock->getRemoteAddress())) {
0048         Out(SYS_GEN | LOG_NOTICE) << "The IP address " << sock->getRemoteIPAddress() << " is blocked" << endl;
0049         onFinish(false);
0050         return;
0051     }
0052 
0053     // try to find a PeerManager which has the right info hash
0054     SHA1Hash rh(hs + 28);
0055     PeerManager *pman = ServerInterface::findPeerManager(rh);
0056     if (!pman) {
0057         onFinish(false);
0058         return;
0059     }
0060 
0061     if (full) {
0062         // check if we aren't connecting to ourself
0063         char tmp[21];
0064         tmp[20] = '\0';
0065         memcpy(tmp, hs + 48, 20);
0066         PeerID peer_id = PeerID(tmp);
0067         if (pman->getTorrent().getPeerID() == peer_id) {
0068             Out(SYS_CON | LOG_NOTICE) << "Lets not connect to our self" << endl;
0069             onFinish(false);
0070             return;
0071         }
0072 
0073         // check if we aren't already connected to the client
0074         if (pman->connectedTo(peer_id)) {
0075             Out(SYS_CON | LOG_NOTICE) << "Already connected to " << peer_id.toString() << endl;
0076             onFinish(false);
0077             return;
0078         }
0079 
0080         // send handshake and finish off
0081         sendHandshake(rh, pman->getTorrent().getPeerID());
0082         onFinish(true);
0083         // hand over connection
0084         pman->newConnection(sock, peer_id, supportedExtensions());
0085     } else {
0086         // if the handshake wasn't fully received just send our handshake
0087         sendHandshake(rh, pman->getTorrent().getPeerID());
0088     }
0089 }
0090 }
0091 
0092 #include "moc_serverauthenticate.cpp"