aboutsummaryrefslogtreecommitdiff
path: root/internal/kahless/kahless.go
blob: 14806121ad9b434921a25989613191ae2679f639 (plain)
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
package kahless

import (
	"bytes"
	"flag"
	"io"
	"log"
	"net"
	"os"
	"path/filepath"

	"github.com/tmc/scp"
	"golang.org/x/crypto/ssh"
	"golang.org/x/crypto/ssh/agent"
)

func getAgent() (agent.Agent, error) {
	agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	return agent.NewClient(agentConn), err
}

var (
	greedoAddr = flag.String("kahless-addr", "lufta:22", "address to use for kahless")
	greedoUser = flag.String("kahless-user", "cadey", "username to use for kahless")
)

// Dial opens a SSH client to greedo.
func Dial() (*ssh.Client, error) {
	agent, err := getAgent()
	if err != nil {
		return nil, err
	}

	client, err := ssh.Dial("tcp", *greedoAddr, &ssh.ClientConfig{
		User: *greedoUser,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeysCallback(agent.Signers),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
	})
	if err != nil {
		return nil, err
	}

	return client, nil
}

// Copy copies a local file reader to the remote destination path. This copies the enitre contents of contents into ram. Don't use this function if doing so is a bad idea. Only works on files less than 2 GB.
func Copy(mode os.FileMode, fileName string, contents io.Reader, destinationPath string) error {
	data, err := io.ReadAll(contents)
	if err != nil {
		return err
	}

	log.Println("dialing kahless...")
	client, err := Dial()
	if err != nil {
		return err
	}
	log.Println("done")

	session, err := client.NewSession()
	if err != nil {
		return err
	}

	err = scp.Copy(int64(len(data)), mode, fileName, bytes.NewBuffer(data), destinationPath, session)
	if err != nil {
		return err
	}

	return nil
}

// CopySlug copies a slug to Greedo's public files folder and returns its public-facing URL.
func CopySlug(fileName string, contents io.Reader) (string, error) {
	err := Copy(0644, fileName, contents, filepath.Join("/srv/http/xena.greedo.xeserv.us", "files", "slugs", fileName))
	if err != nil {
		return "", err
	}

	return WebURL(fileName), nil
}

// WebURL constructs a public-facing URL for a given resource by fragment.
func WebURL(fragment string) string {
	return "https://xena.greedo.xeserv.us/files/slugs/" + fragment
}