Skip to content

Commit

Permalink
Merge pull request #1063 from mysteriumnetwork/master
Browse files Browse the repository at this point in the history
Update 0.8 release with fixes
  • Loading branch information
Waldz authored May 14, 2019
2 parents 87e869f + 586e206 commit 12bf2f4
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 56 deletions.
2 changes: 2 additions & 0 deletions cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd

import (
"github.com/mysteriumnetwork/node/core/node"
"github.com/mysteriumnetwork/node/logconfig"
openvpn_core "github.com/mysteriumnetwork/node/services/openvpn/core"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -75,6 +76,7 @@ func RegisterFlagsNode(flags *[]cli.Flag) error {

// ParseFlagsNode function fills in node options from CLI context
func ParseFlagsNode(ctx *cli.Context) node.Options {
logconfig.ParseFlags(ctx)
return node.Options{
Directories: ParseFlagsDirectory(ctx),

Expand Down
2 changes: 2 additions & 0 deletions cmd/mysterium_node/mysterium_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/mysteriumnetwork/node/cmd/commands/license"
"github.com/mysteriumnetwork/node/cmd/commands/service"
"github.com/mysteriumnetwork/node/cmd/commands/version"
"github.com/mysteriumnetwork/node/logconfig"
"github.com/mysteriumnetwork/node/metadata"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -73,6 +74,7 @@ func NewCommand() (*cli.App, error) {
}
app.Version = metadata.VersionAsString()
app.Copyright = licenseCopyright
logconfig.RegisterFlags(&app.Flags)
if err := cmd.RegisterFlagsNode(&app.Flags); err != nil {
return nil, err
}
Expand Down
34 changes: 9 additions & 25 deletions logconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import (
"bytes"
"text/template"

"github.com/cihub/seelog"
"github.com/mysteriumnetwork/node/metadata"
log "github.com/cihub/seelog"
)

const seewayLogXMLConfigTemplate = `
Expand All @@ -36,43 +35,28 @@ const seewayLogXMLConfigTemplate = `
</seelog>
`

type configParams struct {
LogLevel string
}

func (cp configParams) String() string {
func buildXmlConfig(opts options) string {
tmpl := template.Must(template.New("seelogcfg").Parse(seewayLogXMLConfigTemplate))

var tpl bytes.Buffer
err := tmpl.Execute(&tpl, cp)
err := tmpl.Execute(&tpl, opts)
if err != nil {
panic(err)
}

return tpl.String()
}

var cfg configParams

func init() {
cfg = configParams{
LogLevel: "info",
}
if metadata.VersionAsString() == "source.dev-build" {
cfg.LogLevel = "trace"
}
}

// Bootstrap loads seelog package into the overall system
// Bootstrap loads log package into the overall system
func Bootstrap() {
newLogger, err := seelog.LoggerFromConfigAsString(cfg.String())
newLogger, err := log.LoggerFromConfigAsString(buildXmlConfig(opts))
if err != nil {
seelog.Warn("Error parsing seelog configuration", err)
log.Warn("error parsing log configuration", err)
return
}
err = seelog.UseLogger(newLogger)
err = log.UseLogger(newLogger)
if err != nil {
seelog.Warn("Error setting new logger for seelog", err)
log.Warn("error setting new logger for log", err)
}
seelog.Info("LOG LEVEL: ", cfg.LogLevel)
log.Infof("log level: %s", opts.LogLevel)
}
60 changes: 60 additions & 0 deletions logconfig/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package logconfig

import (
"fmt"
"strings"

log "github.com/cihub/seelog"
"github.com/mysteriumnetwork/node/metadata"
"github.com/urfave/cli"
)

type options struct {
LogLevel string
}

var opts options

var (
logLevel = cli.StringFlag{
Name: "log-level, l",
Usage: func() string {
allLevels := []string{log.TraceStr, log.DebugStr, log.InfoStr, log.WarnStr, log.ErrorStr, log.CriticalStr, log.OffStr}
return fmt.Sprintf("Set the logging level (%s)", strings.Join(allLevels, "|"))
}(),
Value: func() string {
level := log.DebugStr
if metadata.VersionAsString() == "source.dev-build" {
level = log.TraceStr
}
return level
}(),
}
)

// RegisterFlags registers logger CLI flags
func RegisterFlags(flags *[]cli.Flag) {
*flags = append(*flags, logLevel)
}

// ParseFlags parses logger CLI flags from context
func ParseFlags(ctx *cli.Context) {
opts = options{ctx.GlobalString("log-level")}
}
4 changes: 2 additions & 2 deletions market/metrics/oracle/mysterium_morqa.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func NewMorqaClient(qualityOracleAddress string) metrics.QualityOracle {
func (m *mysteriumMorqa) ProposalsMetrics() []json.RawMessage {
req, err := requests.NewGetRequest(m.qualityOracleAddress, "proposals/quality", nil)
if err != nil {
log.Warn(mysteriumMorqaLogPrefix, "Failed to create proposals metrics request", err)
log.Warn(mysteriumMorqaLogPrefix, "Failed to create proposals metrics request: ", err)
return nil
}

var metricsResponse metrics.ServiceMetricsResponse
err = m.http.DoRequestAndParseResponse(req, &metricsResponse)
if err != nil {
log.Warn(mysteriumMorqaLogPrefix, "Failed to request or parse proposals metrics", err)
log.Warn(mysteriumMorqaLogPrefix, "Failed to request or parse proposals metrics: ", err)
return nil
}

Expand Down
22 changes: 19 additions & 3 deletions mobile/mysterium/openvpn_connection_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package mysterium
import (
"sync"

"encoding/json"

log "github.com/cihub/seelog"
"github.com/mysteriumnetwork/go-openvpn/openvpn3"
"github.com/mysteriumnetwork/node/cmd"
Expand Down Expand Up @@ -56,8 +58,8 @@ func (wrapper *sessionWrapper) Start(options connection.ConnectOptions) error {
if clientConfig.LocalPort > 0 {
wrapper.natPinger.BindConsumerPort(clientConfig.LocalPort)
err := wrapper.natPinger.PingProvider(
clientConfig.OriginalRemoteIP,
clientConfig.OriginalRemotePort,
clientConfig.VpnConfig.OriginalRemoteIP,
clientConfig.VpnConfig.OriginalRemotePort,
wrapper.pingerStop)
if err != nil {
return err
Expand Down Expand Up @@ -202,10 +204,23 @@ type OpenvpnConnectionFactory struct {
// Create creates a new openvpn connection
func (ocf *OpenvpnConnectionFactory) Create(stateChannel connection.StateChannel, statisticsChannel connection.StatisticsChannel) (con connection.Connection, err error) {
sessionFactory := func(options connection.ConnectOptions) (*openvpn3.Session, *openvpn.ClientConfig, error) {
sessionConfig := &openvpn.VPNConfig{}
err := json.Unmarshal(options.SessionConfig, sessionConfig)
if err != nil {
return nil, nil, err
}
vpnClientConfig, err := openvpn.NewClientConfigFromSession(options.SessionConfig, "", "")

// override vpnClientConfig params with proxy local IP and pinger port
// do this only if connecting to natted provider
if sessionConfig.LocalPort > 0 {
sessionConfig.OriginalRemoteIP = sessionConfig.RemoteIP
sessionConfig.OriginalRemotePort = sessionConfig.RemotePort
sessionConfig.RemoteIP = "127.0.0.1"
// TODO: randomize this too?
sessionConfig.RemotePort = sessionConfig.LocalPort + 1
}

vpnClientConfig, err := openvpn.NewClientConfigFromSession(sessionConfig, "", "")
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -243,6 +258,7 @@ func (ocf *OpenvpnConnectionFactory) Create(stateChannel connection.StateChannel
createSession: sessionFactory,
natPinger: ocf.natPinger,
ipResolver: ocf.ipResolver,
pingerStop: make(chan struct{}),
}, nil
}

Expand Down
5 changes: 5 additions & 0 deletions nat/traversal/nat_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type NATProxy struct {

func (np *NATProxy) consumerHandOff(consumerPort int, remoteConn *net.UDPConn) chan struct{} {
stop := make(chan struct{})
if np.socketProtect == nil {
// shutdown pinger session since openvpn client will connect directly (without NATProxy)
remoteConn.Close()
return stop
}
go np.consumerProxy(consumerPort, remoteConn, stop)
return stop
}
Expand Down
5 changes: 4 additions & 1 deletion services/openvpn/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (c *Client) Start(options connection.ConnectOptions) error {

if clientConfig.VpnConfig.LocalPort > 0 {
c.natPinger.BindConsumerPort(clientConfig.LocalPort)
err = c.natPinger.PingProvider(clientConfig.OriginalRemoteIP, clientConfig.OriginalRemotePort, c.pingerStop)
err = c.natPinger.PingProvider(clientConfig.VpnConfig.OriginalRemoteIP, clientConfig.VpnConfig.OriginalRemotePort, c.pingerStop)
if err != nil {
return err
}
Expand Down Expand Up @@ -113,6 +113,9 @@ func (c *Client) GetConfig() (connection.ConsumerConfig, error) {

//VPNConfig structure represents VPN configuration options for given session
type VPNConfig struct {
OriginalRemoteIP string
OriginalRemotePort int

RemoteIP string `json:"remote"`
RemotePort int `json:"port"`
LocalPort int `json:"lport"`
Expand Down
29 changes: 5 additions & 24 deletions services/openvpn/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package openvpn

import (
"encoding/json"
"strconv"

"github.com/mysteriumnetwork/go-openvpn/openvpn/config"
Expand All @@ -27,10 +26,8 @@ import (
// ClientConfig represents specific "openvpn as client" configuration
type ClientConfig struct {
*config.GenericConfig
LocalPort int
VpnConfig *VPNConfig
OriginalRemoteIP string
OriginalRemotePort int
LocalPort int
VpnConfig *VPNConfig
}

// SetClientMode adds config arguments for openvpn behave as client
Expand Down Expand Up @@ -81,30 +78,14 @@ func defaultClientConfig(runtimeDir string, scriptSearchPath string) *ClientConf
// 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(sessionConfig []byte, configDir string, runtimeDir string) (*ClientConfig, error) {
vpnConfig := &VPNConfig{}
err := json.Unmarshal(sessionConfig, vpnConfig)
if err != nil {
return nil, err
}

err = NewDefaultValidator().IsValid(vpnConfig)
func NewClientConfigFromSession(vpnConfig *VPNConfig, configDir string, runtimeDir string) (*ClientConfig, error) {
// TODO Rename `vpnConfig` to `sessionConfig`
err := NewDefaultValidator().IsValid(vpnConfig)
if err != nil {
return nil, err
}

clientFileConfig := newClientConfig(runtimeDir, configDir)

// override vpnClientConfig params with proxy local IP and pinger port
// do this only if connecting to natted provider
if vpnConfig.LocalPort > 0 {
clientFileConfig.OriginalRemoteIP = vpnConfig.RemoteIP
clientFileConfig.OriginalRemotePort = vpnConfig.RemotePort
vpnConfig.RemoteIP = "127.0.0.1"
// TODO: randomize this too?
vpnConfig.RemotePort = vpnConfig.LocalPort + 1
}

clientFileConfig.VpnConfig = vpnConfig
clientFileConfig.SetReconnectRetry(2)
clientFileConfig.SetClientMode(vpnConfig.RemoteIP, vpnConfig.RemotePort, vpnConfig.LocalPort)
Expand Down
2 changes: 2 additions & 0 deletions services/openvpn/config_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ YFcPCscvdnZ1U8hTUaREZmDB2w9eaGyCM4YXAg==

func TestValidatorReturnsNilErrorOnValidVPNConfig(t *testing.T) {
vpnConfig := &VPNConfig{
"",
0,
"1.2.3.4",
10999,
1194,
Expand Down
16 changes: 15 additions & 1 deletion services/openvpn/connection_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package openvpn

import (
"encoding/json"
"time"

"github.com/mysteriumnetwork/go-openvpn/openvpn"
Expand Down Expand Up @@ -78,7 +79,20 @@ func (op *ProcessBasedConnectionFactory) newStateMiddleware(session session.ID,
// Create creates a new openvpn connection
func (op *ProcessBasedConnectionFactory) Create(stateChannel connection.StateChannel, statisticsChannel connection.StatisticsChannel) (connection.Connection, error) {
procFactory := func(options connection.ConnectOptions) (openvpn.Process, *ClientConfig, error) {
vpnClientConfig, err := NewClientConfigFromSession(options.SessionConfig, op.configDirectory, op.runtimeDirectory)
sessionConfig := &VPNConfig{}
err := json.Unmarshal(options.SessionConfig, sessionConfig)
if err != nil {
return nil, nil, err
}

// override vpnClientConfig params with proxy local IP and pinger port
// do this only if connecting to natted provider
if sessionConfig.LocalPort > 0 {
sessionConfig.OriginalRemoteIP = sessionConfig.RemoteIP
sessionConfig.OriginalRemotePort = sessionConfig.RemotePort
}

vpnClientConfig, err := NewClientConfigFromSession(sessionConfig, op.configDirectory, op.runtimeDirectory)
if err != nil {
return nil, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions tequilapi/endpoints/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/mysteriumnetwork/node/core/location"
"github.com/mysteriumnetwork/node/tequilapi/utils"
)
Expand Down
4 changes: 4 additions & 0 deletions utils/command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ package utils
import (
"fmt"
"os/exec"

log "github.com/cihub/seelog"
)

func PowerShell(cmd string) ([]byte, error) {
log.Tracef("[powershell] executing: '%s'", cmd)
out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("'powershell -Command %v': %v output: %s", cmd, RemoveErrorsAndBOMUTF8(err.Error()), RemoveErrorsAndBOMUTF8Byte(out))
}
log.Tracef("[powershell] done: '%s', raw output: '%s'", cmd, out)
return RemoveErrorsAndBOMUTF8Byte(out), nil
}

0 comments on commit 12bf2f4

Please sign in to comment.