Skip to content

Commit e603079

Browse files
committed
Plugins::FtpServer: add listen at network interface
1 parent 2499fa8 commit e603079

8 files changed

Lines changed: 158 additions & 43 deletions

File tree

Plugins/FtpServer/BackendFtpServer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ CBackend::OnInitReturnValue CBackendFtpServer::OnInit()
4141
szUser = m_Para->GetUser();
4242
szPassword = m_Para->GetPassword();
4343
}
44-
m_pServer = new FtpServer(this, m_Para->GetRoot(), m_Para->GetPort(),
44+
m_pServer = new CFtpServer(this, m_Para->GetRoot(), m_Para->GetPort(),
4545
szUser, szPassword,
46-
m_Para->GetReadOnly(), false);
46+
m_Para->GetReadOnly());
4747
m_pServer->SetFilter(this);
48-
if(m_pServer->isListening()) {
48+
bool bListen = false;
49+
if(m_Para->GetListenAll())
50+
bListen = m_pServer->Listening();
51+
else {
52+
foreach (auto a, m_Para->GetListen()) {
53+
QHostAddress addr(a);
54+
bListen = m_pServer->Listening(addr);
55+
}
56+
}
57+
if(bListen) {
4958
qInfo(log) << "The ftp server listen in" << m_Para->GetPort();
5059
} else {
5160
QString szErr = tr("The ftp server is not listening in %1").arg(m_Para->GetPort());

Plugins/FtpServer/BackendFtpServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private Q_SLOTS:
3232
void slotDisconnected();
3333

3434
private:
35-
FtpServer* m_pServer;
35+
CFtpServer* m_pServer;
3636
QSharedPointer<CParameterFtpServer> m_Para;
3737
QList<QSslSocket*> m_Sockets;
3838
int m_nTotal;

Plugins/FtpServer/DlgSettings.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
#include <QFileDialog>
55
#include <QLoggingCategory>
6+
#include <QStandardItem>
7+
#include <QNetworkInterface>
8+
69
#include "DlgSettings.h"
710
#include "ui_DlgSettings.h"
11+
812
static Q_LOGGING_CATEGORY(log, "FtpServer.DlgSettings")
913
CDlgSettings::CDlgSettings(QSharedPointer<CParameterFtpServer> para, QWidget *parent)
1014
: QDialog(parent)
@@ -22,6 +26,33 @@ CDlgSettings::CDlgSettings(QSharedPointer<CParameterFtpServer> para, QWidget *pa
2226
ui->cbReadOnly->setChecked(m_Para->GetReadOnly());
2327
ui->sbConnectCount->setValue(m_Para->GetConnectCount());
2428
ui->sbConnectCount->setToolTip(tr("-1: Enable all\n 0: Disable all\n>0: Connect count"));
29+
ui->cbListenAll->setChecked(m_Para->GetListenAll());
30+
ui->lvListen->setModel(&m_ModelNetWorkInterface);
31+
foreach(auto iface, QNetworkInterface::allInterfaces()) {
32+
qDebug(log) << iface;
33+
auto entry = iface.addressEntries();
34+
if(iface.flags() & QNetworkInterface::IsLoopBack)
35+
continue;
36+
/*if(!(iface.flags() & QNetworkInterface::CanBroadcast))
37+
continue;//*/
38+
foreach(auto e, entry) {
39+
if(!e.broadcast().isNull()) {
40+
QStandardItem* item = new QStandardItem(e.ip().toString());
41+
item->setCheckable(true);
42+
m_ModelNetWorkInterface.appendRow(item);
43+
}
44+
}
45+
}
46+
foreach(auto ip, m_Para->GetListen()) {
47+
for (int row = 0; row < m_ModelNetWorkInterface.rowCount(); row++) {
48+
QModelIndex index = m_ModelNetWorkInterface.index(row, 0);
49+
QString szIp = m_ModelNetWorkInterface.data(index).toString();
50+
if (szIp == ip) {
51+
m_ModelNetWorkInterface.item(row)->setCheckState(Qt::Checked);
52+
break;
53+
}
54+
}
55+
}
2556
}
2657

2758
CDlgSettings::~CDlgSettings()
@@ -46,6 +77,15 @@ void CDlgSettings::accept()
4677
m_Para->SetAnonymousLogin(ui->cbAnonmousLogin->isChecked());
4778
m_Para->SetReadOnly(ui->cbReadOnly->isChecked());
4879
m_Para->SetConnectCount(ui->sbConnectCount->value());
80+
m_Para->SetListenAll(ui->cbListenAll->isChecked());
81+
QStringList lstInterface;
82+
for (int row = 0; row < m_ModelNetWorkInterface.rowCount(); row++) {
83+
auto item = m_ModelNetWorkInterface.item(row);
84+
if(item->checkState() == Qt::Checked) {
85+
lstInterface << item->text();
86+
}
87+
}
88+
m_Para->SetListen(lstInterface);
4989
QDialog::accept();
5090
}
5191

@@ -56,3 +96,8 @@ void CDlgSettings::on_cbAnonmousLogin_checkStateChanged(const Qt::CheckState &ar
5696
ui->leUser->setEnabled(bEnable);
5797
}
5898

99+
void CDlgSettings::on_cbListenAll_checkStateChanged(const Qt::CheckState &arg1)
100+
{
101+
ui->lvListen->setEnabled(Qt::Checked != arg1);
102+
}
103+

Plugins/FtpServer/DlgSettings.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
// Author: Kang Lin <kl222@126.com>
33

44
#pragma once
5+
56
#include <QDialog>
7+
#include <QStandardItemModel>
68
#include "ParameterFtpServer.h"
79

810
namespace Ui {
@@ -24,7 +26,10 @@ private slots:
2426

2527
void on_cbAnonmousLogin_checkStateChanged(const Qt::CheckState &arg1);
2628

29+
void on_cbListenAll_checkStateChanged(const Qt::CheckState &arg1);
30+
2731
private:
2832
Ui::CDlgSettings *ui;
2933
QSharedPointer<CParameterFtpServer> m_Para;
34+
QStandardItemModel m_ModelNetWorkInterface;
3035
};

Plugins/FtpServer/DlgSettings.ui

Lines changed: 52 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,31 @@
1414
<string>Dialog</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout">
17-
<item row="1" column="0">
18-
<layout class="QHBoxLayout" name="horizontalLayout_7">
17+
<item row="8" column="0">
18+
<widget class="QDialogButtonBox" name="buttonBox">
19+
<property name="orientation">
20+
<enum>Qt::Orientation::Horizontal</enum>
21+
</property>
22+
<property name="standardButtons">
23+
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
24+
</property>
25+
</widget>
26+
</item>
27+
<item row="4" column="0">
28+
<layout class="QHBoxLayout" name="horizontalLayout_10">
1929
<item>
20-
<widget class="QLabel" name="label_6">
30+
<widget class="QCheckBox" name="cbAnonmousLogin">
2131
<property name="text">
22-
<string>User name:</string>
32+
<string>Aollow anonymous login</string>
2333
</property>
2434
</widget>
2535
</item>
2636
<item>
27-
<widget class="QLineEdit" name="leUser"/>
37+
<widget class="QCheckBox" name="cbReadOnly">
38+
<property name="text">
39+
<string>Read-only</string>
40+
</property>
41+
</widget>
2842
</item>
2943
</layout>
3044
</item>
@@ -49,30 +63,6 @@
4963
</item>
5064
</layout>
5165
</item>
52-
<item row="6" column="0">
53-
<widget class="QDialogButtonBox" name="buttonBox">
54-
<property name="orientation">
55-
<enum>Qt::Orientation::Horizontal</enum>
56-
</property>
57-
<property name="standardButtons">
58-
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
59-
</property>
60-
</widget>
61-
</item>
62-
<item row="2" column="0">
63-
<layout class="QHBoxLayout" name="horizontalLayout_8">
64-
<item>
65-
<widget class="QLabel" name="label_7">
66-
<property name="text">
67-
<string>Password:</string>
68-
</property>
69-
</widget>
70-
</item>
71-
<item>
72-
<widget class="QLineEdit" name="lePassword"/>
73-
</item>
74-
</layout>
75-
</item>
7666
<item row="0" column="0">
7767
<layout class="QHBoxLayout" name="horizontalLayout_5">
7868
<item>
@@ -114,25 +104,28 @@
114104
</item>
115105
</layout>
116106
</item>
117-
<item row="4" column="0">
118-
<layout class="QHBoxLayout" name="horizontalLayout_10">
107+
<item row="1" column="0">
108+
<layout class="QHBoxLayout" name="horizontalLayout_7">
119109
<item>
120-
<widget class="QCheckBox" name="cbAnonmousLogin">
110+
<widget class="QLabel" name="label_6">
121111
<property name="text">
122-
<string>Aollow anonymous login</string>
112+
<string>User name:</string>
123113
</property>
124114
</widget>
125115
</item>
126116
<item>
127-
<widget class="QCheckBox" name="cbReadOnly">
128-
<property name="text">
129-
<string>Read-only</string>
130-
</property>
131-
</widget>
117+
<widget class="QLineEdit" name="leUser"/>
132118
</item>
133119
</layout>
134120
</item>
135121
<item row="5" column="0">
122+
<widget class="QCheckBox" name="cbListenAll">
123+
<property name="text">
124+
<string>Enable listen at all network interface</string>
125+
</property>
126+
</widget>
127+
</item>
128+
<item row="7" column="0">
136129
<spacer name="verticalSpacer">
137130
<property name="orientation">
138131
<enum>Qt::Orientation::Vertical</enum>
@@ -145,6 +138,27 @@
145138
</property>
146139
</spacer>
147140
</item>
141+
<item row="2" column="0">
142+
<layout class="QHBoxLayout" name="horizontalLayout_8">
143+
<item>
144+
<widget class="QLabel" name="label_7">
145+
<property name="text">
146+
<string>Password:</string>
147+
</property>
148+
</widget>
149+
</item>
150+
<item>
151+
<widget class="QLineEdit" name="lePassword"/>
152+
</item>
153+
</layout>
154+
</item>
155+
<item row="6" column="0">
156+
<widget class="QListView" name="lvListen">
157+
<property name="editTriggers">
158+
<set>QAbstractItemView::EditTrigger::NoEditTriggers</set>
159+
</property>
160+
</widget>
161+
</item>
148162
</layout>
149163
</widget>
150164
<resources/>

Plugins/FtpServer/OperateFtpServer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,24 @@ int COperateFtpServer::Initial()
9595
{
9696
qDebug(log) << Q_FUNC_INFO;
9797
int nRet = 0;
98+
bool check = false;
9899

99100
m_Para = QSharedPointer<CParameterFtpServer>(new CParameterFtpServer());
100101
if(!m_Para)
101102
return -1;
102103

104+
check = connect(m_Para.get(), &CParameter::sigChanged,
105+
this, [&](){
106+
emit this->sigUpdateParameters(this);
107+
});
108+
Q_ASSERT(check);
109+
103110
nRet = COperate::Initial();
104111
if(nRet)
105112
return nRet;
106113

107114
m_pStart = m_Menu.addAction(QIcon::fromTheme("media-playback-start"), tr("Start server"));
108-
bool check = connect(m_pStart, &QAction::toggled, this, &COperateFtpServer::slotStart);
115+
check = connect(m_pStart, &QAction::toggled, this, &COperateFtpServer::slotStart);
109116
Q_ASSERT(check);
110117
m_pStart->setCheckable(true);
111118
m_pStart->setToolTip(m_pStart->text());

Plugins/FtpServer/ParameterFtpServer.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CParameterFtpServer::CParameterFtpServer(QObject *parent, const QString &szPrefi
99
, m_bAnonymousLogin(true)
1010
, m_bReadOnly(true)
1111
, m_ConnectCount(-1)
12+
, m_bListenAll(true)
1213
{}
1314

1415
uint16_t CParameterFtpServer::GetPort() const
@@ -81,6 +82,26 @@ void CParameterFtpServer::SetConnectCount(int newConnectCount)
8182
m_ConnectCount = newConnectCount;
8283
}
8384

85+
bool CParameterFtpServer::GetListenAll() const
86+
{
87+
return m_bListenAll;
88+
}
89+
90+
void CParameterFtpServer::SetListenAll(bool newListenAll)
91+
{
92+
m_bListenAll = newListenAll;
93+
}
94+
95+
QStringList CParameterFtpServer::GetListen() const
96+
{
97+
return m_Listen;
98+
}
99+
100+
void CParameterFtpServer::SetListen(const QStringList &newListen)
101+
{
102+
m_Listen = newListen;
103+
}
104+
84105
int CParameterFtpServer::OnLoad(QSettings &set)
85106
{
86107
SetPort(set.value("Port", GetPort()).toUInt());
@@ -89,6 +110,9 @@ int CParameterFtpServer::OnLoad(QSettings &set)
89110
SetRoot(set.value("Root", GetRoot()).toString());
90111
SetAnonymousLogin(set.value("AnonemousLogin", GetAnonymousLogin()).toBool());
91112
SetReadOnly(set.value("ReadOnly", GetReadOnly()).toBool());
113+
SetConnectCount(set.value("ConnectCount", GetConnectCount()).toInt());
114+
SetListenAll(set.value("ListenAll", GetListenAll()).toBool());
115+
SetListen(set.value("Listen", GetListen()).toStringList());
92116
return 0;
93117
}
94118

@@ -100,5 +124,8 @@ int CParameterFtpServer::OnSave(QSettings &set)
100124
set.setValue("Root", GetRoot());
101125
set.setValue("AnonemousLogin", GetAnonymousLogin());
102126
set.setValue("ReadOnly", GetReadOnly());
127+
set.setValue("ConnectCount", GetConnectCount());
128+
set.setValue("ListenAll", GetListenAll());
129+
set.setValue("Listen", GetListen());
103130
return 0;
104131
}

Plugins/FtpServer/ParameterFtpServer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ class CParameterFtpServer : public CParameterOperate
3434
int GetConnectCount() const;
3535
void SetConnectCount(int newConnectCount);
3636

37+
bool GetListenAll() const;
38+
void SetListenAll(bool newListenAll);
39+
40+
QStringList GetListen() const;
41+
void SetListen(const QStringList &newListen);
42+
3743
private:
3844
uint16_t m_nPort;
3945
QString m_szUser;
@@ -42,6 +48,8 @@ class CParameterFtpServer : public CParameterOperate
4248
bool m_bAnonymousLogin;
4349
bool m_bReadOnly;
4450
int m_ConnectCount;
51+
bool m_bListenAll;
52+
QStringList m_Listen;
4553

4654
// CParameter interface
4755
protected:

0 commit comments

Comments
 (0)