github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/policy/requested/provider.go (about)

     1  /*
     2   * Copyright (C) 2023 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 requested
    19  
    20  import (
    21  	"github.com/mysteriumnetwork/node/identity"
    22  	"github.com/mysteriumnetwork/node/requests"
    23  	"github.com/rs/zerolog/log"
    24  )
    25  
    26  // Provider defines the requested provider structure.
    27  type Provider struct {
    28  	client   *requests.HTTPClient
    29  	fetchURL string
    30  }
    31  
    32  // NewRequestedProvider creates a policy provider
    33  func NewRequestedProvider(client *requests.HTTPClient, policyURL string) *Provider {
    34  	return &Provider{
    35  		client:   client,
    36  		fetchURL: policyURL,
    37  	}
    38  }
    39  
    40  // IsIdentityAllowed returns if provided identity exists in any access policy.
    41  func (o *Provider) IsIdentityAllowed(identity identity.Identity) bool {
    42  	req, err := requests.NewGetRequest(o.fetchURL, "", nil)
    43  	if err != nil {
    44  		log.Warn().Err(err).Msg("failed to create policy request")
    45  		return false
    46  	}
    47  
    48  	queryValues := req.URL.Query()
    49  	queryValues.Add("identity-value", identity.Address)
    50  	req.URL.RawQuery = queryValues.Encode()
    51  
    52  	resp, err := o.client.Do(req)
    53  	if err != nil {
    54  		log.Warn().Err(err).Msg("failed to make policy request")
    55  		return false
    56  	}
    57  
    58  	return resp.StatusCode >= 200 && resp.StatusCode <= 299
    59  }
    60  
    61  // HasDNSRules returns if dns rules exist. Currently unsupported with this implemenetation
    62  func (o *Provider) HasDNSRules() bool {
    63  	return false
    64  }
    65  
    66  // IsHostAllowed returns if provided host is allowed. Currently unsupported with this implemenetation
    67  func (o *Provider) IsHostAllowed(host string) bool {
    68  	return false
    69  }