Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/naive detection of nat #292

Merged
merged 3 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions cmd/commands/server/command_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Command struct {

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

// if for some reason we will need truly external IP, use GetPublicIP()
vpnServerIP, err := cmd.ipResolver.GetOutboundIP()
outboundIP, err := cmd.ipResolver.GetOutboundIP()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we doing option openvpn.ip with this?
For those who wants has different public IP and wats to map it in router

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is not such option at the moment. And this is a quick detection with posibility for user to take some action.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sad that we do 2-3 requests. Is it possible to avoid it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without too much refactoring? Yes - by passing outboundIP to openvpnServiceAddress function as parameter instead of looking up it again inside. I can live with this approach :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :) reduced ipify query to 1 request

if err != nil {
return err
}

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

err = cmd.natService.Start()
Expand All @@ -105,7 +106,12 @@ func (cmd *Command) Start() (err error) {
return err
}

sessionManager := cmd.sessionManagerFactory(primitives, vpnServerIP)
openvpnServiceAddress, err := cmd.openvpnServiceAddress()
if err != nil {
return err
}

sessionManager := cmd.sessionManagerFactory(primitives, openvpnServiceAddress)

dialogHandler := session.NewDialogHandler(proposal.ID, sessionManager)
if err := cmd.dialogWaiter.ServeDialogs(dialogHandler); err != nil {
Expand Down
27 changes: 27 additions & 0 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 @@ -137,6 +139,31 @@ func NewCommandWith(
checkOpenvpn: func() error {
return openvpn.CheckOpenvpnBinary(options.OpenvpnBinary)
},
openvpnServiceAddress: func() (string, error) {
publicIP, err := ipResolver.GetPublicIP()
if err != nil {
return "", err
}

outboundIP, err := ipResolver.GetOutboundIP()
if err != nil {
return "", err
}

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.`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..on your router: PUBLIC_IP:1194 -> MACHINE_IP:1194 e.g. %s:1194->%s:1194
and you can put example values from detected values

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, did not see that it's already like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

publicIP,
outboundIP,
forwardInfo,
)

}

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