github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/location/oracle_resolver.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 location 19 20 import ( 21 "github.com/pkg/errors" 22 "github.com/rs/zerolog/log" 23 24 "github.com/mysteriumnetwork/node/core/location/locationstate" 25 "github.com/mysteriumnetwork/node/requests" 26 ) 27 28 type oracleResolver struct { 29 httpClient *requests.HTTPClient 30 address string 31 } 32 33 type oracleLocation struct { 34 ASN int `json:"asn"` 35 City string `json:"city"` 36 Region string `json:"region"` 37 Continent string `json:"continent"` 38 Country string `json:"country"` 39 IP string `json:"ip"` 40 ISP string `json:"isp"` 41 NodeType string `json:"node_type"` 42 } 43 44 func (l oracleLocation) ToLocation() locationstate.Location { 45 return locationstate.Location{ 46 ASN: l.ASN, 47 City: l.City, 48 Region: l.Region, 49 Continent: l.Continent, 50 Country: l.Country, 51 IP: l.IP, 52 ISP: l.ISP, 53 IPType: l.NodeType, 54 } 55 } 56 57 // NewOracleResolver returns new db resolver initialized from Location Oracle service 58 func NewOracleResolver(httpClient *requests.HTTPClient, address string) *oracleResolver { 59 return &oracleResolver{ 60 httpClient: httpClient, 61 address: address, 62 } 63 } 64 65 // DetectLocation detects current IP-address provides location information for the IP. 66 func (o *oracleResolver) DetectLocation() (location locationstate.Location, err error) { 67 log.Debug().Msg("Detecting with oracle resolver") 68 request, err := requests.NewGetRequest(o.address, "", nil) 69 if err != nil { 70 log.Error().Err(err).Msg("") 71 return locationstate.Location{}, errors.Wrap(err, "failed to create request") 72 } 73 74 var res oracleLocation 75 err = o.httpClient.DoRequestAndParseResponse(request, &res) 76 77 return res.ToLocation(), errors.Wrap(err, "failed to execute request") 78 } 79 80 // DetectProxyLocation detects current IP-address provides location information for the IP. 81 func (o *oracleResolver) DetectProxyLocation(proxyPort int) (location locationstate.Location, err error) { 82 log.Debug().Msg("Detecting with oracle resolver") 83 request, err := requests.NewGetRequest(o.address, "", nil) 84 if err != nil { 85 log.Error().Err(err).Msg("") 86 return locationstate.Location{}, errors.Wrap(err, "failed to create request") 87 } 88 89 var res oracleLocation 90 err = o.httpClient.DoRequestViaProxyAndParseResponse(request, &res, proxyPort) 91 92 return res.ToLocation(), errors.Wrap(err, "failed to execute request") 93 }