File: commands_ssh_windows.go

package info (click to toggle)
golang-github-jamesclonk-vultr 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 560 kB
  • sloc: makefile: 35
file content (129 lines) | stat: -rw-r--r-- 2,810 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// +build windows

package cmd

import (
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"os"
	"strings"

	"golang.org/x/crypto/ssh"
	"golang.org/x/crypto/ssh/agent"
	"golang.org/x/crypto/ssh/terminal"
)

func connectSSH(user, host, key string, port int) {
	fd := int(os.Stdin.Fd())
	prevState, err := terminal.MakeRaw(fd)
	if err != nil {
		return
	}
	defer terminal.Restore(fd, prevState)

	// vultr ssh needs to be in a terminal
	if terminal.IsTerminal(fd) {
		var config *ssh.ClientConfig

		// use keyfile if provided
		if key != "" {
			signer, err := ssh.ParsePrivateKey(readKeyFile(key))
			if err != nil {
				log.Println("Could not parse private key file")
				log.Fatal(err)
			}
			config = &ssh.ClientConfig{
				User: user,
				Auth: []ssh.AuthMethod{
					ssh.PublicKeys(signer),
				},
			}
		} else if os.Getenv("SSH_AUTH_SOCK") != "" {
			// if no keyfile is provided, try to use ssh-agent
			conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
			if err != nil {
				log.Fatal(err)
			}
			defer conn.Close()

			sshAgent := agent.NewClient(conn)
			config = &ssh.ClientConfig{
				User: user,
				Auth: []ssh.AuthMethod{
					ssh.PublicKeysCallback(sshAgent.Signers),
				},
			}
		}

		// connect to ssh
		client, err := ssh.Dial("tcp", fmt.Sprintf("%v:%v", host, port), config)
		if err != nil {
			if strings.Contains(err.Error(), "no supported methods remain") {
				// let's see if we can maybe give it one more try by using an interactive password prompt
				fmt.Print("Password: ")
				password, err := terminal.ReadPassword(fd)
				if err != nil {
					log.Fatal(err)
				}

				config = &ssh.ClientConfig{
					User: user,
					Auth: []ssh.AuthMethod{
						ssh.Password(string(password)),
					},
				}
				client, err = ssh.Dial("tcp", fmt.Sprintf("%v:%v", host, port), config)
				if err != nil {
					log.Fatal(err)
				}
			} else {
				log.Fatal(err)
			}
		}
		defer client.Close()

		session, err := client.NewSession()
		if err != nil {
			log.Fatal(err)
		}
		defer session.Close()

		session.Stdout = os.Stdout
		session.Stderr = os.Stderr
		session.Stdin = os.Stdin

		termWidth, termHeight, err := terminal.GetSize(fd)
		if err != nil {
			return
		}

		modes := ssh.TerminalModes{
			ssh.ECHO:          0,
			ssh.TTY_OP_ISPEED: 14400,
			ssh.TTY_OP_OSPEED: 14400,
		}
		if err := session.RequestPty("xterm", termWidth, termHeight, modes); err != nil {
			log.Fatal(err)
		}

		if err := session.Shell(); err != nil {
			log.Fatal(err)
		}
		if err := session.Wait(); err != nil {
			log.Fatal(err)
		}
	} else {
		log.Fatal("vultr ssh needs an interactive terminal to work")
	}
}

func readKeyFile(filename string) []byte {
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Println("Could not read ssh key file: " + filename)
		log.Fatal(err)
	}
	return data
}