github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/defaults.go (about) 1 package launcher 2 3 import ( 4 "fmt" 5 "os" 6 "os/user" 7 "path/filepath" 8 "runtime" 9 "time" 10 11 "github.com/unicornultrafoundation/go-u2u/cmd/utils" 12 "github.com/unicornultrafoundation/go-u2u/node" 13 "github.com/unicornultrafoundation/go-u2u/p2p" 14 "github.com/unicornultrafoundation/go-u2u/p2p/nat" 15 "github.com/unicornultrafoundation/go-u2u/rpc" 16 ) 17 18 const ( 19 DefaultP2PPort = 5050 // Default p2p port for listening 20 DefaultHTTPPort = 18545 // Default TCP port for the HTTP RPC server 21 DefaultWSPort = 18546 // Default TCP port for the websocket RPC server 22 ) 23 24 func overrideFlags() { 25 utils.ListenPortFlag.Value = DefaultP2PPort 26 utils.HTTPPortFlag.Value = DefaultHTTPPort 27 utils.LegacyRPCPortFlag.Value = DefaultHTTPPort 28 utils.WSPortFlag.Value = DefaultWSPort 29 } 30 31 // NodeDefaultConfig contains reasonable default settings. 32 var NodeDefaultConfig = node.Config{ 33 DataDir: DefaultDataDir(), 34 HTTPPort: DefaultHTTPPort, 35 HTTPModules: []string{}, 36 HTTPVirtualHosts: []string{"localhost"}, 37 HTTPTimeouts: rpc.HTTPTimeouts{ 38 ReadTimeout: 120 * time.Second, 39 IdleTimeout: 120 * time.Second, 40 WriteTimeout: 120 * time.Second, 41 }, 42 WSPort: DefaultWSPort, 43 WSModules: []string{}, 44 GraphQLVirtualHosts: []string{"localhost"}, 45 P2P: p2p.Config{ 46 NoDiscovery: false, // enable discovery v4 by default 47 DiscoveryV5: true, // enable discovery v5 by default 48 ListenAddr: fmt.Sprintf(":%d", DefaultP2PPort), 49 MaxPeers: 50, 50 NAT: nat.Any(), 51 }, 52 } 53 54 // DefaultDataDir is the default data directory to use for the databases and other 55 // persistence requirements. 56 func DefaultDataDir() string { 57 // Try to place the data folder in the user's home dir 58 home := homeDir() 59 if home != "" { 60 switch runtime.GOOS { 61 case "darwin": 62 return filepath.Join(home, "Library", "U2UDATA") 63 case "windows": 64 // We used to put everything in %HOME%\AppData\Roaming, but this caused 65 // problems with non-typical setups. If this fallback location exists and 66 // is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%. 67 fallback := filepath.Join(home, "AppData", "Roaming", "U2UDATA") 68 appdata := windowsAppData() 69 if appdata == "" || isNonEmptyDir(fallback) { 70 return fallback 71 } 72 return filepath.Join(appdata, "U2UDATA") 73 default: 74 return filepath.Join(home, ".u2u") 75 } 76 } 77 // As we cannot guess a stable location, return empty and handle later 78 return "" 79 } 80 81 func windowsAppData() string { 82 v := os.Getenv("LOCALAPPDATA") 83 if v == "" { 84 // Windows XP and below don't have LocalAppData. Crash here because 85 // we don't support Windows XP and undefining the variable will cause 86 // other issues. 87 panic("environment variable LocalAppData is undefined") 88 } 89 return v 90 } 91 92 func isNonEmptyDir(dir string) bool { 93 f, err := os.Open(dir) 94 if err != nil { 95 return false 96 } 97 names, _ := f.Readdir(1) 98 f.Close() 99 return len(names) > 0 100 } 101 102 func homeDir() string { 103 if home := os.Getenv("HOME"); home != "" { 104 return home 105 } 106 if usr, err := user.Current(); err == nil { 107 return usr.HomeDir 108 } 109 return "" 110 }