code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_close_connections_to_hostname.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 AdminCloseConnectionsToHostnameParams struct {
    27  	Hostname string `json:"hostname"`
    28  }
    29  
    30  type AdminCloseConnectionsToHostname struct {
    31  	connectionsManager ConnectionsManager
    32  }
    33  
    34  // Handle closes all the connections from the specified hostname to any 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 *AdminCloseConnectionsToHostname) Handle(_ context.Context, rawParams jsonrpc.Params) (jsonrpc.Result, *jsonrpc.ErrorDetails) {
    38  	params, err := validateAdminCloseConnectionsToHostnameParams(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.Hostname == params.Hostname {
    47  			h.connectionsManager.EndSessionConnection(params.Hostname, connection.Wallet)
    48  		}
    49  	}
    50  
    51  	return nil, nil
    52  }
    53  
    54  func validateAdminCloseConnectionsToHostnameParams(rawParams jsonrpc.Params) (AdminCloseConnectionsToHostnameParams, error) {
    55  	if rawParams == nil {
    56  		return AdminCloseConnectionsToHostnameParams{}, ErrParamsRequired
    57  	}
    58  
    59  	params := AdminCloseConnectionsToHostnameParams{}
    60  	if err := mapstructure.Decode(rawParams, &params); err != nil {
    61  		return AdminCloseConnectionsToHostnameParams{}, ErrParamsDoNotMatch
    62  	}
    63  
    64  	if params.Hostname == "" {
    65  		return AdminCloseConnectionsToHostnameParams{}, ErrHostnameIsRequired
    66  	}
    67  
    68  	return params, nil
    69  }
    70  
    71  func NewAdminCloseConnectionsToHostname(connectionsManager ConnectionsManager) *AdminCloseConnectionsToHostname {
    72  	return &AdminCloseConnectionsToHostname{
    73  		connectionsManager: connectionsManager,
    74  	}
    75  }