wizard_faucet.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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("Reuse 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. // Accessing the reCaptcha service requires API authorizations, request it
  101. if infos.captchaToken != "" {
  102. fmt.Println()
  103. fmt.Println("Reuse previous reCaptcha API authorization (y/n)? (default = yes)")
  104. if w.readDefaultString("y") != "y" {
  105. infos.captchaToken, infos.captchaSecret = "", ""
  106. }
  107. }
  108. if infos.captchaToken == "" {
  109. // No previous authorization (or old one discarded)
  110. fmt.Println()
  111. fmt.Println("Enable reCaptcha protection against robots (y/n)? (default = no)")
  112. if w.readDefaultString("n") == "y" {
  113. // Captcha protection explicitly requested, read the site and secret keys
  114. fmt.Println()
  115. fmt.Printf("What is the reCaptcha site key to authenticate human users?\n")
  116. infos.captchaToken = w.readString()
  117. fmt.Println()
  118. fmt.Printf("What is the reCaptcha secret key to verify authentications? (won't be echoed)\n")
  119. infos.captchaSecret = w.readPassword()
  120. }
  121. }
  122. // Figure out where the user wants to store the persistent data
  123. fmt.Println()
  124. if infos.node.datadir == "" {
  125. fmt.Printf("Where should data be stored on the remote machine?\n")
  126. infos.node.datadir = w.readString()
  127. } else {
  128. fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
  129. infos.node.datadir = w.readDefaultString(infos.node.datadir)
  130. }
  131. // Figure out which port to listen on
  132. fmt.Println()
  133. fmt.Printf("Which TCP/UDP port should the light client listen on? (default = %d)\n", infos.node.portFull)
  134. infos.node.portFull = w.readDefaultInt(infos.node.portFull)
  135. // Set a proper name to report on the stats page
  136. fmt.Println()
  137. if infos.node.ethstats == "" {
  138. fmt.Printf("What should the node be called on the stats page?\n")
  139. infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
  140. } else {
  141. fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.node.ethstats)
  142. infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
  143. }
  144. // Load up the credential needed to release funds
  145. if infos.node.keyJSON != "" {
  146. var key keystore.Key
  147. if err := json.Unmarshal([]byte(infos.node.keyJSON), &key); err != nil {
  148. infos.node.keyJSON, infos.node.keyPass = "", ""
  149. } else {
  150. fmt.Println()
  151. fmt.Printf("Reuse previous (%s) funding account (y/n)? (default = yes)\n", key.Address.Hex())
  152. if w.readDefaultString("y") != "y" {
  153. infos.node.keyJSON, infos.node.keyPass = "", ""
  154. }
  155. }
  156. }
  157. if infos.node.keyJSON == "" {
  158. fmt.Println()
  159. fmt.Println("Please paste the faucet's funding account key JSON:")
  160. infos.node.keyJSON = w.readJSON()
  161. fmt.Println()
  162. fmt.Println("What's the unlock password for the account? (won't be echoed)")
  163. infos.node.keyPass = w.readPassword()
  164. if _, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
  165. log.Error("Failed to decrypt key with given passphrase")
  166. return
  167. }
  168. }
  169. // Try to deploy the faucet server on the host
  170. if out, err := deployFaucet(client, w.network, w.conf.bootLight, infos); err != nil {
  171. log.Error("Failed to deploy faucet container", "err", err)
  172. if len(out) > 0 {
  173. fmt.Printf("%s\n", out)
  174. }
  175. return
  176. }
  177. // All ok, run a network scan to pick any changes up
  178. w.networkStats(false)
  179. }