Skip to content

Commit

Permalink
Implemented custom error ErrorLocation and special warning recommen…
Browse files Browse the repository at this point in the history
…dation for Provider
  • Loading branch information
Waldz committed Sep 26, 2018
1 parent f906eea commit 9356bee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
23 changes: 21 additions & 2 deletions cmd/commands/service/command.go
Expand Up @@ -29,6 +29,8 @@ import (
"github.com/urfave/cli"
)

const serviceCommandName = "service"

var (
identityFlag = cli.StringFlag{
Name: "identity",
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}
},
}
}
Expand All @@ -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()
}
6 changes: 3 additions & 3 deletions cmd/flags_location.go
Expand Up @@ -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: "",
Expand All @@ -42,14 +42,14 @@ 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
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),
}
}
7 changes: 7 additions & 0 deletions core/service/manager.go
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions services/openvpn/service/manager.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 9356bee

Please sign in to comment.