-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh.go
189 lines (170 loc) · 3.75 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package ssh
import (
"bufio"
"bytes"
"fmt"
"io"
"net"
"net/http"
"os"
"time"
"golang.org/x/crypto/ssh"
)
type SSH struct {
*ssh.Client
info *Info
bastion *SSH
}
func New(info *Info) *SSH {
return &SSH{
info: info,
}
}
func (s *SSH) Bastion(info *Info) *SSH {
s.bastion = New(info)
return s
}
func (s *SSH) Close() {
_ = s.Client.Close()
if s.bastion != nil {
s.bastion.Close()
}
}
func (s *SSH) Connect() (*SSH, error) {
config, err := s.info.prepare()
if err != nil {
return nil, err
}
if s.bastion != nil {
if _, err := s.bastion.Connect(); err != nil {
return nil, err
}
tunnelConn, err := s.bastion.Dial("tcp", s.bastion.info.serverAddr())
if err != nil {
return nil, err
}
conn, chans, reqs, err := ssh.NewClientConn(tunnelConn, s.bastion.info.serverAddr(), config)
if err != nil {
return nil, err
}
client := ssh.NewClient(conn, chans, reqs)
s.Client = client
fmt.Printf("[SSH] connected to %s over %s..\n", s.info.serverAddr(), s.bastion.info.serverAddr())
} else {
client, err := ssh.Dial("tcp", s.info.serverAddr(), config)
if err != nil {
return nil, err
}
s.Client = client
fmt.Printf("[SSH] connected to %s..\n", s.info.serverAddr())
}
return s, err
}
func (s *SSH) ExecCmd(cmd string) (string, string, error) {
session, err := s.NewSession()
if err != nil {
return "", "", err
}
defer func() {
_ = session.Close()
}()
var stdOut bytes.Buffer
var stdErr bytes.Buffer
session.Stdout = &stdOut
session.Stderr = &stdErr
if err := session.Run(cmd); err != nil {
return stdOut.String(), stdErr.String(), err
}
return stdOut.String(), stdErr.String(), nil
}
func (s *SSH) ExecCmdPipe(cmd string, output chan string) error {
session, err := s.NewSession()
if err != nil {
return err
}
defer func() {
_ = session.Close()
}()
stdout, err := session.StdoutPipe()
if err != nil {
return err
}
if err := session.Start(cmd); err != nil {
return err
}
reader := bufio.NewReader(stdout)
for {
line, err := reader.ReadString('\n')
if err != nil || err == io.EOF {
break
}
output <- line
}
return session.Wait()
}
func (s *SSH) ProxyHttpTransport() http.RoundTripper {
return &http.Transport{
Dial: s.Dial,
TLSHandshakeTimeout: 10 * time.Second,
}
}
func (s *SSH) ProxyHttpClient() *http.Client {
client := http.Client{
Transport: s.ProxyHttpTransport(),
}
return &client
}
func (s *SSH) TunnelProxy(localHost, remoteHost string, localPort, remotePort int, closeCh chan os.Signal) error {
localAddr := fmt.Sprintf("%s:%d", localHost, localPort)
remoateAddr := fmt.Sprintf("%s:%d", remoteHost, remotePort)
localLister, err := net.Listen("tcp", localAddr)
if err != nil {
return err
}
defer localLister.Close()
fmt.Printf("[TUNNEL] start listening on %s...\n", localAddr)
errCh := make(chan error)
go func() {
for {
local, err := localLister.Accept()
if err != nil {
if err == io.EOF {
continue
}
break
}
go s.handleClient(local, remoateAddr, errCh)
}
}()
for {
select {
case <-closeCh:
fmt.Printf("[TUNNEL] stop listening on %s:%d...\n", localHost, localPort)
return nil
case err := <-errCh:
return err
}
}
}
func (s *SSH) handleClient(local net.Conn, remoteAddr string, errCh chan error) {
remote, err := s.Dial("tcp", remoteAddr)
if err != nil {
errCh <- err
return
}
fmt.Printf("[TUNNEL] request %s over %s..\n", remoteAddr, s.info.serverAddr())
defer remote.Close()
defer local.Close()
exchangeErrCh := make(chan error, 1)
go exchange(remote, local, exchangeErrCh)
go exchange(local, remote, exchangeErrCh)
for i := 0; i < 2; i++ {
if err := <-errCh; err != nil {
if err == io.EOF {
fmt.Println("[TUNNEL] request EOF")
}
if _, ok := err.(*net.OpError); !ok {
}
}
}
}