Skip to content

Commit

Permalink
FEATURE: Added UPnP/NAT-PMP port forward for the Web UI port
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Dumez committed Apr 12, 2011
1 parent aec3087 commit da32321
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 57 deletions.
1 change: 1 addition & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* Unreleased - Christophe Dumez <chris@qbittorrent.org> - v2.8.0
- FEATURE: Added support for secure SMTP connection (SSL)
- FEATURE: Added support for SMTP authentication
- FEATURE: Added UPnP/NAT-PMP port forward for the Web UI port
- BUGFIX: Change systray icon on the fly (no restart needed)
- COSMETIC: Added monochrome icon for light themes

Expand Down
99 changes: 52 additions & 47 deletions src/preferences/options.ui
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>503</width>
<width>489</width>
<height>519</height>
</rect>
</property>
Expand Down Expand Up @@ -493,7 +493,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>503</width>
<width>489</width>
<height>849</height>
</rect>
</property>
Expand Down Expand Up @@ -963,7 +963,7 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>503</width>
<width>489</width>
<height>456</height>
</rect>
</property>
Expand Down Expand Up @@ -1406,7 +1406,7 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>518</width>
<width>504</width>
<height>384</height>
</rect>
</property>
Expand Down Expand Up @@ -1784,8 +1784,8 @@ QGroupBox {
<property name="geometry">
<rect>
<x>0</x>
<y>-24</y>
<width>503</width>
<y>0</y>
<width>496</width>
<height>408</height>
</rect>
</property>
Expand Down Expand Up @@ -2161,7 +2161,7 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>518</width>
<width>504</width>
<height>384</height>
</rect>
</property>
Expand All @@ -2179,45 +2179,50 @@ QGroupBox {
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupWebUiServer">
<property name="title">
<string>HTTP Server</string>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="lblWebUiPort">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinWebUiPort">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>8080</number>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>21</width>
<height>29</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="checkWebUIUPnP">
<property name="text">
<string>Use UPnP / NAT-PMP to forward the port from my router</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="lblWebUiPort">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinWebUiPort">
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>8080</number>
</property>
</widget>
</item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>21</width>
<height>29</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
Expand Down Expand Up @@ -2310,8 +2315,8 @@ QGroupBox {
<rect>
<x>0</x>
<y>0</y>
<width>518</width>
<height>384</height>
<width>86</width>
<height>16</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_36"/>
Expand Down
3 changes: 3 additions & 0 deletions src/preferences/options_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ options_imp::options_imp(QWidget *parent):
// Web UI tab
connect(checkWebUi, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
connect(spinWebUiPort, SIGNAL(valueChanged(int)), this, SLOT(enableApplyButton()));
connect(checkWebUIUPnP, SIGNAL(toggled(bool)), SLOT(enableApplyButton()));
connect(textWebUiUsername, SIGNAL(textChanged(QString)), this, SLOT(enableApplyButton()));
connect(textWebUiPassword, SIGNAL(textChanged(QString)), this, SLOT(enableApplyButton()));
connect(checkBypassLocalAuth, SIGNAL(toggled(bool)), this, SLOT(enableApplyButton()));
Expand Down Expand Up @@ -422,6 +423,7 @@ void options_imp::saveOptions(){
if(isWebUiEnabled())
{
pref.setWebUiPort(webUiPort());
pref.setUPnPForWebUIPort(checkWebUIUPnP->isChecked());
pref.setWebUiUsername(webUiUsername());
// FIXME: Check that the password is valid (not empty at least)
pref.setWebUiPassword(webUiPassword());
Expand Down Expand Up @@ -660,6 +662,7 @@ void options_imp::loadOptions(){
// Web UI
checkWebUi->setChecked(pref.isWebUiEnabled());
spinWebUiPort->setValue(pref.getWebUiPort());
checkWebUIUPnP->setChecked(pref.useUPnPForWebUIPort());
textWebUiUsername->setText(pref.getWebUiUsername());
textWebUiPassword->setText(pref.getWebUiPassword());
checkBypassLocalAuth->setChecked(!pref.isWebUiLocalAuthEnabled());
Expand Down
8 changes: 8 additions & 0 deletions src/preferences/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,14 @@ class Preferences : public QIniSettings {
setValue("Preferences/WebUI/Port", port);
}

bool useUPnPForWebUIPort() const {
return value("Preferences/WebUI/UseUPnP", true).toBool();
}

void setUPnPForWebUIPort(bool enabled) {
setValue("Preferences/WebUI/UseUPnP", enabled);
}

QString getWebUiUsername() const {
return value("Preferences/WebUI/Username", "admin").toString();
}
Expand Down
26 changes: 18 additions & 8 deletions src/qtlibtorrent/qbtsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#include <libtorrent/alert_types.hpp>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/version.hpp>
#include <libtorrent/upnp.hpp>
#include <libtorrent/natpmp.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
Expand All @@ -89,13 +91,14 @@ enum VersionType { NORMAL,ALPHA,BETA,RELEASE_CANDIDATE,DEVEL };
QBtSession::QBtSession()
: m_scanFolders(ScanFoldersModel::instance(this)),
preAllocateAll(false), addInPause(false), global_ratio_limit(-1),
UPnPEnabled(false), LSDEnabled(false),
LSDEnabled(false),
DHTEnabled(false), current_dht_port(0), queueingEnabled(false),
torrentExport(false)
#ifndef DISABLE_GUI
, geoipDBLoaded(false), resolve_countries(false)
#endif
, m_tracker(0), m_shutdownAct(NO_SHUTDOWN)
, m_tracker(0), m_shutdownAct(NO_SHUTDOWN),
m_upnp(0), m_natpmp(0)
{
BigRatioTimer = new QTimer(this);
BigRatioTimer->setInterval(10000);
Expand Down Expand Up @@ -1324,19 +1327,26 @@ void QBtSession::setMaxUploadsPerTorrent(int max) {
}

void QBtSession::enableUPnP(bool b) {
Preferences pref;
if(b) {
if(!UPnPEnabled) {
if(!m_upnp) {
qDebug("Enabling UPnP / NAT-PMP");
s->start_upnp();
s->start_natpmp();
UPnPEnabled = true;
m_upnp = s->start_upnp();
m_natpmp = s->start_natpmp();
}
// Use UPnP/NAT-PMP for Web UI too
if(pref.isWebUiEnabled() && pref.useUPnPForWebUIPort()) {
const qint16 port = pref.getWebUiPort();
m_upnp->add_mapping(upnp::tcp, port, port);
m_natpmp->add_mapping(natpmp::tcp, port, port);
}
} else {
if(UPnPEnabled) {
if(m_upnp) {
qDebug("Disabling UPnP / NAT-PMP");
s->stop_upnp();
s->stop_natpmp();
UPnPEnabled = false;
m_upnp = 0;
m_natpmp = 0;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/qtlibtorrent/qbtsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ private slots:
bool addInPause;
qreal global_ratio_limit;
int high_ratio_action;
bool UPnPEnabled;
bool LSDEnabled;
bool DHTEnabled;
int current_dht_port;
Expand Down Expand Up @@ -269,7 +268,9 @@ private slots:
QPointer<QTracker> m_tracker;
TorrentSpeedMonitor *m_speedMonitor;
shutDownAction m_shutdownAct;

// Port forwarding
libtorrent::upnp *m_upnp;
libtorrent::natpmp *m_natpmp;
};

#endif

0 comments on commit da32321

Please sign in to comment.