github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/node/options.go (about)

     1  /*
     2   * Copyright (C) 2018 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package node
    19  
    20  import (
    21  	"path"
    22  	"time"
    23  
    24  	"github.com/rs/zerolog"
    25  	"github.com/rs/zerolog/log"
    26  
    27  	"github.com/mysteriumnetwork/node/config"
    28  	"github.com/mysteriumnetwork/node/logconfig"
    29  	"github.com/mysteriumnetwork/node/metadata"
    30  	openvpn_core "github.com/mysteriumnetwork/node/services/openvpn/core"
    31  )
    32  
    33  // Openvpn interface is abstraction over real openvpn options to unblock mobile development
    34  // will disappear as soon as go-openvpn will unify common factory for openvpn creation
    35  type Openvpn interface {
    36  	Check() error
    37  	BinaryPath() string
    38  }
    39  
    40  // TODO this struct will disappear when we unify go-openvpn embedded lib and external process based session creation/handling
    41  type wrapper struct {
    42  	nodeOptions openvpn_core.NodeOptions
    43  }
    44  
    45  func (w wrapper) Check() error {
    46  	return w.nodeOptions.Check()
    47  }
    48  
    49  func (w wrapper) BinaryPath() string {
    50  	return w.nodeOptions.BinaryPath
    51  }
    52  
    53  var _ Openvpn = wrapper{}
    54  
    55  // Options describes options which are required to start Node
    56  type Options struct {
    57  	Directories OptionsDirectory
    58  
    59  	TequilapiAddress       string
    60  	TequilapiPort          int
    61  	FlagTequilapiDebugMode bool
    62  	TequilapiEnabled       bool
    63  	TequilapiSecured       bool
    64  	BindAddress            string
    65  	UI                     OptionsUI
    66  	FeedbackURL            string
    67  
    68  	Keystore OptionsKeystore
    69  
    70  	logconfig.LogOptions
    71  	OptionsNetwork
    72  	Discovery  OptionsDiscovery
    73  	Quality    OptionsQuality
    74  	Location   OptionsLocation
    75  	Transactor OptionsTransactor
    76  	Affiliator OptionsAffiliator
    77  	Chains     OptionsChains
    78  
    79  	Openvpn  Openvpn
    80  	Firewall OptionsFirewall
    81  
    82  	Payments OptionsPayments
    83  
    84  	Consumer bool
    85  	Mobile   bool
    86  
    87  	SwarmDialerDNSHeadstart time.Duration
    88  	PilvytisAddress         string
    89  	ObserverAddress         string
    90  	SSE                     OptionsSSE
    91  }
    92  
    93  // GetOptions retrieves node options from the app configuration.
    94  func GetOptions() *Options {
    95  	network := OptionsNetwork{
    96  		Network:          config.GetBlockchainNetwork(config.FlagBlockchainNetwork),
    97  		DiscoveryAddress: config.GetString(config.FlagDiscoveryAddress),
    98  		BrokerAddresses:  config.GetStringSlice(config.FlagBrokerAddress),
    99  		EtherClientRPCL1: config.GetStringSlice(config.FlagEtherRPCL1),
   100  		EtherClientRPCL2: config.GetStringSlice(config.FlagEtherRPCL2),
   101  		ChainID:          config.GetInt64(config.FlagChainID),
   102  		DNSMap: map[string][]string{
   103  			"location.mysterium.network": {"51.158.129.204"},
   104  			"quality.mysterium.network":  {"51.158.129.204"},
   105  			"feedback.mysterium.network": {"116.203.17.150"},
   106  			"api.ipify.org": {
   107  				"54.204.14.42", "54.225.153.147", "54.235.83.248", "54.243.161.145",
   108  				"23.21.109.69", "23.21.126.66",
   109  				"50.19.252.36",
   110  				"174.129.214.20",
   111  			},
   112  			"badupnp.benjojo.co.uk": {"104.22.70.70", "104.22.71.70", "172.67.25.154"},
   113  		},
   114  	}
   115  	return &Options{
   116  		Directories:            *GetOptionsDirectory(&network),
   117  		TequilapiAddress:       config.GetString(config.FlagTequilapiAddress),
   118  		TequilapiPort:          config.GetInt(config.FlagTequilapiPort),
   119  		FlagTequilapiDebugMode: config.GetBool(config.FlagTequilapiDebugMode),
   120  		TequilapiEnabled:       true,
   121  		BindAddress:            config.GetString(config.FlagBindAddress),
   122  		UI: OptionsUI{
   123  			UIEnabled:     config.GetBool(config.FlagUIEnable),
   124  			UIBindAddress: config.GetString(config.FlagUIAddress),
   125  			UIPort:        config.GetInt(config.FlagUIPort),
   126  		},
   127  		SwarmDialerDNSHeadstart: config.GetDuration(config.FlagDNSResolutionHeadstart),
   128  		FeedbackURL:             config.GetString(config.FlagFeedbackURL),
   129  		Keystore: OptionsKeystore{
   130  			UseLightweight: config.GetBool(config.FlagKeystoreLightweight),
   131  		},
   132  		LogOptions:     *GetLogOptions(),
   133  		OptionsNetwork: network,
   134  		Discovery:      *GetDiscoveryOptions(),
   135  		Quality: OptionsQuality{
   136  			Type:    QualityType(config.GetString(config.FlagQualityType)),
   137  			Address: config.GetString(config.FlagQualityAddress),
   138  		},
   139  		Location: OptionsLocation{
   140  			IPDetectorURL: config.GetString(config.FlagIPDetectorURL),
   141  			Type:          LocationType(config.GetString(config.FlagLocationType)),
   142  			Address:       config.GetString(config.FlagLocationAddress),
   143  			Country:       config.GetString(config.FlagLocationCountry),
   144  			City:          config.GetString(config.FlagLocationCity),
   145  			IPType:        config.GetString(config.FlagLocationIPType),
   146  		},
   147  		Transactor: OptionsTransactor{
   148  			TransactorEndpointAddress:       config.GetString(config.FlagTransactorAddress),
   149  			ProviderMaxRegistrationAttempts: config.GetInt(config.FlagTransactorProviderMaxRegistrationAttempts),
   150  			TransactorFeesValidTime:         config.GetDuration(config.FlagTransactorFeesValidTime),
   151  			TryFreeRegistration:             config.GetBool(config.FlagProviderTryFreeRegistration),
   152  		},
   153  		Affiliator: OptionsAffiliator{
   154  			AffiliatorEndpointAddress: config.GetString(config.FlagAffiliatorAddress),
   155  		},
   156  		Payments: OptionsPayments{
   157  			MaxAllowedPaymentPercentile:    config.GetInt(config.FlagPaymentsMaxHermesFee),
   158  			BCTimeout:                      config.GetDuration(config.FlagPaymentsBCTimeout),
   159  			HermesPromiseSettlingThreshold: config.GetFloat64(config.FlagPaymentsHermesPromiseSettleThreshold),
   160  			MaxFeeSettlingThreshold:        config.GetFloat64(config.FlagPaymentsPromiseSettleMaxFeeThreshold),
   161  			MaxUnSettledAmount:             config.GetFloat64(config.FlagPaymentsUnsettledMaxAmount),
   162  			SettlementTimeout:              config.GetDuration(config.FlagPaymentsHermesPromiseSettleTimeout),
   163  			SettlementRecheckInterval:      config.GetDuration(config.FlagPaymentsHermesPromiseSettleCheckInterval),
   164  			BalanceLongPollInterval:        config.GetDuration(config.FlagPaymentsLongBalancePollInterval),
   165  			BalanceFastPollInterval:        config.GetDuration(config.FlagPaymentsFastBalancePollInterval),
   166  			BalanceFastPollTimeout:         config.GetDuration(config.FlagPaymentsFastBalancePollTimeout),
   167  			RegistryTransactorPollInterval: config.GetDuration(config.FlagPaymentsRegistryTransactorPollInterval),
   168  			RegistryTransactorPollTimeout:  config.GetDuration(config.FlagPaymentsRegistryTransactorPollTimeout),
   169  			ConsumerDataLeewayMegabytes:    config.GetUInt64(config.FlagPaymentsConsumerDataLeewayMegabytes),
   170  			HermesStatusRecheckInterval:    config.GetDuration(config.FlagPaymentsHermesStatusRecheckInterval),
   171  			MinAutoSettleAmount:            config.GetFloat64(config.FlagPaymentsZeroStakeUnsettledAmount),
   172  
   173  			ProviderInvoiceFrequency:      config.GetDuration(config.FlagPaymentsProviderInvoiceFrequency),
   174  			ProviderLimitInvoiceFrequency: config.GetDuration(config.FlagPaymentsLimitProviderInvoiceFrequency),
   175  			MaxUnpaidInvoiceValue:         config.GetBigInt(config.FlagPaymentsUnpaidInvoiceValue),
   176  			LimitUnpaidInvoiceValue:       config.GetBigInt(config.FlagPaymentsLimitUnpaidInvoiceValue),
   177  		},
   178  		Chains: OptionsChains{
   179  			Chain1: metadata.ChainDefinition{
   180  				RegistryAddress:    config.GetString(config.FlagChain1RegistryAddress),
   181  				HermesID:           config.GetString(config.FlagChain1HermesAddress),
   182  				ChannelImplAddress: config.GetString(config.FlagChain1ChannelImplementationAddress),
   183  				ChainID:            config.GetInt64(config.FlagChain1ChainID),
   184  				MystAddress:        config.GetString(config.FlagChain1MystAddress),
   185  				KnownHermeses:      config.GetStringSlice(config.FlagChain1KnownHermeses),
   186  			},
   187  			Chain2: metadata.ChainDefinition{
   188  				RegistryAddress:    config.GetString(config.FlagChain2RegistryAddress),
   189  				HermesID:           config.GetString(config.FlagChain2HermesAddress),
   190  				ChannelImplAddress: config.GetString(config.FlagChain2ChannelImplementationAddress),
   191  				ChainID:            config.GetInt64(config.FlagChain2ChainID),
   192  				MystAddress:        config.GetString(config.FlagChain2MystAddress),
   193  				KnownHermeses:      config.GetStringSlice(config.FlagChain2KnownHermeses),
   194  			},
   195  		},
   196  		Openvpn: wrapper{nodeOptions: openvpn_core.NodeOptions{
   197  			BinaryPath: config.GetString(config.FlagOpenvpnBinary),
   198  		}},
   199  		Firewall: OptionsFirewall{
   200  			BlockAlways: config.GetBool(config.FlagFirewallKillSwitch),
   201  		},
   202  		Consumer:        config.GetBool(config.FlagConsumer),
   203  		PilvytisAddress: config.GetString(config.FlagPilvytisAddress),
   204  		ObserverAddress: config.GetString(config.FlagObserverAddress),
   205  		SSE: OptionsSSE{
   206  			Enabled: config.GetBool(config.FlagSSEEnable),
   207  		},
   208  	}
   209  }
   210  
   211  // GetLogOptions retrieves logger options from the app configuration.
   212  func GetLogOptions() *logconfig.LogOptions {
   213  	filepath := ""
   214  	if logDir := config.GetString(config.FlagLogDir); logDir != "" {
   215  		filepath = path.Join(logDir, "mysterium-node")
   216  	}
   217  	level, err := zerolog.ParseLevel(config.GetString(config.FlagLogLevel))
   218  	if err != nil {
   219  		log.Error().Err(err).Msg("Failed to parse logging level")
   220  		level = zerolog.DebugLevel
   221  	}
   222  	return &logconfig.LogOptions{
   223  		LogLevel: level,
   224  		LogHTTP:  config.GetBool(config.FlagLogHTTP),
   225  		Filepath: filepath,
   226  	}
   227  }
   228  
   229  // GetDiscoveryOptions retrieves discovery options from the app configuration.
   230  func GetDiscoveryOptions() *OptionsDiscovery {
   231  	typeValues := config.GetStringSlice(config.FlagDiscoveryType)
   232  	types := make([]DiscoveryType, len(typeValues))
   233  	for i, typeValue := range typeValues {
   234  		types[i] = DiscoveryType(typeValue)
   235  	}
   236  
   237  	return &OptionsDiscovery{
   238  		Types:         types,
   239  		PingInterval:  config.GetDuration(config.FlagDiscoveryPingInterval),
   240  		FetchEnabled:  true,
   241  		FetchInterval: config.GetDuration(config.FlagDiscoveryFetchInterval),
   242  		DHT:           *GetDHTOptions(),
   243  	}
   244  }
   245  
   246  // GetDHTOptions retrieves DHT options from the app configuration.
   247  func GetDHTOptions() *OptionsDHT {
   248  	return &OptionsDHT{
   249  		Address:        config.GetString(config.FlagDHTAddress),
   250  		Port:           config.GetInt(config.FlagDHTPort),
   251  		Protocol:       config.GetString(config.FlagDHTProtocol),
   252  		BootstrapPeers: config.GetStringSlice(config.FlagDHTBootstrapPeers),
   253  	}
   254  }
   255  
   256  // OptionsKeystore stores the keystore configuration
   257  type OptionsKeystore struct {
   258  	UseLightweight bool
   259  }