-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove the global --config flag for chia-tools config and instead use it for the config subcommand to point to the chia config * Add trusted peer command * Add support for skipping confirmation with -y or --yes * Allow specifying port * Fix port logic, and log the port we're using * Save wallet full_node_peers when swapping networks, and ensure we never have an empty list of peers * Add port override example
- Loading branch information
1 parent
4f7b8e2
commit dd01f13
Showing
6 changed files
with
189 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/hex" | ||
"net" | ||
"path" | ||
"strconv" | ||
|
||
"github.com/chia-network/go-chia-libs/pkg/config" | ||
"github.com/chia-network/go-chia-libs/pkg/peerprotocol" | ||
"github.com/chia-network/go-modules/pkg/slogs" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/chia-network/chia-tools/internal/utils" | ||
) | ||
|
||
var ( | ||
skipConfirm bool | ||
) | ||
|
||
// addTrustedPeerCmd Adds a trusted peer to the config | ||
var addTrustedPeerCmd = &cobra.Command{ | ||
Use: "add-trusted-peer", | ||
Short: "Adds a trusted peer to the config file", | ||
Example: `chia-tools config add-trusted-peer 1.2.3.4 | ||
# The following version will also override the port to use when connecting to this peer | ||
chia-tools config add-trusted-peer 1.2.3.4 18444`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
chiaRoot, err := config.GetChiaRootPath() | ||
if err != nil { | ||
slogs.Logr.Fatal("Unable to determine CHIA_ROOT", "error", err) | ||
} | ||
|
||
// 1: Peer IP | ||
// 2: Optional, port | ||
if len(args) < 1 || len(args) > 2 { | ||
slogs.Logr.Fatal("Unexpected number of arguments provided") | ||
} | ||
|
||
cfgPath := viper.GetString("config") | ||
if cfgPath == "" { | ||
// Use default chia root | ||
cfgPath = path.Join(chiaRoot, "config", "config.yaml") | ||
} | ||
|
||
cfg, err := config.LoadConfigAtRoot(cfgPath, chiaRoot) | ||
if err != nil { | ||
slogs.Logr.Fatal("error loading chia config", "error", err) | ||
} | ||
|
||
peer := args[0] | ||
port := cfg.FullNode.Port | ||
if len(args) > 1 { | ||
port64, err := strconv.ParseUint(args[1], 10, 16) | ||
if err != nil { | ||
slogs.Logr.Fatal("Invalid port provided") | ||
} | ||
port = uint16(port64) | ||
} | ||
|
||
ip := net.ParseIP(peer) | ||
if ip == nil { | ||
slogs.Logr.Fatal("Invalid IP address", "id", peer) | ||
} | ||
slogs.Logr.Info("Attempting to get peer id", "peer", peer, "port", port) | ||
|
||
keypair, err := cfg.FullNode.SSL.LoadPublicKeyPair(chiaRoot) | ||
if err != nil { | ||
slogs.Logr.Fatal("Error loading certs from CHIA_ROOT", "CHIA_ROOT", chiaRoot, "error", err) | ||
} | ||
if keypair == nil { | ||
slogs.Logr.Fatal("Error loading certs from CHIA_ROOT", "CHIA_ROOT", chiaRoot, "error", "keypair was nil") | ||
} | ||
conn, err := peerprotocol.NewConnection( | ||
&ip, | ||
peerprotocol.WithPeerPort(port), | ||
peerprotocol.WithNetworkID(*cfg.SelectedNetwork), | ||
peerprotocol.WithPeerKeyPair(*keypair), | ||
) | ||
if err != nil { | ||
slogs.Logr.Fatal("Error creating connection", "error", err) | ||
} | ||
peerID, err := conn.PeerID() | ||
if err != nil { | ||
slogs.Logr.Fatal("Error getting peer id", "error", err) | ||
} | ||
peerIDStr := hex.EncodeToString(peerID[:]) | ||
slogs.Logr.Info("peer id received", "peer", peerIDStr) | ||
if !utils.ConfirmAction("Would you like trust this peer? (y/N)", skipConfirm) { | ||
slogs.Logr.Error("Cancelled") | ||
} | ||
cfg.Wallet.TrustedPeers[peerIDStr] = "Does_not_matter" | ||
|
||
peerToAdd := config.Peer{ | ||
Host: ip.String(), | ||
Port: port, | ||
} | ||
|
||
foundPeer := false | ||
for idx, peer := range cfg.Wallet.FullNodePeers { | ||
if peer.Host == ip.String() { | ||
foundPeer = true | ||
cfg.Wallet.FullNodePeers[idx] = peerToAdd | ||
} | ||
} | ||
if !foundPeer { | ||
cfg.Wallet.FullNodePeers = append(cfg.Wallet.FullNodePeers, peerToAdd) | ||
} | ||
|
||
err = cfg.Save() | ||
if err != nil { | ||
slogs.Logr.Fatal("error saving config", "error", err) | ||
} | ||
|
||
slogs.Logr.Info("Added trusted peer. Restart your chia services for the configuration to take effect") | ||
}, | ||
} | ||
|
||
func init() { | ||
addTrustedPeerCmd.Flags().BoolVarP(&skipConfirm, "yes", "y", false, "Skip confirmation") | ||
configCmd.AddCommand(addTrustedPeerCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// ConfirmAction waits for the user to confirm with "yes" or "y" | ||
func ConfirmAction(prompt string, skipConfirm bool) bool { | ||
// Easy support for -y type flags to skip confirmation | ||
if skipConfirm { | ||
return true | ||
} | ||
fmt.Printf("%s ", prompt) | ||
reader := bufio.NewReader(os.Stdin) | ||
response, _ := reader.ReadString('\n') | ||
response = strings.TrimSpace(strings.ToLower(response)) | ||
return response == "yes" || response == "y" | ||
} |