handle https and use it by default

This commit is contained in:
2026-05-26 17:16:04 +08:00
parent 5922ff4ca5
commit d9751a82ff
4 changed files with 55 additions and 33 deletions

View File

@@ -27,6 +27,7 @@ AppController::~AppController()
void AppController::initialize() void AppController::initialize()
{ {
m_security->initialize(); m_security->initialize();
m_server->setSslConfiguration(m_security->sslConfiguration());
LocalSend::InfoDto info = buildInfoDto(); LocalSend::InfoDto info = buildInfoDto();
m_server->setLocalInfo(info, m_security->fingerprint()); m_server->setLocalInfo(info, m_security->fingerprint());

View File

@@ -22,6 +22,12 @@ namespace ApiRoute {
constexpr const char* UPLOAD = "/api/localsend/v2/upload"; constexpr const char* UPLOAD = "/api/localsend/v2/upload";
constexpr const char* CANCEL = "/api/localsend/v2/cancel"; constexpr const char* CANCEL = "/api/localsend/v2/cancel";
constexpr const char* SHOW = "/api/localsend/v2/show"; constexpr const char* SHOW = "/api/localsend/v2/show";
constexpr const char* V1_INFO = "/api/localsend/v1/info";
constexpr const char* V1_REGISTER = "/api/localsend/v1/register";
constexpr const char* V1_PREPARE_UPLOAD = "/api/localsend/v1/send-request";
constexpr const char* V1_UPLOAD = "/api/localsend/v1/send";
constexpr const char* V1_CANCEL = "/api/localsend/v1/cancel";
} }
} }

View File

@@ -4,6 +4,7 @@
#ifdef HAS_QTHTTPSERVER #ifdef HAS_QTHTTPSERVER
#include <QTcpServer> #include <QTcpServer>
#include <QSslServer>
#include <QJsonDocument> #include <QJsonDocument>
#include <QEventLoop> #include <QEventLoop>
#include <QUuid> #include <QUuid>
@@ -77,21 +78,32 @@ bool HttpServer::checkPin(const QHttpServerRequest& request, const QHostAddress&
bool HttpServer::start(quint16 port, bool https) bool HttpServer::start(quint16 port, bool https)
{ {
Q_UNUSED(https)
if (m_tcpServer && m_tcpServer->isListening()) { if (m_tcpServer && m_tcpServer->isListening()) {
stop(); stop();
} }
m_port = port; m_port = port;
m_tcpServer = new QTcpServer(this); if (https && !m_sslConfig.isNull()) {
if (!m_tcpServer->listen(QHostAddress::Any, port)) { auto* sslServer = new QSslServer(this);
delete m_tcpServer; sslServer->setSslConfiguration(m_sslConfig);
m_tcpServer = nullptr; if (!sslServer->listen(QHostAddress::Any, port)) {
return false; delete sslServer;
return false;
}
m_tcpServer = sslServer;
m_server->bind(m_tcpServer);
qDebug() << "[HttpServer] Listening on HTTPS port:" << port;
} else {
m_tcpServer = new QTcpServer(this);
if (!m_tcpServer->listen(QHostAddress::Any, port)) {
delete m_tcpServer;
m_tcpServer = nullptr;
return false;
}
m_server->bind(m_tcpServer);
qDebug() << "[HttpServer] Listening on HTTP port:" << port;
} }
m_server->bind(m_tcpServer);
return true; return true;
} }
@@ -165,30 +177,33 @@ void HttpServer::respondToPrepareUpload(const QString& sessionId, bool accepted,
void HttpServer::setupRoutes() void HttpServer::setupRoutes()
{ {
m_server->route(ApiRoute::INFO, QHttpServerRequest::Method::Get, auto infoHandler = [this](const QHttpServerRequest&) {
[this](const QHttpServerRequest&) { return handleInfoRequest();
return handleInfoRequest(); };
}); auto registerHandler = [this](const QHttpServerRequest& request) {
return handleRegisterRequest(request, request.remoteAddress());
m_server->route(ApiRoute::REGISTER, QHttpServerRequest::Method::Post, };
[this](const QHttpServerRequest& request) { auto prepareUploadHandler = [this](const QHttpServerRequest& request) {
return handleRegisterRequest(request, request.remoteAddress()); return handlePrepareUploadRequest(request);
}); };
auto uploadHandler = [this](const QHttpServerRequest& request) {
m_server->route(ApiRoute::PREPARE_UPLOAD, QHttpServerRequest::Method::Post, return handleUploadRequest(request);
[this](const QHttpServerRequest& request) { };
return handlePrepareUploadRequest(request); auto cancelHandler = [this](const QHttpServerRequest& request) {
}); return handleCancelRequest(request);
};
m_server->route(ApiRoute::UPLOAD, QHttpServerRequest::Method::Post,
[this](const QHttpServerRequest& request) { m_server->route(ApiRoute::INFO, QHttpServerRequest::Method::Get, infoHandler);
return handleUploadRequest(request); m_server->route(ApiRoute::REGISTER, QHttpServerRequest::Method::Post, registerHandler);
}); m_server->route(ApiRoute::PREPARE_UPLOAD, QHttpServerRequest::Method::Post, prepareUploadHandler);
m_server->route(ApiRoute::UPLOAD, QHttpServerRequest::Method::Post, uploadHandler);
m_server->route(ApiRoute::CANCEL, QHttpServerRequest::Method::Post, m_server->route(ApiRoute::CANCEL, QHttpServerRequest::Method::Post, cancelHandler);
[this](const QHttpServerRequest& request) {
return handleCancelRequest(request); m_server->route(ApiRoute::V1_INFO, QHttpServerRequest::Method::Get, infoHandler);
}); m_server->route(ApiRoute::V1_REGISTER, QHttpServerRequest::Method::Post, registerHandler);
m_server->route(ApiRoute::V1_PREPARE_UPLOAD, QHttpServerRequest::Method::Post, prepareUploadHandler);
m_server->route(ApiRoute::V1_UPLOAD, QHttpServerRequest::Method::Post, uploadHandler);
m_server->route(ApiRoute::V1_CANCEL, QHttpServerRequest::Method::Post, cancelHandler);
} }
QHttpServerResponse HttpServer::handleInfoRequest() QHttpServerResponse HttpServer::handleInfoRequest()

View File

@@ -53,7 +53,7 @@ void Settings::setPort(quint16 port)
bool Settings::https() const bool Settings::https() const
{ {
return m_settings.value(QStringLiteral("https"), false).toBool(); return m_settings.value(QStringLiteral("https"), true).toBool();
} }
void Settings::setHttps(bool enabled) void Settings::setHttps(bool enabled)