/
main.go
100 lines (81 loc) · 1.91 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
package main
import (
"bytes"
"context"
"flag"
"net"
"sync"
"github.com/BurntSushi/toml"
"github.com/davecgh/go-spew/spew"
"github.com/melbahja/goph"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
)
func main() {
cfgFile := flag.String("config", "config.toml", "Config file")
flag.Parse()
var cfg Config
_, err := toml.DecodeFile(*cfgFile, &cfg)
if err != nil {
panic(err)
}
auth, err := getAuth(cfg)
if err != nil {
panic(err)
}
callback, err := getKnownHosts(cfg)
if err != nil {
panic(err)
}
wrappedCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
// fmt.Println(hostname, remote, key)
_ = callback(hostname, remote, key)
return nil
}
client, err := goph.NewConn(&goph.Config{
User: cfg.Remote.User,
Addr: cfg.Remote.Host,
Port: cfg.Remote.Port,
Auth: auth,
Timeout: goph.DefaultTimeout,
Callback: wrappedCallback,
})
if err != nil {
panic(err)
}
rootContext, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()
var wg sync.WaitGroup
for i := range cfg.Tail {
cfg.Tail[i].Run(rootContext, &wg, client)
}
for i := range cfg.Pipe {
cfg.Pipe[i].Run(rootContext, &wg, client)
}
wg.Wait()
}
func getAuth(config Config) (goph.Auth, error) {
if config.Remote.Agent {
return goph.UseAgent()
}
return goph.Key(config.Remote.PrivateKeyFile, "")
}
func getKnownHosts(config Config) (ssh.HostKeyCallback, error) {
if config.Remote.KnownHosts {
return goph.DefaultKnownHosts()
}
if config.Remote.KnownHost != "" {
knownKey, err := ssh.ParsePublicKey([]byte(config.Remote.KnownHost))
if err != nil {
return nil, err
}
return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
if bytes.Equal(knownKey.Marshal(), key.Marshal()) {
return nil
}
spew.Dump(key)
return &knownhosts.KeyError{}
}, nil
}
return ssh.InsecureIgnoreHostKey(), nil
}