Skip to content

Commit

Permalink
bump to 4.0.4.4
Browse files Browse the repository at this point in the history
Signed-off-by: c0re100 <[email protected]>
  • Loading branch information
c0re100 committed May 4, 2018
1 parent 8bd15de commit 8649b69
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/base/bittorrent/peerinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ PeerAddress PeerInfo::address() const
m_nativeInfo.ip.port());
}

int PeerInfo::port() const
{
return m_nativeInfo.ip.port();
}

QString PeerInfo::client() const
{
return QString::fromStdString(m_nativeInfo.client);
Expand Down
1 change: 1 addition & 0 deletions src/base/bittorrent/peerinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ namespace BitTorrent
bool isPlaintextEncrypted() const;

PeerAddress address() const;
int port() const;
QString client() const;
QString pid() const;
QString pidtoclient() const;
Expand Down
196 changes: 196 additions & 0 deletions src/base/bittorrent/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ Session::Session(QObject *parent)
, m_isBandwidthSchedulerEnabled(BITTORRENT_SESSION_KEY("BandwidthSchedulerEnabled"), false)
, m_saveResumeDataInterval(BITTORRENT_SESSION_KEY("SaveResumeDataInterval"), 3)
, m_autoBanUnknownPeer(BITTORRENT_SESSION_KEY("AutoBanUnknownPeer"), false)
, m_showTrackerAuthWindow(BITTORRENT_SESSION_KEY("ShowTrackerAuthWindow"), true)
, m_port(BITTORRENT_SESSION_KEY("Port"), 8999)
, m_useRandomPort(BITTORRENT_SESSION_KEY("UseRandomPort"), false)
, m_networkInterface(BITTORRENT_SESSION_KEY("Interface"))
Expand Down Expand Up @@ -484,6 +485,7 @@ Session::Session(QObject *parent)
libt::ip_filter filter;
processBannedIPs(filter);
m_nativeSession->set_ip_filter(filter);
loadOfflineFilter();
}

m_categories = map_cast(m_storedCategories);
Expand Down Expand Up @@ -1053,6 +1055,7 @@ void Session::configure()
enableIPFilter();
else
disableIPFilter();
loadOfflineFilter();
m_IPFilteringChanged = false;
}

Expand Down Expand Up @@ -1908,6 +1911,7 @@ void Session::EraseIPFilter()
m_nativeSession->set_ip_filter(libt::ip_filter());
disableIPFilter();
enableIPFilter();
loadOfflineFilter();
}

// Delete a torrent from the session, given its hash
Expand Down Expand Up @@ -2729,6 +2733,18 @@ void Session::setAutoBanUnknownPeer(bool value)
}
}

bool Session::isShowTrackerAuthWindow() const
{
return m_showTrackerAuthWindow;
}

void Session::setShowTrackerAuthWindow(bool value)
{
if (value != isShowTrackerAuthWindow()) {
m_showTrackerAuthWindow = value;
}
}

int Session::port() const
{
static int randomPort = Utils::Random::rand(1024, 65535);
Expand Down Expand Up @@ -3718,6 +3734,185 @@ void Session::disableIPFilter()
m_nativeSession->set_ip_filter(filter);
}

// Handle ipfilter.dat
int trim(char* const data, int start, int end)
{
if (start >= end) return start;
int newStart = start;

for (int i = start; i <= end; ++i) {
if (isspace(data[i]) != 0) {
data[i] = '\0';
}
else {
newStart = i;
break;
}
}

for (int i = end; i >= start; --i) {
if (isspace(data[i]) != 0)
data[i] = '\0';
else
break;
}

return newStart;
}

int findAndNullDelimiter(char *const data, char delimiter, int start, int end)
{
for (int i = start; i <= end; ++i) {
if (data[i] == delimiter) {
data[i] = '\0';
return i;
}
}

return -1;
}

int Session::parseOfflineFilterFile(QString ipDat, libt::ip_filter &filter)
{
int ruleCount = 0;
QFile file(ipDat);
if (!file.exists()) return ruleCount;

if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
LogMsg(tr("I/O Error: Could not open IP filter file in read mode."), Log::CRITICAL);
return ruleCount;
}

std::vector<char> buffer(2 * 1024 * 1024, 0); // seems a bit faster than QVector
qint64 bytesRead = 0;
int offset = 0;
int start = 0;
int endOfLine = -1;
int nbLine = 0;

while (true) {
bytesRead = file.read(buffer.data() + offset, 2 * 1024 * 1024 - offset - 1);
if (bytesRead < 0)
break;
int dataSize = bytesRead + offset;
if (bytesRead == 0 && dataSize == 0)
break;

for (start = 0; start < dataSize; ++start) {
endOfLine = -1;
// The file might have ended without the last line having a newline
if (!(bytesRead == 0 && dataSize > 0)) {
for (int i = start; i < dataSize; ++i) {
if (buffer[i] == '\n') {
endOfLine = i;
// We need to NULL the newline in case the line has only an IP range.
// In that case the parser won't work for the end IP, because it ends
// with the newline and not with a number.
buffer[i] = '\0';
break;
}
}
}
else {
endOfLine = dataSize;
buffer[dataSize] = '\0';
}

if (endOfLine == -1) {
// read the next chunk from file
// but first move(copy) the leftover data to the front of the buffer
offset = dataSize - start;
memmove(buffer.data(), buffer.data() + start, offset);
break;
}
else {
++nbLine;
}

if ((buffer[start] == '#')
|| ((buffer[start] == '/') && ((start + 1 < dataSize) && (buffer[start + 1] == '/')))) {
start = endOfLine;
continue;
}

// Each line should follow this format:
// 001.009.096.105 - 001.009.096.105 , 000 , Some organization
// The 3rd entry is access level and if above 127 the IP range isn't blocked.
int firstComma = findAndNullDelimiter(buffer.data(), ',', start, endOfLine);
if (firstComma != -1)
findAndNullDelimiter(buffer.data(), ',', firstComma + 1, endOfLine);

// Check if there is an access value (apparently not mandatory)
if (firstComma != -1) {
// There is possibly one
const long int nbAccess = strtol(buffer.data() + firstComma + 1, nullptr, 10);
// Ignoring this rule because access value is too high
if (nbAccess > 127L) {
start = endOfLine;
continue;
}
}

// IP Range should be split by a dash
int endOfIPRange = ((firstComma == -1) ? (endOfLine - 1) : (firstComma - 1));
int delimIP = findAndNullDelimiter(buffer.data(), '-', start, endOfIPRange);
if (delimIP == -1) {
start = endOfLine;
continue;
}

boost::system::error_code ec;
int newStart = trim(buffer.data(), start, delimIP - 1);
libt::address startAddr = libt::address::from_string(buffer.data() + newStart, ec);
Q_ASSERT(!ec);
if (ec) {
start = endOfLine;
continue;
}

newStart = trim(buffer.data(), delimIP + 1, endOfIPRange);
libt::address endAddr = libt::address::from_string(buffer.data() + newStart, ec);
Q_ASSERT(!ec);
if (ec) {
start = endOfLine;
continue;
}

if ((startAddr.is_v4() != endAddr.is_v4())
|| (startAddr.is_v6() != endAddr.is_v6())) {
start = endOfLine;
continue;
}

start = endOfLine;

filter.add_rule(startAddr, endAddr, libt::ip_filter::blocked);
++ruleCount;
}

if (start >= dataSize)
offset = 0;
}

return ruleCount;
}

void Session::loadOfflineFilter() {
int Count = 0;
libt::ip_filter offlineFilter = m_nativeSession->get_ip_filter();

#if defined(Q_OS_WIN)
Count = parseOfflineFilterFile("./ipfilter.dat", offlineFilter);
#endif

#if (defined(Q_OS_UNIX) && !defined(Q_OS_MAC))
Count = parseOfflineFilterFile(QDir::home().absoluteFilePath(".config")+"/qBittorrent/ipfilter.dat", offlineFilter);
#endif

m_nativeSession->set_ip_filter(offlineFilter);
Logger::instance()->addMessage(tr("Successfully parsed the offline downloader IP filter: %1 rules were applied.", "%1 is a number").arg(Count));
}

void Session::recursiveTorrentDownload(const InfoHash &hash)
{
TorrentHandle *const torrent = m_torrents.value(hash);
Expand Down Expand Up @@ -3862,6 +4057,7 @@ void Session::handleIPFilterParsed(int ruleCount)
}
Logger::instance()->addMessage(tr("Successfully parsed the provided IP filter: %1 rules were applied.", "%1 is a number").arg(ruleCount));
emit IPFilterParsed(false, ruleCount);
loadOfflineFilter();
}

void Session::handleIPFilterError()
Expand Down
5 changes: 5 additions & 0 deletions src/base/bittorrent/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ namespace BitTorrent
bool isAutoBanUnknownPeerEnabled() const;
void setSaveResumeDataInterval(uint value);
void setAutoBanUnknownPeer(bool value);
bool isShowTrackerAuthWindow() const;
void setShowTrackerAuthWindow(bool value);
int port() const;
void setPort(int port);
bool useRandomPort() const;
Expand Down Expand Up @@ -592,6 +594,8 @@ namespace BitTorrent
void populateAdditionalTrackers();
void enableIPFilter();
void disableIPFilter();
int parseOfflineFilterFile(QString ipDat, libtorrent::ip_filter &filter);
void loadOfflineFilter();

bool addTorrent_impl(AddTorrentData addData, const MagnetUri &magnetUri,
TorrentInfo torrentInfo = TorrentInfo(),
Expand Down Expand Up @@ -697,6 +701,7 @@ namespace BitTorrent
CachedSettingValue<bool> m_isBandwidthSchedulerEnabled;
CachedSettingValue<uint> m_saveResumeDataInterval;
CachedSettingValue<bool> m_autoBanUnknownPeer;
CachedSettingValue<bool> m_showTrackerAuthWindow;
CachedSettingValue<int> m_port;
CachedSettingValue<bool> m_useRandomPort;
CachedSettingValue<QString> m_networkInterface;
Expand Down
3 changes: 2 additions & 1 deletion src/base/bittorrent/torrenthandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,8 @@ void TorrentHandle::handleTrackerErrorAlert(libtorrent::tracker_error_alert *p)
m_trackerInfos[trackerUrl].lastMessage = message;

if (p->status_code == 401)
m_session->handleTorrentTrackerAuthenticationRequired(this, trackerUrl);
if (Preferences::instance()->getShowTrackerAuthWindow())
m_session->handleTorrentTrackerAuthenticationRequired(this, trackerUrl);

m_session->handleTorrentTrackerError(this, trackerUrl);
}
Expand Down
10 changes: 10 additions & 0 deletions src/base/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,16 @@ void Preferences::setAutoBanUnknownPeer(const bool checked)
setValue("Preferences/Advanced/AutoBanUnknownPeer", checked);
}

bool Preferences::getShowTrackerAuthWindow() const
{
return value("Preferences/Advanced/ShowTrackerAuthWindow", true).toBool();
}

void Preferences::setShowTrackerAuthWindow(const bool checked)
{
setValue("Preferences/Advanced/ShowTrackerAuthWindow", checked);
}

// Stuff that don't appear in the Options GUI but are saved
// in the same file.

Expand Down
2 changes: 2 additions & 0 deletions src/base/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ class Preferences: public QObject
#endif
bool getAutoBanUnknownPeer() const;
void setAutoBanUnknownPeer(const bool checked);
bool getShowTrackerAuthWindow() const;
void setShowTrackerAuthWindow(const bool checked);

// Stuff that don't appear in the Options GUI but are saved
// in the same file.
Expand Down
1 change: 1 addition & 0 deletions src/base/settingsstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace
{"BitTorrent/Session/InterfaceAddress", "Preferences/Connection/InterfaceAddress"},
{"BitTorrent/Session/SaveResumeDataInterval", "Preferences/Downloads/SaveResumeDataInterval"},
{"BitTorrent/Session/AutoBanUnknownPeer", "Preferences/Advanced/AutoBanUnknownPeer"},
{"BitTorrent/Session/ShowTrackerAuthWindow", "Preferences/Advanced/ShowTrackerAuthWindow"},
{"BitTorrent/Session/Encryption", "Preferences/Bittorrent/Encryption"},
{"BitTorrent/Session/ForceProxy", "Preferences/Connection/ProxyForce"},
{"BitTorrent/Session/ProxyPeerConnections", "Preferences/Connection/ProxyPeerConnections"},
Expand Down
6 changes: 6 additions & 0 deletions src/gui/advancedsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum AdvSettingsRows
// behavior
SAVE_RESUME_DATA_INTERVAL,
CONFIRM_AUTO_BAN,
SHOW_TRACKER_AUTH_WINDOW,
CONFIRM_RECHECK_TORRENT,
RECHECK_COMPLETED,
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
Expand Down Expand Up @@ -195,6 +196,8 @@ void AdvancedSettings::saveAdvancedSettings()
session->setAnnounceIP(addr.isNull() ? "" : addr.toString());
// Enable Auto ban Unknown Peer
session->setAutoBanUnknownPeer(cb_auto_ban_unknown_peer.isChecked());
// Show Tracker Authenticaion Window
session->setShowTrackerAuthWindow(cb_show_tracker_auth_window.isChecked());

// Program notification
MainWindow * const mainWindow = static_cast<Application*>(QCoreApplication::instance())->mainWindow();
Expand Down Expand Up @@ -418,6 +421,9 @@ void AdvancedSettings::loadAdvancedSettings()
// Auto Ban Unknown Peer from China
cb_auto_ban_unknown_peer.setChecked(session->isAutoBanUnknownPeerEnabled());
addRow(CONFIRM_AUTO_BAN, tr("Auto Ban Unknown Peer from China"), &cb_auto_ban_unknown_peer);
// Show Tracker Authenticaion Window
cb_show_tracker_auth_window.setChecked(session->isShowTrackerAuthWindow());
addRow(SHOW_TRACKER_AUTH_WINDOW, tr("Show Tracker Authenticaion Window"), &cb_show_tracker_auth_window);

// Program notifications
const MainWindow * const mainWindow = static_cast<Application*>(QCoreApplication::instance())->mainWindow();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/advancedsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private slots:
QCheckBox cb_os_cache, cb_recheck_completed, cb_resolve_countries, cb_resolve_hosts, cb_super_seeding,
cb_program_notifications, cb_torrent_added_notifications, cb_tracker_favicon, cb_tracker_status,
cb_confirm_torrent_recheck, cb_confirm_remove_all_tags, cb_listen_ipv6, cb_announce_all_trackers, cb_announce_all_tiers,
cbGuidedReadCache, cbMultiConnectionsPerIp, cbSuggestMode, cb_auto_ban_unknown_peer;
cbGuidedReadCache, cbMultiConnectionsPerIp, cbSuggestMode, cb_auto_ban_unknown_peer, cb_show_tracker_auth_window;
QComboBox combo_iface, combo_iface_address, comboUtpMixedMode, comboChokingAlgorithm, comboSeedChokingAlgorithm;
QLineEdit txtAnnounceIP;

Expand Down
Loading

0 comments on commit 8649b69

Please sign in to comment.