github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/mmn/client.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package mmn
    19  
    20  import (
    21  	"encoding/json"
    22  
    23  	"github.com/rs/zerolog/log"
    24  
    25  	"github.com/mysteriumnetwork/node/identity"
    26  	"github.com/mysteriumnetwork/node/requests"
    27  )
    28  
    29  // NodeClaimRequest contains node information to be sent to MMN
    30  type NodeClaimRequest struct {
    31  	// local IP is used to give quick access to WebUI from MMN
    32  	LocalIP     string `json:"local_ip"`
    33  	Identity    string `json:"identity"`
    34  	APIKey      string `json:"api_key"`
    35  	VendorID    string `json:"vendor_id"`
    36  	OS          string `json:"os"`
    37  	Arch        string `json:"arch"`
    38  	NodeVersion string `json:"node_version"`
    39  	RedirectURL string `json:"redirect_url,omitempty"`
    40  }
    41  
    42  func (ncr NodeClaimRequest) json() ([]byte, error) {
    43  	payload, err := json.Marshal(ncr)
    44  	if err != nil {
    45  		return []byte{}, err
    46  	}
    47  	return payload, nil
    48  }
    49  
    50  // NewClient returns MMN API client
    51  func NewClient(httpClient *requests.HTTPClient, mmnAddress string, signer identity.SignerFactory) *client {
    52  	return &client{
    53  		httpClient: httpClient,
    54  		mmnAddress: mmnAddress,
    55  		signer:     signer,
    56  	}
    57  }
    58  
    59  type client struct {
    60  	httpClient *requests.HTTPClient
    61  	mmnAddress string
    62  	signer     identity.SignerFactory
    63  }
    64  
    65  // ClaimNode does an HTTP call to MMN and registers node
    66  func (m *client) ClaimNode(info NodeClaimRequest) error {
    67  	log.Debug().Msgf("Registering node to MMN: %+v", info)
    68  
    69  	id := identity.FromAddress(info.Identity)
    70  	req, err := requests.NewSignedPostRequest(m.mmnAddress, "node", info, m.signer(id))
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	return m.httpClient.DoRequest(req)
    76  }