forked from dan-v/awslambdaproxy
/
tunnelconnection.go
227 lines (201 loc) · 6.37 KB
/
tunnelconnection.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
package awslambdaproxy
import (
"log"
"net"
"sync"
"time"
"github.com/hashicorp/yamux"
"github.com/pkg/errors"
"strconv"
)
const (
maxTunnels = 10
forwardPort = "8082"
tunnelPort = "8081"
)
type tunnelConnection struct {
conn net.Conn
sess *yamux.Session
streams map[uint32]*yamux.Stream
time time.Time
}
type connectionManager struct {
forwardListener net.Listener
tunnelListener net.Listener
tunnelConnections map[string]tunnelConnection
tunnelMutex sync.RWMutex
tunnelExpectedRuntime float64
tunnelRedeployNeeded chan bool
activeTunnel string
localProxy *LocalProxy
}
func (t *connectionManager) runForwarder() {
t.waitUntilTunnelIsAvailable()
for {
c, err := t.forwardListener.Accept()
if err != nil {
log.Println("Failed to accept user connection")
return
}
go t.handleForwardConnection(c)
}
}
func (t *connectionManager) handleForwardConnection(localProxyConn net.Conn) {
tunnelStream, err := t.openNewStreamInActiveTunnel()
if err != nil {
log.Println("Failed to open new stream in active tunnel", err)
return
}
bidirectionalCopy(localProxyConn, tunnelStream)
}
func (t *connectionManager) runTunnel() {
for {
if len(t.tunnelConnections) > maxTunnels {
log.Println("Too many active tunnelConnections: " + string(len(t.tunnelConnections)) + ". MAX=" +
string(maxTunnels) + ". Waiting for cleanup.")
time.Sleep(time.Second * 5)
continue
}
c, err := t.tunnelListener.Accept()
if err != nil {
log.Println("Failed to accept tunnel connection")
time.Sleep(time.Second * 5)
continue
}
log.Println("Accepted tunnel connection from", c.RemoteAddr())
tunnelSession, err := yamux.Client(c, nil)
if err != nil {
log.Println("Failed to start session inside tunnel")
time.Sleep(time.Second * 5)
continue
}
log.Println("Established session to", tunnelSession.RemoteAddr())
t.tunnelMutex.Lock()
t.activeTunnel = c.RemoteAddr().String()
t.tunnelConnections[t.activeTunnel] = tunnelConnection{
conn: c,
sess: tunnelSession,
streams: make(map[uint32]*yamux.Stream),
time: time.Now(),
}
t.tunnelMutex.Unlock()
go t.monitorTunnelSessionHealth(t.activeTunnel)
log.Println("Active tunnel count: ", len(t.tunnelConnections))
for k, v := range t.tunnelConnections {
log.Println("---------------")
log.Println("Connection: " + k)
log.Println("Start Time: " + v.time.String())
log.Println("Total Streams: " + strconv.Itoa(v.sess.NumStreams()))
log.Println("---------------")
}
}
}
func (t *connectionManager) removeTunnelConnection(connectionID string) {
t.tunnelConnections[connectionID].sess.Close()
t.tunnelConnections[connectionID].conn.Close()
t.tunnelMutex.Lock()
delete(t.tunnelConnections, connectionID)
t.tunnelMutex.Unlock()
}
func (t *connectionManager) monitorTunnelSessionHealth(connectionID string) {
for {
_, err := t.tunnelConnections[connectionID].sess.Ping()
if err != nil {
if time.Since(t.tunnelConnections[connectionID].time).Seconds() < t.tunnelExpectedRuntime {
log.Println("Signaling for emergency tunnel due to tunnel ending early: ", time.Since(t.tunnelConnections[connectionID].time).Seconds())
t.tunnelRedeployNeeded <- true
}
t.removeTunnelConnection(connectionID)
break
}
if time.Since(t.tunnelConnections[connectionID].time).Seconds() > t.tunnelExpectedRuntime {
numStreams := t.tunnelConnections[connectionID].sess.NumStreams()
if numStreams > 0 {
log.Println("Tunnel " + connectionID + " that is being closed still has open streams: " + strconv.Itoa(numStreams) + ". Delaying cleanup.")
time.Sleep(20 * time.Second)
log.Println("Delayed cleanup now running for ", connectionID)
} else {
log.Println("Tunnel " + connectionID + " is safe to close")
}
log.Println("Removing tunnel", connectionID)
t.removeTunnelConnection(connectionID)
break
}
time.Sleep(time.Millisecond * 50)
}
}
func (t *connectionManager) openNewStreamInActiveTunnel() (*yamux.Stream, error) {
for {
t.tunnelMutex.RLock()
tunnel, ok := t.tunnelConnections[t.activeTunnel]
t.tunnelMutex.RUnlock()
if ok {
stream, err := tunnel.sess.OpenStream()
tunnel.streams[stream.StreamID()] = stream
return stream, err
}
log.Println("No active tunnel session available. Retrying..")
time.Sleep(time.Second)
}
}
func (t *connectionManager) waitUntilTunnelIsAvailable() error {
timeout := time.After(time.Second * time.Duration(t.tunnelExpectedRuntime))
tick := time.Tick(time.Second)
for {
select {
case <-timeout:
return errors.New("Timed out waiting for tunnel to be established. Likely the " +
"Lambda function is having issues communicating with this host.")
case <-tick:
if t.isReady() == true {
return nil
}
log.Println("Waiting for tunnel to be established..")
}
}
}
func (t *connectionManager) isReady() bool {
if t.activeTunnel == "" {
return false
}
return true
}
func newTunnelConnectionManager(frequency time.Duration, localProxy *LocalProxy) (*connectionManager, error) {
forwardListener, err := startForwardListener()
if err != nil {
return nil, errors.Wrap(err, "Failed to start UserListener")
}
tunnelListener, err := startTunnelListener()
if err != nil {
return nil, errors.Wrap(err, "Failed to start TunnelListener")
}
connectionManager := &connectionManager{
forwardListener: forwardListener,
tunnelListener: tunnelListener,
tunnelConnections: make(map[string]tunnelConnection),
tunnelRedeployNeeded: make(chan bool),
tunnelExpectedRuntime: frequency.Seconds(),
localProxy: localProxy,
}
go connectionManager.runTunnel()
go connectionManager.runForwarder()
return connectionManager, nil
}
func startTunnelListener() (net.Listener, error) {
tunnelAddress := "localhost:" + tunnelPort
tunnelListener, err := net.Listen("tcp", tunnelAddress)
if err != nil {
return nil, errors.Wrap(err, "Failed to start TCP tunnel listener on port "+tunnelPort)
}
log.Println("Started tunnel listener on port " + tunnelPort)
return tunnelListener, nil
}
func startForwardListener() (net.Listener, error) {
forwardAddress := "localhost:" + forwardPort
forwardListener, err := net.Listen("tcp", forwardAddress)
if err != nil {
return nil, errors.Wrap(err, "Failed to start TCP user listener on port "+forwardPort)
}
log.Println("Started user listener on port " + forwardPort)
return forwardListener, nil
}