-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
355 lines (302 loc) · 8.12 KB
/
server.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package tcprouter
import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
"strings"
"sync"
"time"
"github.com/libp2p/go-yamux"
"github.com/rs/zerolog/log"
"github.com/abronan/valkeyrie/store"
)
// ServerOptions hold the configuration of server listeners
type ServerOptions struct {
ListeningAddr string
ListeningTLSPort uint
ListeningHTTPPort uint
ListeningForClientsPort uint
}
// HTTPAddr returns the HTTP listener address
func (o ServerOptions) HTTPAddr() string {
return fmt.Sprintf("%s:%d", o.ListeningAddr, o.ListeningHTTPPort)
}
// TLSAddr returns the TLS listener address
func (o ServerOptions) TLSAddr() string {
return fmt.Sprintf("%s:%d", o.ListeningAddr, o.ListeningTLSPort)
}
// ClientsAddr returns the client listener address
func (o ServerOptions) ClientsAddr() string {
return fmt.Sprintf("%s:%d", o.ListeningAddr, o.ListeningForClientsPort)
}
//Server is tcp router server
type Server struct {
ServerOptions ServerOptions
DbStore store.Store
Services map[string]Service
activeConnections map[string]*yamux.Session
listeners []net.Listener
listenersMU sync.Mutex
wg sync.WaitGroup
}
// NewServer creates a new server
func NewServer(forwardOptions ServerOptions, store store.Store, services map[string]Service) *Server {
if services == nil {
services = make(map[string]Service)
}
return &Server{
ServerOptions: forwardOptions,
Services: services,
DbStore: store,
activeConnections: make(map[string]*yamux.Session),
listeners: []net.Listener{},
}
}
// Start starts the server and blocks forever
func (s *Server) Start(ctx context.Context) error {
s.wg.Add(3)
go s.listen(ctx, s.ServerOptions.HTTPAddr(), HandlerFunc(s.handleHTTPConnection))
go s.listen(ctx, s.ServerOptions.TLSAddr(), HandlerFunc(s.handleConnection))
go s.listen(ctx, s.ServerOptions.ClientsAddr(), HandlerFunc(s.handleTCPRouterClientConnection))
s.wg.Wait()
log.Info().Msg("stopping server...")
s.listenersMU.Lock()
defer s.listenersMU.Unlock()
for _, ln := range s.listeners {
if ln != nil {
if err := ln.Close(); err != nil {
log.Error().Err(err).Msg("error closing connection")
}
}
}
log.Info().Msg("stopped")
return nil
}
func (s *Server) listen(ctx context.Context, addr string, handler Handler) {
defer s.wg.Done()
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
log.Fatal().
Err(err).
Str("addr", addr).
Msg("failed to resolve addr")
}
ln, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
log.Fatal().
Err(err).
Str("addr", addr).
Msg("failed to start listener")
}
s.listenersMU.Lock()
s.listeners = append(s.listeners, tcpKeepAliveListener{ln})
s.listenersMU.Unlock()
for {
select {
case <-ctx.Done():
return
default:
ln.SetDeadline(time.Now().Add(time.Second))
conn, err := ln.AcceptTCP()
if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue
}
log.Fatal().Err(err).Msg("Failed to accept connection")
}
go handler.ServeTCP(conn)
}
}
}
func (s *Server) getHost(host string) (Service, error) {
service := Service{}
key := fmt.Sprintf("tcprouter/service/%s", host)
servicePair, err := s.DbStore.Get(key, nil)
if err != nil {
return service, fmt.Errorf("host not found at key %s: %v", key, err)
}
err = json.Unmarshal(servicePair.Value, &service)
if err != nil {
return service, fmt.Errorf("invalid service content")
}
log.Debug().
Str("key", key).
Str("service", fmt.Sprintf("%v", service)).
Msg("service found")
return service, nil
}
func (s *Server) handleTCPRouterClientConnection(conn WriteCloser) {
session, err := yamux.Server(conn, nil)
if err != nil {
log.Error().Err(err).Send()
return
}
stream, err := session.Accept()
if err != nil {
log.Error().Err(err).Send()
return
}
defer stream.Close()
hs := &Handshake{}
if err := hs.Read(stream); err != nil {
log.Error().Err(err).Msg("handshake failed")
conn.Close()
return
}
if hs.MagicNr != MagicNr {
log.Error().Msgf("expected %d MagicNr and received %d", MagicNr, hs.MagicNr)
conn.Close()
return
}
log.Info().
Str("remote addr", conn.RemoteAddr().String()).
Msg("handshake done... adding to active connections")
s.activeConnections[string(hs.Secret[:])] = session
}
func (s *Server) handleConnection(conn WriteCloser) {
br := bufio.NewReader(conn)
serverName, isTLS, peeked := clientHelloServerName(br)
log.Info().
Str("server name", serverName).
Bool("is TLS", isTLS).
Msg("connection analyzed")
if err := s.handleService(conn, serverName, peeked, isTLS); err != nil {
log.Error().
Str("server name", serverName).
Err(err).
Msg("error forwarding traffic")
}
}
func (s *Server) handleHTTPConnection(conn WriteCloser) {
br := bufio.NewReader(conn)
peeked := ""
host := ""
for {
line, err := br.ReadString('\n')
if err != nil {
log.Error().Err(err).Msg("failed to decode HTTP header")
return
}
peeked = peeked + line
if strings.HasPrefix(line, "Host:") {
host = strings.Trim(line[6:], " \n\r")
if strings.Contains(host, ":") {
host, _, err = net.SplitHostPort(host)
if err != nil {
log.Error().
Err(err).
Str("server name", host).
Msg("failed to parse split host port from server name")
return
}
}
}
if strings.Trim(line, " \n\r") == "" {
break
}
}
if host == "" {
log.Error().Msg("could not find host in HTTP header")
conn.Close()
return
}
peeked += getPeeked(br)
log.Info().Msgf("Host found: '%s'", host)
if err := s.handleService(conn, host, peeked, false); err != nil {
log.Error().
Str("server name", host).
Err(err).
Msg("error forwarding traffic")
}
}
func (s *Server) handleService(incoming WriteCloser, serverName, peeked string, isTLS bool) error {
serverName = strings.ToLower(serverName)
service, exists := s.Services[serverName]
if !exists {
log.Info().Msg("not found in file config, try to load it from db backend")
var err error
service, err = s.getHost(serverName)
exists = err == nil
}
if !exists {
service, exists = s.Services["CATCH_ALL"]
if !exists {
incoming.Close()
return fmt.Errorf("service doesn't exist: %v and no 'CATCH_ALL' service for request", service)
}
}
log.Info().Str("service", fmt.Sprintf("%v", service)).Msg("service found")
incoming = GetConn(incoming, peeked)
var (
outgoing WriteCloser
err error
)
if service.ClientSecret != "" {
// retrive an active connection and forward traffic on it
activeConn, ok := s.activeConnections[service.ClientSecret]
if !ok {
incoming.Close()
return fmt.Errorf("no active connection for service %s", serverName)
}
log.Info().Msgf("open new stream to client %s", serverName)
stream, err := activeConn.OpenStream()
if err != nil {
incoming.Close()
return fmt.Errorf("failed to open stream: %w", err)
}
outgoing = WrapConn(stream)
} else {
// Dial target server and forward traffic on it
remotePort := service.HTTPPort
if isTLS {
remotePort = service.TLSPort
}
outgoing, err = net.DialTCP("tcp", nil, &net.TCPAddr{IP: net.ParseIP(service.Addr), Port: remotePort})
if err != nil {
incoming.Close()
return fmt.Errorf("error while connection to service: %v", err)
}
}
forwardConnection(incoming, outgoing)
return nil
}
func forwardConnection(local, remote WriteCloser) {
log.Info().
Str("remote", remote.RemoteAddr().String()).
Str("local", local.RemoteAddr().String()).
Msg("forward active connection")
cErr := make(chan error)
defer func() {
local.Close()
remote.Close()
}()
go forward(local, remote, cErr)
go forward(remote, local, cErr)
err := <-cErr
if err != nil {
log.Error().
Str("remote", remote.RemoteAddr().String()).
Str("local", local.RemoteAddr().String()).
Err(err).
Msg("Error during connection")
}
<-cErr
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
if err = tc.SetKeepAlive(true); err != nil {
return nil, err
}
if err = tc.SetKeepAlivePeriod(3 * time.Minute); err != nil {
return nil, err
}
return tc, nil
}