-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh.go
131 lines (115 loc) · 2.58 KB
/
ssh.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"io"
"net"
"strconv"
"go.uber.org/zap"
"golang.org/x/crypto/ssh"
)
// SSH client and server
type SshClientDataChannel struct {
sess *ssh.Session
rdc io.Reader
wrc io.WriteCloser
}
func (sdc SshClientDataChannel) Read(data []byte) (int, error) {
return sdc.rdc.Read(data)
}
func (sdc SshClientDataChannel) Write(data []byte) (int, error) {
return sdc.wrc.Write(data)
}
func (sdc SshClientDataChannel) Close() error {
return sdc.sess.Close()
}
func (sdc SshClientDataChannel) CloseWrite() error {
return sdc.wrc.Close()
}
type SshClient struct {
est bool
dc SshClientDataChannel
addr string
cfg ssh.ClientConfig
}
func (c *SshClient) Connect() (DataChannel, error) {
c.est = false
lg.Debug("Setting up SSH client connection", zap.String("remote", c.addr))
cc, err := ssh.Dial("tcp", c.addr, &c.cfg)
if err != nil {
lg.Debug("Unable to establish SSH connection", zap.Error(err))
return nil, err
}
c.dc.sess, err = cc.NewSession()
if err != nil {
lg.Debug("Unable to get new SSH client session")
return nil, err
}
c.est = true
c.dc.rdc, err = c.dc.sess.StdoutPipe()
if err != nil {
lg.Debug("Unable to get read channel")
return nil, err
}
c.dc.wrc, err = c.dc.sess.StdinPipe()
if err != nil {
lg.Debug("Unable to get write channel")
return nil, err
}
err = c.dc.sess.Shell()
return c.dc, nil
}
func (c *SshClient) Close() error {
if c.est {
c.est = false
return c.dc.Close()
}
return nil
}
type SshServer struct {
lnr net.Listener
addr string
cfg ssh.ServerConfig
}
func (s *SshServer) Listen() error {
var err error
s.lnr, err = net.Listen("tcp", s.addr)
return err
}
func (s *SshServer) Accept(dcs chan<- interface{}) error {
tcpConn, err := s.lnr.Accept()
if err != nil {
lg.Warn("Accept() failed")
return err
}
_, chans, reqs, err := ssh.NewServerConn(tcpConn, &s.cfg)
go ssh.DiscardRequests(reqs)
go func() {
for newChannel := range chans {
connection, requests, err := newChannel.Accept()
if err != nil {
lg.Debug("Unable to accept channel")
continue
}
lg.Debug("New channel accepted")
dcs <- connection
go func() {
for req := range requests {
lg.Debug("Request received", zap.String("request", req.Type), zap.String("want-reply", strconv.FormatBool(req.WantReply)))
switch req.Type {
case "exec":
req.Reply(true, nil)
case "shell":
req.Reply(true, nil)
case "pty-req":
req.Reply(true, nil)
case "window-change":
req.Reply(true, nil)
}
}
}()
}
}()
return nil
}
func (s *SshServer) Close() error {
return s.lnr.Close()
}