github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/location/fallback_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 ) 26 27 // ErrLocationResolutionFailed represents a failure to resolve location and running out of fallbacks to try 28 var ErrLocationResolutionFailed = errors.New("location resolution failed") 29 30 // FallbackResolver represents a resolver that tries multiple resolution techniques in sequence until one of them completes successfully, or all of them fail. 31 type FallbackResolver struct { 32 LocationResolvers []Resolver 33 } 34 35 // NewFallbackResolver returns a new instance of fallback resolver 36 func NewFallbackResolver(resolvers []Resolver) *FallbackResolver { 37 return &FallbackResolver{ 38 LocationResolvers: resolvers, 39 } 40 } 41 42 // DetectLocation allows us to detect our current location 43 func (fr *FallbackResolver) DetectLocation() (locationstate.Location, error) { 44 log.Debug().Msg("Detecting with fallback resolver") 45 for _, v := range fr.LocationResolvers { 46 loc, err := v.DetectLocation() 47 if err != nil { 48 log.Warn().Err(err).Msg("Could not resolve location") 49 } else { 50 return loc, err 51 } 52 } 53 return locationstate.Location{}, ErrLocationResolutionFailed 54 } 55 56 // DetectProxyLocation allows us to detect our current location via a proxy. 57 func (fr *FallbackResolver) DetectProxyLocation(proxyPort int) (locationstate.Location, error) { 58 log.Debug().Msg("Detecting with fallback resolver") 59 for _, v := range fr.LocationResolvers { 60 loc, err := v.DetectProxyLocation(proxyPort) 61 if err != nil { 62 log.Warn().Err(err).Msg("Could not resolve location") 63 } else { 64 return loc, err 65 } 66 } 67 return locationstate.Location{}, ErrLocationResolutionFailed 68 }