github.com/core-coin/go-core/v2@v2.1.9/node/defaults.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core 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/core-coin/go-core/v2/p2p"
    26  	"github.com/core-coin/go-core/v2/p2p/nat"
    27  	"github.com/core-coin/go-core/v2/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{"xcb", "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:      ":30300",
    64  		MaxPeers:        128,
    65  		MaxPendingPeers: 70,
    66  		NAT:             nat.Any(),
    67  	},
    68  }
    69  
    70  // DefaultDataDir is the default data directory to use for the databases and other
    71  // persistence requirements.
    72  func DefaultDataDir() string {
    73  	// Try to place the data folder in the user's home dir
    74  	home := homeDir()
    75  	if home != "" {
    76  		switch runtime.GOOS {
    77  		case "darwin":
    78  			return filepath.Join(home, "Library", "Core")
    79  		case "windows":
    80  			// We used to put everything in %HOME%\AppData\Roaming, but this caused
    81  			// problems with non-typical setups. If this fallback location exists and
    82  			// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
    83  			fallback := filepath.Join(home, "AppData", "Roaming", "Core")
    84  			appdata := windowsAppData()
    85  			if appdata == "" || isNonEmptyDir(fallback) {
    86  				return fallback
    87  			}
    88  			return filepath.Join(appdata, "Core")
    89  		default:
    90  			return filepath.Join(home, ".core")
    91  		}
    92  	}
    93  	// As we cannot guess a stable location, return empty and handle later
    94  	return ""
    95  }
    96  
    97  func windowsAppData() string {
    98  	v := os.Getenv("LOCALAPPDATA")
    99  	if v == "" {
   100  		// Windows XP and below don't have LocalAppData. Crash here because
   101  		// we don't support Windows XP and undefining the variable will cause
   102  		// other issues.
   103  		panic("environment variable LocalAppData is undefined")
   104  	}
   105  	return v
   106  }
   107  
   108  func isNonEmptyDir(dir string) bool {
   109  	f, err := os.Open(dir)
   110  	if err != nil {
   111  		return false
   112  	}
   113  	names, _ := f.Readdir(1)
   114  	f.Close()
   115  	return len(names) > 0
   116  }
   117  
   118  func homeDir() string {
   119  	if home := os.Getenv("HOME"); home != "" {
   120  		return home
   121  	}
   122  	if usr, err := user.Current(); err == nil {
   123  		return usr.HomeDir
   124  	}
   125  	return ""
   126  }