From 9356bee25a596fe872720762dbe8e6a836d5e006 Mon Sep 17 00:00:00 2001 From: Waldz Date: Wed, 26 Sep 2018 15:19:36 +0300 Subject: [PATCH] Implemented custom error `ErrorLocation` and special warning recommendation for Provider --- cmd/commands/service/command.go | 23 +++++++++++++++++++++-- cmd/flags_location.go | 6 +++--- core/service/manager.go | 7 +++++++ services/openvpn/service/manager.go | 3 +++ 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/cmd/commands/service/command.go b/cmd/commands/service/command.go index b198d5b57a..5eb7c489af 100644 --- a/cmd/commands/service/command.go +++ b/cmd/commands/service/command.go @@ -29,6 +29,8 @@ import ( "github.com/urfave/cli" ) +const serviceCommandName = "service" + var ( identityFlag = cli.StringFlag{ Name: "identity", @@ -73,7 +75,7 @@ func NewCommand(licenseCommandName string) *cli.Command { } return &cli.Command{ - Name: "service", + Name: serviceCommandName, Usage: "Starts and publishes service on Mysterium Network", ArgsUsage: " ", Flags: []cli.Flag{ @@ -119,7 +121,14 @@ func NewCommand(licenseCommandName string) *cli.Command { cmd.RegisterSignalCallback(utils.SoftKiller(stopCommand)) - return <-errorChannel + err := <-errorChannel + switch err { + case service.ErrorLocation: + printLocationWarning("myst") + return nil + default: + return err + } }, } } @@ -133,3 +142,13 @@ func printTermWarning(licenseCommandName string) { fmt.Println("If you agree with these Terms & Conditions, run program again with '--agreed-terms-and-conditions' flag") } + +func printLocationWarning(executableName string) { + fmt.Printf( + "Automatic location detection failed. Enter country manually by running program again with '%s %s --%s=US' flag", + executableName, + serviceCommandName, + cmd.LocationCountryFlag.Name, + ) + fmt.Println() +} diff --git a/cmd/flags_location.go b/cmd/flags_location.go index 021c458932..2526b32691 100644 --- a/cmd/flags_location.go +++ b/cmd/flags_location.go @@ -33,7 +33,7 @@ var ( Usage: "Service location autodetect database of GeoLite2 format e.g. http://dev.maxmind.com/geoip/geoip2/geolite2/", Value: "GeoLite2-Country.mmdb", } - locationCountryFlag = cli.StringFlag{ + LocationCountryFlag = cli.StringFlag{ Name: "location.country", Usage: "Service location country. If not given country is autodetected", Value: "", @@ -42,7 +42,7 @@ var ( // RegisterFlagsLocation function register location flags to flag list func RegisterFlagsLocation(flags *[]cli.Flag) { - *flags = append(*flags, ipifyUrlFlag, locationDatabaseFlag, locationCountryFlag) + *flags = append(*flags, ipifyUrlFlag, locationDatabaseFlag, LocationCountryFlag) } // ParseFlagsLocation function fills in location options from CLI context @@ -50,6 +50,6 @@ func ParseFlagsLocation(ctx *cli.Context) node.OptionsLocation { return node.OptionsLocation{ ctx.GlobalString(ipifyUrlFlag.Name), ctx.GlobalString(locationDatabaseFlag.Name), - ctx.GlobalString(locationCountryFlag.Name), + ctx.GlobalString(LocationCountryFlag.Name), } } diff --git a/core/service/manager.go b/core/service/manager.go index 92f5015391..e3e226e035 100644 --- a/core/service/manager.go +++ b/core/service/manager.go @@ -18,6 +18,8 @@ package service import ( + "errors" + log "github.com/cihub/seelog" "github.com/mysteriumnetwork/node/communication" nats_dialog "github.com/mysteriumnetwork/node/communication/nats/dialog" @@ -34,6 +36,11 @@ import ( const logPrefix = "[service-manager] " +var ( + // ErrorLocation error indicates that action (i.e. disconnect) + ErrorLocation = errors.New("failed to detect service location") +) + // Service interface represents pluggable Mysterium service type Service interface { Start(providerID identity.Identity) (dto_discovery.ServiceProposal, session.Manager, error) diff --git a/services/openvpn/service/manager.go b/services/openvpn/service/manager.go index 86cfbd2ea6..7470c86f37 100644 --- a/services/openvpn/service/manager.go +++ b/services/openvpn/service/manager.go @@ -25,6 +25,7 @@ import ( "github.com/mysteriumnetwork/go-openvpn/openvpn/tls" "github.com/mysteriumnetwork/node/core/ip" "github.com/mysteriumnetwork/node/core/location" + "github.com/mysteriumnetwork/node/core/service" "github.com/mysteriumnetwork/node/identity" "github.com/mysteriumnetwork/node/nat" dto_discovery "github.com/mysteriumnetwork/node/service_discovery/dto" @@ -83,6 +84,8 @@ func (manager *Manager) Start(providerID identity.Identity) ( currentCountry, err := manager.locationResolver.ResolveCountry(publicIP) if err != nil { + log.Warn(logPrefix, "Failed to detect service country. ", err) + err = service.ErrorLocation return } currentLocation := dto_discovery.Location{Country: currentCountry}