github.com/waltonchain/waltonchain_gwtc_src@v1.1.4-0.20201225072101-8a298c95a819/cmd/puppeth/wizard_faucet.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-wtc.
     3  //
     4  // go-wtc 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-wtc 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-wtc. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"github.com/wtc/go-wtc/accounts/keystore"
    25  	"github.com/wtc/go-wtc/log"
    26  )
    27  
    28  // deployFaucet queries the user for various input on deploying a faucet, after
    29  // which it executes it.
    30  func (w *wizard) deployFaucet() {
    31  	// Select the server to interact with
    32  	server := w.selectServer()
    33  	if server == "" {
    34  		return
    35  	}
    36  	client := w.servers[server]
    37  
    38  	// Retrieve any active faucet configurations from the server
    39  	infos, err := checkFaucet(client, w.network)
    40  	if err != nil {
    41  		infos = &faucetInfos{
    42  			node:    &nodeInfos{portFull: 10101, peersTotal: 25},
    43  			port:    80,
    44  			host:    client.server,
    45  			amount:  1,
    46  			minutes: 1440,
    47  			tiers:   3,
    48  		}
    49  	}
    50  	infos.node.genesis, _ = json.MarshalIndent(w.conf.genesis, "", "  ")
    51  	infos.node.network = w.conf.genesis.Config.ChainId.Int64()
    52  
    53  	// Figure out which port to listen on
    54  	fmt.Println()
    55  	fmt.Printf("Which port should the faucet listen on? (default = %d)\n", infos.port)
    56  	infos.port = w.readDefaultInt(infos.port)
    57  
    58  	// Figure which virtual-host to deploy ethstats on
    59  	if infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host); err != nil {
    60  		log.Error("Failed to decide on faucet host", "err", err)
    61  		return
    62  	}
    63  	// Port and proxy settings retrieved, figure out the funcing amount per perdion configurations
    64  	fmt.Println()
    65  	fmt.Printf("How many Ethers to release per request? (default = %d)\n", infos.amount)
    66  	infos.amount = w.readDefaultInt(infos.amount)
    67  
    68  	fmt.Println()
    69  	fmt.Printf("How many minutes to enforce between requests? (default = %d)\n", infos.minutes)
    70  	infos.minutes = w.readDefaultInt(infos.minutes)
    71  
    72  	fmt.Println()
    73  	fmt.Printf("How many funding tiers to feature (x2.5 amounts, x3 timeout)? (default = %d)\n", infos.tiers)
    74  	infos.tiers = w.readDefaultInt(infos.tiers)
    75  	if infos.tiers == 0 {
    76  		log.Error("At least one funding tier must be set")
    77  		return
    78  	}
    79  	// Accessing GitHub gists requires API authorization, retrieve it
    80  	if infos.githubUser != "" {
    81  		fmt.Println()
    82  		fmt.Printf("Reuse previous (%s) GitHub API authorization (y/n)? (default = yes)\n", infos.githubUser)
    83  		if w.readDefaultString("y") != "y" {
    84  			infos.githubUser, infos.githubToken = "", ""
    85  		}
    86  	}
    87  	if infos.githubUser == "" {
    88  		// No previous authorization (or new one requested)
    89  		fmt.Println()
    90  		fmt.Println("Which GitHub user to verify Gists through?")
    91  		infos.githubUser = w.readString()
    92  
    93  		fmt.Println()
    94  		fmt.Println("What is the GitHub personal access token of the user? (won't be echoed)")
    95  		infos.githubToken = w.readPassword()
    96  
    97  		// Do a sanity check query against github to ensure it's valid
    98  		req, _ := http.NewRequest("GET", "https://api.github.com/user", nil)
    99  		req.SetBasicAuth(infos.githubUser, infos.githubToken)
   100  		res, err := http.DefaultClient.Do(req)
   101  		if err != nil {
   102  			log.Error("Failed to verify GitHub authentication", "err", err)
   103  			return
   104  		}
   105  		defer res.Body.Close()
   106  
   107  		var msg struct {
   108  			Login   string `json:"login"`
   109  			Message string `json:"message"`
   110  		}
   111  		if err = json.NewDecoder(res.Body).Decode(&msg); err != nil {
   112  			log.Error("Failed to decode authorization response", "err", err)
   113  			return
   114  		}
   115  		if msg.Login != infos.githubUser {
   116  			log.Error("GitHub authorization failed", "user", infos.githubUser, "message", msg.Message)
   117  			return
   118  		}
   119  	}
   120  	// Accessing the reCaptcha service requires API authorizations, request it
   121  	if infos.captchaToken != "" {
   122  		fmt.Println()
   123  		fmt.Println("Reuse previous reCaptcha API authorization (y/n)? (default = yes)")
   124  		if w.readDefaultString("y") != "y" {
   125  			infos.captchaToken, infos.captchaSecret = "", ""
   126  		}
   127  	}
   128  	if infos.captchaToken == "" {
   129  		// No previous authorization (or old one discarded)
   130  		fmt.Println()
   131  		fmt.Println("Enable reCaptcha protection against robots (y/n)? (default = no)")
   132  		if w.readDefaultString("n") == "y" {
   133  			// Captcha protection explicitly requested, read the site and secret keys
   134  			fmt.Println()
   135  			fmt.Printf("What is the reCaptcha site key to authenticate human users?\n")
   136  			infos.captchaToken = w.readString()
   137  
   138  			fmt.Println()
   139  			fmt.Printf("What is the reCaptcha secret key to verify authentications? (won't be echoed)\n")
   140  			infos.captchaSecret = w.readPassword()
   141  		}
   142  	}
   143  	// Figure out where the user wants to store the persistent data
   144  	fmt.Println()
   145  	if infos.node.datadir == "" {
   146  		fmt.Printf("Where should data be stored on the remote machine?\n")
   147  		infos.node.datadir = w.readString()
   148  	} else {
   149  		fmt.Printf("Where should data be stored on the remote machine? (default = %s)\n", infos.node.datadir)
   150  		infos.node.datadir = w.readDefaultString(infos.node.datadir)
   151  	}
   152  	// Figure out which port to listen on
   153  	fmt.Println()
   154  	fmt.Printf("Which TCP/UDP port should the light client listen on? (default = %d)\n", infos.node.portFull)
   155  	infos.node.portFull = w.readDefaultInt(infos.node.portFull)
   156  
   157  	// Set a proper name to report on the stats page
   158  	fmt.Println()
   159  	if infos.node.ethstats == "" {
   160  		fmt.Printf("What should the node be called on the stats page?\n")
   161  		infos.node.ethstats = w.readString() + ":" + w.conf.ethstats
   162  	} else {
   163  		fmt.Printf("What should the node be called on the stats page? (default = %s)\n", infos.node.ethstats)
   164  		infos.node.ethstats = w.readDefaultString(infos.node.ethstats) + ":" + w.conf.ethstats
   165  	}
   166  	// Load up the credential needed to release funds
   167  	if infos.node.keyJSON != "" {
   168  		if key, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   169  			infos.node.keyJSON, infos.node.keyPass = "", ""
   170  		} else {
   171  			fmt.Println()
   172  			fmt.Printf("Reuse previous (%s) funding account (y/n)? (default = yes)\n", key.Address.Hex())
   173  			if w.readDefaultString("y") != "y" {
   174  				infos.node.keyJSON, infos.node.keyPass = "", ""
   175  			}
   176  		}
   177  	}
   178  	if infos.node.keyJSON == "" {
   179  		fmt.Println()
   180  		fmt.Println("Please paste the faucet's funding account key JSON:")
   181  		infos.node.keyJSON = w.readJSON()
   182  
   183  		fmt.Println()
   184  		fmt.Println("What's the unlock password for the account? (won't be echoed)")
   185  		infos.node.keyPass = w.readPassword()
   186  
   187  		if _, err := keystore.DecryptKey([]byte(infos.node.keyJSON), infos.node.keyPass); err != nil {
   188  			log.Error("Failed to decrypt key with given passphrase")
   189  			return
   190  		}
   191  	}
   192  	// Try to deploy the faucet server on the host
   193  	if out, err := deployFaucet(client, w.network, w.conf.bootLight, infos); err != nil {
   194  		log.Error("Failed to deploy faucet container", "err", err)
   195  		if len(out) > 0 {
   196  			fmt.Printf("%s\n", out)
   197  		}
   198  		return
   199  	}
   200  	// All ok, run a network scan to pick any changes up
   201  	w.networkStats(false)
   202  }