forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
meek.go
1396 lines (1167 loc) · 45.8 KB
/
meek.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2016, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"bytes"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"hash/crc64"
"io"
"net"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/Psiphon-Labs/goarista/monotime"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/nacl/box"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/obfuscator"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
tris "github.com/Psiphon-Labs/tls-tris"
)
// MeekServer is based on meek-server.go from Tor and Psiphon:
//
// https://gitweb.torproject.org/pluggable-transports/meek.git/blob/HEAD:/meek-client/meek-client.go
// CC0 1.0 Universal
//
// https://bitbucket.org/psiphon/psiphon-circumvention-system/src/default/go/meek-client/meek-client.go
const (
// Protocol version 1 clients can handle arbitrary length response bodies. Older clients
// report no version number and expect at most 64K response bodies.
MEEK_PROTOCOL_VERSION_1 = 1
// Protocol version 2 clients initiate a session by sending an encrypted and obfuscated meek
// cookie with their initial HTTP request. Connection information is contained within the
// encrypted cookie payload. The server inspects the cookie and establishes a new session and
// returns a new random session ID back to client via Set-Cookie header. The client uses this
// session ID on all subsequent requests for the remainder of the session.
MEEK_PROTOCOL_VERSION_2 = 2
// Protocol version 3 clients include resiliency enhancements and will add a Range header
// when retrying a request for a partially downloaded response payload.
MEEK_PROTOCOL_VERSION_3 = 3
MEEK_MAX_REQUEST_PAYLOAD_LENGTH = 65536
MEEK_TURN_AROUND_TIMEOUT = 20 * time.Millisecond
MEEK_EXTENDED_TURN_AROUND_TIMEOUT = 100 * time.Millisecond
MEEK_MAX_SESSION_STALENESS = 45 * time.Second
MEEK_HTTP_CLIENT_IO_TIMEOUT = 45 * time.Second
MEEK_MIN_SESSION_ID_LENGTH = 8
MEEK_MAX_SESSION_ID_LENGTH = 20
MEEK_DEFAULT_RESPONSE_BUFFER_LENGTH = 65536
MEEK_DEFAULT_POOL_BUFFER_LENGTH = 65536
MEEK_DEFAULT_POOL_BUFFER_COUNT = 2048
)
// MeekServer implements the meek protocol, which tunnels TCP traffic (in the case of Psiphon,
// Obfuscated SSH traffic) over HTTP. Meek may be fronted (through a CDN) or direct and may be
// HTTP or HTTPS.
//
// Upstream traffic arrives in HTTP request bodies and downstream traffic is sent in response
// bodies. The sequence of traffic for a given flow is associated using a session ID that's
// set as a HTTP cookie for the client to submit with each request.
//
// MeekServer hooks into TunnelServer via the net.Conn interface by transforming the
// HTTP payload traffic for a given session into net.Conn conforming Read()s and Write()s via
// the meekConn struct.
type MeekServer struct {
support *SupportServices
listener net.Listener
tlsConfig *tris.Config
clientHandler func(clientTunnelProtocol string, clientConn net.Conn)
openConns *common.Conns
stopBroadcast <-chan struct{}
sessionsLock sync.RWMutex
sessions map[string]*meekSession
checksumTable *crc64.Table
bufferPool *CachedResponseBufferPool
rateLimitLock sync.Mutex
rateLimitHistory map[string][]monotime.Time
rateLimitCount int
rateLimitSignalGC chan struct{}
}
// NewMeekServer initializes a new meek server.
func NewMeekServer(
support *SupportServices,
listener net.Listener,
useTLS, isFronted, useObfuscatedSessionTickets bool,
clientHandler func(clientTunnelProtocol string, clientConn net.Conn),
stopBroadcast <-chan struct{}) (*MeekServer, error) {
checksumTable := crc64.MakeTable(crc64.ECMA)
bufferLength := MEEK_DEFAULT_POOL_BUFFER_LENGTH
if support.Config.MeekCachedResponsePoolBufferSize != 0 {
bufferLength = support.Config.MeekCachedResponsePoolBufferSize
}
bufferCount := MEEK_DEFAULT_POOL_BUFFER_COUNT
if support.Config.MeekCachedResponsePoolBufferCount != 0 {
bufferCount = support.Config.MeekCachedResponsePoolBufferCount
}
bufferPool := NewCachedResponseBufferPool(bufferLength, bufferCount)
meekServer := &MeekServer{
support: support,
listener: listener,
clientHandler: clientHandler,
openConns: common.NewConns(),
stopBroadcast: stopBroadcast,
sessions: make(map[string]*meekSession),
checksumTable: checksumTable,
bufferPool: bufferPool,
rateLimitHistory: make(map[string][]monotime.Time),
rateLimitSignalGC: make(chan struct{}, 1),
}
if useTLS {
tlsConfig, err := makeMeekTLSConfig(
support, isFronted, useObfuscatedSessionTickets)
if err != nil {
return nil, common.ContextError(err)
}
meekServer.tlsConfig = tlsConfig
}
return meekServer, nil
}
// Run runs the meek server; this function blocks while serving HTTP or
// HTTPS connections on the specified listener. This function also runs
// a goroutine which cleans up expired meek client sessions.
//
// To stop the meek server, both Close() the listener and set the stopBroadcast
// signal specified in NewMeekServer.
func (server *MeekServer) Run() error {
waitGroup := new(sync.WaitGroup)
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
ticker := time.NewTicker(MEEK_MAX_SESSION_STALENESS / 2)
defer ticker.Stop()
for {
select {
case <-ticker.C:
server.deleteExpiredSessions()
case <-server.stopBroadcast:
return
}
}
}()
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
server.rateLimitWorker()
}()
// Serve HTTP or HTTPS
//
// - WriteTimeout may include time awaiting request, as per:
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts
// - Legacy meek-server wrapped each client HTTP connection with an explicit idle
// timeout net.Conn and didn't use http.Server timeouts. We could do the same
// here (use ActivityMonitoredConn) but the stock http.Server timeouts should
// now be sufficient.
httpServer := &http.Server{
ReadTimeout: MEEK_HTTP_CLIENT_IO_TIMEOUT,
WriteTimeout: MEEK_HTTP_CLIENT_IO_TIMEOUT,
Handler: server,
ConnState: server.httpConnStateCallback,
// Disable auto HTTP/2 (https://golang.org/doc/go1.6)
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)),
}
// Note: Serve() will be interrupted by listener.Close() call
var err error
if server.tlsConfig != nil {
httpsServer := HTTPSServer{Server: httpServer}
err = httpsServer.ServeTLS(server.listener, server.tlsConfig)
} else {
err = httpServer.Serve(server.listener)
}
// Can't check for the exact error that Close() will cause in Accept(),
// (see: https://code.google.com/p/go/issues/detail?id=4373). So using an
// explicit stop signal to stop gracefully.
select {
case <-server.stopBroadcast:
err = nil
default:
}
// deleteExpiredSessions calls deleteSession which may block waiting
// for active request handlers to complete; timely shutdown requires
// stopping the listener and closing all existing connections before
// awaiting the reaperWaitGroup.
server.listener.Close()
server.openConns.CloseAll()
waitGroup.Wait()
return err
}
// ServeHTTP handles meek client HTTP requests, where the request body
// contains upstream traffic and the response will contain downstream
// traffic.
func (server *MeekServer) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
// Note: no longer requiring that the request method is POST
// Check for the expected meek/session ID cookie.
// Also check for prohibited HTTP headers.
var meekCookie *http.Cookie
for _, c := range request.Cookies() {
meekCookie = c
break
}
if meekCookie == nil || len(meekCookie.Value) == 0 {
log.WithContext().Warning("missing meek cookie")
common.TerminateHTTPConnection(responseWriter, request)
return
}
if len(server.support.Config.MeekProhibitedHeaders) > 0 {
for _, header := range server.support.Config.MeekProhibitedHeaders {
value := request.Header.Get(header)
if header != "" {
log.WithContextFields(LogFields{
"header": header,
"value": value,
}).Warning("prohibited meek header")
common.TerminateHTTPConnection(responseWriter, request)
return
}
}
}
// A valid meek cookie indicates which class of request this is:
//
// 1. A new meek session. Create a new session ID and proceed with
// relaying tunnel traffic.
//
// 2. An existing meek session. Resume relaying tunnel traffic.
//
// 3. A request to an endpoint. This meek connection is not for relaying
// tunnel traffic. Instead, the request is handed off to a custom handler.
sessionID, session, endPoint, clientIP, err := server.getSessionOrEndpoint(request, meekCookie)
if err != nil {
// Debug since session cookie errors commonly occur during
// normal operation.
log.WithContextFields(LogFields{"error": err}).Debug("session lookup failed")
common.TerminateHTTPConnection(responseWriter, request)
return
}
if endPoint != "" {
// Endpoint mode. Currently, this means it's handled by the tactics
// request handler.
geoIPData := server.support.GeoIPService.Lookup(clientIP)
handled := server.support.TacticsServer.HandleEndPoint(
endPoint, common.GeoIPData(geoIPData), responseWriter, request)
if !handled {
log.WithContextFields(LogFields{"endPoint": endPoint}).Info("unhandled endpoint")
common.TerminateHTTPConnection(responseWriter, request)
}
return
}
// Tunnel relay mode.
// Ensure that there's only one concurrent request handler per client
// session. Depending on the nature of a network disruption, it can
// happen that a client detects a failure and retries while the server
// is still streaming response in the handler for the _previous_ client
// request.
//
// Even if the session.cachedResponse were safe for concurrent
// use (it is not), concurrent handling could lead to loss of session
// since upstream data read by the first request may not reach the
// cached response before the second request reads the cached data.
//
// The existing handler will stream response data, holding the lock,
// for no more than MEEK_EXTENDED_TURN_AROUND_TIMEOUT.
//
// TODO: interrupt an existing handler? The existing handler will be
// sending data to the cached response, but if that buffer fills, the
// session will be lost.
requestNumber := atomic.AddInt64(&session.requestCount, 1)
// Wait for the existing request to complete.
session.lock.Lock()
defer session.lock.Unlock()
// If a newer request has arrived while waiting, discard this one.
// Do not delay processing the newest request.
//
// If the session expired and was deleted while this request was waiting,
// discard this request. The session is no longer valid, and the final call
// to session.cachedResponse.Reset may have already occured, so any further
// session.cachedResponse access may deplete resources (fail to refill the pool).
if atomic.LoadInt64(&session.requestCount) > requestNumber || session.deleted {
common.TerminateHTTPConnection(responseWriter, request)
return
}
// pumpReads causes a TunnelServer/SSH goroutine blocking on a Read to
// read the request body as upstream traffic.
// TODO: run pumpReads and pumpWrites concurrently?
// pumpReads checksums the request payload and skips relaying it when
// it matches the immediately previous request payload. This allows
// clients to resend request payloads, when retrying due to connection
// interruption, without knowing whether the server has received or
// relayed the data.
err = session.clientConn.pumpReads(request.Body)
if err != nil {
if err != io.EOF {
// Debug since errors such as "i/o timeout" occur during normal operation;
// also, golang network error messages may contain client IP.
log.WithContextFields(LogFields{"error": err}).Debug("read request failed")
}
common.TerminateHTTPConnection(responseWriter, request)
// Note: keep session open to allow client to retry
return
}
// Set cookie before writing the response.
if session.meekProtocolVersion >= MEEK_PROTOCOL_VERSION_2 && session.sessionIDSent == false {
// Replace the meek cookie with the session ID.
// SetCookie for the the session ID cookie is only set once, to reduce overhead. This
// session ID value replaces the original meek cookie value.
http.SetCookie(responseWriter, &http.Cookie{Name: meekCookie.Name, Value: sessionID})
session.sessionIDSent = true
}
// When streaming data into the response body, a copy is
// retained in the cachedResponse buffer. This allows the
// client to retry and request that the response be resent
// when the HTTP connection is interrupted.
//
// If a Range header is present, the client is retrying,
// possibly after having received a partial response. In
// this case, use any cached response to attempt to resend
// the response, starting from the resend position the client
// indicates.
//
// When the resend position is not available -- because the
// cachedResponse buffer could not hold it -- the client session
// is closed, as there's no way to resume streaming the payload
// uninterrupted.
//
// The client may retry before a cached response is prepared,
// so a cached response is not always used when a Range header
// is present.
//
// TODO: invalid Range header is ignored; should it be otherwise?
position, isRetry := checkRangeHeader(request)
if isRetry {
atomic.AddInt64(&session.metricClientRetries, 1)
}
hasCompleteCachedResponse := session.cachedResponse.HasPosition(0)
// The client is not expected to send position > 0 when there is
// no cached response; let that case fall through to the next
// HasPosition check which will fail and close the session.
var responseSize int
var responseError error
if isRetry && (hasCompleteCachedResponse || position > 0) {
if !session.cachedResponse.HasPosition(position) {
greaterThanSwapInt64(&session.metricCachedResponseMissPosition, int64(position))
common.TerminateHTTPConnection(responseWriter, request)
session.delete(true)
return
}
responseWriter.WriteHeader(http.StatusPartialContent)
// TODO:
// - enforce a max extended buffer count per client, for
// fairness? Throttling may make this unnecessary.
// - cachedResponse can now start releasing extended buffers,
// as response bytes before "position" will never be requested
// again?
responseSize, responseError = session.cachedResponse.CopyFromPosition(position, responseWriter)
greaterThanSwapInt64(&session.metricPeakCachedResponseHitSize, int64(responseSize))
// The client may again fail to receive the payload and may again
// retry, so not yet releasing cachedResponse buffers.
} else {
// _Now_ we release buffers holding data from the previous
// response. And then immediately stream the new response into
// newly acquired buffers.
session.cachedResponse.Reset()
// Note: this code depends on an implementation detail of
// io.MultiWriter: a Write() to the MultiWriter writes first
// to the cache, and then to the response writer. So if the
// write to the response writer fails, the payload is cached.
multiWriter := io.MultiWriter(session.cachedResponse, responseWriter)
// The client expects 206, not 200, whenever it sets a Range header,
// which it may do even when no cached response is prepared.
if isRetry {
responseWriter.WriteHeader(http.StatusPartialContent)
}
// pumpWrites causes a TunnelServer/SSH goroutine blocking on a Write to
// write its downstream traffic through to the response body.
responseSize, responseError = session.clientConn.pumpWrites(multiWriter)
greaterThanSwapInt64(&session.metricPeakResponseSize, int64(responseSize))
greaterThanSwapInt64(&session.metricPeakCachedResponseSize, int64(session.cachedResponse.Available()))
}
// responseError is the result of writing the body either from CopyFromPosition or pumpWrites
if responseError != nil {
if responseError != io.EOF {
// Debug since errors such as "i/o timeout" occur during normal operation;
// also, golang network error messages may contain client IP.
log.WithContextFields(LogFields{"error": responseError}).Debug("write response failed")
}
common.TerminateHTTPConnection(responseWriter, request)
// Note: keep session open to allow client to retry
return
}
}
func checkRangeHeader(request *http.Request) (int, bool) {
rangeHeader := request.Header.Get("Range")
if rangeHeader == "" {
return 0, false
}
prefix := "bytes="
suffix := "-"
if !strings.HasPrefix(rangeHeader, prefix) ||
!strings.HasSuffix(rangeHeader, suffix) {
return 0, false
}
rangeHeader = strings.TrimPrefix(rangeHeader, prefix)
rangeHeader = strings.TrimSuffix(rangeHeader, suffix)
position, err := strconv.Atoi(rangeHeader)
if err != nil {
return 0, false
}
return position, true
}
// getSessionOrEndpoint checks if the cookie corresponds to an existing tunnel
// relay session ID. If no session is found, the cookie must be an obfuscated
// meek cookie. A new session is created when the meek cookie indicates relay
// mode; or the endpoint is returned when the meek cookie indicates endpoint
// mode.
func (server *MeekServer) getSessionOrEndpoint(
request *http.Request, meekCookie *http.Cookie) (string, *meekSession, string, string, error) {
// Check for an existing session.
server.sessionsLock.RLock()
existingSessionID := meekCookie.Value
session, ok := server.sessions[existingSessionID]
server.sessionsLock.RUnlock()
if ok {
// TODO: can multiple http client connections using same session cookie
// cause race conditions on session struct?
session.touch()
return existingSessionID, session, "", "", nil
}
// Determine the client remote address, which is used for geolocation
// and stats. When an intermediate proxy or CDN is in use, we may be
// able to determine the original client address by inspecting HTTP
// headers such as X-Forwarded-For.
clientIP := strings.Split(request.RemoteAddr, ":")[0]
if len(server.support.Config.MeekProxyForwardedForHeaders) > 0 {
for _, header := range server.support.Config.MeekProxyForwardedForHeaders {
value := request.Header.Get(header)
if len(value) > 0 {
// Some headers, such as X-Forwarded-For, are a comma-separated
// list of IPs (each proxy in a chain). The first IP should be
// the client IP.
proxyClientIP := strings.Split(value, ",")[0]
if net.ParseIP(proxyClientIP) != nil &&
server.support.GeoIPService.Lookup(proxyClientIP).Country != GEOIP_UNKNOWN_VALUE {
clientIP = proxyClientIP
break
}
}
}
}
if server.rateLimit(clientIP) {
return "", nil, "", "", common.ContextError(errors.New("rate limit exceeded"))
}
// The session is new (or expired). Treat the cookie value as a new meek
// cookie, extract the payload, and create a new session.
payloadJSON, err := getMeekCookiePayload(server.support, meekCookie.Value)
if err != nil {
return "", nil, "", "", common.ContextError(err)
}
// Note: this meek server ignores legacy values PsiphonClientSessionId
// and PsiphonServerAddress.
var clientSessionData protocol.MeekCookieData
err = json.Unmarshal(payloadJSON, &clientSessionData)
if err != nil {
return "", nil, "", "", common.ContextError(err)
}
// Handle endpoints before enforcing the GetEstablishTunnels check.
// Currently, endpoints are tactics requests, and we allow these to be
// handled by servers which would otherwise reject new tunnels.
if clientSessionData.EndPoint != "" {
return "", nil, clientSessionData.EndPoint, clientIP, nil
}
// Don't create new sessions when not establishing. A subsequent SSH handshake
// will not succeed, so creating a meek session just wastes resources.
if server.support.TunnelServer != nil &&
!server.support.TunnelServer.GetEstablishTunnels() {
return "", nil, "", "", common.ContextError(errors.New("not establishing tunnels"))
}
// Create a new session
bufferLength := MEEK_DEFAULT_RESPONSE_BUFFER_LENGTH
if server.support.Config.MeekCachedResponseBufferSize != 0 {
bufferLength = server.support.Config.MeekCachedResponseBufferSize
}
cachedResponse := NewCachedResponse(bufferLength, server.bufferPool)
session = &meekSession{
meekProtocolVersion: clientSessionData.MeekProtocolVersion,
sessionIDSent: false,
cachedResponse: cachedResponse,
}
session.touch()
// Create a new meek conn that will relay the payload
// between meek request/responses and the tunnel server client
// handler. The client IP is also used to initialize the
// meek conn with a useful value to return when the tunnel
// server calls conn.RemoteAddr() to get the client's IP address.
// Assumes clientIP is a valid IP address; the port value is a stub
// and is expected to be ignored.
clientConn := newMeekConn(
server,
session,
&net.TCPAddr{
IP: net.ParseIP(clientIP),
Port: 0,
},
clientSessionData.MeekProtocolVersion)
session.clientConn = clientConn
// Note: MEEK_PROTOCOL_VERSION_1 doesn't support changing the
// meek cookie to a session ID; v1 clients always send the
// original meek cookie value with each request. The issue with
// v1 is that clients which wake after a device sleep will attempt
// to resume a meek session and the server can't differentiate
// between resuming a session and creating a new session. This
// causes the v1 client connection to hang/timeout.
sessionID := meekCookie.Value
if clientSessionData.MeekProtocolVersion >= MEEK_PROTOCOL_VERSION_2 {
sessionID, err = makeMeekSessionID()
if err != nil {
return "", nil, "", "", common.ContextError(err)
}
}
server.sessionsLock.Lock()
server.sessions[sessionID] = session
server.sessionsLock.Unlock()
// Note: from the tunnel server's perspective, this client connection
// will close when session.delete calls Close() on the meekConn.
server.clientHandler(clientSessionData.ClientTunnelProtocol, session.clientConn)
return sessionID, session, "", "", nil
}
func (server *MeekServer) rateLimit(clientIP string) bool {
historySize, thresholdSeconds, regions, ISPs, GCTriggerCount, _ :=
server.support.TrafficRulesSet.GetMeekRateLimiterConfig()
if historySize == 0 {
return false
}
if len(regions) > 0 || len(ISPs) > 0 {
// TODO: avoid redundant GeoIP lookups?
geoIPData := server.support.GeoIPService.Lookup(clientIP)
if len(regions) > 0 {
if !common.Contains(regions, geoIPData.Country) {
return false
}
}
if len(ISPs) > 0 {
if !common.Contains(ISPs, geoIPData.ISP) {
return false
}
}
}
limit := true
triggerGC := false
now := monotime.Now()
threshold := now.Add(-time.Duration(thresholdSeconds) * time.Second)
server.rateLimitLock.Lock()
history, ok := server.rateLimitHistory[clientIP]
if !ok || len(history) != historySize {
history = make([]monotime.Time, historySize)
server.rateLimitHistory[clientIP] = history
}
for i := 0; i < len(history); i++ {
if history[i] == 0 || history[i].Before(threshold) {
limit = false
}
if i == len(history)-1 {
history[i] = now
} else {
history[i] = history[i+1]
}
}
if limit {
server.rateLimitCount += 1
if server.rateLimitCount >= GCTriggerCount {
triggerGC = true
server.rateLimitCount = 0
}
}
server.rateLimitLock.Unlock()
if triggerGC {
select {
case server.rateLimitSignalGC <- *new(struct{}):
default:
}
}
return limit
}
func (server *MeekServer) rateLimitWorker() {
_, _, _, _, _, reapFrequencySeconds :=
server.support.TrafficRulesSet.GetMeekRateLimiterConfig()
timer := time.NewTimer(time.Duration(reapFrequencySeconds) * time.Second)
defer timer.Stop()
for {
select {
case <-timer.C:
_, thresholdSeconds, _, _, _, reapFrequencySeconds :=
server.support.TrafficRulesSet.GetMeekRateLimiterConfig()
server.rateLimitLock.Lock()
threshold := monotime.Now().Add(-time.Duration(thresholdSeconds) * time.Second)
for key, history := range server.rateLimitHistory {
reap := true
for i := 0; i < len(history); i++ {
if history[i] != 0 && !history[i].Before(threshold) {
reap = false
}
}
if reap {
delete(server.rateLimitHistory, key)
}
}
// Enable rate limit history map to be garbage collected when possible.
if len(server.rateLimitHistory) == 0 {
server.rateLimitHistory = make(map[string][]monotime.Time)
}
server.rateLimitLock.Unlock()
timer.Reset(time.Duration(reapFrequencySeconds) * time.Second)
case <-server.rateLimitSignalGC:
runtime.GC()
case <-server.stopBroadcast:
return
}
}
}
func (server *MeekServer) deleteSession(sessionID string) {
// Don't obtain the server.sessionsLock write lock until modifying
// server.sessions, as the session.delete can block for up to
// MEEK_HTTP_CLIENT_IO_TIMEOUT. Allow new sessions to be added
// concurrently.
//
// Since a lock isn't held for the duration, concurrent calls to
// deleteSession with the same sessionID could happen; this is
// not expected since only the reaper goroutine calls deleteExpiredSessions
// (and in any case concurrent execution of the ok block is not an issue).
server.sessionsLock.RLock()
session, ok := server.sessions[sessionID]
server.sessionsLock.RUnlock()
if ok {
session.delete(false)
server.sessionsLock.Lock()
delete(server.sessions, sessionID)
server.sessionsLock.Unlock()
}
}
func (server *MeekServer) deleteExpiredSessions() {
// A deleteSession call may block for up to MEEK_HTTP_CLIENT_IO_TIMEOUT,
// so grab a snapshot list of expired sessions and do not hold a lock for
// the duration of deleteExpiredSessions. This allows new sessions to be
// added concurrently.
//
// New sessions added after the snapshot is taken will be checked for
// expiry on subsequent periodic calls to deleteExpiredSessions.
//
// To avoid long delays in releasing resources, individual deletes are
// performed concurrently.
server.sessionsLock.Lock()
expiredSessionIDs := make([]string, 0)
for sessionID, session := range server.sessions {
if session.expired() {
expiredSessionIDs = append(expiredSessionIDs, sessionID)
}
}
server.sessionsLock.Unlock()
start := monotime.Now()
deleteWaitGroup := new(sync.WaitGroup)
for _, sessionID := range expiredSessionIDs {
deleteWaitGroup.Add(1)
go func(sessionID string) {
defer deleteWaitGroup.Done()
server.deleteSession(sessionID)
}(sessionID)
}
deleteWaitGroup.Wait()
log.WithContextFields(
LogFields{"elapsed time": monotime.Since(start)}).Debug("deleted expired sessions")
}
// httpConnStateCallback tracks open persistent HTTP/HTTPS connections to the
// meek server.
func (server *MeekServer) httpConnStateCallback(conn net.Conn, connState http.ConnState) {
switch connState {
case http.StateNew:
server.openConns.Add(conn)
case http.StateHijacked, http.StateClosed:
server.openConns.Remove(conn)
}
}
type meekSession struct {
// Note: 64-bit ints used with atomic operations are placed
// at the start of struct to ensure 64-bit alignment.
// (https://golang.org/pkg/sync/atomic/#pkg-note-BUG)
lastActivity int64
requestCount int64
metricClientRetries int64
metricPeakResponseSize int64
metricPeakCachedResponseSize int64
metricPeakCachedResponseHitSize int64
metricCachedResponseMissPosition int64
lock sync.Mutex
deleted bool
clientConn *meekConn
meekProtocolVersion int
sessionIDSent bool
cachedResponse *CachedResponse
}
func (session *meekSession) touch() {
atomic.StoreInt64(&session.lastActivity, int64(monotime.Now()))
}
func (session *meekSession) expired() bool {
lastActivity := monotime.Time(atomic.LoadInt64(&session.lastActivity))
return monotime.Since(lastActivity) > MEEK_MAX_SESSION_STALENESS
}
// delete releases all resources allocated by a session.
func (session *meekSession) delete(haveLock bool) {
// TODO: close the persistent HTTP client connection, if one exists?
// This final call session.cachedResponse.Reset releases shared resources.
//
// This call requires exclusive access. session.lock is be obtained before
// calling session.cachedResponse.Reset. Once the lock is obtained, no
// request for this session is being processed concurrently, and pending
// requests will block at session.lock.
//
// This logic assumes that no further session.cachedResponse access occurs,
// or else resources may deplete (buffers won't be returned to the pool).
// These requirements are achieved by obtaining the lock, setting
// session.deleted, and any subsequent request handlers checking
// session.deleted immediately after obtaining the lock.
//
// session.lock.Lock may block for up to MEEK_HTTP_CLIENT_IO_TIMEOUT,
// the timeout for any active request handler processing a session
// request.
//
// When the lock must be acquired, clientConn.Close is called first, to
// interrupt any existing request handler blocking on pumpReads or pumpWrites.
session.clientConn.Close()
if !haveLock {
session.lock.Lock()
}
// Release all extended buffers back to the pool.
// session.cachedResponse.Reset is not safe for concurrent calls.
session.cachedResponse.Reset()
session.deleted = true
if !haveLock {
session.lock.Unlock()
}
}
// GetMetrics implements the common.MetricsSource interface.
func (session *meekSession) GetMetrics() common.LogFields {
logFields := make(common.LogFields)
logFields["meek_client_retries"] = atomic.LoadInt64(&session.metricClientRetries)
logFields["meek_peak_response_size"] = atomic.LoadInt64(&session.metricPeakResponseSize)
logFields["meek_peak_cached_response_size"] = atomic.LoadInt64(&session.metricPeakCachedResponseSize)
logFields["meek_peak_cached_response_hit_size"] = atomic.LoadInt64(&session.metricPeakCachedResponseHitSize)
logFields["meek_cached_response_miss_position"] = atomic.LoadInt64(&session.metricCachedResponseMissPosition)
return logFields
}
// makeMeekTLSConfig creates a TLS config for a meek HTTPS listener.
// Currently, this config is optimized for fronted meek where the nature
// of the connection is non-circumvention; it's optimized for performance
// assuming the peer is an uncensored CDN.
func makeMeekTLSConfig(
support *SupportServices,
isFronted, useObfuscatedSessionTickets bool) (*tris.Config, error) {
certificate, privateKey, err := common.GenerateWebServerCertificate(common.GenerateHostName())
if err != nil {
return nil, common.ContextError(err)
}
tlsCertificate, err := tris.X509KeyPair(
[]byte(certificate), []byte(privateKey))
if err != nil {
return nil, common.ContextError(err)
}
config := &tris.Config{
Certificates: []tris.Certificate{tlsCertificate},
NextProtos: []string{"http/1.1"},
MinVersion: tris.VersionTLS10,
UseExtendedMasterSecret: true,
}
if isFronted {
// This is a reordering of the supported CipherSuites in golang 1.6. Non-ephemeral key
// CipherSuites greatly reduce server load, and we try to select these since the meek
// protocol is providing obfuscation, not privacy/integrity (this is provided by the
// tunneled SSH), so we don't benefit from the perfect forward secrecy property provided
// by ephemeral key CipherSuites.
// https://github.com/golang/go/blob/1cb3044c9fcd88e1557eca1bf35845a4108bc1db/src/crypto/tls/cipher_suites.go#L75
//
// This optimization is applied only when there's a CDN in front of the meek server; in
// unfronted cases we prefer a more natural TLS handshake.
config.CipherSuites = []uint16{
tris.TLS_RSA_WITH_AES_128_GCM_SHA256,
tris.TLS_RSA_WITH_AES_256_GCM_SHA384,
tris.TLS_RSA_WITH_RC4_128_SHA,
tris.TLS_RSA_WITH_AES_128_CBC_SHA,
tris.TLS_RSA_WITH_AES_256_CBC_SHA,
tris.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
tris.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tris.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tris.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tris.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tris.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
tris.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
tris.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tris.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tris.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tris.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tris.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
}
config.PreferServerCipherSuites = true
}
if useObfuscatedSessionTickets {
// See obfuscated session ticket overview
// in NewObfuscatedClientSessionCache.
var obfuscatedSessionTicketKey [32]byte
key, err := hex.DecodeString(support.Config.MeekObfuscatedKey)
if err == nil && len(key) != 32 {
err = errors.New("invalid obfuscated session key length")
}
if err != nil {