github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/location/db_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  	"net"
    22  
    23  	"github.com/oschwald/geoip2-golang"
    24  	"github.com/pkg/errors"
    25  	"github.com/rs/zerolog/log"
    26  
    27  	"github.com/mysteriumnetwork/node/core/ip"
    28  	"github.com/mysteriumnetwork/node/core/location/locationstate"
    29  )
    30  
    31  // DBResolver struct represents ip -> country resolver which uses geoip2 data reader
    32  type DBResolver struct {
    33  	dbReader   *geoip2.Reader
    34  	ipResolver ip.Resolver
    35  }
    36  
    37  // NewExternalDBResolver returns Resolver which uses external country database
    38  func NewExternalDBResolver(databasePath string, ipResolver ip.Resolver) (*DBResolver, error) {
    39  	db, err := geoip2.Open(databasePath)
    40  	if err != nil {
    41  		return nil, errors.Wrap(err, "failed to open external db")
    42  	}
    43  
    44  	return &DBResolver{
    45  		dbReader:   db,
    46  		ipResolver: ipResolver,
    47  	}, nil
    48  }
    49  
    50  // DetectLocation detects current IP-address provides location information for the IP.
    51  func (r *DBResolver) DetectLocation() (loc locationstate.Location, err error) {
    52  	ipAddress, err := r.ipResolver.GetPublicIP()
    53  	if err != nil {
    54  		return locationstate.Location{}, errors.Wrap(err, "failed to get public IP")
    55  	}
    56  
    57  	return r.detectLocation(ipAddress)
    58  }
    59  
    60  // DetectProxyLocation detects proxy IP-address provides location information for the IP.
    61  func (r *DBResolver) DetectProxyLocation(proxyPort int) (loc locationstate.Location, err error) {
    62  	ipAddress, err := r.ipResolver.GetProxyIP(proxyPort)
    63  	if err != nil {
    64  		return locationstate.Location{}, errors.Wrap(err, "failed to get public IP")
    65  	}
    66  
    67  	return r.detectLocation(ipAddress)
    68  }
    69  
    70  func (r *DBResolver) detectLocation(ipAddress string) (loc locationstate.Location, err error) {
    71  	log.Debug().Msg("Detecting with DB resolver")
    72  
    73  	ip := net.ParseIP(ipAddress)
    74  
    75  	countryRecord, err := r.dbReader.Country(ip)
    76  	if err != nil {
    77  		return loc, errors.Wrap(err, "failed to get a country")
    78  	}
    79  
    80  	country := countryRecord.Country.IsoCode
    81  	if country == "" {
    82  		country = countryRecord.RegisteredCountry.IsoCode
    83  		if country == "" {
    84  			return loc, errors.New("failed to resolve country")
    85  		}
    86  	}
    87  
    88  	loc.IP = ip.String()
    89  	loc.Country = country
    90  	return loc, nil
    91  }