Skip to content

Commit

Permalink
Merge pull request #292 from MysteriumNetwork/improvement/naive-detec…
Browse files Browse the repository at this point in the history
…tion-of-NAT

Improvement/naive detection of nat
  • Loading branch information
tadovas authored Jul 12, 2018
2 parents d0bcee1 + 5b1e460 commit b84f64e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 21 deletions.
20 changes: 13 additions & 7 deletions cmd/commands/server/command_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type Command struct {
ipResolver ip.Resolver
mysteriumClient server.Client
natService nat.NATService
locationDetector location.Detector
locationResolver location.Resolver

dialogWaiterFactory func(identity identity.Identity) communication.DialogWaiter
dialogWaiter communication.DialogWaiter
Expand All @@ -54,6 +54,7 @@ type Command struct {

vpnServer openvpn.Process
checkOpenvpn func() error
openvpnServiceAddress func(string, string) string
protocol string
proposalAnnouncementStopped *sync.WaitGroup
}
Expand All @@ -75,28 +76,33 @@ func (cmd *Command) Start() (err error) {
cmd.dialogWaiter = cmd.dialogWaiterFactory(providerID)
providerContact, err := cmd.dialogWaiter.Start()

publicIP, err := cmd.ipResolver.GetPublicIP()
if err != nil {
return err
}

// if for some reason we will need truly external IP, use GetPublicIP()
vpnServerIP, err := cmd.ipResolver.GetOutboundIP()
outboundIP, err := cmd.ipResolver.GetOutboundIP()
if err != nil {
return err
}

cmd.natService.Add(nat.RuleForwarding{
SourceAddress: "10.8.0.0/24",
TargetIP: vpnServerIP,
TargetIP: outboundIP,
})

err = cmd.natService.Start()
if err != nil {
log.Warn("received nat service error: ", err, " trying to proceed.")
}

currentLocation, err := cmd.locationDetector.DetectLocation()
currentCountry, err := cmd.locationResolver.ResolveCountry(publicIP)
if err != nil {
return err
}
log.Info("Country detected: ", currentLocation.Country)
serviceLocation := dto_discovery.Location{Country: currentLocation.Country}
log.Info("Country detected: ", currentCountry)
serviceLocation := dto_discovery.Location{Country: currentCountry}

proposal := discovery.NewServiceProposalWithLocation(providerID, providerContact, serviceLocation, cmd.protocol)

Expand All @@ -105,7 +111,7 @@ func (cmd *Command) Start() (err error) {
return err
}

sessionManager := cmd.sessionManagerFactory(primitives, vpnServerIP)
sessionManager := cmd.sessionManagerFactory(primitives, cmd.openvpnServiceAddress(outboundIP, publicIP))

dialogHandler := session.NewDialogHandler(proposal.ID, sessionManager)
if err := cmd.dialogWaiter.ServeDialogs(dialogHandler); err != nil {
Expand Down
40 changes: 29 additions & 11 deletions cmd/commands/server/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package server

import (
"fmt"
log "github.com/cihub/seelog"
"github.com/ethereum/go-ethereum/accounts/keystore"
identity_handler "github.com/mysterium/node/cmd/commands/server/identity"
"github.com/mysterium/node/communication"
Expand Down Expand Up @@ -73,23 +75,14 @@ func NewCommandWith(
createSigner,
)

var locationResolver location.Resolver
if options.LocationCountry != "" {
locationResolver = location.NewResolverFake(options.LocationCountry)
} else if options.LocationDatabase != "" {
locationResolver = location.NewResolver(filepath.Join(options.DirectoryConfig, options.LocationDatabase))
} else {
locationResolver = location.NewResolver(filepath.Join(options.DirectoryConfig, defaultLocationDatabase))
}

locationDetector := location.NewDetectorWithLocationResolver(ipResolver, locationResolver)
locationResolver := locationResolver(options)

return &Command{
identityLoader: func() (identity.Identity, error) {
return identity_handler.LoadIdentity(identityHandler, options.Identity, options.Passphrase)
},
createSigner: createSigner,
locationDetector: locationDetector,
locationResolver: locationResolver,
ipResolver: ipResolver,
mysteriumClient: mysteriumClient,
natService: natService,
Expand Down Expand Up @@ -137,11 +130,36 @@ func NewCommandWith(
checkOpenvpn: func() error {
return openvpn.CheckOpenvpnBinary(options.OpenvpnBinary)
},
openvpnServiceAddress: func(outboundIP, publicIP string) string {
//TODO public ip could be overriden by arg options if needed
if publicIP != outboundIP {
forwardInfo := fmt.Sprintf("%s:%v -> %s:%v", publicIP, options.OpenvpnPort, outboundIP, options.OpenvpnPort)
log.Warnf(
`WARNING: It seems that publicaly visible ip: [%s] does not match your local machines ip: [%s].
You should probaly need to do port forwarding on your router: %s.`,
publicIP,
outboundIP,
forwardInfo,
)

}

return publicIP
},
protocol: options.Protocol,
proposalAnnouncementStopped: &sync.WaitGroup{},
}
}

func locationResolver(options CommandOptions) location.Resolver {
switch {
case options.LocationCountry != "":
return location.NewResolverFake(options.LocationCountry)
default:
return location.NewResolver(filepath.Join(options.DirectoryConfig, options.LocationDatabase))
}
}

// TODO this function can be aligned with client function when client and server options will merge into
func getNetworkDefinition(options CommandOptions) metadata.NetworkDefinition {
network := metadata.DefaultNetwork
Expand Down
4 changes: 1 addition & 3 deletions cmd/commands/server/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ type CommandOptions struct {
Localnet bool
}

const defaultLocationDatabase = "GeoLite2-Country.mmdb"

// ParseArguments parses CLI flags and adds to CommandOptions structure
func ParseArguments(args []string) (options CommandOptions, err error) {
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
Expand Down Expand Up @@ -112,7 +110,7 @@ func ParseArguments(args []string) (options CommandOptions, err error) {
flags.StringVar(
&options.LocationDatabase,
"location.database",
defaultLocationDatabase,
"GeoLite2-Country.mmdb",
"Service location autodetect database of GeoLite2 format e.g. http://dev.maxmind.com/geoip/geoip2/geolite2/",
)
flags.StringVar(
Expand Down

0 comments on commit b84f64e

Please sign in to comment.