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()
{
m_security->initialize();
m_server->setSslConfiguration(m_security->sslConfiguration());
LocalSend::InfoDto info = buildInfoDto();
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* CANCEL = "/api/localsend/v2/cancel";
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
#include <QTcpServer>
#include <QSslServer>
#include <QJsonDocument>
#include <QEventLoop>
#include <QUuid>
@@ -77,14 +78,23 @@ bool HttpServer::checkPin(const QHttpServerRequest& request, const QHostAddress&
bool HttpServer::start(quint16 port, bool https)
{
Q_UNUSED(https)
if (m_tcpServer && m_tcpServer->isListening()) {
stop();
}
m_port = port;
if (https && !m_sslConfig.isNull()) {
auto* sslServer = new QSslServer(this);
sslServer->setSslConfiguration(m_sslConfig);
if (!sslServer->listen(QHostAddress::Any, port)) {
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;
@@ -92,6 +102,8 @@ bool HttpServer::start(quint16 port, bool https)
return false;
}
m_server->bind(m_tcpServer);
qDebug() << "[HttpServer] Listening on HTTP port:" << port;
}
return true;
}
@@ -165,30 +177,33 @@ void HttpServer::respondToPrepareUpload(const QString& sessionId, bool accepted,
void HttpServer::setupRoutes()
{
m_server->route(ApiRoute::INFO, QHttpServerRequest::Method::Get,
[this](const QHttpServerRequest&) {
auto infoHandler = [this](const QHttpServerRequest&) {
return handleInfoRequest();
});
m_server->route(ApiRoute::REGISTER, QHttpServerRequest::Method::Post,
[this](const QHttpServerRequest& request) {
};
auto registerHandler = [this](const QHttpServerRequest& request) {
return handleRegisterRequest(request, request.remoteAddress());
});
m_server->route(ApiRoute::PREPARE_UPLOAD, QHttpServerRequest::Method::Post,
[this](const QHttpServerRequest& request) {
};
auto prepareUploadHandler = [this](const QHttpServerRequest& request) {
return handlePrepareUploadRequest(request);
});
m_server->route(ApiRoute::UPLOAD, QHttpServerRequest::Method::Post,
[this](const QHttpServerRequest& request) {
};
auto uploadHandler = [this](const QHttpServerRequest& request) {
return handleUploadRequest(request);
});
m_server->route(ApiRoute::CANCEL, QHttpServerRequest::Method::Post,
[this](const QHttpServerRequest& request) {
};
auto cancelHandler = [this](const QHttpServerRequest& request) {
return handleCancelRequest(request);
});
};
m_server->route(ApiRoute::INFO, QHttpServerRequest::Method::Get, infoHandler);
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, cancelHandler);
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()

View File

@@ -53,7 +53,7 @@ void Settings::setPort(quint16 port)
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)