2024-04-01 11:02:07 +00:00
|
|
|
// Package sftp contains the implementation for an SFTP uploader
|
2024-03-27 14:20:45 +00:00
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"git.luzifer.io/luzifer/publish-vod/pkg/uploader"
|
|
|
|
"github.com/Luzifer/go_helpers/v2/fieldcollection"
|
2024-04-01 11:02:07 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2024-03-27 14:20:45 +00:00
|
|
|
|
|
|
|
pkgSFTP "github.com/pkg/sftp"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"golang.org/x/crypto/ssh/agent"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2024-04-01 11:02:07 +00:00
|
|
|
impl struct{}
|
2024-03-27 14:20:45 +00:00
|
|
|
)
|
|
|
|
|
2024-04-01 11:02:07 +00:00
|
|
|
// New returns a SFTP Uploader
|
|
|
|
func New() uploader.Uploader { return impl{} }
|
2024-03-27 14:20:45 +00:00
|
|
|
|
2024-04-01 11:02:07 +00:00
|
|
|
func (impl) Prepare(context.Context, uploader.Opts) error { return nil }
|
2024-03-27 17:26:13 +00:00
|
|
|
|
2024-04-01 11:02:07 +00:00
|
|
|
func (impl) UploadFile(_ context.Context, opts uploader.Opts) error {
|
2024-03-27 14:20:45 +00:00
|
|
|
socket := os.Getenv("SSH_AUTH_SOCK")
|
|
|
|
conn, err := net.Dial("unix", socket)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("opening SSH_AUTH_SOCK: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
agentClient := agent.NewClient(conn)
|
|
|
|
config := &ssh.ClientConfig{
|
|
|
|
User: opts.Config.MustString("user", nil),
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
// Use a callback rather than PublicKeys so we only consult the
|
|
|
|
// agent once the remote server wants it.
|
|
|
|
ssh.PublicKeysCallback(agentClient.Signers),
|
|
|
|
},
|
2024-04-01 11:02:07 +00:00
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //#nosec G106
|
2024-03-27 14:20:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := ssh.Dial("tcp", opts.Config.MustString("host", nil), config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing SSH server: %w", err)
|
|
|
|
}
|
2024-04-01 11:02:07 +00:00
|
|
|
defer func() {
|
|
|
|
if err := client.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing SSH client (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2024-03-27 14:20:45 +00:00
|
|
|
|
|
|
|
sftpClient, err := pkgSFTP.NewClient(client)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating sftp client: %w", err)
|
|
|
|
}
|
2024-04-01 11:02:07 +00:00
|
|
|
defer func() {
|
|
|
|
if err := sftpClient.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing SFTP client (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2024-03-27 14:20:45 +00:00
|
|
|
|
|
|
|
remoteFile := path.Join(opts.Config.MustString("path", nil), path.Base(opts.Filename))
|
|
|
|
f, err := sftpClient.OpenFile(remoteFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("opening remote file: %w", err)
|
|
|
|
}
|
2024-04-01 11:02:07 +00:00
|
|
|
defer func() {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
logrus.WithError(err).Error("closing SFTP file (leaked fd)")
|
|
|
|
}
|
|
|
|
}()
|
2024-03-27 14:20:45 +00:00
|
|
|
|
|
|
|
progressReader := opts.ProgressBar.NewProxyReader(opts.Content)
|
|
|
|
|
2024-04-01 11:02:07 +00:00
|
|
|
if _, err = f.ReadFrom(progressReader); err != nil {
|
|
|
|
return fmt.Errorf("uploading video: %w", err)
|
|
|
|
}
|
2024-03-27 14:20:45 +00:00
|
|
|
|
|
|
|
opts.FinalMessage("Video uploaded to %s", remoteFile)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-01 11:02:07 +00:00
|
|
|
func (impl) ValidateConfig(config *fieldcollection.FieldCollection) error {
|
2024-03-27 14:20:45 +00:00
|
|
|
for _, key := range []string{"host", "path", "user"} {
|
|
|
|
if v, err := config.String(key); err != nil || v == "" {
|
|
|
|
return fmt.Errorf("key %q must be non-empty string", key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|