github.com/codingfuture/orig-energi3@v0.8.4/console/console.go (about) 1 // Copyright 2018 The Energi Core Authors 2 // Copyright 2016 The go-ethereum Authors 3 // This file is part of the Energi Core library. 4 // 5 // The Energi Core library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Energi Core library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>. 17 18 package console 19 20 import ( 21 "fmt" 22 "io" 23 "io/ioutil" 24 "os" 25 "os/signal" 26 "path/filepath" 27 "regexp" 28 "sort" 29 "strings" 30 "syscall" 31 32 "github.com/ethereum/go-ethereum/internal/jsre" 33 "github.com/ethereum/go-ethereum/internal/web3ext" 34 "github.com/ethereum/go-ethereum/log" 35 "github.com/ethereum/go-ethereum/rpc" 36 "github.com/mattn/go-colorable" 37 "github.com/peterh/liner" 38 "github.com/robertkrimen/otto" 39 ) 40 41 var ( 42 passwordRegexp = regexp.MustCompile(`personal.[nus]`) 43 onlyWhitespace = regexp.MustCompile(`^\s*$`) 44 exit = regexp.MustCompile(`^\s*exit\s*;*\s*$`) 45 ) 46 47 // HistoryFile is the file within the data directory to store input scrollback. 48 const HistoryFile = "history" 49 50 // DefaultPrompt is the default prompt line prefix to use for user input querying. 51 const DefaultPrompt = "> " 52 53 // Config is the collection of configurations to fine tune the behavior of the 54 // JavaScript console. 55 type Config struct { 56 DataDir string // Data directory to store the console history at 57 DocRoot string // Filesystem path from where to load JavaScript files from 58 Client *rpc.Client // RPC client to execute Ethereum requests through 59 Prompt string // Input prompt prefix string (defaults to DefaultPrompt) 60 Prompter UserPrompter // Input prompter to allow interactive user feedback (defaults to TerminalPrompter) 61 Printer io.Writer // Output writer to serialize any display strings to (defaults to os.Stdout) 62 Preload []string // Absolute paths to JavaScript files to preload 63 } 64 65 // Console is a JavaScript interpreted runtime environment. It is a fully fledged 66 // JavaScript console attached to a running node via an external or in-process RPC 67 // client. 68 type Console struct { 69 client *rpc.Client // RPC client to execute Ethereum requests through 70 jsre *jsre.JSRE // JavaScript runtime environment running the interpreter 71 prompt string // Input prompt prefix string 72 prompter UserPrompter // Input prompter to allow interactive user feedback 73 histPath string // Absolute path to the console scrollback history 74 history []string // Scroll history maintained by the console 75 printer io.Writer // Output writer to serialize any display strings to 76 77 passMasking *maskingJSRE 78 } 79 80 // New initializes a JavaScript interpreted runtime environment and sets defaults 81 // with the config struct. 82 func New(config Config) (*Console, error) { 83 // Handle unset config values gracefully 84 if config.Prompter == nil { 85 config.Prompter = Stdin 86 } 87 if config.Prompt == "" { 88 config.Prompt = DefaultPrompt 89 } 90 if config.Printer == nil { 91 config.Printer = colorable.NewColorableStdout() 92 } 93 // Initialize the console and return 94 console := &Console{ 95 client: config.Client, 96 jsre: jsre.New(config.DocRoot, config.Printer), 97 prompt: config.Prompt, 98 prompter: config.Prompter, 99 printer: config.Printer, 100 histPath: filepath.Join(config.DataDir, HistoryFile), 101 } 102 if err := os.MkdirAll(config.DataDir, 0700); err != nil { 103 return nil, err 104 } 105 if err := console.init(config.Preload); err != nil { 106 return nil, err 107 } 108 return console, nil 109 } 110 111 // init retrieves the available APIs from the remote RPC provider and initializes 112 // the console's JavaScript namespaces based on the exposed modules. 113 func (c *Console) init(preload []string) error { 114 // Initialize the JavaScript <-> Go RPC bridge 115 bridge := newBridge(c.client, c.prompter, c.printer) 116 c.jsre.Set("jeth", struct{}{}) 117 118 jethObj, _ := c.jsre.Get("jeth") 119 jethObj.Object().Set("send", bridge.Send) 120 jethObj.Object().Set("sendAsync", bridge.Send) 121 122 var err error 123 c.passMasking, err = newMaskingJSRE(bridge.Send) 124 if err != nil { 125 return err 126 } 127 128 consoleObj, _ := c.jsre.Get("console") 129 consoleObj.Object().Set("log", c.consoleOutput) 130 consoleObj.Object().Set("error", c.consoleOutput) 131 132 // Load all the internal utility JavaScript libraries 133 if err := c.jsre.Compile("bignumber.js", jsre.BigNumber_JS); err != nil { 134 return fmt.Errorf("bignumber.js: %v", err) 135 } 136 if err := c.jsre.Compile("web3.js", jsre.Web3_JS); err != nil { 137 return fmt.Errorf("web3.js: %v", err) 138 } 139 if _, err := c.jsre.Run("var Web3 = require('web3');"); err != nil { 140 return fmt.Errorf("web3 require: %v", err) 141 } 142 if _, err := c.jsre.Run("var web3 = new Web3(jeth);"); err != nil { 143 return fmt.Errorf("web3 provider: %v", err) 144 } 145 // Load the supported APIs into the JavaScript runtime environment 146 apis, err := c.client.SupportedModules() 147 if err != nil { 148 return fmt.Errorf("api modules: %v", err) 149 } 150 flatten := "var eth = web3.eth; var personal = web3.personal; " 151 for api := range apis { 152 if api == "web3" { 153 continue // manually mapped or ignore 154 } 155 if file, ok := web3ext.Modules[api]; ok { 156 // Load our extension for the module. 157 if err = c.jsre.Compile(fmt.Sprintf("%s.js", api), file); err != nil { 158 return fmt.Errorf("%s.js: %v", api, err) 159 } 160 flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) 161 } else if obj, err := c.jsre.Run("web3." + api); err == nil && obj.IsObject() { 162 // Enable web3.js built-in extension if available. 163 flatten += fmt.Sprintf("var %s = web3.%s; ", api, api) 164 } 165 } 166 if _, err = c.jsre.Run(flatten); err != nil { 167 return fmt.Errorf("namespace flattening: %v", err) 168 } 169 // Initialize the global name register (disabled for now) 170 //c.jsre.Run(`var GlobalRegistrar = eth.contract(` + registrar.GlobalRegistrarAbi + `); registrar = GlobalRegistrar.at("` + registrar.GlobalRegistrarAddr + `");`) 171 172 // If the console is in interactive mode, instrument password related methods to query the user 173 if c.prompter != nil { 174 // Retrieve the account management object to instrument 175 personal, err := c.jsre.Get("personal") 176 if err != nil { 177 return err 178 } 179 // Override the openWallet, unlockAccount, newAccount and sign methods since 180 // these require user interaction. Assign these method in the Console the 181 // original web3 callbacks. These will be called by the jeth.* methods after 182 // they got the password from the user and send the original web3 request to 183 // the backend. 184 if obj := personal.Object(); obj != nil { // make sure the personal api is enabled over the interface 185 if _, err = c.jsre.Run(`jeth.openWallet = personal.openWallet;`); err != nil { 186 return fmt.Errorf("personal.openWallet: %v", err) 187 } 188 if _, err = c.jsre.Run(`jeth.unlockAccount = personal.unlockAccount;`); err != nil { 189 return fmt.Errorf("personal.unlockAccount: %v", err) 190 } 191 if _, err = c.jsre.Run(`jeth.newAccount = personal.newAccount;`); err != nil { 192 return fmt.Errorf("personal.newAccount: %v", err) 193 } 194 if _, err = c.jsre.Run(`jeth.sign = personal.sign;`); err != nil { 195 return fmt.Errorf("personal.sign: %v", err) 196 } 197 obj.Set("openWallet", bridge.OpenWallet) 198 obj.Set("unlockAccount", bridge.UnlockAccount) 199 obj.Set("newAccount", bridge.NewAccount) 200 obj.Set("sign", bridge.Sign) 201 } 202 } 203 // The admin.sleep and admin.sleepBlocks are offered by the console and not by the RPC layer. 204 admin, err := c.jsre.Get("admin") 205 if err != nil { 206 return err 207 } 208 if obj := admin.Object(); obj != nil { // make sure the admin api is enabled over the interface 209 obj.Set("sleepBlocks", bridge.SleepBlocks) 210 obj.Set("sleep", bridge.Sleep) 211 obj.Set("clearHistory", c.clearHistory) 212 } 213 // Preload any JavaScript files before starting the console 214 for _, path := range preload { 215 if err := c.jsre.Exec(path); err != nil { 216 failure := err.Error() 217 if ottoErr, ok := err.(*otto.Error); ok { 218 failure = ottoErr.String() 219 } 220 return fmt.Errorf("%s: %v", path, failure) 221 } 222 } 223 // Configure the console's input prompter for scrollback and tab completion 224 if c.prompter != nil { 225 if content, err := ioutil.ReadFile(c.histPath); err != nil { 226 c.prompter.SetHistory(nil) 227 } else { 228 history := strings.Split(string(content), "\n") 229 // Mask all passwords in the previous history. 230 for _, cmd := range history { 231 if !c.passMasking.IsPasswordMasked(cmd) { 232 cmd, err = c.passMasking.MaskPassword(cmd) 233 if err != nil { 234 log.Debug("Passphrase masking failed", "err", err) 235 } 236 } 237 if err == nil { 238 // Only append commands with correct format to the history file. 239 c.history = append(c.history, cmd) 240 } 241 } 242 c.prompter.SetHistory(c.history) 243 } 244 c.prompter.SetWordCompleter(c.AutoCompleteInput) 245 } 246 return nil 247 } 248 249 func (c *Console) clearHistory() { 250 c.history = nil 251 c.prompter.ClearHistory() 252 if err := os.Remove(c.histPath); err != nil { 253 fmt.Fprintln(c.printer, "can't delete history file:", err) 254 } else { 255 fmt.Fprintln(c.printer, "history file deleted") 256 } 257 } 258 259 // consoleOutput is an override for the console.log and console.error methods to 260 // stream the output into the configured output stream instead of stdout. 261 func (c *Console) consoleOutput(call otto.FunctionCall) otto.Value { 262 output := []string{} 263 for _, argument := range call.ArgumentList { 264 output = append(output, fmt.Sprintf("%v", argument)) 265 } 266 fmt.Fprintln(c.printer, strings.Join(output, " ")) 267 return otto.Value{} 268 } 269 270 // AutoCompleteInput is a pre-assembled word completer to be used by the user 271 // input prompter to provide hints to the user about the methods available. 272 func (c *Console) AutoCompleteInput(line string, pos int) (string, []string, string) { 273 // No completions can be provided for empty inputs 274 if len(line) == 0 || pos == 0 { 275 return "", nil, "" 276 } 277 // Chunck data to relevant part for autocompletion 278 // E.g. in case of nested lines eth.getBalance(eth.coinb<tab><tab> 279 start := pos - 1 280 for ; start > 0; start-- { 281 // Skip all methods and namespaces (i.e. including the dot) 282 if line[start] == '.' || (line[start] >= 'a' && line[start] <= 'z') || (line[start] >= 'A' && line[start] <= 'Z') { 283 continue 284 } 285 // Handle web3 in a special way (i.e. other numbers aren't auto completed) 286 if start >= 3 && line[start-3:start] == "web3" { 287 start -= 3 288 continue 289 } 290 // We've hit an unexpected character, autocomplete form here 291 start++ 292 break 293 } 294 return line[:start], c.jsre.CompleteKeywords(line[start:pos]), line[pos:] 295 } 296 297 // Welcome show summary of current Geth instance and some metadata about the 298 // console's available modules. 299 func (c *Console) Welcome() { 300 // Print some generic Geth metadata 301 fmt.Fprintf(c.printer, "Welcome to the Energi Core JavaScript console!\n\n") 302 c.jsre.Run(` 303 console.log("instance: " + web3.version.node); 304 console.log("coinbase: " + eth.coinbase); 305 console.log("at block: " + eth.blockNumber + " (" + new Date(1000 * eth.getBlock(eth.blockNumber).timestamp) + ")"); 306 console.log(" datadir: " + admin.datadir); 307 `) 308 // List all the supported modules for the user to call 309 if apis, err := c.client.SupportedModules(); err == nil { 310 modules := make([]string, 0, len(apis)) 311 for api, version := range apis { 312 modules = append(modules, fmt.Sprintf("%s:%s", api, version)) 313 } 314 sort.Strings(modules) 315 fmt.Fprintln(c.printer, " modules:", strings.Join(modules, " ")) 316 } 317 fmt.Fprintln(c.printer) 318 } 319 320 // Evaluate executes code and pretty prints the result to the specified output 321 // stream. 322 func (c *Console) Evaluate(statement string) error { 323 defer func() { 324 if r := recover(); r != nil { 325 fmt.Fprintf(c.printer, "[native] error: %v\n", r) 326 } 327 }() 328 return c.jsre.Evaluate(statement, c.printer) 329 } 330 331 // Interactive starts an interactive user session, where input is propted from 332 // the configured user prompter. 333 func (c *Console) Interactive() { 334 var ( 335 prompt = c.prompt // Current prompt line (used for multi-line inputs) 336 indents = 0 // Current number of input indents (used for multi-line inputs) 337 input = "" // Current user input 338 scheduler = make(chan string) // Channel to send the next prompt on and receive the input 339 ) 340 // Start a goroutine to listen for prompt requests and send back inputs 341 go func() { 342 for { 343 // Read the next user input 344 line, err := c.prompter.PromptInput(<-scheduler) 345 if err != nil { 346 // In case of an error, either clear the prompt or fail 347 if err == liner.ErrPromptAborted { // ctrl-C 348 prompt, indents, input = c.prompt, 0, "" 349 scheduler <- "" 350 continue 351 } 352 close(scheduler) 353 return 354 } 355 // User input retrieved, send for interpretation and loop 356 scheduler <- line 357 } 358 }() 359 // Monitor Ctrl-C too in case the input is empty and we need to bail 360 abort := make(chan os.Signal, 1) 361 signal.Notify(abort, syscall.SIGINT, syscall.SIGTERM) 362 363 // Start sending prompts to the user and reading back inputs 364 for { 365 // Send the next prompt, triggering an input read and process the result 366 scheduler <- prompt 367 select { 368 case <-abort: 369 // User forcefully quite the console 370 fmt.Fprintln(c.printer, "caught interrupt, exiting") 371 return 372 373 case line, ok := <-scheduler: 374 // User input was returned by the prompter, handle special cases 375 if !ok || (indents <= 0 && exit.MatchString(line)) { 376 return 377 } 378 if onlyWhitespace.MatchString(line) { 379 continue 380 } 381 // Append the line to the input and check for multi-line interpretation 382 input += line + "\n" 383 384 indents = countIndents(input) 385 if indents <= 0 { 386 prompt = c.prompt 387 } else { 388 prompt = strings.Repeat(".", indents*3) + " " 389 } 390 // If all the needed lines are present, save the command and run 391 if indents <= 0 { 392 var err error 393 if len(input) > 0 && input[0] != ' ' && !passwordRegexp.MatchString(input) { 394 if command := strings.TrimSpace(input); len(c.history) == 0 || command != c.history[len(c.history)-1] { 395 // if password not masked, mask it then. 396 if !c.passMasking.IsPasswordMasked(command) { 397 command, err = c.passMasking.MaskPassword(command) 398 if err != nil { 399 log.Debug("Passphrase masking failed", "err", err) 400 } 401 } 402 403 if err == nil { 404 // Only append commands with correct format to the history file. 405 c.history = append(c.history, command) 406 if c.prompter != nil { 407 c.prompter.AppendHistory(command) 408 } 409 } 410 } 411 } 412 413 // if masked password is found, prompt new password input. 414 if c.passMasking.IsPasswordMasked(input) { 415 pass, err := c.prompter.PromptPassword("Re-enter passphrase again: ") 416 if err != nil { 417 fmt.Fprintf(c.printer, "[native] error: %v\n", err) 418 continue 419 } 420 input, err = c.passMasking.UnMaskPassword(input, pass) 421 if err != nil { 422 log.Debug("Passphrase unmasking failed", "err", err) 423 } 424 } 425 426 c.Evaluate(input) 427 input = "" 428 } 429 } 430 } 431 } 432 433 // countIndents returns the number of identations for the given input. 434 // In case of invalid input such as var a = } the result can be negative. 435 func countIndents(input string) int { 436 var ( 437 indents = 0 438 inString = false 439 strOpenChar = ' ' // keep track of the string open char to allow var str = "I'm ...."; 440 charEscaped = false // keep track if the previous char was the '\' char, allow var str = "abc\"def"; 441 ) 442 443 for _, c := range input { 444 switch c { 445 case '\\': 446 // indicate next char as escaped when in string and previous char isn't escaping this backslash 447 if !charEscaped && inString { 448 charEscaped = true 449 } 450 case '\'', '"': 451 if inString && !charEscaped && strOpenChar == c { // end string 452 inString = false 453 } else if !inString && !charEscaped { // begin string 454 inString = true 455 strOpenChar = c 456 } 457 charEscaped = false 458 case '{', '(': 459 if !inString { // ignore brackets when in string, allow var str = "a{"; without indenting 460 indents++ 461 } 462 charEscaped = false 463 case '}', ')': 464 if !inString { 465 indents-- 466 } 467 charEscaped = false 468 default: 469 charEscaped = false 470 } 471 } 472 473 return indents 474 } 475 476 // Execute runs the JavaScript file specified as the argument. 477 func (c *Console) Execute(path string) error { 478 return c.jsre.Exec(path) 479 } 480 481 // Stop cleans up the console and terminates the runtime environment. 482 func (c *Console) Stop(graceful bool) error { 483 if err := ioutil.WriteFile(c.histPath, []byte(strings.Join(c.history, "\n")), 0600); err != nil { 484 return err 485 } 486 if err := os.Chmod(c.histPath, 0600); err != nil { // Force 0600, even if it was different previously 487 return err 488 } 489 c.jsre.Stop(graceful) 490 return nil 491 }