github.com/tuotoo/go-ethereum@v1.7.4-0.20171121184211-049797d40a24/cmd/puppeth/wizard_ethstats.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  	"fmt"
    21  	"sort"
    22  
    23  	"github.com/ethereum/go-ethereum/log"
    24  )
    25  
    26  // deployEthstats queries the user for various input on deploying an ethstats
    27  // monitoring server, after which it executes it.
    28  func (w *wizard) deployEthstats() {
    29  	// Select the server to interact with
    30  	server := w.selectServer()
    31  	if server == "" {
    32  		return
    33  	}
    34  	client := w.servers[server]
    35  
    36  	// Retrieve any active ethstats configurations from the server
    37  	infos, err := checkEthstats(client, w.network)
    38  	if err != nil {
    39  		infos = &ethstatsInfos{
    40  			port:   80,
    41  			host:   client.server,
    42  			secret: "",
    43  		}
    44  	}
    45  	// Figure out which port to listen on
    46  	fmt.Println()
    47  	fmt.Printf("Which port should ethstats listen on? (default = %d)\n", infos.port)
    48  	infos.port = w.readDefaultInt(infos.port)
    49  
    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 ethstats host", "err", err)
    53  		return
    54  	}
    55  	// Port and proxy settings retrieved, figure out the secret and boot ethstats
    56  	fmt.Println()
    57  	if infos.secret == "" {
    58  		fmt.Printf("What should be the secret password for the API? (must not be empty)\n")
    59  		infos.secret = w.readString()
    60  	} else {
    61  		fmt.Printf("What should be the secret password for the API? (default = %s)\n", infos.secret)
    62  		infos.secret = w.readDefaultString(infos.secret)
    63  	}
    64  	// Gather any blacklists to ban from reporting
    65  	fmt.Println()
    66  	fmt.Printf("Keep existing IP %v blacklist (y/n)? (default = yes)\n", infos.banned)
    67  	if w.readDefaultString("y") != "y" {
    68  		// The user might want to clear the entire list, although generally probably not
    69  		fmt.Println()
    70  		fmt.Printf("Clear out blacklist and start over (y/n)? (default = no)\n")
    71  		if w.readDefaultString("n") != "n" {
    72  			infos.banned = nil
    73  		}
    74  		// Offer the user to explicitly add/remove certain IP addresses
    75  		fmt.Println()
    76  		fmt.Println("Which additional IP addresses should be blacklisted?")
    77  		for {
    78  			if ip := w.readIPAddress(); ip != "" {
    79  				infos.banned = append(infos.banned, ip)
    80  				continue
    81  			}
    82  			break
    83  		}
    84  		fmt.Println()
    85  		fmt.Println("Which IP addresses should not be blacklisted?")
    86  		for {
    87  			if ip := w.readIPAddress(); ip != "" {
    88  				for i, addr := range infos.banned {
    89  					if ip == addr {
    90  						infos.banned = append(infos.banned[:i], infos.banned[i+1:]...)
    91  						break
    92  					}
    93  				}
    94  				continue
    95  			}
    96  			break
    97  		}
    98  		sort.Strings(infos.banned)
    99  	}
   100  	// Try to deploy the ethstats server on the host
   101  	trusted := make([]string, 0, len(w.servers))
   102  	for _, client := range w.servers {
   103  		if client != nil {
   104  			trusted = append(trusted, client.address)
   105  		}
   106  	}
   107  	if out, err := deployEthstats(client, w.network, infos.port, infos.secret, infos.host, trusted, infos.banned); err != nil {
   108  		log.Error("Failed to deploy ethstats container", "err", err)
   109  		if len(out) > 0 {
   110  			fmt.Printf("%s\n", out)
   111  		}
   112  		return
   113  	}
   114  	// All ok, run a network scan to pick any changes up
   115  	w.networkStats(false)
   116  }