|
| 1 | +// Copyright (c) 2023 ysicing(ysicing.me, ysicing@ysicing.cloud) All rights reserved. |
| 2 | +// Use of this source code is covered by the following dual licenses: |
| 3 | +// (1) Y PUBLIC LICENSE 1.0 (YPL 1.0) |
| 4 | +// (2) Affero General Public License 3.0 (AGPL 3.0) |
| 5 | +// License that can be found in the LICENSE file. |
| 6 | + |
| 7 | +package ssh |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "context" |
| 12 | + "github.com/cockroachdb/errors" |
| 13 | + "golang.org/x/crypto/ssh" |
| 14 | + "io" |
| 15 | + "k8s.io/apimachinery/pkg/util/wait" |
| 16 | + "time" |
| 17 | +) |
| 18 | + |
| 19 | +var defaultBackoff = wait.Backoff{ |
| 20 | + Duration: 15 * time.Second, |
| 21 | + Factor: 1, |
| 22 | + Steps: 5, |
| 23 | +} |
| 24 | + |
| 25 | +type SSH struct { |
| 26 | + SSHPort string `json:"ssh-port,omitempty" yaml:"ssh-port,omitempty" default:"22"` |
| 27 | + SSHUser string `json:"ssh-user,omitempty" yaml:"ssh-user,omitempty" default:"root"` |
| 28 | + SSHPassword string `json:"ssh-password,omitempty" yaml:"ssh-password,omitempty"` |
| 29 | + SSHKey string `json:"ssh-key,omitempty" yaml:"ssh-key,omitempty" wrangler:"writeOnly,nullable"` |
| 30 | +} |
| 31 | + |
| 32 | +type Node struct { |
| 33 | + SSH `json:",inline"` |
| 34 | + PublicIPAddress []string `json:"public-ip-address,omitempty" yaml:"public-ip-address,omitempty"` |
| 35 | +} |
| 36 | + |
| 37 | +// SSHDialer is a dialer that uses SSH to connect to a remote host. |
| 38 | +type SSHDialer struct { |
| 39 | + sshKey string |
| 40 | + sshCert string |
| 41 | + sshAddress string |
| 42 | + username string |
| 43 | + password string |
| 44 | + passphrase string |
| 45 | + |
| 46 | + Stdin io.ReadCloser |
| 47 | + Stdout io.Writer |
| 48 | + Stderr io.Writer |
| 49 | + Writer io.Writer |
| 50 | + |
| 51 | + Height int |
| 52 | + Weight int |
| 53 | + |
| 54 | + Term string |
| 55 | + Modes ssh.TerminalModes |
| 56 | + |
| 57 | + ctx context.Context |
| 58 | + conn *ssh.Client |
| 59 | + session *ssh.Session |
| 60 | + cmd *bytes.Buffer |
| 61 | + |
| 62 | + err error |
| 63 | +} |
| 64 | + |
| 65 | +func NewSSHDialer(n *Node, timeout bool) (*SSHDialer, error) { |
| 66 | + if len(n.PublicIPAddress) == 0 { |
| 67 | + return nil, errors.New("no ip address") |
| 68 | + } |
| 69 | + d := &SSHDialer{ |
| 70 | + username: n.SSHUser, |
| 71 | + password: n.SSHPassword, |
| 72 | + passphrase: n.SSHPassword, |
| 73 | + sshKey: n.SSHKey, |
| 74 | + ctx: context.Background(), |
| 75 | + } |
| 76 | + return d, nil |
| 77 | +} |
| 78 | + |
| 79 | +// Dial handshake with ssh address. |
| 80 | +func (d *SSHDialer) Dial(t bool) (*ssh.Client, error) { |
| 81 | + timeout := defaultBackoff.Duration |
| 82 | + if !t { |
| 83 | + timeout = 0 |
| 84 | + } |
| 85 | + |
| 86 | + cfg, err := utils.GetSSHConfig(d.username, d.sshKey, d.passphrase, d.sshCert, d.password, timeout, d.useSSHAgentAuth) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + // establish connection with SSH server. |
| 91 | + return ssh.Dial("tcp", d.sshAddress, cfg) |
| 92 | +} |
0 commit comments