Skip to content

Commit

Permalink
Fix OpenVPN config formatting for mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
soffokl committed Feb 27, 2020
1 parent 86b8972 commit 1a5b237
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
20 changes: 15 additions & 5 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 Down Expand Up @@ -84,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 @@ -160,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
2 changes: 0 additions & 2 deletions nat/traversal/nat_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"io"
"net"
"time"

"github.com/rs/zerolog/log"
)
Expand All @@ -43,7 +42,6 @@ func newNATProxy() *natProxy {
}

func (np *natProxy) consumerHandOff(consumerAddr string, remoteConn *net.UDPConn) chan struct{} {
time.Sleep(400 * time.Millisecond)
stop := make(chan struct{})
if np.socketProtect == nil {
// shutdown pinger session since openvpn client will connect directly (without natProxy)
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

0 comments on commit 1a5b237

Please sign in to comment.