Skip to content

Commit

Permalink
cmd/puppeth: code polishes + fix heath check double ports
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Aug 20, 2018
1 parent 411ec22 commit a20e8dd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
32 changes: 14 additions & 18 deletions cmd/puppeth/ssh.go
Expand Up @@ -47,46 +47,42 @@ type sshClient struct {
// the user's configured private RSA key. If that fails, password authentication
// is fallen back to. server can be a string like user:identity@server:port.
func dial(server string, pubkey []byte) (*sshClient, error) {
// Figure out username, identity_file, hostname and port
login := ""
identity_file := "id_rsa" // default
hostport := server
// Figure out username, identity, hostname and port
hostname := ""
hostport := server
username := ""
identity := "id_rsa" // default

if strings.Contains(server, "@") {
prefix := server[:strings.Index(server, "@")]
if strings.Contains(prefix, ":") {
login = prefix[:strings.Index(prefix, ":")]
identity_file = prefix[strings.Index(prefix, ":")+1:]
username = prefix[:strings.Index(prefix, ":")]
identity = prefix[strings.Index(prefix, ":")+1:]
} else {
login = prefix
username = prefix
}
hostport = server[strings.Index(server, "@")+1:]
}

// parse hostname and port
if strings.Contains(hostport, ":") {
hostname = hostport[:strings.Index(hostport, ":")]
} else {
hostname = hostport
hostport += ":22"
}

logger := log.New("server", server)
logger.Debug("Attempting to establish SSH connection")

user, err := user.Current()
if err != nil {
return nil, err
}
if login == "" {
login = user.Username
if username == "" {
username = user.Username
}
fmt.Printf("parsed %s:%s@%s\nhn=%s", login, identity_file, hostport, hostname)
// Configure the supported authentication methods (private key and password)
var auths []ssh.AuthMethod

path := filepath.Join(user.HomeDir, ".ssh", identity_file)
path := filepath.Join(user.HomeDir, ".ssh", identity)
if buf, err := ioutil.ReadFile(path); err != nil {
log.Warn("No SSH key, falling back to passwords", "path", path, "err", err)
} else {
Expand All @@ -109,7 +105,7 @@ func dial(server string, pubkey []byte) (*sshClient, error) {
}
}
auths = append(auths, ssh.PasswordCallback(func() (string, error) {
fmt.Printf("What's the login password for %s at %s? (won't be echoed)\n> ", login, server)
fmt.Printf("What's the login password for %s at %s? (won't be echoed)\n> ", username, server)
blob, err := terminal.ReadPassword(int(os.Stdin.Fd()))

fmt.Println()
Expand All @@ -124,7 +120,7 @@ func dial(server string, pubkey []byte) (*sshClient, error) {
return nil, errors.New("no IPs associated with domain")
}
// Try to dial in to the remote server
logger.Trace("Dialing remote SSH server", "user", login)
logger.Trace("Dialing remote SSH server", "user", username)
keycheck := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// If no public key is known for SSH, ask the user to confirm
if pubkey == nil {
Expand All @@ -151,13 +147,13 @@ func dial(server string, pubkey []byte) (*sshClient, error) {
// We have a mismatch, forbid connecting
return errors.New("ssh key mismatch, readd the machine to update")
}
client, err := ssh.Dial("tcp", hostport, &ssh.ClientConfig{User: login, Auth: auths, HostKeyCallback: keycheck})
client, err := ssh.Dial("tcp", hostport, &ssh.ClientConfig{User: username, Auth: auths, HostKeyCallback: keycheck})
if err != nil {
return nil, err
}
// Connection established, return our utility wrapper
c := &sshClient{
server: hostport,
server: hostname,
address: addr[0],
pubkey: pubkey,
client: client,
Expand Down
10 changes: 1 addition & 9 deletions cmd/puppeth/wizard_network.go
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"os/user"
"strings"

"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -70,15 +69,8 @@ func (w *wizard) manageServers() {
//
// If connection succeeds, the server is added to the wizards configs!
func (w *wizard) makeServer() string {
login := ""
user, err := user.Current()
if err == nil {
login = user.Username
}

fmt.Println()
fmt.Println("Please enter remote server (username:identity@hostname:port)")
fmt.Printf("If not given, will use username = %s, identity = id_rsa\n", login)
fmt.Println("What is the remote server's address ([username[:identity]@]hostname[:port])?")

// Read and dial the server to ensure docker is present
input := w.readString()
Expand Down

0 comments on commit a20e8dd

Please sign in to comment.