* @brief Sets whether the server accepts connections from external hosts. */
| 469 | * @brief Sets whether the server accepts connections from external hosts. |
| 470 | */ |
| 471 | void API::Server::setExternalConnections(const bool enabled) |
| 472 | { |
| 473 | if (m_externalConnections == enabled) |
| 474 | return; |
| 475 | |
| 476 | if (enabled) { |
| 477 | const int result = Misc::Utilities::showMessageBox( |
| 478 | tr("Allow External API Connections?"), |
| 479 | tr("Exposing the API server to external hosts allows other devices on your " |
| 480 | "network to connect to Serial Studio on port 7777.\n\n" |
| 481 | "Only enable this on trusted networks. " |
| 482 | "Untrusted clients may read live data or send commands to your device."), |
| 483 | QMessageBox::Warning, |
| 484 | QString(), |
| 485 | QMessageBox::Yes | QMessageBox::No, |
| 486 | QMessageBox::No); |
| 487 | |
| 488 | if (result == QMessageBox::No) { |
| 489 | m_externalConnections = false; |
| 490 | m_settings.setValue("API/ExternalConnections", false); |
| 491 | Q_EMIT externalConnectionsChanged(); |
| 492 | return; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | m_externalConnections = enabled; |
| 497 | m_settings.setValue("API/ExternalConnections", m_externalConnections); |
| 498 | Q_EMIT externalConnectionsChanged(); |
| 499 | |
| 500 | if (m_externalConnections) |
| 501 | ensureAuthToken(); |
| 502 | |
| 503 | if (m_enabled) { |
| 504 | m_server.close(); |
| 505 | m_connections.clear(); |
| 506 | auto* worker = static_cast<ServerWorker*>(m_worker); |
| 507 | QMetaObject::invokeMethod(worker, "closeResources", Qt::QueuedConnection); |
| 508 | |
| 509 | const auto address = m_externalConnections ? QHostAddress::Any : QHostAddress::LocalHost; |
| 510 | m_server.setMaxPendingConnections(kMaxApiClients); |
| 511 | if (!m_server.listen(address, API_TCP_PORT)) { |
| 512 | Misc::Utilities::showMessageBox( |
| 513 | tr("Unable to restart API TCP server"), m_server.errorString(), QMessageBox::Warning); |
| 514 | m_server.close(); |
| 515 | m_enabled = false; |
| 516 | Q_EMIT enabledChanged(); |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | //-------------------------------------------------------------------------------------------------- |
| 522 | // Server: authentication (external connections) |
nothing calls this directly
no test coverage detected