github.hscsec.cn/scroll-tech/go-ethereum@v1.9.7/cmd/puppeth/wizard_faucet.go (about)

     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  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  
    23  	"github.com/ethereum/go-ethereum/accounts/keystore"
    24  	"github.com/ethereum/go-ethereum/log"
    25  )
    26  
    27  // deployFaucet queries the user for various input on deploying a faucet, after
    28  // which it executes it.
    29  func (w *wizard) deployFaucet() {
    30  	// Select the server to interact with
    31  	server := w.selectServer()
    32  	if server == "" {
    33  		return
    34  	}
    35  	client := w.servers[server]
    36  
    37  	// Retrieve any active faucet configurations from the server
    38  	infos, err := checkFaucet(client, w.network)
    39  	if err != nil {
    40  		infos = &faucetInfos{
    41  			node:    &nodeInfos{port: 30303, peersTotal: 25},
    42  			port:    80,
    43  			host:    client.server,
    44  			amount:  1,
    45  			minutes: 1440,
    46  			tiers:   3,
    47  		}
    48  	}
    49  	existed := err == nil
    50  
    51  	infos.node.genesis, _ = json.MarshalIndent(w.conf.Genesis, "", "  ")
    52  	infos.node.network = w.conf.Genesis.Config.ChainID.Int64()
    53  
    54  	// Figure out which port to listen on
    55  	fmt.Println()
    56  	fmt.Printf("Which port should the faucet listen on? (default = %d)\n", infos.port)
    57  	infos.port = w.readDefaultInt(infos.port)
    58  
    59  	// Figure which virtual-host to deploy ethstats on
    60  	if infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host); err != nil {
    61  		log.Error("Failed to decide on faucet host", "err", err)
    62  		return
    63  	}
    64  	// Port and proxy settings retrieved, figure out the funding amount per period configurations
    65  	fmt.Println()
    66  	fmt.Printf("How many Ethers to release per request? (default = %d)\n", infos.amount)
    67  	infos.amount = w.readDefaultInt(infos.amount)
    68  
    69  	fmt.Println()
    70  	fmt.Printf("How many minutes to enforce between requests? (default = %d)\n", infos.minutes)
    71  	infos.minutes = w.readDefaultInt(infos.minutes)
    72  
    73  	fmt.Println()
    74  	fmt.Printf("How many funding tiers to feature (x2.5 amounts, x3 timeout)? (default = %d)\n", infos.tiers)
    75  	infos.tiers = w.readDefaultInt(infos.tiers)
    76  	if infos.tiers == 0 {
    77  		log.Error("At least one funding tier must be set")
    78  		return
    79  	}
    80  	// Accessing the reCaptcha service requires API authorizations, request it
    81  	if infos.captchaToken != "" {
    82  		fmt.Println()
    83  		fmt.Println("Reuse previous reCaptcha API authorization (y/n)? (default = yes)")
    84  		if !w.readDefaultYesNo(true) {
    85  			infos.captchaToken, infos.captchaSecret = "", ""
    86  		}
    87  	}
    88  	if infos.captchaToken == "" {
    89  		// No previous authorization (or old one discarded)
    90  		fmt.Println()
    91  		fmt.Println("Enable reCaptcha protection against robots (y/n)? (default = no)")
    92  		if !w.readDefaultYesNo(false) {
    93  			log.Warn("Users will be able to requests funds via automated scripts")
    94  		} else {
    95  			// Captcha protection explicitly requested, read the site and secret keys
    96  			fmt.Println()
    97  			fmt.Printf("What is the reCaptcha site key to authenticate human users?\n")
    98  			infos.captchaToken = w.readString()
    99  
   100  			fmt.Println()
   101  			fmt.Printf("What is the reCaptcha secret key to verify authentications? (won't be echoed)\n")
   102  			infos.captchaSecret = w.readPassword()
   103  		}
   104  	}
   105  	// Figure out where the user wants to store the persistent data
   106  	fmt.Println()
   107  	if infos.node.datadir == "" {
   108  		fmt.Printf("Where should data be stored on the remote machine?\n")
   109  		infos.node.datadir = w.readString()
   110  	} else {
   111  		fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
   112  		infos.node.datadir = w.readDefaultString(infos.node.datadir)
   113  	}
   114  	// Figure out which port to listen on
   115  	fmt.Println()
   116  	fmt.Printf("Which TCP/UDP port should the light client listen on? (default = %d)\n", infos.node.port)
   117  	infos.node.port = w.readDefaultInt(infos.node.port)
   118  
   119  	// Set a proper name to report on the stats page
   120  	fmt.Println()
   121  	if infos.node.ethstats == "" {
   122  		fmt.Printf("What should the node be called on the stats page?\n")
   123  		infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
   124  	} else {
   125  		fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.node.ethstats)
   126  		infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
   127  	}
   128  	// Load up the credential needed to release funds
   129  	if infos.node.keyJSON != "" {
   130  		if key, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   131  			infos.node.keyJSON, infos.node.keyPass = "", ""
   132  		} else {
   133  			fmt.Println()
   134  			fmt.Printf("Reuse previous (%s) funding account (y/n)? (default = yes)\n", key.Address.Hex())
   135  			if !w.readDefaultYesNo(true) {
   136  				infos.node.keyJSON, infos.node.keyPass = "", ""
   137  			}
   138  		}
   139  	}
   140  	for i := 0; i < 3 && infos.node.keyJSON == ""; i++ {
   141  		fmt.Println()
   142  		fmt.Println("Please paste the faucet's funding account key JSON:")
   143  		infos.node.keyJSON = w.readJSON()
   144  
   145  		fmt.Println()
   146  		fmt.Println("What's the unlock password for the account? (won't be echoed)")
   147  		infos.node.keyPass = w.readPassword()
   148  
   149  		if _, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   150  			log.Error("Failed to decrypt key with given password")
   151  			infos.node.keyJSON = ""
   152  			infos.node.keyPass = ""
   153  		}
   154  	}
   155  	// Check if the user wants to run the faucet in debug mode (noauth)
   156  	noauth := "n"
   157  	if infos.noauth {
   158  		noauth = "y"
   159  	}
   160  	fmt.Println()
   161  	fmt.Printf("Permit non-authenticated funding requests (y/n)? (default = %v)\n", infos.noauth)
   162  	infos.noauth = w.readDefaultString(noauth) != "n"
   163  
   164  	// Try to deploy the faucet server on the host
   165  	nocache := false
   166  	if existed {
   167  		fmt.Println()
   168  		fmt.Printf("Should the faucet be built from scratch (y/n)? (default = no)\n")
   169  		nocache = w.readDefaultYesNo(false)
   170  	}
   171  	if out, err := deployFaucet(client, w.network, w.conf.bootnodes, infos, nocache); err != nil {
   172  		log.Error("Failed to deploy faucet container", "err", err)
   173  		if len(out) > 0 {
   174  			fmt.Printf("%s\n", out)
   175  		}
   176  		return
   177  	}
   178  	// All ok, run a network scan to pick any changes up
   179  	w.networkStats()
   180  }