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

     1  /*
     2   * Copyright (C) 2019 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 config
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/urfave/cli/v2"
    26  
    27  	"github.com/mysteriumnetwork/node/metadata"
    28  )
    29  
    30  var (
    31  	// FlagBlockchainNetwork uses specified blockchain network.
    32  	FlagBlockchainNetwork = cli.StringFlag{
    33  		Name:  "network",
    34  		Usage: "Defines default blockchain network configuration",
    35  		Value: string(Mainnet),
    36  	}
    37  	// FlagAPIAddress Mysterium API URL
    38  	// Deprecated: use FlagDiscoveryAddress
    39  	FlagAPIAddress = cli.StringFlag{
    40  		Name:  metadata.FlagNames.MysteriumAPIAddress,
    41  		Usage: "Deprecated flag. Use `discovery.address` flag instead to specify URL of Discovery API",
    42  		Value: metadata.DefaultNetwork.MysteriumAPIAddress,
    43  	}
    44  	// FlagDiscoveryAddress discovery url
    45  	FlagDiscoveryAddress = cli.StringFlag{
    46  		Name:  metadata.FlagNames.DiscoveryAddress,
    47  		Usage: "URL of Discovery API",
    48  		Value: metadata.DefaultNetwork.DiscoveryAddress,
    49  	}
    50  	// FlagChainID chain id to use
    51  	FlagChainID = cli.Int64Flag{
    52  		Name:  metadata.FlagNames.DefaultChainIDFlag,
    53  		Usage: "The chain ID to use",
    54  		Value: metadata.DefaultNetwork.DefaultChainID,
    55  	}
    56  	// FlagBrokerAddress message broker URI.
    57  	FlagBrokerAddress = cli.StringSliceFlag{
    58  		Name:  metadata.FlagNames.BrokerAddressesFlag,
    59  		Usage: "URI of message broker",
    60  		Value: cli.NewStringSlice(metadata.DefaultNetwork.BrokerAddresses...),
    61  	}
    62  	// FlagEtherRPCL1 URL or IPC socket to connect to Ethereum node.
    63  	FlagEtherRPCL1 = cli.StringSliceFlag{
    64  		Name:  metadata.FlagNames.Chain1Flag.EtherClientRPCFlag,
    65  		Usage: "L1 URL or IPC socket to connect to ethereum node, anything what ethereum client accepts - works",
    66  		Value: cli.NewStringSlice(metadata.DefaultNetwork.Chain1.EtherClientRPC...),
    67  	}
    68  	// FlagEtherRPCL2 URL or IPC socket to connect to Ethereum node.
    69  	FlagEtherRPCL2 = cli.StringSliceFlag{
    70  		Name:  metadata.FlagNames.Chain2Flag.EtherClientRPCFlag,
    71  		Usage: "L2 URL or IPC socket to connect to ethereum node, anything what ethereum client accepts - works",
    72  		Value: cli.NewStringSlice(metadata.DefaultNetwork.Chain2.EtherClientRPC...),
    73  	}
    74  	// FlagNATHolePunching remove the deprecated flag once all users stop to call it.
    75  	FlagNATHolePunching = cli.BoolFlag{
    76  		Name:    "nat-hole-punching",
    77  		Aliases: []string{"experiment-natpunching"}, // TODO: remove the deprecated alias once all users stop to use it.
    78  		Usage:   "Deprecated flag use `traversal` flag instead to disable or enable methods",
    79  		Value:   true,
    80  	}
    81  	// FlagPortMapping enables NAT port mapping.
    82  	FlagPortMapping = cli.BoolFlag{
    83  		Name:  "nat-port-mapping",
    84  		Usage: "Deprecated flag use `traversal` flag instead to disable or enable methods",
    85  		Value: true,
    86  	}
    87  	// FlagIncomingFirewall enables incoming traffic filtering.
    88  	FlagIncomingFirewall = cli.BoolFlag{
    89  		Name:  "incoming-firewall",
    90  		Usage: "Enables incoming traffic filtering",
    91  		Value: false,
    92  	}
    93  	// FlagOutgoingFirewall enables outgoing traffic filtering.
    94  	FlagOutgoingFirewall = cli.BoolFlag{
    95  		Name:  "outgoing-firewall",
    96  		Usage: "Enables outgoing traffic filtering",
    97  		Value: false,
    98  	}
    99  	// FlagKeepConnectedOnFail keeps connection active to prevent traffic leaks.
   100  	FlagKeepConnectedOnFail = cli.BoolFlag{
   101  		Name:  "keep-connected-on-fail",
   102  		Usage: "Do not disconnect consumer on session fail to prevent traffic leaks",
   103  		Value: false,
   104  	}
   105  	// FlagAutoReconnect restore connection automatically once it failed.
   106  	FlagAutoReconnect = cli.BoolFlag{
   107  		Name:  "auto-reconnect",
   108  		Usage: "Restore connection automatically once it failed",
   109  		Value: false,
   110  	}
   111  	// FlagSTUNservers list of STUN server to be used to detect NAT type.
   112  	FlagSTUNservers = cli.StringSliceFlag{
   113  		Name:  "stun-servers",
   114  		Usage: "Comma separated list of STUN server to be used to detect NAT type",
   115  		Value: cli.NewStringSlice("stun.l.google.com:19302", "stun1.l.google.com:19302", "stun2.l.google.com:19302"),
   116  	}
   117  	// FlagLocalServiceDiscovery enables SSDP and Bonjour local service discovery.
   118  	FlagLocalServiceDiscovery = cli.BoolFlag{
   119  		Name:  "local-service-discovery",
   120  		Usage: "Enables SSDP and Bonjour local service discovery",
   121  		Value: true,
   122  	}
   123  	// FlagUDPListenPorts sets allowed UDP port range for listening.
   124  	FlagUDPListenPorts = cli.StringFlag{
   125  		Name:  "udp.ports",
   126  		Usage: "Range of UDP listen ports used for connections",
   127  		Value: "10000:60000",
   128  	}
   129  	// FlagTraversal order of NAT traversal methods to be used for providing service.
   130  	FlagTraversal = cli.StringFlag{
   131  		Name:  "traversal",
   132  		Usage: "Comma separated order of NAT traversal methods to be used for providing service",
   133  		Value: "manual,upnp,holepunching",
   134  	}
   135  	// FlagPortCheckServers list of asymmetric UDP echo servers for checking port availability
   136  	FlagPortCheckServers = cli.StringFlag{
   137  		Name:   "port-check-servers",
   138  		Usage:  "Comma separated list of asymmetric UDP echo servers for checking port availability",
   139  		Value:  "echo.mysterium.network:4589",
   140  		Hidden: true,
   141  	}
   142  
   143  	// FlagStatsReportInterval is interval for consumer connection statistics reporting.
   144  	FlagStatsReportInterval = cli.DurationFlag{
   145  		Name:   "stats-report-interval",
   146  		Usage:  "Duration between syncing stats from the network interface with a node",
   147  		Value:  1 * time.Second,
   148  		Hidden: true,
   149  	}
   150  
   151  	// FlagDNSListenPort sets the port for listening by DNS service.
   152  	FlagDNSListenPort = cli.IntFlag{
   153  		Name:  "dns.listen-port",
   154  		Usage: "DNS listen port for services",
   155  		Value: 11253,
   156  	}
   157  )
   158  
   159  // RegisterFlagsNetwork function register network flags to flag list
   160  func RegisterFlagsNetwork(flags *[]cli.Flag) {
   161  	*flags = append(
   162  		*flags,
   163  		&FlagPortMapping,
   164  		&FlagNATHolePunching,
   165  		&FlagAPIAddress,
   166  		&FlagDiscoveryAddress,
   167  		&FlagBrokerAddress,
   168  		&FlagEtherRPCL1,
   169  		&FlagEtherRPCL2,
   170  		&FlagIncomingFirewall,
   171  		&FlagOutgoingFirewall,
   172  		&FlagChainID,
   173  		&FlagKeepConnectedOnFail,
   174  		&FlagAutoReconnect,
   175  		&FlagSTUNservers,
   176  		&FlagLocalServiceDiscovery,
   177  		&FlagUDPListenPorts,
   178  		&FlagTraversal,
   179  		&FlagPortCheckServers,
   180  		&FlagStatsReportInterval,
   181  		&FlagDNSListenPort,
   182  	)
   183  }
   184  
   185  // ParseFlagsNetwork function fills in directory options from CLI context
   186  func ParseFlagsNetwork(ctx *cli.Context) {
   187  	Current.ParseStringFlag(ctx, FlagAPIAddress)
   188  	Current.ParseStringFlag(ctx, FlagDiscoveryAddress)
   189  	Current.ParseStringSliceFlag(ctx, FlagBrokerAddress)
   190  	Current.ParseStringSliceFlag(ctx, FlagEtherRPCL1)
   191  	Current.ParseStringSliceFlag(ctx, FlagEtherRPCL2)
   192  	Current.ParseBoolFlag(ctx, FlagPortMapping)
   193  	Current.ParseBoolFlag(ctx, FlagNATHolePunching)
   194  	Current.ParseBoolFlag(ctx, FlagIncomingFirewall)
   195  	Current.ParseBoolFlag(ctx, FlagOutgoingFirewall)
   196  	Current.ParseInt64Flag(ctx, FlagChainID)
   197  	Current.ParseBoolFlag(ctx, FlagKeepConnectedOnFail)
   198  	Current.ParseBoolFlag(ctx, FlagAutoReconnect)
   199  	Current.ParseStringSliceFlag(ctx, FlagSTUNservers)
   200  	Current.ParseBoolFlag(ctx, FlagLocalServiceDiscovery)
   201  	Current.ParseStringFlag(ctx, FlagUDPListenPorts)
   202  	Current.ParseStringFlag(ctx, FlagTraversal)
   203  	Current.ParseStringFlag(ctx, FlagPortCheckServers)
   204  	Current.ParseDurationFlag(ctx, FlagStatsReportInterval)
   205  	Current.ParseIntFlag(ctx, FlagDNSListenPort)
   206  }
   207  
   208  // BlockchainNetwork defines a blockchain network
   209  type BlockchainNetwork string
   210  
   211  var (
   212  	// Mainnet defines the mainnet blockchain network
   213  	Mainnet BlockchainNetwork = "mainnet"
   214  	// Testnet defines the testnet blockchain network
   215  	Testnet BlockchainNetwork = "testnet"
   216  	// Localnet defines the localnet blockchain network
   217  	Localnet BlockchainNetwork = "localnet"
   218  )
   219  
   220  // ParseBlockchainNetwork parses a string argument into blockchain network
   221  func ParseBlockchainNetwork(network string) (BlockchainNetwork, error) {
   222  	if isValidBlockchainNetwork(network) {
   223  		return BlockchainNetwork(strings.ToLower(network)), nil
   224  	}
   225  	return Mainnet, fmt.Errorf("unknown blockchain network: %s", network)
   226  }
   227  
   228  func isValidBlockchainNetwork(network string) bool {
   229  	parsedNetwork := BlockchainNetwork(strings.ToLower(network))
   230  	return parsedNetwork.IsMainnet() || parsedNetwork.IsTestnet() || parsedNetwork.IsLocalnet()
   231  }
   232  
   233  // IsMainnet returns whether the blockchain network is mainnet or not
   234  func (n BlockchainNetwork) IsMainnet() bool {
   235  	return n == Mainnet
   236  }
   237  
   238  // IsTestnet returns whether the blockchain network is testnet or not
   239  func (n BlockchainNetwork) IsTestnet() bool {
   240  	return n == Testnet
   241  }
   242  
   243  // IsLocalnet returns whether the blockchain network is localnet or not
   244  func (n BlockchainNetwork) IsLocalnet() bool {
   245  	return n == Localnet
   246  }
   247  
   248  // ParseFlagsBlockchainNetwork function fills in directory options from CLI context
   249  func ParseFlagsBlockchainNetwork(ctx *cli.Context) {
   250  	Current.ParseBlockchainNetworkFlag(ctx, FlagBlockchainNetwork)
   251  }
   252  
   253  // RegisterFlagsBlockchainNetwork function registers blockchain network flags to flag list
   254  func RegisterFlagsBlockchainNetwork(flags *[]cli.Flag) {
   255  	*flags = append(
   256  		*flags,
   257  		&FlagBlockchainNetwork,
   258  	)
   259  }