File: ssh.go

package info (click to toggle)
immich-go 0.31.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 94,260 kB
  • sloc: sh: 770; makefile: 21
file content (72 lines) | stat: -rw-r--r-- 1,392 bytes parent folder | download | duplicates (2)
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
package docker

import (
	"context"
	"fmt"
	"io"
	"net/url"
	"os/user"
	"path/filepath"

	"github.com/melbahja/goph"
)

type sshProxy struct {
	sshAuth   goph.Auth
	sshUser   string
	sshHost   string
	sshClient *goph.Client
}

func newSSHProxy(host string) (*sshProxy, error) {
	u, err := url.Parse(host)
	if err != nil {
		return nil, err
	}
	if u.Scheme != "ssh" {
		return nil, fmt.Errorf("unsupported protocol %s: %s", u.Scheme, host)
	}
	p := sshProxy{}
	p.sshUser = u.User.Username()
	p.sshHost = u.Host
	pass, set := u.User.Password()
	if set {
		p.sshAuth = goph.Password(pass)
	} else {
		var u *user.User
		u, err = user.Current()
		if err != nil {
			return nil, err
		}
		if p.sshUser == "" {
			p.sshUser = u.Username
		}
		keyFile := filepath.Join(u.HomeDir, ".ssh", "id_rsa")
		p.sshAuth, err = goph.Key(keyFile, "")
		if err != nil {
			return nil, err
		}
	}
	return &p, nil
}

func (p *sshProxy) connect(ctx context.Context) error {
	var err error
	p.sshClient, err = goph.New(p.sshUser, p.sshHost, p.sshAuth)
	return err
}

func (p *sshProxy) docker(ctx context.Context, args ...string) (cmdAdaptor, error) {
	cmd, err := p.sshClient.CommandContext(ctx, "docker", args...)
	return &sshCmd{Cmd: cmd}, err
}

// sshCmd shim
type sshCmd struct {
	*goph.Cmd
}

func (c *sshCmd) StdoutPipe() (io.ReadCloser, error) {
	r, err := c.Cmd.StdoutPipe()
	return io.NopCloser(r), err
}