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

Cherry pick fix mobile nat hole punching #1795

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 18 additions & 6 deletions mobile/mysterium/openvpn_connection_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"time"

"github.com/mysteriumnetwork/go-openvpn/openvpn3"
"github.com/mysteriumnetwork/node/config"
"github.com/mysteriumnetwork/node/core/connection"
"github.com/mysteriumnetwork/node/core/ip"
"github.com/mysteriumnetwork/node/core/port"
Expand All @@ -35,6 +34,8 @@ import (
"github.com/rs/zerolog/log"
)

const natPunchingMaxTTL = 10

type natPinger interface {
traversal.NATProviderPinger
SetProtectSocketCallback(SocketProtect func(socket int) bool)
Expand Down Expand Up @@ -82,19 +83,20 @@ func NewOpenVPNConnection(sessionTracker *sessionTracker, signerFactory identity
Password: password,
}

natPinger.SetProtectSocketCallback(tunnelSetup.SocketProtect)
newSession := openvpn3.NewMobileSession(config, credentials, conn, tunnelSetup)
sessionTracker.sessionCreated(newSession)
return newSession, vpnClientConfig, nil
}
conn.createSession = sessionFactory
conn.tunnelSetup = tunnelSetup
return conn, nil
}

type openvpnConnection struct {
ports []int
stateCh chan connection.State
stats connection.Statistics
tunnelSetup Openvpn3TunnelSetup
statsMu sync.RWMutex
session *openvpn3.Session
createSession openvpn3SessionFactory
Expand Down Expand Up @@ -158,17 +160,27 @@ func (c *openvpnConnection) Start(options connection.ConnectOptions) error {
sessionConfig.Ports = []int{sessionConfig.RemotePort}
}

if sessionConfig.LocalPort == 0 {
port, err := port.NewPool().Acquire()
if err != nil {
return errors.Wrap(err, "failed to acquire free port")
}

sessionConfig.LocalPort = port.Num()
}

ip := sessionConfig.RemoteIP
localPorts := c.ports
remotePorts := sessionConfig.Ports

lPort, rPort, err := c.natPinger.PingProvider(ip, localPorts, remotePorts, sessionConfig.LocalPort)
c.natPinger.SetProtectSocketCallback(c.tunnelSetup.SocketProtect)
_, _, err := c.natPinger.PingProvider(ip, localPorts, remotePorts, sessionConfig.LocalPort)
if err != nil {
return errors.Wrap(err, "could not ping provider")
}

sessionConfig.LocalPort = lPort
sessionConfig.RemotePort = rPort
sessionConfig.RemoteIP = "127.0.0.1"
sessionConfig.RemotePort = sessionConfig.LocalPort
}

newSession, clientConfig, err := c.createSession(options, sessionConfig)
Expand Down Expand Up @@ -210,7 +222,7 @@ func (c *openvpnConnection) GetConfig() (connection.ConsumerConfig, error) {
return nil, errors.Wrap(err, "failed to get consumer public IP")
}

ports, err := port.NewPool().AcquireMultiple(config.GetInt(config.FlagNATPunchingMaxTTL))
ports, err := port.NewPool().AcquireMultiple(natPunchingMaxTTL)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions mobile/mysterium/wireguard_connection_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"sync"
"time"

"github.com/mysteriumnetwork/node/config"
"github.com/mysteriumnetwork/node/core/connection"
"github.com/mysteriumnetwork/node/core/ip"
"github.com/mysteriumnetwork/node/core/port"
Expand Down Expand Up @@ -187,7 +186,7 @@ func (c *wireguardConnection) GetConfig() (connection.ConsumerConfig, error) {
return nil, errors.Wrap(err, "failed to get consumer public IP")
}

ports, err := port.NewPool().AcquireMultiple(config.GetInt(config.FlagNATPunchingMaxTTL))
ports, err := port.NewPool().AcquireMultiple(natPunchingMaxTTL)
if err != nil {
return nil, err
}
Expand Down
5 changes: 5 additions & 0 deletions services/openvpn/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ func NewClientConfigFromSession(vpnConfig VPNConfig, configDir string, runtimeDi
return nil, err
}

vpnConfig, err = FormatTLSPresharedKey(vpnConfig)
if err != nil {
return nil, err
}

clientFileConfig := newClientConfig(runtimeDir, configDir)
dnsIPs, err := dnsOption.ResolveIPs(vpnConfig.DNSIPs)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions services/openvpn/config_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ func validIPFormat(config VPNConfig) error {
}

func validTLSPresharedKey(config VPNConfig) error {
_, err := formatTLSPresharedKey(config)
_, err := FormatTLSPresharedKey(config)
return err
}

// preshared key format (PEM blocks with data encoded to hex) are taken from
// FormatTLSPresharedKey formats preshared key (PEM blocks with data encoded to hex) are taken from
// openvpn --genkey --secret static.key, which is openvpn specific.
// it reformats key from single line to multiline fixed length strings.
func formatTLSPresharedKey(config VPNConfig) (VPNConfig, error) {
func FormatTLSPresharedKey(config VPNConfig) (VPNConfig, error) {
contentScanner := bufio.NewScanner(bytes.NewBufferString(config.TLSPresharedKey))
for contentScanner.Scan() {
line := contentScanner.Text()
Expand Down
2 changes: 1 addition & 1 deletion services/openvpn/config_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func TestPortOutOfRangeIsNotAllowed(t *testing.T) {
func TestTLSPresharedKeyIsValid(t *testing.T) {
vpnConfig := VPNConfig{TLSPresharedKey: tlsTestKey}
assert.NoError(t, validTLSPresharedKey(vpnConfig))
newVPNConfig, err := formatTLSPresharedKey(vpnConfig)
newVPNConfig, err := FormatTLSPresharedKey(vpnConfig)
assert.NoError(t, err)
assert.Equal(t, tlsTestKeyPreformatted, newVPNConfig.TLSPresharedKey)
}
Expand Down