github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/location/static_resolver.go (about)

     1  /*
     2   * Copyright (C) 2017 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 location
    19  
    20  import (
    21  	"github.com/pkg/errors"
    22  	"github.com/rs/zerolog/log"
    23  
    24  	"github.com/mysteriumnetwork/node/core/ip"
    25  	"github.com/mysteriumnetwork/node/core/location/locationstate"
    26  )
    27  
    28  // StaticResolver struct represents country by ip ExternalDBResolver which always returns specified country
    29  type StaticResolver struct {
    30  	country string
    31  	city    string
    32  	ipType  string
    33  	err     error
    34  
    35  	ipResolver ip.Resolver
    36  }
    37  
    38  // NewStaticResolver creates new StaticResolver with specified country
    39  func NewStaticResolver(country, city, ipType string, ipResolver ip.Resolver) *StaticResolver {
    40  	return &StaticResolver{
    41  		country: country,
    42  		city:    city,
    43  		ipType:  ipType,
    44  		err:     nil,
    45  
    46  		ipResolver: ipResolver,
    47  	}
    48  }
    49  
    50  // NewFailingResolver returns StaticResolver with entered error
    51  func NewFailingResolver(err error) *StaticResolver {
    52  	return &StaticResolver{
    53  		err:        err,
    54  		ipResolver: ip.NewResolverMock(""),
    55  	}
    56  }
    57  
    58  // DetectLocation detects current IP-address provides location information for the IP.
    59  func (d *StaticResolver) DetectLocation() (locationstate.Location, error) {
    60  	log.Debug().Msg("Detecting with static resolver")
    61  	pubIP, err := d.ipResolver.GetPublicIP()
    62  	if err != nil {
    63  		return locationstate.Location{}, errors.Wrap(err, "failed to get public IP")
    64  	}
    65  	return locationstate.Location{
    66  		Country: d.country,
    67  		City:    d.city,
    68  		IPType:  d.ipType,
    69  		IP:      pubIP,
    70  	}, d.err
    71  }
    72  
    73  // DetectProxyLocation detects current IP-address provides location information for the IP.
    74  func (d *StaticResolver) DetectProxyLocation(_ int) (locationstate.Location, error) {
    75  	return d.DetectLocation()
    76  }