-
Notifications
You must be signed in to change notification settings - Fork 0
/
remotesite.go
260 lines (228 loc) · 6.88 KB
/
remotesite.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reversetunnel
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"sync"
"time"
"github.com/gravitational/roundtrip"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
log "github.com/Sirupsen/logrus"
"github.com/mailgun/oxy/forward"
"golang.org/x/crypto/ssh"
)
// remoteSite is a remote site who established the inbound connecton to
// the local reverse tunnel server, and now it can provide access to the
// cluster behind it.
type remoteSite struct {
sync.Mutex
log *log.Entry
domainName string
connections []*remoteConn
lastUsed int
lastActive time.Time
srv *server
transport *http.Transport
clt *auth.Client
}
func (s *remoteSite) GetClient() (auth.ClientI, error) {
return s.clt, nil
}
func (s *remoteSite) String() string {
return fmt.Sprintf("remoteSite(%v)", s.domainName)
}
func (s *remoteSite) connectionCount() int {
s.Lock()
defer s.Unlock()
return len(s.connections)
}
func (s *remoteSite) nextConn() (*remoteConn, error) {
s.Lock()
defer s.Unlock()
for {
if len(s.connections) == 0 {
return nil, trace.NotFound("no active tunnels to cluster %v", s.GetName())
}
s.lastUsed = (s.lastUsed + 1) % len(s.connections)
remoteConn := s.connections[s.lastUsed]
if !remoteConn.isInvalid() {
return remoteConn, nil
}
s.connections = append(s.connections[:s.lastUsed], s.connections[s.lastUsed+1:]...)
s.lastUsed = 0
go remoteConn.Close()
}
}
// addConn helper adds a new active remote cluster connection to the list
// of such connections
func (s *remoteSite) addConn(conn net.Conn, sshConn ssh.Conn) (*remoteConn, error) {
rc := &remoteConn{
sshConn: sshConn,
conn: conn,
log: s.log,
}
s.Lock()
defer s.Unlock()
s.connections = append(s.connections, rc)
s.lastUsed = 0
return rc, nil
}
func (s *remoteSite) GetStatus() string {
s.Lock()
defer s.Unlock()
diff := time.Now().Sub(s.lastActive)
if diff > 2*defaults.ReverseTunnelAgentHeartbeatPeriod {
return RemoteSiteStatusOffline
}
return RemoteSiteStatusOnline
}
func (s *remoteSite) setLastActive(t time.Time) {
s.Lock()
defer s.Unlock()
s.lastActive = t
}
func (s *remoteSite) handleHeartbeat(conn *remoteConn, ch ssh.Channel, reqC <-chan *ssh.Request) {
defer func() {
s.log.Infof("[TUNNEL] site connection closed: %v", s.domainName)
conn.Close()
}()
for {
select {
case req := <-reqC:
if req == nil {
s.log.Infof("[TUNNEL] site disconnected: %v", s.domainName)
conn.markInvalid(trace.ConnectionProblem(nil, "agent disconnected"))
return
}
log.Debugf("[TUNNEL] ping from \"%s\" %s", s.domainName, conn.conn.RemoteAddr())
s.setLastActive(time.Now())
case <-time.After(3 * defaults.ReverseTunnelAgentHeartbeatPeriod):
conn.markInvalid(trace.ConnectionProblem(nil, "agent missed 3 heartbeats"))
}
}
}
func (s *remoteSite) GetName() string {
return s.domainName
}
func (s *remoteSite) GetLastConnected() time.Time {
s.Lock()
defer s.Unlock()
return s.lastActive
}
// dialAccessPoint establishes a connection from the proxy (reverse tunnel server)
// back into the client using previously established tunnel.
func (s *remoteSite) dialAccessPoint(network, addr string) (net.Conn, error) {
s.log.Infof("[TUNNEL] dial to site '%s'", s.GetName())
try := func() (net.Conn, error) {
remoteConn, err := s.nextConn()
if err != nil {
return nil, trace.Wrap(err)
}
ch, _, err := remoteConn.sshConn.OpenChannel(chanAccessPoint, nil)
if err != nil {
remoteConn.markInvalid(err)
s.log.Errorf("[TUNNEL] disconnecting site '%s' on %v. Err: %v",
s.GetName(),
remoteConn.conn.RemoteAddr(),
err)
return nil, trace.Wrap(err)
}
s.log.Infof("[TUNNEL] success dialing to site '%s'", s.GetName())
return utils.NewChConn(remoteConn.sshConn, ch), nil
}
for {
conn, err := try()
if err != nil {
if trace.IsNotFound(err) {
return nil, trace.Wrap(err)
}
continue
}
return conn, nil
}
}
// Dial is used to connect a requesting client (say, tsh) to an SSH server
// located in a remote connected site, the connection goes through the
// reverse proxy tunnel.
func (s *remoteSite) Dial(from, to net.Addr) (conn net.Conn, err error) {
s.log.Infof("[TUNNEL] dialing %v@%v through the tunnel", to, s.domainName)
stop := false
_, addr := to.Network(), to.String()
try := func() (net.Conn, error) {
remoteConn, err := s.nextConn()
if err != nil {
return nil, trace.Wrap(err)
}
var ch ssh.Channel
ch, _, err = remoteConn.sshConn.OpenChannel(chanTransport, nil)
if err != nil {
remoteConn.markInvalid(err)
return nil, trace.Wrap(err)
}
stop = true
// send a special SSH out-of-band request called "teleport-transport"
// the agent on the other side will create a new TCP/IP connection to
// 'addr' on its network and will start proxying that connection over
// this SSH channel:
var dialed bool
dialed, err = ch.SendRequest(chanTransportDialReq, true, []byte(addr))
if err != nil {
return nil, trace.Wrap(err)
}
if !dialed {
defer ch.Close()
// pull the error message from the tunnel client (remote cluster)
// passed to us via stderr:
errMessage, _ := ioutil.ReadAll(ch.Stderr())
if errMessage == nil {
errMessage = []byte("failed connecting to " + addr)
}
return nil, trace.Errorf(strings.TrimSpace(string(errMessage)))
}
return utils.NewChConn(remoteConn.sshConn, ch), nil
}
// loop through existing TCP/IP connections (reverse tunnels) and try
// to establish an inbound connection-over-ssh-channel to the remote
// cluster (AKA "remotetunnel agent"):
for i := 0; i < s.connectionCount() && !stop; i++ {
conn, err = try()
if err == nil {
return conn, nil
}
s.log.Errorf("[TUNNEL] Dial(addr=%v) failed: %v", addr, err)
}
// didn't connect and no error? this means we didn't have any connected
// tunnels to try
if err == nil {
err = trace.Errorf("%v is offline", s.GetName())
}
return nil, err
}
func (s *remoteSite) handleAuthProxy(w http.ResponseWriter, r *http.Request) {
s.log.Infof("[TUNNEL] handleAuthProxy()")
fwd, err := forward.New(forward.RoundTripper(s.transport), forward.Logger(s.log))
if err != nil {
roundtrip.ReplyJSON(w, http.StatusInternalServerError, err.Error())
return
}
r.URL.Scheme = "http"
r.URL.Host = "stub"
fwd.ServeHTTP(w, r)
}