defaults.go 4.2 KB

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