wizard_faucet.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2017 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. "encoding/json"
  19. "fmt"
  20. "net/http"
  21. "github.com/ethereum/go-ethereum/accounts/keystore"
  22. "github.com/ethereum/go-ethereum/log"
  23. )
  24. // deployFaucet queries the user for various input on deploying a faucet, after
  25. // which it executes it.
  26. func (w *wizard) deployFaucet() {
  27. // Select the server to interact with
  28. server := w.selectServer()
  29. if server == "" {
  30. return
  31. }
  32. client := w.servers[server]
  33. // Retrieve any active faucet configurations from the server
  34. infos, err := checkFaucet(client, w.network)
  35. if err != nil {
  36. infos = &faucetInfos{
  37. node: &nodeInfos{portFull: 30303, peersTotal: 25},
  38. port: 80,
  39. host: client.server,
  40. amount: 1,
  41. minutes: 1440,
  42. }
  43. }
  44. infos.node.genesis, _ = json.MarshalIndent(w.conf.genesis, "", " ")
  45. infos.node.network = w.conf.genesis.Config.ChainId.Int64()
  46. // Figure out which port to listen on
  47. fmt.Println()
  48. fmt.Printf("Which port should the faucet listen on? (default = %d)\n", infos.port)
  49. infos.port = w.readDefaultInt(infos.port)
  50. // Figure which virtual-host to deploy ethstats on
  51. if infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host); err != nil {
  52. log.Error("Failed to decide on faucet host", "err", err)
  53. return
  54. }
  55. // Port and proxy settings retrieved, figure out the funcing amount per perdion configurations
  56. fmt.Println()
  57. fmt.Printf("How many Ethers to release per request? (default = %d)\n", infos.amount)
  58. infos.amount = w.readDefaultInt(infos.amount)
  59. fmt.Println()
  60. fmt.Printf("How many minutes to enforce between requests? (default = %d)\n", infos.minutes)
  61. infos.minutes = w.readDefaultInt(infos.minutes)
  62. // Accessing GitHub gists requires API authorization, retrieve it
  63. if infos.githubUser != "" {
  64. fmt.Println()
  65. fmt.Printf("Reused previous (%s) GitHub API authorization (y/n)? (default = yes)\n", infos.githubUser)
  66. if w.readDefaultString("y") != "y" {
  67. infos.githubUser, infos.githubToken = "", ""
  68. }
  69. }
  70. if infos.githubUser == "" {
  71. // No previous authorization (or new one requested)
  72. fmt.Println()
  73. fmt.Println("Which GitHub user to verify Gists through?")
  74. infos.githubUser = w.readString()
  75. fmt.Println()
  76. fmt.Println("What is the GitHub personal access token of the user? (won't be echoed)")
  77. infos.githubToken = w.readPassword()
  78. // Do a sanity check query against github to ensure it's valid
  79. req, _ := http.NewRequest("GET", "https://api.github.com/user", nil)
  80. req.SetBasicAuth(infos.githubUser, infos.githubToken)
  81. res, err := http.DefaultClient.Do(req)
  82. if err != nil {
  83. log.Error("Failed to verify GitHub authentication", "err", err)
  84. return
  85. }
  86. defer res.Body.Close()
  87. var msg struct {
  88. Login string `json:"login"`
  89. Message string `json:"message"`
  90. }
  91. if err = json.NewDecoder(res.Body).Decode(&msg); err != nil {
  92. log.Error("Failed to decode authorization response", "err", err)
  93. return
  94. }
  95. if msg.Login != infos.githubUser {
  96. log.Error("GitHub authorization failed", "user", infos.githubUser, "message", msg.Message)
  97. return
  98. }
  99. }
  100. // Figure out where the user wants to store the persistent data
  101. fmt.Println()
  102. if infos.node.datadir == "" {
  103. fmt.Printf("Where should data be stored on the remote machine?\n")
  104. infos.node.datadir = w.readString()
  105. } else {
  106. fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
  107. infos.node.datadir = w.readDefaultString(infos.node.datadir)
  108. }
  109. // Figure out which port to listen on
  110. fmt.Println()
  111. fmt.Printf("Which TCP/UDP port should the light client listen on? (default = %d)\n", infos.node.portFull)
  112. infos.node.portFull = w.readDefaultInt(infos.node.portFull)
  113. // Set a proper name to report on the stats page
  114. fmt.Println()
  115. if infos.node.ethstats == "" {
  116. fmt.Printf("What should the node be called on the stats page?\n")
  117. infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
  118. } else {
  119. fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.node.ethstats)
  120. infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
  121. }
  122. // Load up the credential needed to release funds
  123. if infos.node.keyJSON != "" {
  124. var key keystore.Key
  125. if err := json.Unmarshal([]byte(infos.node.keyJSON), &key); err != nil {
  126. infos.node.keyJSON, infos.node.keyPass = "", ""
  127. } else {
  128. fmt.Println()
  129. fmt.Printf("Reuse previous (%s) funding account (y/n)? (default = yes)\n", key.Address.Hex())
  130. if w.readDefaultString("y") != "y" {
  131. infos.node.keyJSON, infos.node.keyPass = "", ""
  132. }
  133. }
  134. }
  135. if infos.node.keyJSON == "" {
  136. fmt.Println()
  137. fmt.Println("Please paste the faucet's funding account key JSON:")
  138. infos.node.keyJSON = w.readJSON()
  139. fmt.Println()
  140. fmt.Println("What's the unlock password for the account? (won't be echoed)")
  141. infos.node.keyPass = w.readPassword()
  142. if _, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
  143. log.Error("Failed to decrypt key with given passphrase")
  144. return
  145. }
  146. }
  147. // Try to deploy the faucet server on the host
  148. if out, err := deployFaucet(client, w.network, w.conf.bootLight, infos); err != nil {
  149. log.Error("Failed to deploy faucet container", "err", err)
  150. if len(out) > 0 {
  151. fmt.Printf("%s\n", out)
  152. }
  153. return
  154. }
  155. // All ok, run a network scan to pick any changes up
  156. w.networkStats(false)
  157. }