github.com/valorbit/go-ethereum@v1.9.11-rc4/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  	"strings"
    25  
    26  	"github.com/valorbit/go-ethereum/p2p"
    27  	"github.com/valorbit/go-ethereum/p2p/nat"
    28  	"github.com/valorbit/go-ethereum/rpc"
    29  )
    30  
    31  const (
    32  	DefaultHTTPHost    = "localhost" // Default host interface for the HTTP RPC server
    33  	DefaultHTTPPort    = 8545        // Default TCP port for the HTTP RPC server
    34  	DefaultWSHost      = "localhost" // Default host interface for the websocket RPC server
    35  	DefaultWSPort      = 8546        // Default TCP port for the websocket RPC server
    36  	DefaultGraphQLHost = "localhost" // Default host interface for the GraphQL server
    37  	DefaultGraphQLPort = 8547        // Default TCP port for the GraphQL server
    38  )
    39  
    40  // DefaultConfig contains reasonable default settings.
    41  var DefaultConfig = Config{
    42  	DataDir:             DefaultDataDir(),
    43  	HTTPPort:            DefaultHTTPPort,
    44  	HTTPModules:         []string{"net", "web3"},
    45  	HTTPVirtualHosts:    []string{"localhost"},
    46  	HTTPTimeouts:        rpc.DefaultHTTPTimeouts,
    47  	WSPort:              DefaultWSPort,
    48  	WSModules:           []string{"net", "web3"},
    49  	GraphQLPort:         DefaultGraphQLPort,
    50  	GraphQLVirtualHosts: []string{"localhost"},
    51  	P2P: p2p.Config{
    52  		ListenAddr: ":30303",
    53  		MaxPeers:   50,
    54  		NAT:        nat.Any(),
    55  	},
    56  }
    57  
    58  // DefaultDataDir is the default data directory to use for the databases and other
    59  // persistence requirements.
    60  func DefaultDataDir() string {
    61  	// Try to place the data folder in the user's home dir
    62  	defaultSubDir := "Valorbit"
    63  	home := homeDir()
    64  	if home != "" {
    65  		switch runtime.GOOS {
    66  		case "darwin":
    67  			return filepath.Join(home, "Library", defaultSubDir)
    68  		case "windows":
    69  			// We used to put everything in %HOME%\AppData\Roaming, but this caused
    70  			// problems with non-typical setups. If this fallback location exists and
    71  			// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
    72  			fallback := filepath.Join(home, "AppData", "Roaming", defaultSubDir)
    73  			appdata := windowsAppData()
    74  			if appdata == "" || isNonEmptyDir(fallback) {
    75  				return fallback
    76  			}
    77  			return filepath.Join(appdata, defaultSubDir)
    78  		default:
    79  			return filepath.Join(home, "."+strings.ToLower(defaultSubDir))
    80  		}
    81  	}
    82  	// As we cannot guess a stable location, return empty and handle later
    83  	return ""
    84  }
    85  
    86  func windowsAppData() string {
    87  	v := os.Getenv("LOCALAPPDATA")
    88  	if v == "" {
    89  		// Windows XP and below don't have LocalAppData. Crash here because
    90  		// we don't support Windows XP and undefining the variable will cause
    91  		// other issues.
    92  		panic("environment variable LocalAppData is undefined")
    93  	}
    94  	return v
    95  }
    96  
    97  func isNonEmptyDir(dir string) bool {
    98  	f, err := os.Open(dir)
    99  	if err != nil {
   100  		return false
   101  	}
   102  	names, _ := f.Readdir(1)
   103  	f.Close()
   104  	return len(names) > 0
   105  }
   106  
   107  func homeDir() string {
   108  	if home := os.Getenv("HOME"); home != "" {
   109  		return home
   110  	}
   111  	if usr, err := user.Current(); err == nil {
   112  		return usr.HomeDir
   113  	}
   114  	return ""
   115  }