diff --git a/src/preferences/options.ui b/src/preferences/options.ui
index 470d54b47..09f4f0721 100755
--- a/src/preferences/options.ui
+++ b/src/preferences/options.ui
@@ -6,7 +6,7 @@
0
0
- 789
+ 779
591
@@ -177,8 +177,8 @@
0
-161
- 486
- 638
+ 484
+ 693
@@ -262,6 +262,16 @@
+ -
+
+
+ Use random port to communicate
+
+
+ false
+
+
+
-
@@ -516,8 +526,8 @@
0
0
- 482
- 1025
+ 501
+ 930
@@ -735,6 +745,15 @@
false
+
+ false
+
+
+ true
+
+
+ 80
+
-
@@ -1022,8 +1041,8 @@
0
0
- 474
- 577
+ 451
+ 524
@@ -1466,8 +1485,8 @@
0
0
- 359
- 480
+ 398
+ 458
@@ -1741,6 +1760,12 @@
-
+
+ hh:mm
+
+
+ false
+
-
- hh:mm
-
-
- false
-
-
@@ -1768,6 +1787,9 @@
-
+
+ hh:mm
+
-
- hh:mm
-
-
@@ -1878,8 +1897,8 @@
0
0
- 557
- 555
+ 546
+ 524
@@ -2292,8 +2311,8 @@
0
0
- 426
- 611
+ 436
+ 553
diff --git a/src/preferences/options_imp.cpp b/src/preferences/options_imp.cpp
index 46f0602a1..086658c2e 100755
--- a/src/preferences/options_imp.cpp
+++ b/src/preferences/options_imp.cpp
@@ -138,6 +138,8 @@ options_imp::options_imp(QWidget *parent):
// General tab
connect(comboI18n, SIGNAL(currentIndexChanged(int)), this, SLOT(enableApplyButton()));
connect(checkAltRowColors, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
+ //add checkbox for random port
+ connect(checkRandomPort, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(checkShowSystray, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(checkCloseToSystray, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(checkMinimizeToSysTray, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
@@ -367,6 +369,8 @@ void options_imp::saveOptions() {
// General preferences
pref.setLocale(locale);
pref.setAlternatingRowColors(checkAltRowColors->isChecked());
+ //set random port
+ pref.setRandomPort(checkRandomPort->isChecked());
pref.setSystrayIntegration(systrayIntegration());
pref.setTrayIconStyle(TrayIcon::Style(comboTrayIcon->currentIndex()));
pref.setCloseToTray(closeToTray());
@@ -540,6 +544,8 @@ void options_imp::loadOptions() {
const Preferences pref;
setLocale(pref.getLocale());
checkAltRowColors->setChecked(pref.useAlternatingRowColors());
+ //get random port
+ checkRandomPort->setChecked(pref.useRandomPort());
checkShowSystray->setChecked(pref.systrayIntegration());
checkShowSplash->setChecked(!pref.isSlashScreenDisabled());
if (checkShowSystray->isChecked()) {
@@ -792,6 +798,7 @@ void options_imp::on_randomButton_clicked() {
spinPort->setValue(rand() % 64512 + 1024);
}
+
int options_imp::getEncryptionSetting() const {
return comboEncryption->currentIndex();
}
diff --git a/src/preferences/preferences.h b/src/preferences/preferences.h
index dc7e41769..d13ffd9c2 100755
--- a/src/preferences/preferences.h
+++ b/src/preferences/preferences.h
@@ -78,6 +78,7 @@ public:
}
// General options
+
QString getLocale() const {
return value(QString::fromUtf8("Preferences/General/Locale"), "en_GB").toString();
}
@@ -85,7 +86,7 @@ public:
void setLocale(const QString &locale) {
setValue(QString::fromUtf8("Preferences/General/Locale"), locale);
}
-
+
bool useProgramNotification() const {
return value(QString::fromUtf8("Preferences/General/ProgramNotification"), true).toBool();
}
@@ -126,6 +127,14 @@ public:
setValue("Preferences/General/AlternatingRowColors", b);
}
+ bool useRandomPort() const {
+ return value(QString::fromUtf8("Preferences/General/RandomPort"), true).toBool();
+ }
+
+ void setRandomPort(bool b) {
+ setValue("Preferences/General/RandomPort", b);
+ }
+
bool systrayIntegration() const {
#ifdef Q_WS_MAC
return false;
diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp
index c88f85262..ff6e572ec 100755
--- a/src/qtlibtorrent/qbtsession.cpp
+++ b/src/qtlibtorrent/qbtsession.cpp
@@ -115,6 +115,7 @@ QBtSession::QBtSession()
, m_tracker(0), m_shutdownAct(NO_SHUTDOWN),
m_upnp(0), m_natpmp(0), m_dynDNSUpdater(0)
{
+ setRandomPortset(false);
BigRatioTimer = new QTimer(this);
BigRatioTimer->setInterval(10000);
connect(BigRatioTimer, SIGNAL(timeout()), SLOT(processBigRatios()));
@@ -281,17 +282,32 @@ void QBtSession::setQueueingEnabled(bool enable) {
}
}
+void QBtSession::setRandomPortset(bool set) {
+ randomPortSet = set;
+}
+
// Set BT session configuration
void QBtSession::configureSession() {
qDebug("Configuring session");
- const Preferences pref;
- // * Ports binding
+ //removed the constant modifier for Preferences
+ Preferences pref;
const unsigned short old_listenPort = getListenPort();
const unsigned short new_listenPort = pref.getSessionPort();
- if (old_listenPort != new_listenPort) {
- qDebug("Session port changes in program preferences: %d -> %d", old_listenPort, new_listenPort);
- setListeningPort(new_listenPort);
+ if(pref.useRandomPort() && !isRandomPortset()) { // to check if the randomPort checkbox is selected
+ setRandomPortset(true);
+ srand(time(0));
+ const unsigned short randomPort = rand() % USHRT_MAX + 1025;
+ setListeningPort(randomPort);
addConsoleMessage(tr("qBittorrent is bound to port: TCP/%1", "e.g: qBittorrent is bound to port: 6881").arg(QString::number(getListenPort())));
+ pref.setSessionPort(randomPort);
+ } else {
+ // * Ports binding
+
+ if (old_listenPort != new_listenPort) {
+ qDebug("Session port changes in program preferences: %d -> %d", old_listenPort, new_listenPort);
+ setListeningPort(new_listenPort);
+ addConsoleMessage(tr("qBittorrent is bound to port: TCP/%1", "e.g: qBittorrent is bound to port: 6881").arg(QString::number(getListenPort())));
+ }
}
// Downloads
@@ -1519,7 +1535,7 @@ void QBtSession::loadSessionState() {
// bdecode
lazy_entry e;
#if LIBTORRENT_VERSION_NUM >= 001600
- libtorrent::error_code ec;
+ error_code ec;
lazy_bdecode(&in[0], &in[0] + in.size(), e, ec);
if (!ec) {
#else
@@ -1903,7 +1919,7 @@ void QBtSession::setListeningPort(int port) {
Preferences pref;
std::pair ports(port, port);
#if LIBTORRENT_VERSION_NUM >= 001600
- libtorrent::error_code ec;
+ error_code ec;
#endif
const QString iface_name = pref.getNetworkInterface();
if (iface_name.isEmpty()) {
diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h
index 7a20ab7c8..44c578571 100755
--- a/src/qtlibtorrent/qbtsession.h
+++ b/src/qtlibtorrent/qbtsession.h
@@ -107,6 +107,8 @@ public:
inline bool isLSDEnabled() const { return LSDEnabled; }
inline bool isPexEnabled() const { return PeXEnabled; }
inline bool isQueueingEnabled() const { return queueingEnabled; }
+ bool isRandomPortset() { return randomPortSet; };
+ void setRandomPortset(bool set);
public slots:
QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false);
@@ -257,6 +259,7 @@ private:
bool m_torrentExportEnabled;
bool m_finishedTorrentExportEnabled;
bool appendqBExtension;
+ bool randomPortSet;
QString defaultSavePath;
QString defaultTempPath;
// IP filtering