From 5d3d4bdbd944b2111d21812695fc02921ad186d1 Mon Sep 17 00:00:00 2001 From: Brad Hefta-Gaub Date: Wed, 21 Sep 2016 18:01:08 -0700 Subject: [PATCH] add support for redirection on domain at max capacity --- .../resources/describe-settings.json | 8 ++++ domain-server/src/DomainGatekeeper.cpp | 41 +++++++++++++------ domain-server/src/DomainGatekeeper.h | 3 +- interface/src/Application.cpp | 11 ++++- interface/src/Application.h | 2 +- .../src/scripting/WindowScriptingInterface.h | 2 +- libraries/networking/src/DomainHandler.cpp | 9 +++- libraries/networking/src/DomainHandler.h | 2 +- .../networking/src/udt/PacketHeaders.cpp | 2 +- libraries/networking/src/udt/PacketHeaders.h | 3 +- 10 files changed, 61 insertions(+), 22 deletions(-) diff --git a/domain-server/resources/describe-settings.json b/domain-server/resources/describe-settings.json index c9d7ea77d5..84408d2e9a 100644 --- a/domain-server/resources/describe-settings.json +++ b/domain-server/resources/describe-settings.json @@ -380,6 +380,14 @@ "default": "0", "advanced": false }, + { + "name": "maximum_user_capacity_redirect_location", + "label": "Redirect to Location on Maximum Capacity", + "help": "Is there another domain, you'd like to redirect clients to when the maximum number of avatars are connected.", + "placeholder": "", + "default": "", + "advanced": false + }, { "name": "standard_permissions", "type": "table", diff --git a/domain-server/src/DomainGatekeeper.cpp b/domain-server/src/DomainGatekeeper.cpp index 23a53c3eb0..e33cbe1755 100644 --- a/domain-server/src/DomainGatekeeper.cpp +++ b/domain-server/src/DomainGatekeeper.cpp @@ -317,6 +317,7 @@ SharedNodePointer DomainGatekeeper::processAssignmentConnectRequest(const NodeCo } const QString MAXIMUM_USER_CAPACITY = "security.maximum_user_capacity"; +const QString MAXIMUM_USER_CAPACITY_REDIRECT_LOCATION = "security.maximum_user_capacity_redirect_location"; SharedNodePointer DomainGatekeeper::processAgentConnectRequest(const NodeConnectionData& nodeConnection, const QString& username, @@ -363,7 +364,7 @@ SharedNodePointer DomainGatekeeper::processAgentConnectRequest(const NodeConnect if (!userPerms.can(NodePermissions::Permission::canConnectToDomain)) { sendConnectionDeniedPacket("You lack the required permissions to connect to this domain.", - nodeConnection.senderSockAddr, DomainHandler::ConnectionRefusedReason::TooManyUsers); + nodeConnection.senderSockAddr, DomainHandler::ConnectionRefusedReason::NotAuthorized); #ifdef WANT_DEBUG qDebug() << "stalling login due to permissions:" << username; #endif @@ -372,8 +373,16 @@ SharedNodePointer DomainGatekeeper::processAgentConnectRequest(const NodeConnect if (!userPerms.can(NodePermissions::Permission::canConnectPastMaxCapacity) && !isWithinMaxCapacity()) { // we can't allow this user to connect because we are at max capacity + QString redirectOnMaxCapacity; + const QVariant* redirectOnMaxCapacityVariant = + valueForKeyPath(_server->_settingsManager.getSettingsMap(), MAXIMUM_USER_CAPACITY_REDIRECT_LOCATION); + if (redirectOnMaxCapacityVariant && redirectOnMaxCapacityVariant->canConvert()) { + redirectOnMaxCapacity = redirectOnMaxCapacityVariant->toString(); + qDebug() << "Redirection domain:" << redirectOnMaxCapacity; + } + sendConnectionDeniedPacket("Too many connected users.", nodeConnection.senderSockAddr, - DomainHandler::ConnectionRefusedReason::TooManyUsers); + DomainHandler::ConnectionRefusedReason::TooManyUsers, redirectOnMaxCapacity); #ifdef WANT_DEBUG qDebug() << "stalling login due to max capacity:" << username; #endif @@ -623,22 +632,30 @@ void DomainGatekeeper::sendProtocolMismatchConnectionDenial(const HifiSockAddr& } void DomainGatekeeper::sendConnectionDeniedPacket(const QString& reason, const HifiSockAddr& senderSockAddr, - DomainHandler::ConnectionRefusedReason reasonCode) { + DomainHandler::ConnectionRefusedReason reasonCode, + QString extraInfo) { // this is an agent and we've decided we won't let them connect - send them a packet to deny connection - QByteArray utfString = reason.toUtf8(); - quint16 payloadSize = utfString.size(); + QByteArray utfReasonString = reason.toUtf8(); + quint16 reasonSize = utfReasonString.size(); + + QByteArray utfExtraInfo = extraInfo.toUtf8(); + quint16 extraInfoSize = utfExtraInfo.size(); // setup the DomainConnectionDenied packet auto connectionDeniedPacket = NLPacket::create(PacketType::DomainConnectionDenied, - payloadSize + sizeof(payloadSize) + sizeof(uint8_t)); + sizeof(uint8_t) + // reasonCode + reasonSize + sizeof(reasonSize) + + extraInfoSize + sizeof(extraInfoSize)); // pack in the reason the connection was denied (the client displays this) - if (payloadSize > 0) { - uint8_t reasonCodeWire = (uint8_t)reasonCode; - connectionDeniedPacket->writePrimitive(reasonCodeWire); - connectionDeniedPacket->writePrimitive(payloadSize); - connectionDeniedPacket->write(utfString); - } + uint8_t reasonCodeWire = (uint8_t)reasonCode; + connectionDeniedPacket->writePrimitive(reasonCodeWire); + connectionDeniedPacket->writePrimitive(reasonSize); + connectionDeniedPacket->write(utfReasonString); + + // write the extra info as well + connectionDeniedPacket->writePrimitive(extraInfoSize); + connectionDeniedPacket->write(utfExtraInfo); // send the packet off DependencyManager::get()->sendPacket(std::move(connectionDeniedPacket), senderSockAddr); diff --git a/domain-server/src/DomainGatekeeper.h b/domain-server/src/DomainGatekeeper.h index 06ecfcf285..b7d2a03af6 100644 --- a/domain-server/src/DomainGatekeeper.h +++ b/domain-server/src/DomainGatekeeper.h @@ -88,7 +88,8 @@ private: void sendConnectionTokenPacket(const QString& username, const HifiSockAddr& senderSockAddr); static void sendConnectionDeniedPacket(const QString& reason, const HifiSockAddr& senderSockAddr, - DomainHandler::ConnectionRefusedReason reasonCode = DomainHandler::ConnectionRefusedReason::Unknown); + DomainHandler::ConnectionRefusedReason reasonCode = DomainHandler::ConnectionRefusedReason::Unknown, + QString extraInfo = QString()); void pingPunchForConnectingPeer(const SharedNetworkPeer& peer); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 8a37662ca9..97a10ea232 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1239,8 +1239,15 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) : firstRun.set(false); } -void Application::domainConnectionRefused(const QString& reasonMessage, int reasonCode) { - switch (static_cast(reasonCode)) { +void Application::domainConnectionRefused(const QString& reasonMessage, int reasonCodeInt, const QString& extraInfo) { + DomainHandler::ConnectionRefusedReason reasonCode = static_cast(reasonCodeInt); + + if (reasonCode == DomainHandler::ConnectionRefusedReason::TooManyUsers && !extraInfo.isEmpty()) { + DependencyManager::get()->handleLookupString(extraInfo); + return; + } + + switch (reasonCode) { case DomainHandler::ConnectionRefusedReason::ProtocolMismatch: case DomainHandler::ConnectionRefusedReason::TooManyUsers: case DomainHandler::ConnectionRefusedReason::Unknown: { diff --git a/interface/src/Application.h b/interface/src/Application.h index 02682defca..4c52ff8526 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -375,7 +375,7 @@ private slots: void nodeKilled(SharedNodePointer node); static void packetSent(quint64 length); void updateDisplayMode(); - void domainConnectionRefused(const QString& reasonMessage, int reason); + void domainConnectionRefused(const QString& reasonMessage, int reason, const QString& extraInfo); private: static void initDisplay(); diff --git a/interface/src/scripting/WindowScriptingInterface.h b/interface/src/scripting/WindowScriptingInterface.h index 715d0657a3..9303636a1f 100644 --- a/interface/src/scripting/WindowScriptingInterface.h +++ b/interface/src/scripting/WindowScriptingInterface.h @@ -58,7 +58,7 @@ public slots: signals: void domainChanged(const QString& domainHostname); void svoImportRequested(const QString& url); - void domainConnectionRefused(const QString& reasonMessage, int reasonCode); + void domainConnectionRefused(const QString& reasonMessage, int reasonCode, const QString& extraInfo); void snapshotTaken(const QString& path, bool notify); void snapshotShared(const QString& error); diff --git a/libraries/networking/src/DomainHandler.cpp b/libraries/networking/src/DomainHandler.cpp index 739c0f8f4a..eecc1515f5 100644 --- a/libraries/networking/src/DomainHandler.cpp +++ b/libraries/networking/src/DomainHandler.cpp @@ -402,13 +402,18 @@ void DomainHandler::processDomainServerConnectionDeniedPacket(QSharedPointerreadWithoutCopy(reasonSize); QString reasonMessage = QString::fromUtf8(reasonText); + quint16 extraInfoSize; + message->readPrimitive(&extraInfoSize); + auto extraInfoUtf8= message->readWithoutCopy(extraInfoSize); + QString extraInfo = QString::fromUtf8(extraInfoUtf8); + // output to the log so the user knows they got a denied connection request // and check and signal for an access token so that we can make sure they are logged in - qCWarning(networking) << "The domain-server denied a connection request: " << reasonMessage; + qCWarning(networking) << "The domain-server denied a connection request: " << reasonMessage << " extraInfo:" << extraInfo; if (!_domainConnectionRefusals.contains(reasonMessage)) { _domainConnectionRefusals.insert(reasonMessage); - emit domainConnectionRefused(reasonMessage, (int)reasonCode); + emit domainConnectionRefused(reasonMessage, (int)reasonCode, extraInfo); } auto accountManager = DependencyManager::get(); diff --git a/libraries/networking/src/DomainHandler.h b/libraries/networking/src/DomainHandler.h index 50639a4817..7f89b47197 100644 --- a/libraries/networking/src/DomainHandler.h +++ b/libraries/networking/src/DomainHandler.h @@ -123,7 +123,7 @@ signals: void settingsReceived(const QJsonObject& domainSettingsObject); void settingsReceiveFail(); - void domainConnectionRefused(QString reasonMessage, int reason); + void domainConnectionRefused(QString reasonMessage, int reason, const QString& extraInfo); private: bool reasonSuggestsLogin(ConnectionRefusedReason reasonCode); diff --git a/libraries/networking/src/udt/PacketHeaders.cpp b/libraries/networking/src/udt/PacketHeaders.cpp index 0f3d5885ff..ec4e724c1b 100644 --- a/libraries/networking/src/udt/PacketHeaders.cpp +++ b/libraries/networking/src/udt/PacketHeaders.cpp @@ -64,7 +64,7 @@ PacketVersion versionForPacketType(PacketType packetType) { return 18; // Introduction of node ignore request (which replaced an unused packet tpye) case PacketType::DomainConnectionDenied: - return static_cast(DomainConnectionDeniedVersion::IncludesReasonCode); + return static_cast(DomainConnectionDeniedVersion::IncludesExtraInfo); case PacketType::DomainConnectRequest: return static_cast(DomainConnectRequestVersion::HasProtocolVersions); diff --git a/libraries/networking/src/udt/PacketHeaders.h b/libraries/networking/src/udt/PacketHeaders.h index 25500b984f..aa775b9f53 100644 --- a/libraries/networking/src/udt/PacketHeaders.h +++ b/libraries/networking/src/udt/PacketHeaders.h @@ -206,7 +206,8 @@ enum class DomainConnectRequestVersion : PacketVersion { enum class DomainConnectionDeniedVersion : PacketVersion { ReasonMessageOnly = 17, - IncludesReasonCode + IncludesReasonCode, + IncludesExtraInfo }; enum class DomainServerAddedNodeVersion : PacketVersion {