forked from inconshreveable/go-tunnel
/
session.go
330 lines (263 loc) · 7.06 KB
/
session.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package server
import (
"fmt"
conn "github.com/inconshreveable/go-tunnel/conn"
log "github.com/inconshreveable/go-tunnel/log"
proto "github.com/inconshreveable/go-tunnel/proto"
util "github.com/inconshreveable/go-tunnel/util"
muxado "github.com/inconshreveable/muxado"
"reflect"
"runtime/debug"
"sort"
"sync"
"time"
)
type Session struct {
// logger
log.Logger
// auth message
auth *proto.Auth
// session start time
start time.Time
// underlying mux session
mux muxado.Session
// all of the tunnels this session handles
tunnels map[string]*Tunnel
// identifier
id string
// session hooks
hooks SessionHooks
// binders
binders Binders
// synchronization for accessing Session.tunnels
sync.Mutex
// hooks for tunnels
tunnelHooks TunnelHooks
// safe-shutdown synchronization
guard shutdownGuard
// registry
registry *sessionRegistry
}
type SessionHooks interface {
OnAuth(*Session, *proto.Auth) error
OnBind(*Session, *proto.Bind) error
OnClose(*Session) error
}
func NewSession(mux muxado.Session, registry *sessionRegistry, sessHooks SessionHooks, tunnelHooks TunnelHooks, binders Binders) *Session {
return &Session{
start: time.Now(),
Logger: log.NewTaggedLogger("session"),
mux: mux,
tunnels: make(map[string]*Tunnel, 0),
registry: registry,
hooks: sessHooks,
binders: binders,
tunnelHooks: tunnelHooks,
}
}
// Run runs the the tunnel session
func (s *Session) Run() (err error) {
defer s.recoverPanic("Session.Run")
go func() {
defer s.recoverPanic("Session.mux.Wait")
code, err, debug := s.mux.Wait()
s.Info("Session mux shutdown with code %v error %v debug %v", code, err, debug)
}()
defer s.mux.Close()
// A tunnel session starts with an auth stream
if err = s.handleAuth(); err != nil {
return
}
// then we handle new streams sent from the client
for {
stream, err := s.mux.Accept()
if err != nil {
s.Shutdown()
return s.Error("Failed to accept stream: %v", err)
}
go s.handleStream(conn.Wrap(stream, "stream", s.id))
}
}
func (s *Session) handleAuth() error {
// accept ann auth stream
raw, err := s.mux.Accept()
if err != nil {
return s.Error("Failed to accept auth stream: %v", err)
}
defer raw.Close()
stream := conn.Wrap(raw, "session", "auth")
// read the Auth message
if err = proto.ReadMsgInto(stream, &s.auth); err != nil {
return s.Error("Failed to read auth message; %v", err)
}
failAuth := func(e error) error {
_ = proto.WriteMsg(stream, &proto.AuthResp{Error: e.Error()})
return e
}
// generate a client identifier
s.id = s.auth.ClientId
if s.id == "" {
// it's a new session, assign an ID
if s.id, err = util.SecureRandId(16); err != nil {
return failAuth(fmt.Errorf("Failed generate client identifier: %v", err))
}
}
// put ourselves in the registry
s.registry.register(s)
// set logging prefix
s.Logger.AddTags(s.id)
// agree on protocol version
// if proto.Version not in s.auth.Version
if sort.SearchStrings(s.auth.Version, proto.Version) == len(s.auth.Version) {
return failAuth(fmt.Errorf("No acceptable protocol version. Requested: %v, capable: %v", s.auth.Version, proto.Version))
}
// auth hook
if err = s.hooks.OnAuth(s, s.auth); err != nil {
return failAuth(err)
}
// Respond to authentication
authResp := &proto.AuthResp{
Version: proto.Version,
ClientId: s.id,
}
if err = proto.WriteMsg(stream, authResp); err != nil {
return failAuth(fmt.Errorf("Failed to write authentication response: %v", err))
}
return nil
}
func (s *Session) handleStream(stream conn.Conn) {
defer s.recoverPanic("Session.handleStream")
defer stream.Close()
// make sure we only process streams while we're not shutting down
if err := s.guard.Enter(); err != nil {
stream.Error("Failing stream, session is shutting down")
return
}
defer s.guard.Exit()
raw, err := proto.ReadMsg(stream)
if err != nil {
stream.Error("Failed to read message: %v")
go s.Shutdown()
return
}
switch msg := raw.(type) {
case *proto.Bind:
err = s.handleBind(stream, msg)
case *proto.Unbind:
err = s.handleUnbind(stream, msg)
default:
err = fmt.Errorf("Unknown message type: %v", reflect.TypeOf(raw))
}
if err != nil {
stream.Error("Error on stream: %v", err)
go s.Shutdown()
return
}
return
}
func (s *Session) handleBind(stream conn.Conn, bind *proto.Bind) (err error) {
stream.Debug("Binding new tunnel: %v", bind)
respond := func(resp *proto.BindResp) {
if err = proto.WriteMsg(stream, resp); err != nil {
err = stream.Error("Failed to send bind response: %v", err)
}
}
if err = s.hooks.OnBind(s, bind); err != nil {
return
}
t, err := newTunnel(bind, s, s.binders, s.tunnelHooks)
if err != nil {
respond(&proto.BindResp{Error: err.Error()})
return
}
t.Info("Registered new tunnel on session %s", s.id)
// add it to the list of tunnels
s.addTunnel(t)
// acknowledge success
respond(&proto.BindResp{Url: t.url})
return
}
func (s *Session) handleUnbind(stream conn.Conn, unbind *proto.Unbind) (err error) {
s.Debug("Unbinding tunnel")
// remote it from the list of tunnels
t, ok := s.delTunnel(unbind.Url)
if !ok {
return s.Error("Failed to unbind tunnel %s: no tunnel found.", unbind.Url)
}
if err = t.shutdown(); err != nil {
return s.Error("Failed to unbind tunnel %s: %v", unbind.Url, err)
}
// acknowledge success
unbindResp := &proto.UnbindResp{}
if err = proto.WriteMsg(stream, unbindResp); err != nil {
return s.Error("Failed to write unbind resp: %v", err)
}
return
}
func (s *Session) Shutdown() {
s.recoverPanic("Session.Shutdown")
s.guard.BeginShutdown()
defer s.guard.CompleteShutdown()
// run shutdown hooks
if err := s.hooks.OnClose(s); err != nil {
s.Error("OnClose hook failed with error: %v", err)
return
}
// shutdown all of the tunnels
for _, t := range s.tunnels {
t.shutdown()
}
// remove ourselves from the registry
s.registry.unregister(s)
// close underlying mux session
s.mux.Close()
s.Info("Shutdown complete")
}
// Opens a new proxy stream to the client and writes a StartProxy message
// with the given client address and tunnel url.
func (s *Session) openProxy(clientAddr, tunnelUrl string) (pxy conn.Conn, err error) {
// open a new proxy stream
pxyStream, err := s.mux.Open()
if err != nil {
return
}
pxy = conn.Wrap(pxyStream)
// tell the client we're going to start using this proxy connection
startProxy := &proto.StartProxy{
ClientAddr: clientAddr,
Url: tunnelUrl,
}
if err = proto.WriteMsg(pxy, startProxy); err != nil {
return
}
pxy.AddTags(tunnelUrl)
return
}
func (s *Session) addTunnel(t *Tunnel) {
s.Lock()
defer s.Unlock()
s.tunnels[t.url] = t
}
func (s *Session) delTunnel(url string) (*Tunnel, bool) {
s.Lock()
defer s.Unlock()
t, ok := s.tunnels[url]
if ok {
delete(s.tunnels, url)
}
return t, ok
}
func (s *Session) recoverPanic(name string) {
if r := recover(); r != nil {
s.Error("%s failed with error %v: %s", name, r, debug.Stack())
}
}
func (s *Session) Auth() *proto.Auth {
return s.auth
}
func (s *Session) Id() string {
return s.id
}
func (s *Session) Start() time.Time {
return s.start
}