From 065c89fc30d8ee3dbfb829a826566e76d35f46a1 Mon Sep 17 00:00:00 2001 From: "yuchang.xu" Date: Sun, 27 Sep 2020 14:56:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E4=B8=8D=E8=AF=86?= =?UTF-8?q?=E5=88=AB=20=E5=BC=95=E7=94=A8=E5=A4=B4=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QFtpServer/mainwindow.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/QFtpServer/mainwindow.cpp b/QFtpServer/mainwindow.cpp index ab544a8..228375d 100644 --- a/QFtpServer/mainwindow.cpp +++ b/QFtpServer/mainwindow.cpp @@ -6,6 +6,7 @@ #include #include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) From 161697f7d5fe7423a9329101d03eb61b66f39f93 Mon Sep 17 00:00:00 2001 From: "yuchang.xu" Date: Fri, 10 May 2024 00:00:44 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=BC=E5=AE=B9Qt6?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + QFtpServerCommandLine/main.cpp | 10 ++++++++- QFtpServerLib/ftpcontrolconnection.cpp | 30 ++++++++++++++++++++++---- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 5439c79..88f73e0 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,4 @@ Thumbs.db *.exe +/build diff --git a/QFtpServerCommandLine/main.cpp b/QFtpServerCommandLine/main.cpp index 1d4ca1b..c2435d0 100644 --- a/QFtpServerCommandLine/main.cpp +++ b/QFtpServerCommandLine/main.cpp @@ -3,10 +3,17 @@ #include #include #include +#include QChar getRandomChar() { +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + const int range = 'z'-'a' + 1; + const int randomNumber = QRandomGenerator::global()->bounded(range); + return QChar('a' + randomNumber); +#else return QChar('a' + (qrand()%('z'-'a'))); +#endif } QString getRandomString(int n) @@ -21,9 +28,10 @@ QString getRandomString(int n) int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); - +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) // Seed the random numbers. qsrand(QTime::currentTime().msec()); +#endif const QString &userName = getRandomString(3); const QString &password = getRandomString(3); diff --git a/QFtpServerLib/ftpcontrolconnection.cpp b/QFtpServerLib/ftpcontrolconnection.cpp index 7af56e2..e56d8cc 100644 --- a/QFtpServerLib/ftpcontrolconnection.cpp +++ b/QFtpServerLib/ftpcontrolconnection.cpp @@ -15,6 +15,8 @@ #include #include +#include + FtpControlConnection::FtpControlConnection(QObject *parent, QSslSocket *socket, const QString &rootPath, const QString &userName, const QString &password, bool readOnly) : QObject(parent) { @@ -137,7 +139,7 @@ QString FtpControlConnection::toLocalPath(const QString &fileName) const // Note we do this **before** prepending the root path, in order to avoid // "jailbreaking" out of the "chroot". QStringList components; - foreach (const QString &component, localPath.split('/', QString::SkipEmptyParts)) { + foreach (const QString &component, localPath.split('/', Qt::SkipEmptyParts)) { if (component == "..") { if (!components.isEmpty()) { components.pop_back(); @@ -256,10 +258,30 @@ void FtpControlConnection::port(const QString &addressAndPort) // PORT h1,h2,h3,h4,p1,p2 // Get IP and port. + QString hostName; + int port = 0; +#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) + QRegularExpression exp("\\s*(\\d+,\\d+,\\d+,\\d+),(\\d+),(\\d+)"); + auto match = exp.match(addressAndPort); + if(match.hasMatch()) { + hostName = match.captured(1).replace(',', '.'); + int h = match.captured(2).toInt(); + int l = match.captured(3).toInt(); + port = h * 256 + l; + } +#else QRegExp exp("\\s*(\\d+,\\d+,\\d+,\\d+),(\\d+),(\\d+)"); - exp.indexIn(addressAndPort); - QString hostName = exp.cap(1).replace(',', '.'); - int port = exp.cap(2).toInt() * 256 + exp.cap(3).toInt(); + if (exp.indexIn(addressAndPort) != -1) { + QString hostName = exp.cap(1).replace(',', '.'); + int h = exp.cap(2).toInt(); + int l = exp.cap(3).toInt(); + port = h * 256 + l; + } +#endif + if(hostName.isEmpty()) { + reply("address or port invalid!!!"); + return; + } dataConnection->scheduleConnectToHost(hostName, port, encryptDataConnection); reply("200 Command okay."); }