Skip to content

Commit

Permalink
Merge pull request #1285 from mysteriumnetwork/feature/consumer-uses-…
Browse files Browse the repository at this point in the history
…provider-dns

Use provider's DNS for consumer connection
  • Loading branch information
Waldz authored Sep 4, 2019
2 parents ee7e0ea + 976be1f commit 9b1f6db
Show file tree
Hide file tree
Showing 28 changed files with 278 additions and 77 deletions.
1 change: 1 addition & 0 deletions bin/package/installation/systemd.service
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ KillMode=process
TimeoutStopSec=10
SendSIGKILL=yes
Restart=on-failure
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target
26 changes: 17 additions & 9 deletions cmd/commands/cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"io"
stdlog "log"
"path/filepath"
"strconv"
"strings"

"github.com/chzyer/readline"
Expand Down Expand Up @@ -308,25 +307,34 @@ func (c *cliApp) serviceGet(id string) {
func (c *cliApp) connect(argsString string) {
args := strings.Fields(argsString)

helpMsg := "Please type in the provider identity. connect <consumer-identity> <provider-identity> <service-type> [disable-kill-switch] [enable-dns]"
if len(args) < 3 {
info("Please type in the provider identity. connect <consumer-identity> <provider-identity> <service-type> [disable-kill-switch]")
info(helpMsg)
return
}

consumerID, providerID, serviceType := args[0], args[1], args[2]

var disableKill bool
var disableKillSwitch bool
var enableDNS bool
var err error
if len(args) > 3 {
disableKillStr := args[3]
disableKill, err = strconv.ParseBool(disableKillStr)
if err != nil {
info("Please use true / false for <disable-kill-switch>")
for _, arg := range args[3:] {
switch arg {
case "enable-dns":
enableDNS = true
case "disable-kill-switch":
disableKillSwitch = true
default:
warn("Unexpected arg:", arg)
info(helpMsg)
return
}
}

connectOptions := tequilapi_client.ConnectOptions{DisableKillSwitch: disableKill}
connectOptions := tequilapi_client.ConnectOptions{
EnableDNS: enableDNS,
DisableKillSwitch: disableKillSwitch,
}

if consumerID == "new" {
id, err := c.tequilapi.NewIdentity(identityDefaultPassphrase)
Expand Down
6 changes: 3 additions & 3 deletions cmd/di_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ func (di *Dependencies) bootstrapServiceComponents(nodeOptions node.Options) err
func (di *Dependencies) registerConnections(nodeOptions node.Options) {
di.registerOpenvpnConnection(nodeOptions)
di.registerNoopConnection()
di.registerWireguardConnection()
di.registerWireguardConnection(nodeOptions)
}

func (di *Dependencies) registerWireguardConnection() {
func (di *Dependencies) registerWireguardConnection(nodeOptions node.Options) {
wireguard.Bootstrap()
di.ConnectionRegistry.Register(wireguard.ServiceType, wireguard_connection.NewConnectionCreator())
di.ConnectionRegistry.Register(wireguard.ServiceType, wireguard_connection.NewConnectionCreator(nodeOptions.Directories.Config))
}

func (di *Dependencies) bootstrapUIServer(options node.Options) {
Expand Down
2 changes: 1 addition & 1 deletion config/urfavecli/clicontext/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func LoadUserConfig(ctx *cli.Context) error {
func LoadUserConfigQuietly(ctx *cli.Context) error {
err := LoadUserConfig(ctx)
if err != nil {
_ = log.Warn(err)
log.Warn(err)
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions core/connection/connect_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
type ConnectParams struct {
// kill switch option restricting communication only through VPN
DisableKillSwitch bool
EnableDNS bool
}

// ConnectOptions represents the params we need to ensure a successful connection
Expand All @@ -35,5 +36,6 @@ type ConnectOptions struct {
ProviderID identity.Identity
Proposal market.ServiceProposal
SessionID session.ID
EnableDNS bool
SessionConfig []byte
}
1 change: 1 addition & 0 deletions core/connection/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ func (manager *connectionManager) startConnection(
connectOptions := ConnectOptions{
SessionID: sessionDTO.ID,
SessionConfig: sessionDTO.Config,
EnableDNS: params.EnableDNS,
ConsumerID: consumerID,
ProviderID: identity.FromAddress(proposal.ProviderID),
Proposal: proposal,
Expand Down
4 changes: 2 additions & 2 deletions firewall/iptables/iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (b Iptables) Setup() error {
// Reset tries to cleanup all changes made by setup and leave system in the state before setup
func (Iptables) Reset() {
if err := cleanupStaleRules(); err != nil {
_ = log.Warn(logPrefix, "Error cleaning up iptables rules, you might want to do it yourself: ", err)
log.Warn(logPrefix, "Error cleaning up iptables rules, you might want to do it yourself: ", err)
}
}

Expand All @@ -184,7 +184,7 @@ func addRuleWithRemoval(chain chainInfo) (firewall.RemoveRule, error) {
return func() {
_, err := iptablesExec(chain.removeArgs()...)
if err != nil {
_ = log.Warn(logPrefix, "Error executing rule: ", chain.removeArgs(), " you might wanna do it yourself. Error was: ", err)
log.Warn(logPrefix, "Error executing rule: ", chain.removeArgs(), " you might wanna do it yourself. Error was: ", err)
}
}, nil
}
Expand Down
12 changes: 6 additions & 6 deletions logconfig/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,18 @@ func (l Logger) Info(v ...interface{}) {
}

// Warn warn log
func (l Logger) Warn(v ...interface{}) error {
return seelog.Warn(append([]interface{}{l.prefix}, v...)...)
func (l Logger) Warn(v ...interface{}) {
_ = seelog.Warn(append([]interface{}{l.prefix}, v...)...)
}

// Error error log
func (l Logger) Error(v ...interface{}) error {
return seelog.Error(append([]interface{}{l.prefix}, v...)...)
func (l Logger) Error(v ...interface{}) {
_ = seelog.Error(append([]interface{}{l.prefix}, v...)...)
}

// Critical critical log
func (l Logger) Critical(v ...interface{}) error {
return seelog.Critical(append([]interface{}{l.prefix}, v...)...)
func (l Logger) Critical(v ...interface{}) {
_ = seelog.Critical(append([]interface{}{l.prefix}, v...)...)
}

// IsTrace indicates if trace should be logged
Expand Down
2 changes: 1 addition & 1 deletion mobile/mysterium/openvpn_connection_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (ocf *OpenvpnConnectionFactory) Create(stateChannel connection.StateChannel
sessionConfig.RemotePort = sessionConfig.LocalPort + 1
}

vpnClientConfig, err := openvpn.NewClientConfigFromSession(sessionConfig, "", "")
vpnClientConfig, err := openvpn.NewClientConfigFromSession(sessionConfig, "", "", false)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions services/openvpn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ package openvpn
import (
"sync"

"github.com/mysteriumnetwork/node/firewall"

log "github.com/cihub/seelog"
"github.com/mysteriumnetwork/go-openvpn/openvpn"
"github.com/mysteriumnetwork/node/core/connection"
"github.com/mysteriumnetwork/node/core/ip"
"github.com/mysteriumnetwork/node/firewall"
"github.com/mysteriumnetwork/node/nat/traversal"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -132,6 +131,7 @@ type VPNConfig struct {
OriginalRemoteIP string
OriginalRemotePort int

DNS string `json:"dns"`
RemoteIP string `json:"remote"`
RemotePort int `json:"port"`
LocalPort int `json:"lport"`
Expand Down
9 changes: 5 additions & 4 deletions services/openvpn/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,24 @@ func defaultClientConfig(runtimeDir string, scriptSearchPath string) *ClientConf
clientConfig.SetParam("reneg-sec", "60")
clientConfig.SetParam("resolv-retry", "infinite")
clientConfig.SetParam("redirect-gateway", "def1", "bypass-dhcp")
clientConfig.SetParam("dhcp-option", "DNS", "208.67.222.222")
clientConfig.SetParam("dhcp-option", "DNS", "208.67.220.220")

return &clientConfig
}

// NewClientConfigFromSession creates client configuration structure for given VPNConfig, configuration dir to store serialized file args, and
// configuration filename to store other args
// TODO this will become the part of openvpn service consumer separate package
func NewClientConfigFromSession(vpnConfig *VPNConfig, configDir string, runtimeDir string) (*ClientConfig, error) {
func NewClientConfigFromSession(vpnConfig *VPNConfig, configDir string, runtimeDir string, enableDNS bool) (*ClientConfig, error) {
// TODO Rename `vpnConfig` to `sessionConfig`
err := NewDefaultValidator().IsValid(vpnConfig)
if err != nil {
return nil, err
}

clientFileConfig := newClientConfig(runtimeDir, configDir)
clientFileConfig := newClientConfig(runtimeDir, configDir, enableDNS)
if enableDNS && len(vpnConfig.DNS) > 0 {
clientFileConfig.SetParam("dhcp-option", "DNS", vpnConfig.DNS)
}
clientFileConfig.VpnConfig = vpnConfig
clientFileConfig.SetReconnectRetry(2)
clientFileConfig.SetClientMode(vpnConfig.RemoteIP, vpnConfig.RemotePort, vpnConfig.LocalPort)
Expand Down
9 changes: 6 additions & 3 deletions services/openvpn/client_config_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ package openvpn

import "github.com/mysteriumnetwork/go-openvpn/openvpn/config"

func newClientConfig(runtimeDir string, scriptSearchPath string) *ClientConfig {
func newClientConfig(runtimeDir string, scriptSearchPath string, enableDNS bool) *ClientConfig {
clientConfig := defaultClientConfig(runtimeDir, scriptSearchPath)
clientConfig.SetScriptParam("up", config.QuotedPath("update-resolv-conf"))
clientConfig.SetScriptParam("down", config.QuotedPath("update-resolv-conf"))

if enableDNS {
clientConfig.SetScriptParam("up", config.QuotedPath("update-resolv-conf"))
clientConfig.SetScriptParam("down", config.QuotedPath("update-resolv-conf"))
}

return clientConfig
}
6 changes: 4 additions & 2 deletions services/openvpn/client_config_factory_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package openvpn

func newClientConfig(runtimeDir string, scriptSearchPath string) *ClientConfig {
func newClientConfig(runtimeDir string, scriptSearchPath string, enableDNS bool) *ClientConfig {
clientConfig := defaultClientConfig(runtimeDir, scriptSearchPath)
clientConfig.SetFlag("register-dns")
if enableDNS {
clientConfig.SetFlag("register-dns")
}
return clientConfig
}
17 changes: 9 additions & 8 deletions services/openvpn/config_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ YFcPCscvdnZ1U8hTUaREZmDB2w9eaGyCM4YXAg==

func TestValidatorReturnsNilErrorOnValidVPNConfig(t *testing.T) {
vpnConfig := &VPNConfig{
"",
0,
"1.2.3.4",
10999,
1194,
"tcp",
tlsTestKey,
caCertificate,
OriginalRemoteIP: "",
OriginalRemotePort: 0,
DNS: "",
RemoteIP: "1.2.3.4",
RemotePort: 10999,
LocalPort: 1194,
RemoteProtocol: "tcp",
TLSPresharedKey: tlsTestKey,
CACertificate: caCertificate,
}
assert.NoError(t, NewDefaultValidator().IsValid(vpnConfig))
}
Expand Down
2 changes: 1 addition & 1 deletion services/openvpn/connection_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (op *ProcessBasedConnectionFactory) Create(stateChannel connection.StateCha
sessionConfig.OriginalRemotePort = sessionConfig.RemotePort
}

vpnClientConfig, err := NewClientConfigFromSession(sessionConfig, op.configDirectory, op.runtimeDirectory)
vpnClientConfig, err := NewClientConfigFromSession(sessionConfig, op.configDirectory, op.runtimeDirectory, options.EnableDNS)
if err != nil {
return nil, nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion services/openvpn/service/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,12 @@ func newServerFactory(nodeOptions node.Options, sessionValidator *openvpn_sessio

// newSessionConfigNegotiatorFactory returns function generating session config for remote client
func newSessionConfigNegotiatorFactory(networkOptions node.OptionsNetwork, serviceOptions Options, natEventGetter NATEventGetter, portPool port.ServicePortSupplier) SessionConfigNegotiatorFactory {
return func(secPrimitives *tls.Primitives, outboundIP, publicIP string, port int) session.ConfigNegotiator {
return func(secPrimitives *tls.Primitives, dnsIP, outboundIP, publicIP string, port int) session.ConfigNegotiator {
serverIP := vpnServerIP(serviceOptions, outboundIP, publicIP, networkOptions.Localnet)
return &OpenvpnConfigNegotiator{
natEventGetter: natEventGetter,
vpnConfig: &openvpn_service.VPNConfig{
DNS: dnsIP,
RemoteIP: serverIP,
RemotePort: port,
RemoteProtocol: serviceOptions.Protocol,
Expand Down
29 changes: 11 additions & 18 deletions services/openvpn/service/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (

"github.com/mysteriumnetwork/go-openvpn/openvpn"
"github.com/mysteriumnetwork/go-openvpn/openvpn/tls"
"github.com/pkg/errors"

"github.com/mysteriumnetwork/node/core/port"
"github.com/mysteriumnetwork/node/dns"
"github.com/mysteriumnetwork/node/firewall"
Expand All @@ -38,6 +36,7 @@ import (
openvpn_service "github.com/mysteriumnetwork/node/services/openvpn"
"github.com/mysteriumnetwork/node/session"
"github.com/mysteriumnetwork/node/utils"
"github.com/pkg/errors"
)

// ServerConfigFactory callback generates session config for remote client
Expand All @@ -50,7 +49,7 @@ type ServerFactory func(*openvpn_service.ServerConfig, chan openvpn.State) openv
type ProposalFactory func(currentLocation market.Location) market.ServiceProposal

// SessionConfigNegotiatorFactory initiates ConfigProvider instance during runtime
type SessionConfigNegotiatorFactory func(secPrimitives *tls.Primitives, outboundIP, publicIP string, port int) session.ConfigNegotiator
type SessionConfigNegotiatorFactory func(secPrimitives *tls.Primitives, dnsIP, outboundIP, publicIP string, port int) session.ConfigNegotiator

// NATPinger defined Pinger interface for Provider
type NATPinger interface {
Expand Down Expand Up @@ -118,7 +117,8 @@ func (m *Manager) Serve(providerID identity.Identity) (err error) {
return
}

m.vpnServiceConfigProvider = m.sessionConfigNegotiatorFactory(primitives, m.outboundIP, m.publicIP, m.vpnServerPort)
dnsIP := utils.FirstIP(m.vpnNetwork).String()
m.vpnServiceConfigProvider = m.sessionConfigNegotiatorFactory(primitives, dnsIP, m.outboundIP, m.publicIP, m.vpnServerPort)

vpnServerConfig := m.vpnServerConfigFactory(primitives, m.vpnServerPort)
stateChannel := make(chan openvpn.State, 10)
Expand All @@ -141,14 +141,13 @@ func (m *Manager) Serve(providerID identity.Identity) (err error) {
return errors.Wrap(err, "failed to start Openvpn server")
}

m.dnsServer = dns.NewServer(
net.JoinHostPort(providerIP(m.vpnNetwork).String(), "53"),
dns.ResolveViaConfigured(),
)
log.Info("Starting DNS on: ", m.dnsServer.Addr)
if err = m.dnsServer.Run(); err != nil {
return errors.Wrap(err, "failed to start DNS server")
}
m.dnsServer = dns.NewServer(net.JoinHostPort(dnsIP, "53"), dns.ResolveViaConfigured())
log.Info("starting DNS on: ", m.dnsServer.Addr)
go func() {
if err := m.dnsServer.Run(); err != nil {
log.Error("failed to start DNS server: ", err)
}
}()

log.Info("OpenVPN server waiting")
return m.vpnServer.Wait()
Expand Down Expand Up @@ -262,9 +261,3 @@ func (m *Manager) portMappingFailed() bool {
}
return event.Stage == mapping.StageName && !event.Successful
}

func providerIP(subnet net.IPNet) net.IP {
ip := subnet.IP
ip[len(ip)-1] = byte(1)
return ip
}
Loading

0 comments on commit 9b1f6db

Please sign in to comment.