Skip to content

Commit

Permalink
use the new API in example.
Browse files Browse the repository at this point in the history
  • Loading branch information
melbahja committed Oct 8, 2020
1 parent 7166246 commit cf2eb65
Showing 1 changed file with 44 additions and 36 deletions.
80 changes: 44 additions & 36 deletions examples/goph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
client *goph.Client
addr string
user string
port int
port uint
key string
cmd string
pass bool
Expand All @@ -49,65 +49,73 @@ func init() {

flag.StringVar(&addr, "ip", "127.0.0.1", "machine ip address.")
flag.StringVar(&user, "user", "root", "ssh user.")
flag.IntVar(&port, "port", 22, "ssh port number.")
flag.UintVar(&port, "port", 22, "ssh port number.")
flag.StringVar(&key, "key", strings.Join([]string{os.Getenv("HOME"), ".ssh", "id_rsa"}, "/"), "private key path.")
flag.StringVar(&cmd, "cmd", "", "command to run.")
flag.BoolVar(&pass, "pass", false, "ask for ssh password instead of private key.")
flag.BoolVar(&agent, "agent", false, "use ssh agent for authentication (unix systems only).")
flag.BoolVar(&passphrase, "passphrase", false, "ask for private key passphrase.")
}

func main() {
func VerifyHost(host string, remote net.Addr, key ssh.PublicKey) error {

flag.Parse()
//
// If you want to connect to new hosts.
// here your should check new connections public keys
// if the key not trusted you shuld return an error
//

if agent {
// hostFound: is host in known hosts file.
// err: error if key not in known hosts file OR host in known hosts file but key changed!
hostFound, err := goph.CheckKnownHost(host, remote, key, "")

auth = goph.UseAgent()
// Host in known hosts but key mismatch!
// Maybe because of MAN IN THE MIDDLE ATTACK!
if hostFound && err != nil {

} else if pass {
return err
}

auth = goph.Password(askPass("Enter SSH Password: "))
// handshake because public key already exists.
if hostFound && err == nil {

} else {
return nil
}

auth = goph.Key(key, getPassphrase(passphrase))
// Ask user to check if he trust the host public key.
if askIsHostTrusted(host, key) == false {

// Make sure to return error on non trusted keys.
return errors.New("you typed no, aborted!")
}

client, err = goph.NewConn(user, addr, auth, func(host string, remote net.Addr, key ssh.PublicKey) error {
// Add the new host to known hosts file.
return goph.AddKnownHost(host, remote, key, "")
}

//
// If you want to connect to new hosts.
// here your should check new connections public keys
// if the key not trusted you shuld return an error
//
func main() {

// hostFound: is host in known hosts file.
// err: error if key not in known hosts file OR host in known hosts file but key changed!
hostFound, err := goph.CheckKnownHost(host, remote, key, "")
flag.Parse()

// Host in known hosts but key mismatch!
// Maybe because of MAN IN THE MIDDLE ATTACK!
if hostFound && err != nil {
if agent || goph.HasAgent() {

return err
}
auth = goph.UseAgent()

// handshake because public key already exists.
if hostFound && err == nil {
} else if pass {

return nil
}
auth = goph.Password(askPass("Enter SSH Password: "))

// Ask user to check if he trust the host public key.
if askIsHostTrusted(host, key) == false {
} else {

// Make sure to return error on non trusted keys.
return errors.New("you typed no, aborted!")
}
auth = goph.Key(key, getPassphrase(passphrase))
}

// Add the new host to known hosts file.
return goph.AddKnownHost(host, remote, key, "")
client, err = goph.NewConn(&goph.Config{
User: user,
Addr: addr,
Port: port,
Auth: auth,
Callback: VerifyHost,
})

if err != nil {
Expand Down Expand Up @@ -174,7 +182,7 @@ func askIsHostTrusted(host string, key ssh.PublicKey) bool {
func playWithSSHJustForTestingThisProgram(client *goph.Client) {

fmt.Println("Welcome To Goph :D")
fmt.Printf("Connected to %s\n", client.Addr)
fmt.Printf("Connected to %s\n", client.Config.Addr)
fmt.Println("Type your shell command and enter.")
fmt.Println("To download file from remote type: download remote/path local/path")
fmt.Println("To upload file to remote type: upload local/path remote/path")
Expand Down

0 comments on commit cf2eb65

Please sign in to comment.