1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/*
SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "functions.h"
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <QNetworkInterface>
#include <QRandomGenerator>
#include <QStandardPaths>
#include <KLocalizedString>
#include "settings.h"
#include <dht/dhtbase.h>
#include <diskio/cache.h>
#include <diskio/chunkmanager.h>
#include <download/downloader.h>
#include <download/webseed.h>
#include <interfaces/queuemanagerinterface.h>
#include <mse/encryptedpacketsocket.h>
#include <net/socketmonitor.h>
#include <net/socks.h>
#include <peer/authenticationmonitor.h>
#include <peer/connectionlimit.h>
#include <peer/peerconnector.h>
#include <peer/peermanager.h>
#include <peer/utpex.h>
#include <torrent/choker.h>
#include <torrent/server.h>
#include <torrent/timeestimator.h>
#include <torrent/torrentcontrol.h>
#include <tracker/httptracker.h>
#include <tracker/udptrackersocket.h>
#include <util/functions.h>
#include <util/log.h>
using namespace bt;
namespace kt
{
QString DataDir(CreationMode mode)
{
QString dataDirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (mode == CreateIfNotExists) {
QFileInfo fileInfo(dataDirPath);
if (!fileInfo.exists()) {
QString ktorrent4DataFolder = QDir::homePath() + QLatin1String("/.kde/share/apps/ktorrent");
if (!QFile::exists(ktorrent4DataFolder)) {
ktorrent4DataFolder = QDir::homePath() + QLatin1String("/.kde4/share/apps/ktorrent");
if (!QFile::exists(ktorrent4DataFolder))
ktorrent4DataFolder.clear();
}
if (ktorrent4DataFolder.isEmpty() || !QFile::rename(ktorrent4DataFolder, dataDirPath))
fileInfo.dir().mkpath(fileInfo.fileName());
}
}
// if (!str.endsWith(bt::DirSeparator()))
return dataDirPath + bt::DirSeparator();
// else
// return str;
}
Uint16 RandomGoodPort()
{
Uint16 start = 50000;
while (true) {
Uint16 port = start + QRandomGenerator::global()->bounded(10000);
if (port != Settings::port() && port != Settings::dhtPort() && port != Settings::udpTrackerPort())
return port;
}
}
void ApplySettings()
{
PeerManager::connectionLimits().setLimits(Settings::maxTotalConnections(), Settings::maxConnections());
net::SocketMonitor::setDownloadCap(Settings::maxDownloadRate() * 1024);
net::SocketMonitor::setUploadCap(Settings::maxUploadRate() * 1024);
net::SocketMonitor::setSleepTime(Settings::cpuUsage());
mse::EncryptedPacketSocket::setTOS(Settings::dscp() << 2);
bt::PeerConnector::setMaxActive(Settings::maxConnectingSockets());
// Check for port conflicts
if (Settings::port() == Settings::udpTrackerPort())
Settings::setUdpTrackerPort(RandomGoodPort());
if (Settings::port() == Settings::dhtPort())
Settings::setDhtPort(RandomGoodPort());
UDPTrackerSocket::setPort(Settings::udpTrackerPort());
Choker::setNumUploadSlots(Settings::numUploadSlots());
dht::DHTBase &ht = Globals::instance().getDHT();
if (Settings::dhtSupport() && !ht.isRunning()) {
ht.start(kt::DataDir() + QLatin1String("dht_table"), kt::DataDir() + QLatin1String("dht_key"), Settings::dhtPort());
} else if (!Settings::dhtSupport() && ht.isRunning()) {
ht.stop();
} else if (Settings::dhtSupport() && ht.getPort() != Settings::dhtPort()) {
Out(SYS_GEN | LOG_NOTICE) << "Restarting DHT with new port " << Settings::dhtPort() << endl;
ht.stop();
ht.start(kt::DataDir() + QLatin1String("dht_table"), kt::DataDir() + QLatin1String("dht_key"), Settings::dhtPort());
}
UTPex::setEnabled(Settings::pexEnabled());
if (Settings::useEncryption()) {
ServerInterface::enableEncryption(Settings::allowUnencryptedConnections());
} else {
ServerInterface::disableEncryption();
}
if (Settings::useCustomIP())
Tracker::setCustomIP(Settings::customIP());
else
Tracker::setCustomIP(QString());
QString proxy = Settings::httpProxy();
bt::HTTPTracker::setProxyEnabled(!Settings::useKDEProxySettings() && Settings::useProxyForTracker());
bt::HTTPTracker::setProxy(proxy, Settings::httpProxyPort());
bt::WebSeed::setProxy(proxy, Settings::httpProxyPort());
bt::WebSeed::setProxyEnabled(!Settings::useKDEProxySettings() && Settings::useProxyForWebSeeds());
bt::Cache::setPreallocationEnabled(Settings::diskPrealloc());
bt::Cache::setPreallocateFully(Settings::fullDiskPrealloc());
bt::TorrentControl::setDataCheckWhenCompleted(Settings::checkWhenFinished());
bt::TorrentControl::setMinimumDiskSpace(Settings::minDiskSpace());
bt::SetNetworkInterface(Settings::networkInterface());
net::Socks::setSocksEnabled(Settings::socksEnabled());
net::Socks::setSocksVersion(Settings::socksVersion());
net::Socks::setSocksServerAddress(Settings::socksProxy(), Settings::socksPort());
if (Settings::socksUsePassword())
net::Socks::setSocksAuthentication(Settings::socksUsername(), Settings::socksPassword());
else
net::Socks::setSocksAuthentication(QString(), QString());
bt::ChunkManager::setPreviewSizes(Settings::previewSizeAudio() * 1024, Settings::previewSizeVideo() * 1024);
bt::QueueManagerInterface::setQueueManagerEnabled(!Settings::manuallyControlTorrents());
bt::Downloader::setUseWebSeeds(Settings::webseedsEnabled());
bt::Peer::setResolveHostnames(Settings::lookUpHostnameOfPeers());
}
QString TorrentFileFilter(bool all_files_included)
{
QString ret = i18nc("*.torrent", "Torrents") + QLatin1String(" (*.torrent)");
if (all_files_included)
ret += QLatin1String(";;") + i18n("All files") + QLatin1String(" (*)");
return ret;
}
}
|