/
main.go
106 lines (94 loc) · 2.43 KB
/
main.go
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
// Copyright 2019 Google LLC
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net"
"net/http"
"os"
"path/filepath"
"time"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
func main() {
f, err := os.Open(os.Args[3])
if err != nil {
log.Fatalln("Failed to open file:", err)
}
defer f.Close()
log.Println("Connecting via SSH...")
sshc, err := sshConnect(os.Args[1], os.Args[2])
if err != nil {
log.Fatalln("Failed to connect via SSH:", err)
}
defer sshc.Close()
log.Println("Uploading file to Web UI...")
if err := uploadFile(sshc.Dial, f); err != nil {
log.Fatalln("Failed to upload file:", err)
}
log.Println("Success!")
}
func sshConnect(endpoint, fingerprint string) (*ssh.Client, error) {
socket := os.Getenv("SSH_AUTH_SOCK")
conn, err := net.Dial("unix", socket)
if err != nil {
return nil, fmt.Errorf("failed to open SSH_AUTH_SOCK: %v", err)
}
agentClient := agent.NewClient(conn)
config := &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.PublicKeysCallback(agentClient.Signers),
},
HostKeyAlgorithms: []string{"ssh-ed25519"},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
if fp := ssh.FingerprintSHA256(key); fp != fingerprint {
return fmt.Errorf("incorrect host key: %v", fp)
}
return nil
},
}
return ssh.Dial("tcp", endpoint, config)
}
func uploadFile(dial func(network, addr string) (net.Conn, error), f *os.File) error {
body := &bytes.Buffer{}
w := multipart.NewWriter(body)
name := filepath.Base(f.Name())
if fw, err := w.CreateFormFile("file", name); err != nil {
return err
} else if _, err := io.Copy(fw, f); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
client := &http.Client{
Transport: &http.Transport{
Dial: dial,
DisableKeepAlives: true,
},
Timeout: 5 * time.Minute,
}
url := "http://10.11.99.1/upload"
req, err := http.NewRequest("POST", url, body)
if err != nil {
return err
}
req.Header.Set("Content-Type", w.FormDataContentType())
res, err := client.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
return fmt.Errorf("got HTTP status %d: %s", res.StatusCode, res.Status)
}
return nil
}