github.com/bcnmy/go-ethereum@v1.10.27/node/defaults.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser 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  // The go-ethereum library 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 Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package node
    18  
    19  import (
    20  	"os"
    21  	"os/user"
    22  	"path/filepath"
    23  	"runtime"
    24  
    25  	"github.com/ethereum/go-ethereum/p2p"
    26  	"github.com/ethereum/go-ethereum/p2p/nat"
    27  	"github.com/ethereum/go-ethereum/rpc"
    28  )
    29  
    30  const (
    31  	DefaultHTTPHost    = "localhost" // Default host interface for the HTTP RPC server
    32  	DefaultHTTPPort    = 8545        // Default TCP port for the HTTP RPC server
    33  	DefaultWSHost      = "localhost" // Default host interface for the websocket RPC server
    34  	DefaultWSPort      = 8546        // Default TCP port for the websocket RPC server
    35  	DefaultGraphQLHost = "localhost" // Default host interface for the GraphQL server
    36  	DefaultGraphQLPort = 8547        // Default TCP port for the GraphQL server
    37  	DefaultAuthHost    = "localhost" // Default host interface for the authenticated apis
    38  	DefaultAuthPort    = 8551        // Default port for the authenticated apis
    39  )
    40  
    41  var (
    42  	DefaultAuthCors    = []string{"localhost"} // Default cors domain for the authenticated apis
    43  	DefaultAuthVhosts  = []string{"localhost"} // Default virtual hosts for the authenticated apis
    44  	DefaultAuthOrigins = []string{"localhost"} // Default origins for the authenticated apis
    45  	DefaultAuthPrefix  = ""                    // Default prefix for the authenticated apis
    46  	DefaultAuthModules = []string{"eth", "engine"}
    47  )
    48  
    49  // DefaultConfig contains reasonable default settings.
    50  var DefaultConfig = Config{
    51  	DataDir:             DefaultDataDir(),
    52  	HTTPPort:            DefaultHTTPPort,
    53  	AuthAddr:            DefaultAuthHost,
    54  	AuthPort:            DefaultAuthPort,
    55  	AuthVirtualHosts:    DefaultAuthVhosts,
    56  	HTTPModules:         []string{"net", "web3"},
    57  	HTTPVirtualHosts:    []string{"localhost"},
    58  	HTTPTimeouts:        rpc.DefaultHTTPTimeouts,
    59  	WSPort:              DefaultWSPort,
    60  	WSModules:           []string{"net", "web3"},
    61  	GraphQLVirtualHosts: []string{"localhost"},
    62  	P2P: p2p.Config{
    63  		ListenAddr: ":30303",
    64  		MaxPeers:   50,
    65  		NAT:        nat.Any(),
    66  	},
    67  }
    68  
    69  // DefaultDataDir is the default data directory to use for the databases and other
    70  // persistence requirements.
    71  func DefaultDataDir() string {
    72  	// Try to place the data folder in the user's home dir
    73  	home := homeDir()
    74  	if home != "" {
    75  		switch runtime.GOOS {
    76  		case "darwin":
    77  			return filepath.Join(home, "Library", "Ethereum")
    78  		case "windows":
    79  			// We used to put everything in %HOME%\AppData\Roaming, but this caused
    80  			// problems with non-typical setups. If this fallback location exists and
    81  			// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
    82  			fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
    83  			appdata := windowsAppData()
    84  			if appdata == "" || isNonEmptyDir(fallback) {
    85  				return fallback
    86  			}
    87  			return filepath.Join(appdata, "Ethereum")
    88  		default:
    89  			return filepath.Join(home, ".ethereum")
    90  		}
    91  	}
    92  	// As we cannot guess a stable location, return empty and handle later
    93  	return ""
    94  }
    95  
    96  func windowsAppData() string {
    97  	v := os.Getenv("LOCALAPPDATA")
    98  	if v == "" {
    99  		// Windows XP and below don't have LocalAppData. Crash here because
   100  		// we don't support Windows XP and undefining the variable will cause
   101  		// other issues.
   102  		panic("environment variable LocalAppData is undefined")
   103  	}
   104  	return v
   105  }
   106  
   107  func isNonEmptyDir(dir string) bool {
   108  	f, err := os.Open(dir)
   109  	if err != nil {
   110  		return false
   111  	}
   112  	names, _ := f.Readdir(1)
   113  	f.Close()
   114  	return len(names) > 0
   115  }
   116  
   117  func homeDir() string {
   118  	if home := os.Getenv("HOME"); home != "" {
   119  		return home
   120  	}
   121  	if usr, err := user.Current(); err == nil {
   122  		return usr.HomeDir
   123  	}
   124  	return ""
   125  }