code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_close_connections_to_wallet.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package api 17 18 import ( 19 "context" 20 21 "code.vegaprotocol.io/vega/libs/jsonrpc" 22 23 "github.com/mitchellh/mapstructure" 24 ) 25 26 type AdminCloseConnectionsToWalletParams struct { 27 Wallet string `json:"wallet"` 28 } 29 30 type AdminCloseConnectionsToWallet struct { 31 connectionsManager ConnectionsManager 32 } 33 34 // Handle closes all the connections from any hostname to the specified wallet 35 // opened in the service that run against the specified network. 36 // It does not fail if the service or the connections are already closed. 37 func (h *AdminCloseConnectionsToWallet) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) { 38 params, err := validateAdminCloseConnectionsToWalletParams(rawParams) 39 if err != nil { 40 return nil, InvalidParams(err) 41 } 42 43 connections := h.connectionsManager.ListSessionConnections() 44 45 for _, connection := range connections { 46 if connection.Wallet == params.Wallet { 47 h.connectionsManager.EndSessionConnection(connection.Hostname, params.Wallet) 48 } 49 } 50 51 return nil, nil 52 } 53 54 func validateAdminCloseConnectionsToWalletParams(rawParams jsonrpc.Params) (AdminCloseConnectionsToWalletParams, error) { 55 if rawParams == nil { 56 return AdminCloseConnectionsToWalletParams{}, ErrParamsRequired 57 } 58 59 params := AdminCloseConnectionsToWalletParams{} 60 if err := mapstructure.Decode(rawParams, ¶ms); err != nil { 61 return AdminCloseConnectionsToWalletParams{}, ErrParamsDoNotMatch 62 } 63 64 if params.Wallet == "" { 65 return AdminCloseConnectionsToWalletParams{}, ErrWalletIsRequired 66 } 67 68 return params, nil 69 } 70 71 func NewAdminCloseConnectionsToWallet(connectionsManager ConnectionsManager) *AdminCloseConnectionsToWallet { 72 return &AdminCloseConnectionsToWallet{ 73 connectionsManager: connectionsManager, 74 } 75 }