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