-
Notifications
You must be signed in to change notification settings - Fork 256
/
serverApi.go
1291 lines (1056 loc) · 41.4 KB
/
serverApi.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) 2015, 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 psiphon
import (
"bytes"
"context"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/buildinfo"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/fragmentor"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/transferstats"
)
// ServerContext is a utility struct which holds all of the data associated
// with a Psiphon server connection. In addition to the established tunnel, this
// includes data and transport mechanisms for Psiphon API requests. Legacy servers
// offer the Psiphon API through a web service; newer servers offer the Psiphon
// API through SSH requests made directly through the tunnel's SSH client.
type ServerContext struct {
tunnel *Tunnel
psiphonHttpsClient *http.Client
statsRegexps *transferstats.Regexps
clientUpgradeVersion string
serverHandshakeTimestamp string
paddingPRNG *prng.PRNG
}
// MakeSessionId creates a new session ID. The same session ID is used across
// multi-tunnel controller runs, where each tunnel has its own ServerContext
// instance.
// In server-side stats, we now consider a "session" to be the lifetime of the
// Controller (e.g., the user's commanded start and stop) and we measure this
// duration as well as the duration of each tunnel within the session.
func MakeSessionId() (string, error) {
randomId, err := common.MakeSecureRandomBytes(protocol.PSIPHON_API_CLIENT_SESSION_ID_LENGTH)
if err != nil {
return "", errors.Trace(err)
}
return hex.EncodeToString(randomId), nil
}
// NewServerContext makes the tunneled handshake request to the Psiphon server
// and returns a ServerContext struct for use with subsequent Psiphon server API
// requests (e.g., periodic connected and status requests).
func NewServerContext(tunnel *Tunnel) (*ServerContext, error) {
// For legacy servers, set up psiphonHttpsClient for
// accessing the Psiphon API via the web service.
var psiphonHttpsClient *http.Client
if !tunnel.dialParams.ServerEntry.SupportsSSHAPIRequests() ||
tunnel.config.TargetApiProtocol == protocol.PSIPHON_WEB_API_PROTOCOL {
var err error
psiphonHttpsClient, err = makePsiphonHttpsClient(tunnel)
if err != nil {
return nil, errors.Trace(err)
}
}
serverContext := &ServerContext{
tunnel: tunnel,
psiphonHttpsClient: psiphonHttpsClient,
paddingPRNG: prng.NewPRNGWithSeed(tunnel.dialParams.APIRequestPaddingSeed),
}
ignoreRegexps := tunnel.config.GetParameters().Get().Bool(
parameters.IgnoreHandshakeStatsRegexps)
err := serverContext.doHandshakeRequest(ignoreRegexps)
if err != nil {
return nil, errors.Trace(err)
}
return serverContext, nil
}
// doHandshakeRequest performs the "handshake" API request. The handshake
// returns upgrade info, newly discovered server entries -- which are
// stored -- and sponsor info (home pages, stat regexes).
func (serverContext *ServerContext) doHandshakeRequest(
ignoreStatsRegexps bool) error {
params := serverContext.getBaseAPIParameters(baseParametersAll)
// The server will return a signed copy of its own server entry when the
// client specifies this 'missing_server_entry_signature' parameter.
//
// The purpose of this mechanism is to rapidly upgrade client local storage
// from unsigned to signed server entries, and to ensure that the client has
// a signed server entry for its currently connected server as required for
// the client-to-client exchange feature.
//
// The server entry will be included in handshakeResponse.EncodedServerList,
// along side discovery servers.
requestedMissingSignature := false
if !serverContext.tunnel.dialParams.ServerEntry.HasSignature() {
requestedMissingSignature = true
params["missing_server_entry_signature"] =
serverContext.tunnel.dialParams.ServerEntry.Tag
}
doTactics := !serverContext.tunnel.config.DisableTactics
networkID := ""
if doTactics {
// Limitation: it is assumed that the network ID obtained here is the
// one that is active when the handshake request is received by the
// server. However, it is remotely possible to switch networks
// immediately after invoking the GetNetworkID callback and initiating
// the handshake, if the tunnel protocol is meek.
//
// The response handling code below calls GetNetworkID again and ignores
// any tactics payload if the network ID is not the same. While this
// doesn't detect all cases of changing networks, it reduces the already
// narrow window.
networkID = serverContext.tunnel.config.GetNetworkID()
err := tactics.SetTacticsAPIParameters(
GetTacticsStorer(serverContext.tunnel.config),
networkID,
params)
if err != nil {
return errors.Trace(err)
}
}
// When split tunnel mode is enabled, indicate this to the server. When
// indicated, the server will perform split tunnel classifications on TCP
// port forwards and reject, with a distinct response, port forwards which
// the client should connect to directly, untunneled.
if serverContext.tunnel.config.SplitTunnelOwnRegion {
params["split_tunnel"] = "1"
}
// While regular split tunnel mode makes untunneled connections to
// destinations in the client's own country, selected split tunnel mode
// allows the client to specify a list of untunneled countries. Either or
// both modes may be enabled.
if len(serverContext.tunnel.config.SplitTunnelRegions) > 0 {
params["split_tunnel_regions"] = serverContext.tunnel.config.SplitTunnelRegions
}
var response []byte
if serverContext.psiphonHttpsClient == nil {
params[protocol.PSIPHON_API_HANDSHAKE_AUTHORIZATIONS] =
serverContext.tunnel.config.GetAuthorizations()
request, err := serverContext.makeSSHAPIRequestPayload(params)
if err != nil {
return errors.Trace(err)
}
response, err = serverContext.tunnel.SendAPIRequest(
protocol.PSIPHON_API_HANDSHAKE_REQUEST_NAME, request)
if err != nil {
return errors.Trace(err)
}
} else {
// Legacy web service API request
responseBody, err := serverContext.doGetRequest(
makeRequestUrl(serverContext.tunnel, "", "handshake", params))
if err != nil {
return errors.Trace(err)
}
// Skip legacy format lines and just parse the JSON config line
configLinePrefix := []byte("Config: ")
for _, line := range bytes.Split(responseBody, []byte("\n")) {
if bytes.HasPrefix(line, configLinePrefix) {
response = line[len(configLinePrefix):]
break
}
}
if len(response) == 0 {
return errors.TraceNew("no config line found")
}
}
// Legacy fields:
// - 'preemptive_reconnect_lifetime_milliseconds' is unused and ignored
// - 'ssh_session_id' is ignored; client session ID is used instead
var handshakeResponse protocol.HandshakeResponse
// Initialize these fields to distinguish between psiphond omitting values in
// the response and the zero value, which means unlimited rate.
handshakeResponse.UpstreamBytesPerSecond = -1
handshakeResponse.DownstreamBytesPerSecond = -1
err := json.Unmarshal(response, &handshakeResponse)
if err != nil {
return errors.Trace(err)
}
if serverContext.tunnel.config.EmitClientAddress {
NoticeClientAddress(handshakeResponse.ClientAddress)
}
NoticeClientRegion(handshakeResponse.ClientRegion)
// Emit a SplitTunnelRegions notice indicating active split tunnel region.
// For SplitTunnelOwnRegion, the handshake ClientRegion is the split
// tunnel region and this region is always listed first.
splitTunnelRegions := []string{}
if serverContext.tunnel.config.SplitTunnelOwnRegion {
splitTunnelRegions = []string{handshakeResponse.ClientRegion}
}
for _, region := range serverContext.tunnel.config.SplitTunnelRegions {
if !serverContext.tunnel.config.SplitTunnelOwnRegion ||
region != handshakeResponse.ClientRegion {
splitTunnelRegions = append(splitTunnelRegions, region)
}
}
if len(splitTunnelRegions) > 0 {
NoticeSplitTunnelRegions(splitTunnelRegions)
}
var serverEntries []protocol.ServerEntryFields
// Store discovered server entries
// We use the server's time, as it's available here, for the server entry
// timestamp since this is more reliable than the client time.
for _, encodedServerEntry := range handshakeResponse.EncodedServerList {
serverEntryFields, err := protocol.DecodeServerEntryFields(
encodedServerEntry,
common.TruncateTimestampToHour(handshakeResponse.ServerTimestamp),
protocol.SERVER_ENTRY_SOURCE_DISCOVERY)
if err != nil {
return errors.Trace(err)
}
// Retain the original timestamp and source in the requestedMissingSignature
// case, as this server entry was not discovered here.
//
// Limitation: there is a transient edge case where
// requestedMissingSignature will be set for a discovery server entry that
// _is_ also discovered here.
if requestedMissingSignature &&
serverEntryFields.GetIPAddress() == serverContext.tunnel.dialParams.ServerEntry.IpAddress {
serverEntryFields.SetLocalTimestamp(serverContext.tunnel.dialParams.ServerEntry.LocalTimestamp)
serverEntryFields.SetLocalSource(serverContext.tunnel.dialParams.ServerEntry.LocalSource)
}
err = protocol.ValidateServerEntryFields(serverEntryFields)
if err != nil {
// Skip this entry and continue with the next one
NoticeWarning("invalid handshake server entry: %s", err)
continue
}
serverEntries = append(serverEntries, serverEntryFields)
}
err = StoreServerEntries(
serverContext.tunnel.config,
serverEntries,
true)
if err != nil {
return errors.Trace(err)
}
NoticeHomepages(handshakeResponse.Homepages)
serverContext.clientUpgradeVersion = handshakeResponse.UpgradeClientVersion
if handshakeResponse.UpgradeClientVersion != "" {
NoticeClientUpgradeAvailable(handshakeResponse.UpgradeClientVersion)
} else {
NoticeClientIsLatestVersion("")
}
if !ignoreStatsRegexps {
// The handshake returns page_view_regexes and https_request_regexes.
// page_view_regexes is obsolete and not used. https_request_regexes, which
// are actually host/domain name regexes, are used for host/domain name
// bytes transferred metrics: tunneled traffic TLS SNI server names and HTTP
// Host header host names are matched against these regexes to select flows
// for bytes transferred counting.
var regexpsNotices []string
serverContext.statsRegexps, regexpsNotices = transferstats.MakeRegexps(
handshakeResponse.HttpsRequestRegexes)
for _, notice := range regexpsNotices {
NoticeWarning(notice)
}
}
diagnosticID := serverContext.tunnel.dialParams.ServerEntry.GetDiagnosticID()
serverContext.serverHandshakeTimestamp = handshakeResponse.ServerTimestamp
NoticeServerTimestamp(diagnosticID, serverContext.serverHandshakeTimestamp)
NoticeActiveAuthorizationIDs(diagnosticID, handshakeResponse.ActiveAuthorizationIDs)
NoticeTrafficRateLimits(
diagnosticID,
handshakeResponse.UpstreamBytesPerSecond,
handshakeResponse.DownstreamBytesPerSecond)
if doTactics && handshakeResponse.TacticsPayload != nil &&
networkID == serverContext.tunnel.config.GetNetworkID() {
var payload *tactics.Payload
err := json.Unmarshal(handshakeResponse.TacticsPayload, &payload)
if err != nil {
return errors.Trace(err)
}
// handshakeResponse.TacticsPayload may be "null", and payload
// will successfully unmarshal as nil. As a result, the previous
// handshakeResponse.TacticsPayload != nil test is insufficient.
if payload != nil {
tacticsRecord, err := tactics.HandleTacticsPayload(
GetTacticsStorer(serverContext.tunnel.config),
networkID,
payload)
if err != nil {
return errors.Trace(err)
}
if tacticsRecord != nil &&
prng.FlipWeightedCoin(tacticsRecord.Tactics.Probability) {
err := serverContext.tunnel.config.SetParameters(
tacticsRecord.Tag, true, tacticsRecord.Tactics.Parameters)
if err != nil {
NoticeInfo("apply handshake tactics failed: %s", err)
}
// The error will be due to invalid tactics values
// from the server. When SetParameters fails, all
// previous tactics values are left in place.
}
}
}
return nil
}
// DoConnectedRequest performs the "connected" API request. This request is
// used for statistics, including unique user counting; reporting the full
// tunnel establishment duration including the handshake request; and updated
// fragmentor metrics.
//
// Users are not assigned identifiers. Instead, daily unique users are
// calculated by having clients submit their last connected timestamp
// (truncated to an hour, as a privacy measure). As client clocks are
// unreliable, the server returns new last_connected values for the client to
// store and send next time it connects.
func (serverContext *ServerContext) DoConnectedRequest() error {
// Limitation: as currently implemented, the last_connected exchange isn't a
// distributed, atomic operation. When clients send the connected request,
// the server may receive the request, count a unique user based on the
// client's last_connected, and then the tunnel fails before the client
// receives the response, so the client will not update its last_connected
// value and submit the same one again, resulting in an inflated unique user
// count.
//
// The SetInFlightConnectedRequest mechanism mitigates one class of connected
// request interruption, a commanded shutdown in the middle of a connected
// request, by allowing some time for the request to complete before
// terminating the tunnel.
//
// TODO: consider extending the connected request protocol with additional
// "acknowledgment" messages so that the server does not commit its unique
// user count until after the client has acknowledged receipt and durable
// storage of the new last_connected value.
requestDone := make(chan struct{})
defer close(requestDone)
if !serverContext.tunnel.SetInFlightConnectedRequest(requestDone) {
return errors.TraceNew("tunnel is closing")
}
defer serverContext.tunnel.SetInFlightConnectedRequest(nil)
params := serverContext.getBaseAPIParameters(
baseParametersOnlyUpstreamFragmentorDialParameters)
lastConnected, err := getLastConnected()
if err != nil {
return errors.Trace(err)
}
params["last_connected"] = lastConnected
// serverContext.tunnel.establishDuration is nanoseconds; report milliseconds
params["establishment_duration"] =
fmt.Sprintf("%d", serverContext.tunnel.establishDuration/time.Millisecond)
var response []byte
if serverContext.psiphonHttpsClient == nil {
request, err := serverContext.makeSSHAPIRequestPayload(params)
if err != nil {
return errors.Trace(err)
}
response, err = serverContext.tunnel.SendAPIRequest(
protocol.PSIPHON_API_CONNECTED_REQUEST_NAME, request)
if err != nil {
return errors.Trace(err)
}
} else {
// Legacy web service API request
response, err = serverContext.doGetRequest(
makeRequestUrl(serverContext.tunnel, "", "connected", params))
if err != nil {
return errors.Trace(err)
}
}
var connectedResponse protocol.ConnectedResponse
err = json.Unmarshal(response, &connectedResponse)
if err != nil {
return errors.Trace(err)
}
err = SetKeyValue(
datastoreLastConnectedKey, connectedResponse.ConnectedTimestamp)
if err != nil {
return errors.Trace(err)
}
return nil
}
func getLastConnected() (string, error) {
lastConnected, err := GetKeyValue(datastoreLastConnectedKey)
if err != nil {
return "", errors.Trace(err)
}
if lastConnected == "" {
lastConnected = "None"
}
return lastConnected, nil
}
// StatsRegexps gets the Regexps used for the statistics for this tunnel.
func (serverContext *ServerContext) StatsRegexps() *transferstats.Regexps {
return serverContext.statsRegexps
}
// DoStatusRequest makes a "status" API request to the server, sending session stats.
func (serverContext *ServerContext) DoStatusRequest(tunnel *Tunnel) error {
params := serverContext.getBaseAPIParameters(baseParametersNoDialParameters)
// Note: ensure putBackStatusRequestPayload is called, to replace
// payload for future attempt, in all failure cases.
statusPayload, statusPayloadInfo, err := makeStatusRequestPayload(
serverContext.tunnel.config,
tunnel.dialParams.ServerEntry.IpAddress)
if err != nil {
return errors.Trace(err)
}
// Skip the request when there's no payload to send.
if len(statusPayload) == 0 {
return nil
}
var response []byte
if serverContext.psiphonHttpsClient == nil {
rawMessage := json.RawMessage(statusPayload)
params["statusData"] = &rawMessage
var request []byte
request, err = serverContext.makeSSHAPIRequestPayload(params)
if err == nil {
response, err = serverContext.tunnel.SendAPIRequest(
protocol.PSIPHON_API_STATUS_REQUEST_NAME, request)
}
} else {
// Legacy web service API request
response, err = serverContext.doPostRequest(
makeRequestUrl(serverContext.tunnel, "", "status", params),
"application/json",
bytes.NewReader(statusPayload))
}
if err != nil {
// Resend the transfer stats and tunnel stats later
// Note: potential duplicate reports if the server received and processed
// the request but the client failed to receive the response.
putBackStatusRequestPayload(statusPayloadInfo)
return errors.Trace(err)
}
confirmStatusRequestPayload(statusPayloadInfo)
var statusResponse protocol.StatusResponse
err = json.Unmarshal(response, &statusResponse)
if err != nil {
return errors.Trace(err)
}
for _, serverEntryTag := range statusResponse.InvalidServerEntryTags {
PruneServerEntry(serverContext.tunnel.config, serverEntryTag)
}
return nil
}
// statusRequestPayloadInfo is a temporary structure for data used to
// either "clear" or "put back" status request payload data depending
// on whether or not the request succeeded.
type statusRequestPayloadInfo struct {
serverId string
transferStats *transferstats.AccumulatedStats
persistentStats map[string][][]byte
}
func makeStatusRequestPayload(
config *Config,
serverId string) ([]byte, *statusRequestPayloadInfo, error) {
transferStats := transferstats.TakeOutStatsForServer(serverId)
hostBytes := transferStats.GetStatsForStatusRequest()
persistentStats, err := TakeOutUnreportedPersistentStats(config)
if err != nil {
NoticeWarning(
"TakeOutUnreportedPersistentStats failed: %s", errors.Trace(err))
persistentStats = nil
// Proceed with transferStats only
}
if len(hostBytes) == 0 && len(persistentStats) == 0 {
// There is no payload to send.
return nil, nil, nil
}
payloadInfo := &statusRequestPayloadInfo{
serverId, transferStats, persistentStats}
payload := make(map[string]interface{})
payload["host_bytes"] = hostBytes
// We're not recording these fields, but legacy servers require them.
payload["bytes_transferred"] = 0
payload["page_views"] = make([]string, 0)
payload["https_requests"] = make([]string, 0)
persistentStatPayloadNames := make(map[string]string)
persistentStatPayloadNames[datastorePersistentStatTypeRemoteServerList] = "remote_server_list_stats"
persistentStatPayloadNames[datastorePersistentStatTypeFailedTunnel] = "failed_tunnel_stats"
for statType, stats := range persistentStats {
// Persistent stats records are already in JSON format
jsonStats := make([]json.RawMessage, len(stats))
for i, stat := range stats {
jsonStats[i] = json.RawMessage(stat)
}
payload[persistentStatPayloadNames[statType]] = jsonStats
}
jsonPayload, err := json.Marshal(payload)
if err != nil {
// Send the transfer stats and tunnel stats later
putBackStatusRequestPayload(payloadInfo)
return nil, nil, errors.Trace(err)
}
return jsonPayload, payloadInfo, nil
}
func putBackStatusRequestPayload(payloadInfo *statusRequestPayloadInfo) {
transferstats.PutBackStatsForServer(
payloadInfo.serverId, payloadInfo.transferStats)
err := PutBackUnreportedPersistentStats(payloadInfo.persistentStats)
if err != nil {
// These persistent stats records won't be resent until after a
// datastore re-initialization.
NoticeWarning(
"PutBackUnreportedPersistentStats failed: %s", errors.Trace(err))
}
}
func confirmStatusRequestPayload(payloadInfo *statusRequestPayloadInfo) {
err := ClearReportedPersistentStats(payloadInfo.persistentStats)
if err != nil {
// These persistent stats records may be resent.
NoticeWarning(
"ClearReportedPersistentStats failed: %s", errors.Trace(err))
}
}
// RecordRemoteServerListStat records a completed common or OSL remote server
// list resource download.
//
// The RSL download event could occur when the client is unable to immediately
// send a status request to a server, so these records are stored in the
// persistent datastore and reported via subsequent status requests sent to
// any Psiphon server.
//
// Note that some common event field values may change between the stat
// recording and reporting, including client geolocation and host_id.
//
// The bytes/duration fields reflect the size and download time for the _last
// chunk only_ in the case of a resumed download. The purpose of these fields
// is to calculate rough data transfer rates. Both bytes and duration are
// included in the log, to allow for filtering out of small transfers which
// may not produce accurate rate numbers.
//
// Multiple "status" requests may be in flight at once (due to multi-tunnel,
// asynchronous final status retry, and aggressive status requests for
// pre-registered tunnels), To avoid duplicate reporting, persistent stats
// records are "taken-out" by a status request and then "put back" in case the
// request fails.
//
// Duplicate reporting may also occur when a server receives and processes a
// status request but the client fails to receive the response.
func RecordRemoteServerListStat(
config *Config,
tunneled bool,
url string,
etag string,
bytes int64,
duration time.Duration,
authenticated bool) error {
if !config.GetParameters().Get().WeightedCoinFlip(
parameters.RecordRemoteServerListPersistentStatsProbability) {
return nil
}
params := make(common.APIParameters)
params["session_id"] = config.SessionID
params["propagation_channel_id"] = config.PropagationChannelId
params["sponsor_id"] = config.GetSponsorID()
params["client_version"] = config.ClientVersion
params["client_platform"] = config.ClientPlatform
params["client_build_rev"] = buildinfo.GetBuildInfo().BuildRev
if config.DeviceRegion != "" {
params["device_region"] = config.DeviceRegion
}
params["client_download_timestamp"] = common.TruncateTimestampToHour(common.GetCurrentTimestamp())
tunneledStr := "0"
if tunneled {
tunneledStr = "1"
}
params["tunneled"] = tunneledStr
params["url"] = url
params["etag"] = etag
params["bytes"] = fmt.Sprintf("%d", bytes)
// duration is nanoseconds; report milliseconds
params["duration"] = fmt.Sprintf("%d", duration/time.Millisecond)
authenticatedStr := "0"
if authenticated {
authenticatedStr = "1"
}
params["authenticated"] = authenticatedStr
remoteServerListStatJson, err := json.Marshal(params)
if err != nil {
return errors.Trace(err)
}
return StorePersistentStat(
config, datastorePersistentStatTypeRemoteServerList, remoteServerListStatJson)
}
// RecordFailedTunnelStat records metrics for a failed tunnel dial, including
// dial parameters and error condition (tunnelErr). No record is created when
// tunnelErr is nil.
//
// This uses the same reporting facility, with the same caveats, as
// RecordRemoteServerListStat.
func RecordFailedTunnelStat(
config *Config,
dialParams *DialParameters,
livenessTestMetrics *livenessTestMetrics,
bytesUp int64,
bytesDown int64,
tunnelErr error) error {
if !config.GetParameters().Get().WeightedCoinFlip(
parameters.RecordFailedTunnelPersistentStatsProbability) {
return nil
}
// Callers should not call RecordFailedTunnelStat with a nil tunnelErr, as
// this is not a useful stat and it results in a nil pointer dereference.
// This check catches potential bug cases. An example edge case, now
// fixed, is deferred error handlers, such as the ones in in
// dialTunnel/tunnel.Activate, which may be invoked in the case of a
// panic, which can occur before any error value is returned.
if tunnelErr == nil {
return errors.TraceNew("no error")
}
lastConnected, err := getLastConnected()
if err != nil {
return errors.Trace(err)
}
params := getBaseAPIParameters(baseParametersAll, config, dialParams)
delete(params, "server_secret")
params["server_entry_tag"] = dialParams.ServerEntry.Tag
params["last_connected"] = lastConnected
params["client_failed_timestamp"] = common.TruncateTimestampToHour(common.GetCurrentTimestamp())
if livenessTestMetrics != nil {
params["liveness_test_upstream_bytes"] = strconv.Itoa(livenessTestMetrics.UpstreamBytes)
params["liveness_test_sent_upstream_bytes"] = strconv.Itoa(livenessTestMetrics.SentUpstreamBytes)
params["liveness_test_downstream_bytes"] = strconv.Itoa(livenessTestMetrics.DownstreamBytes)
params["liveness_test_received_downstream_bytes"] = strconv.Itoa(livenessTestMetrics.ReceivedDownstreamBytes)
}
if bytesUp >= 0 {
params["bytes_up"] = fmt.Sprintf("%d", bytesUp)
}
if bytesDown >= 0 {
params["bytes_down"] = fmt.Sprintf("%d", bytesDown)
}
// Ensure direct server IPs are not exposed in logs. The "net" package, and
// possibly other 3rd party packages, will include destination addresses in
// I/O error messages.
tunnelError := common.RedactIPAddressesString(tunnelErr.Error())
params["tunnel_error"] = tunnelError
failedTunnelStatJson, err := json.Marshal(params)
if err != nil {
return errors.Trace(err)
}
return StorePersistentStat(
config, datastorePersistentStatTypeFailedTunnel, failedTunnelStatJson)
}
// doGetRequest makes a tunneled HTTPS request and returns the response body.
func (serverContext *ServerContext) doGetRequest(
requestUrl string) (responseBody []byte, err error) {
request, err := http.NewRequest("GET", requestUrl, nil)
if err != nil {
return nil, errors.Trace(err)
}
request.Header.Set("User-Agent", MakePsiphonUserAgent(serverContext.tunnel.config))
response, err := serverContext.psiphonHttpsClient.Do(request)
if err == nil && response.StatusCode != http.StatusOK {
response.Body.Close()
err = fmt.Errorf("HTTP GET request failed with response code: %d", response.StatusCode)
}
if err != nil {
// Trim this error since it may include long URLs
return nil, errors.Trace(TrimError(err))
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, errors.Trace(err)
}
return body, nil
}
// doPostRequest makes a tunneled HTTPS POST request.
func (serverContext *ServerContext) doPostRequest(
requestUrl string, bodyType string, body io.Reader) (responseBody []byte, err error) {
request, err := http.NewRequest("POST", requestUrl, body)
if err != nil {
return nil, errors.Trace(err)
}
request.Header.Set("User-Agent", MakePsiphonUserAgent(serverContext.tunnel.config))
request.Header.Set("Content-Type", bodyType)
response, err := serverContext.psiphonHttpsClient.Do(request)
if err == nil && response.StatusCode != http.StatusOK {
response.Body.Close()
err = fmt.Errorf("HTTP POST request failed with response code: %d", response.StatusCode)
}
if err != nil {
// Trim this error since it may include long URLs
return nil, errors.Trace(TrimError(err))
}
defer response.Body.Close()
responseBody, err = ioutil.ReadAll(response.Body)
if err != nil {
return nil, errors.Trace(err)
}
return responseBody, nil
}
// makeSSHAPIRequestPayload makes a JSON payload for an SSH API request.
func (serverContext *ServerContext) makeSSHAPIRequestPayload(
params common.APIParameters) ([]byte, error) {
jsonPayload, err := json.Marshal(params)
if err != nil {
return nil, errors.Trace(err)
}
return jsonPayload, nil
}
type baseParametersFilter int
const (
baseParametersAll baseParametersFilter = iota
baseParametersOnlyUpstreamFragmentorDialParameters
baseParametersNoDialParameters
)
func (serverContext *ServerContext) getBaseAPIParameters(
filter baseParametersFilter) common.APIParameters {
params := getBaseAPIParameters(
filter,
serverContext.tunnel.config,
serverContext.tunnel.dialParams)
// Add a random amount of padding to defend against API call traffic size
// fingerprints. The "pad_response" field instructs the server to pad its
// response accordingly.
p := serverContext.tunnel.config.GetParameters().Get()
minUpstreamPadding := p.Int(parameters.APIRequestUpstreamPaddingMinBytes)
maxUpstreamPadding := p.Int(parameters.APIRequestUpstreamPaddingMaxBytes)
minDownstreamPadding := p.Int(parameters.APIRequestDownstreamPaddingMinBytes)
maxDownstreamPadding := p.Int(parameters.APIRequestDownstreamPaddingMaxBytes)
if maxUpstreamPadding > 0 {
size := serverContext.paddingPRNG.Range(minUpstreamPadding, maxUpstreamPadding)
params["padding"] = strings.Repeat(" ", size)
}
if maxDownstreamPadding > 0 {
size := serverContext.paddingPRNG.Range(minDownstreamPadding, maxDownstreamPadding)
params["pad_response"] = strconv.Itoa(size)
}
return params
}
// getBaseAPIParameters returns all the common API parameters that are
// included with each Psiphon API request. These common parameters are used
// for metrics.
func getBaseAPIParameters(
filter baseParametersFilter,
config *Config,
dialParams *DialParameters) common.APIParameters {
params := make(common.APIParameters)
params["session_id"] = config.SessionID
params["client_session_id"] = config.SessionID
params["server_secret"] = dialParams.ServerEntry.WebServerSecret
params["propagation_channel_id"] = config.PropagationChannelId
params["sponsor_id"] = config.GetSponsorID()
params["client_version"] = config.ClientVersion
params["client_platform"] = config.ClientPlatform
params["client_features"] = config.clientFeatures
params["client_build_rev"] = buildinfo.GetBuildInfo().BuildRev
// Blank parameters must be omitted.
if config.DeviceRegion != "" {
params["device_region"] = config.DeviceRegion
}
if filter == baseParametersAll {
params["relay_protocol"] = dialParams.TunnelProtocol
params["network_type"] = dialParams.GetNetworkType()
if dialParams.BPFProgramName != "" {
params["client_bpf"] = dialParams.BPFProgramName
}
if dialParams.SelectedSSHClientVersion {
params["ssh_client_version"] = dialParams.SSHClientVersion
}
if dialParams.UpstreamProxyType != "" {
params["upstream_proxy_type"] = dialParams.UpstreamProxyType
}
if dialParams.UpstreamProxyCustomHeaderNames != nil {
params["upstream_proxy_custom_header_names"] = dialParams.UpstreamProxyCustomHeaderNames
}
if dialParams.FrontingProviderID != "" {
params["fronting_provider_id"] = dialParams.FrontingProviderID
}
if dialParams.MeekDialAddress != "" {
params["meek_dial_address"] = dialParams.MeekDialAddress
}
if protocol.TunnelProtocolUsesFrontedMeek(dialParams.TunnelProtocol) {
meekResolvedIPAddress := dialParams.MeekResolvedIPAddress.Load().(string)
if meekResolvedIPAddress != "" {
params["meek_resolved_ip_address"] = meekResolvedIPAddress
}
}
if dialParams.MeekSNIServerName != "" {
params["meek_sni_server_name"] = dialParams.MeekSNIServerName
}
if dialParams.MeekHostHeader != "" {
params["meek_host_header"] = dialParams.MeekHostHeader
}
// MeekTransformedHostName is meaningful when meek is used, which is when
// MeekDialAddress != ""
if dialParams.MeekDialAddress != "" {
transformedHostName := "0"
if dialParams.MeekTransformedHostName {
transformedHostName = "1"
}
params["meek_transformed_host_name"] = transformedHostName
}
if dialParams.SelectedUserAgent {
params["user_agent"] = dialParams.UserAgent
}
if dialParams.SelectedTLSProfile {
params["tls_profile"] = dialParams.TLSProfile
params["tls_version"] = dialParams.GetTLSVersionForMetrics()
}
if dialParams.ServerEntry.Region != "" {
params["server_entry_region"] = dialParams.ServerEntry.Region
}
if dialParams.ServerEntry.LocalSource != "" {
params["server_entry_source"] = dialParams.ServerEntry.LocalSource
}