github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/puppeth/wizard_ethstats.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package main
    19  
    20  import (
    21  	"fmt"
    22  	"sort"
    23  
    24  	"github.com/AigarNetwork/aigar/log"
    25  )
    26  
    27  // deployEthstats queries the user for various input on deploying an ethstats
    28  // monitoring server, after which it executes it.
    29  func (w *wizard) deployEthstats() {
    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 ethstats configurations from the server
    38  	infos, err := checkEthstats(client, w.network)
    39  	if err != nil {
    40  		infos = &ethstatsInfos{
    41  			port:   80,
    42  			host:   client.server,
    43  			secret: "",
    44  		}
    45  	}
    46  	existed := err == nil
    47  
    48  	// Figure out which port to listen on
    49  	fmt.Println()
    50  	fmt.Printf("Which port should ethstats listen on? (default = %d)\n", infos.port)
    51  	infos.port = w.readDefaultInt(infos.port)
    52  
    53  	// Figure which virtual-host to deploy ethstats on
    54  	if infos.host, err = w.ensureVirtualHost(client, infos.port, infos.host); err != nil {
    55  		log.Error("Failed to decide on ethstats host", "err", err)
    56  		return
    57  	}
    58  	// Port and proxy settings retrieved, figure out the secret and boot ethstats
    59  	fmt.Println()
    60  	if infos.secret == "" {
    61  		fmt.Printf("What should be the secret password for the API? (must not be empty)\n")
    62  		infos.secret = w.readString()
    63  	} else {
    64  		fmt.Printf("What should be the secret password for the API? (default = %s)\n", infos.secret)
    65  		infos.secret = w.readDefaultString(infos.secret)
    66  	}
    67  	// Gather any blacklists to ban from reporting
    68  	if existed {
    69  		fmt.Println()
    70  		fmt.Printf("Keep existing IP %v blacklist (y/n)? (default = yes)\n", infos.banned)
    71  		if !w.readDefaultYesNo(true) {
    72  			// The user might want to clear the entire list, although generally probably not
    73  			fmt.Println()
    74  			fmt.Printf("Clear out blacklist and start over (y/n)? (default = no)\n")
    75  			if w.readDefaultYesNo(false) {
    76  				infos.banned = nil
    77  			}
    78  			// Offer the user to explicitly add/remove certain IP addresses
    79  			fmt.Println()
    80  			fmt.Println("Which additional IP addresses should be blacklisted?")
    81  			for {
    82  				if ip := w.readIPAddress(); ip != "" {
    83  					infos.banned = append(infos.banned, ip)
    84  					continue
    85  				}
    86  				break
    87  			}
    88  			fmt.Println()
    89  			fmt.Println("Which IP addresses should not be blacklisted?")
    90  			for {
    91  				if ip := w.readIPAddress(); ip != "" {
    92  					for i, addr := range infos.banned {
    93  						if ip == addr {
    94  							infos.banned = append(infos.banned[:i], infos.banned[i+1:]...)
    95  							break
    96  						}
    97  					}
    98  					continue
    99  				}
   100  				break
   101  			}
   102  			sort.Strings(infos.banned)
   103  		}
   104  	}
   105  	// Try to deploy the ethstats server on the host
   106  	nocache := false
   107  	if existed {
   108  		fmt.Println()
   109  		fmt.Printf("Should the ethstats be built from scratch (y/n)? (default = no)\n")
   110  		nocache = w.readDefaultYesNo(false)
   111  	}
   112  	trusted := make([]string, 0, len(w.servers))
   113  	for _, client := range w.servers {
   114  		if client != nil {
   115  			trusted = append(trusted, client.address)
   116  		}
   117  	}
   118  	if out, err := deployEthstats(client, w.network, infos.port, infos.secret, infos.host, trusted, infos.banned, nocache); err != nil {
   119  		log.Error("Failed to deploy ethstats container", "err", err)
   120  		if len(out) > 0 {
   121  			fmt.Printf("%s\n", out)
   122  		}
   123  		return
   124  	}
   125  	// All ok, run a network scan to pick any changes up
   126  	w.networkStats()
   127  }