consolecmd.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU 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. // go-ethereum 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 General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "fmt"
  19. "os"
  20. "os/signal"
  21. "path/filepath"
  22. "strings"
  23. "syscall"
  24. "github.com/ethereum/go-ethereum/cmd/utils"
  25. "github.com/ethereum/go-ethereum/console"
  26. "github.com/ethereum/go-ethereum/node"
  27. "github.com/ethereum/go-ethereum/rpc"
  28. "gopkg.in/urfave/cli.v1"
  29. )
  30. var (
  31. consoleFlags = []cli.Flag{utils.JSpathFlag, utils.ExecFlag, utils.PreloadJSFlag}
  32. consoleCommand = cli.Command{
  33. Action: utils.MigrateFlags(localConsole),
  34. Name: "console",
  35. Usage: "Start an interactive JavaScript environment",
  36. Flags: append(append(append(nodeFlags, rpcFlags...), consoleFlags...), whisperFlags...),
  37. Category: "CONSOLE COMMANDS",
  38. Description: `
  39. The Geth console is an interactive shell for the JavaScript runtime environment
  40. which exposes a node admin interface as well as the Ðapp JavaScript API.
  41. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console.`,
  42. }
  43. attachCommand = cli.Command{
  44. Action: utils.MigrateFlags(remoteConsole),
  45. Name: "attach",
  46. Usage: "Start an interactive JavaScript environment (connect to node)",
  47. ArgsUsage: "[endpoint]",
  48. Flags: append(consoleFlags, utils.DataDirFlag),
  49. Category: "CONSOLE COMMANDS",
  50. Description: `
  51. The Geth console is an interactive shell for the JavaScript runtime environment
  52. which exposes a node admin interface as well as the Ðapp JavaScript API.
  53. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console.
  54. This command allows to open a console on a running geth node.`,
  55. }
  56. javascriptCommand = cli.Command{
  57. Action: utils.MigrateFlags(ephemeralConsole),
  58. Name: "js",
  59. Usage: "Execute the specified JavaScript files",
  60. ArgsUsage: "<jsfile> [jsfile...]",
  61. Flags: append(nodeFlags, consoleFlags...),
  62. Category: "CONSOLE COMMANDS",
  63. Description: `
  64. The JavaScript VM exposes a node admin interface as well as the Ðapp
  65. JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console`,
  66. }
  67. )
  68. // localConsole starts a new geth node, attaching a JavaScript console to it at the
  69. // same time.
  70. func localConsole(ctx *cli.Context) error {
  71. // Create and start the node based on the CLI flags
  72. node := makeFullNode(ctx)
  73. startNode(ctx, node)
  74. defer node.Stop()
  75. // Attach to the newly started node and start the JavaScript console
  76. client, err := node.Attach()
  77. if err != nil {
  78. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  79. }
  80. config := console.Config{
  81. DataDir: utils.MakeDataDir(ctx),
  82. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  83. Client: client,
  84. Preload: utils.MakeConsolePreloads(ctx),
  85. }
  86. console, err := console.New(config)
  87. if err != nil {
  88. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  89. }
  90. defer console.Stop(false)
  91. // If only a short execution was requested, evaluate and return
  92. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  93. console.Evaluate(script)
  94. return nil
  95. }
  96. // Otherwise print the welcome screen and enter interactive mode
  97. console.Welcome()
  98. console.Interactive()
  99. return nil
  100. }
  101. // remoteConsole will connect to a remote geth instance, attaching a JavaScript
  102. // console to it.
  103. func remoteConsole(ctx *cli.Context) error {
  104. // Attach to a remotely running geth instance and start the JavaScript console
  105. endpoint := ctx.Args().First()
  106. if endpoint == "" {
  107. path := node.DefaultDataDir()
  108. if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
  109. path = ctx.GlobalString(utils.DataDirFlag.Name)
  110. }
  111. if path != "" {
  112. if ctx.GlobalBool(utils.TestnetFlag.Name) {
  113. path = filepath.Join(path, "testnet")
  114. } else if ctx.GlobalBool(utils.RinkebyFlag.Name) {
  115. path = filepath.Join(path, "rinkeby")
  116. }
  117. }
  118. endpoint = fmt.Sprintf("%s/geth.ipc", path)
  119. }
  120. client, err := dialRPC(endpoint)
  121. if err != nil {
  122. utils.Fatalf("Unable to attach to remote geth: %v", err)
  123. }
  124. config := console.Config{
  125. DataDir: utils.MakeDataDir(ctx),
  126. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  127. Client: client,
  128. Preload: utils.MakeConsolePreloads(ctx),
  129. }
  130. console, err := console.New(config)
  131. if err != nil {
  132. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  133. }
  134. defer console.Stop(false)
  135. if script := ctx.GlobalString(utils.ExecFlag.Name); script != "" {
  136. console.Evaluate(script)
  137. return nil
  138. }
  139. // Otherwise print the welcome screen and enter interactive mode
  140. console.Welcome()
  141. console.Interactive()
  142. return nil
  143. }
  144. // dialRPC returns a RPC client which connects to the given endpoint.
  145. // The check for empty endpoint implements the defaulting logic
  146. // for "geth attach" and "geth monitor" with no argument.
  147. func dialRPC(endpoint string) (*rpc.Client, error) {
  148. if endpoint == "" {
  149. endpoint = node.DefaultIPCEndpoint(clientIdentifier)
  150. } else if strings.HasPrefix(endpoint, "rpc:") || strings.HasPrefix(endpoint, "ipc:") {
  151. // Backwards compatibility with geth < 1.5 which required
  152. // these prefixes.
  153. endpoint = endpoint[4:]
  154. }
  155. return rpc.Dial(endpoint)
  156. }
  157. // ephemeralConsole starts a new geth node, attaches an ephemeral JavaScript
  158. // console to it, executes each of the files specified as arguments and tears
  159. // everything down.
  160. func ephemeralConsole(ctx *cli.Context) error {
  161. // Create and start the node based on the CLI flags
  162. node := makeFullNode(ctx)
  163. startNode(ctx, node)
  164. defer node.Stop()
  165. // Attach to the newly started node and start the JavaScript console
  166. client, err := node.Attach()
  167. if err != nil {
  168. utils.Fatalf("Failed to attach to the inproc geth: %v", err)
  169. }
  170. config := console.Config{
  171. DataDir: utils.MakeDataDir(ctx),
  172. DocRoot: ctx.GlobalString(utils.JSpathFlag.Name),
  173. Client: client,
  174. Preload: utils.MakeConsolePreloads(ctx),
  175. }
  176. console, err := console.New(config)
  177. if err != nil {
  178. utils.Fatalf("Failed to start the JavaScript console: %v", err)
  179. }
  180. defer console.Stop(false)
  181. // Evaluate each of the specified JavaScript files
  182. for _, file := range ctx.Args() {
  183. if err = console.Execute(file); err != nil {
  184. utils.Fatalf("Failed to execute %s: %v", file, err)
  185. }
  186. }
  187. // Wait for pending callbacks, but stop for Ctrl-C.
  188. abort := make(chan os.Signal, 1)
  189. signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM)
  190. go func() {
  191. <-abort
  192. os.Exit(0)
  193. }()
  194. console.Stop(true)
  195. return nil
  196. }