gitee.com/liu-zhao234568/cntest@v1.0.0/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  	"gitee.com/liu-zhao234568/cntest/accounts/keystore"
    24  	"gitee.com/liu-zhao234568/cntest/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  	// Accessing the Twitter API requires a bearer token, request it
   106  	if infos.twitterToken != "" {
   107  		fmt.Println()
   108  		fmt.Println("Reuse previous Twitter API token (y/n)? (default = yes)")
   109  		if !w.readDefaultYesNo(true) {
   110  			infos.twitterToken = ""
   111  		}
   112  	}
   113  	if infos.twitterToken == "" {
   114  		// No previous twitter token (or old one discarded)
   115  		fmt.Println()
   116  		fmt.Println()
   117  		fmt.Printf("What is the Twitter API app Bearer token?\n")
   118  		infos.twitterToken = w.readString()
   119  	}
   120  	// Figure out where the user wants to store the persistent data
   121  	fmt.Println()
   122  	if infos.node.datadir == "" {
   123  		fmt.Printf("Where should data be stored on the remote machine?\n")
   124  		infos.node.datadir = w.readString()
   125  	} else {
   126  		fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
   127  		infos.node.datadir = w.readDefaultString(infos.node.datadir)
   128  	}
   129  	// Figure out which port to listen on
   130  	fmt.Println()
   131  	fmt.Printf("Which TCP/UDP port should the light client listen on? (default = %d)\n", infos.node.port)
   132  	infos.node.port = w.readDefaultInt(infos.node.port)
   133  
   134  	// Set a proper name to report on the stats page
   135  	fmt.Println()
   136  	if infos.node.ethstats == "" {
   137  		fmt.Printf("What should the node be called on the stats page?\n")
   138  		infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
   139  	} else {
   140  		fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.node.ethstats)
   141  		infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
   142  	}
   143  	// Load up the credential needed to release funds
   144  	if infos.node.keyJSON != "" {
   145  		if key, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   146  			infos.node.keyJSON, infos.node.keyPass = "", ""
   147  		} else {
   148  			fmt.Println()
   149  			fmt.Printf("Reuse previous (%s) funding account (y/n)? (default = yes)\n", key.Address.Hex())
   150  			if !w.readDefaultYesNo(true) {
   151  				infos.node.keyJSON, infos.node.keyPass = "", ""
   152  			}
   153  		}
   154  	}
   155  	for i := 0; i < 3 && infos.node.keyJSON == ""; i++ {
   156  		fmt.Println()
   157  		fmt.Println("Please paste the faucet's funding account key JSON:")
   158  		infos.node.keyJSON = w.readJSON()
   159  
   160  		fmt.Println()
   161  		fmt.Println("What's the unlock password for the account? (won't be echoed)")
   162  		infos.node.keyPass = w.readPassword()
   163  
   164  		if _, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   165  			log.Error("Failed to decrypt key with given password")
   166  			infos.node.keyJSON = ""
   167  			infos.node.keyPass = ""
   168  		}
   169  	}
   170  	// Check if the user wants to run the faucet in debug mode (noauth)
   171  	noauth := "n"
   172  	if infos.noauth {
   173  		noauth = "y"
   174  	}
   175  	fmt.Println()
   176  	fmt.Printf("Permit non-authenticated funding requests (y/n)? (default = %v)\n", infos.noauth)
   177  	infos.noauth = w.readDefaultString(noauth) != "n"
   178  
   179  	// Try to deploy the faucet server on the host
   180  	nocache := false
   181  	if existed {
   182  		fmt.Println()
   183  		fmt.Printf("Should the faucet be built from scratch (y/n)? (default = no)\n")
   184  		nocache = w.readDefaultYesNo(false)
   185  	}
   186  	if out, err := deployFaucet(client, w.network, w.conf.bootnodes, infos, nocache); err != nil {
   187  		log.Error("Failed to deploy faucet container", "err", err)
   188  		if len(out) > 0 {
   189  			fmt.Printf("%s\n", out)
   190  		}
   191  		return
   192  	}
   193  	// All ok, run a network scan to pick any changes up
   194  	w.networkStats()
   195  }