forked from Psiphon-Labs/psiphon-tunnel-core
/
dialParameters.go
1357 lines (1111 loc) · 47.2 KB
/
dialParameters.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) 2018, 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"
"crypto/md5"
"encoding/binary"
"net"
"net/http"
"strconv"
"strings"
"sync/atomic"
"time"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"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/resolver"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/values"
utls "github.com/Psiphon-Labs/utls"
regen "github.com/zach-klippenstein/goregen"
"golang.org/x/net/bpf"
)
// DialParameters represents a selected protocol and all the related selected
// protocol attributes, many chosen at random, for a tunnel dial attempt.
//
// DialParameters is used:
// - to configure dialers
// - as a persistent record to store successful dial parameters for replay
// - to report dial stats in notices and Psiphon API calls.
//
// MeekResolvedIPAddress is set asynchronously, as it is not known until the
// dial process has begun. The atomic.Value will contain a string, initialized
// to "", and set to the resolved IP address once that part of the dial
// process has completed.
//
// DialParameters is not safe for concurrent use.
type DialParameters struct {
ServerEntry *protocol.ServerEntry `json:"-"`
NetworkID string `json:"-"`
IsReplay bool `json:"-"`
CandidateNumber int `json:"-"`
EstablishedTunnelsCount int `json:"-"`
IsExchanged bool
LastUsedTimestamp time.Time
LastUsedConfigStateHash []byte
NetworkLatencyMultiplier float64
TunnelProtocol string
DirectDialAddress string
DialPortNumber string
UpstreamProxyType string `json:"-"`
UpstreamProxyCustomHeaderNames []string `json:"-"`
BPFProgramName string
BPFProgramInstructions []bpf.RawInstruction
SelectedSSHClientVersion bool
SSHClientVersion string
SSHKEXSeed *prng.Seed
ObfuscatorPaddingSeed *prng.Seed
FragmentorSeed *prng.Seed
FrontingProviderID string
MeekFrontingDialAddress string
MeekFrontingHost string
MeekDialAddress string
MeekTransformedHostName bool
MeekSNIServerName string
MeekVerifyServerName string
MeekVerifyPins []string
MeekHostHeader string
MeekObfuscatorPaddingSeed *prng.Seed
MeekTLSPaddingSize int
MeekResolvedIPAddress atomic.Value `json:"-"`
SelectedUserAgent bool
UserAgent string
SelectedTLSProfile bool
TLSProfile string
NoDefaultTLSSessionID bool
TLSVersion string
RandomizedTLSProfileSeed *prng.Seed
QUICVersion string
QUICDialSNIAddress string
QUICClientHelloSeed *prng.Seed
ObfuscatedQUICPaddingSeed *prng.Seed
QUICDisablePathMTUDiscovery bool
ConjureCachedRegistrationTTL time.Duration
ConjureAPIRegistration bool
ConjureAPIRegistrarBidirectionalURL string
ConjureAPIRegistrarDelay time.Duration
ConjureDecoyRegistration bool
ConjureDecoyRegistrarDelay time.Duration
ConjureDecoyRegistrarWidth int
ConjureTransport string
LivenessTestSeed *prng.Seed
APIRequestPaddingSeed *prng.Seed
HoldOffTunnelDuration time.Duration
DialConnMetrics common.MetricsSource `json:"-"`
DialConnNoticeMetrics common.NoticeMetricsSource `json:"-"`
ObfuscatedSSHConnMetrics common.MetricsSource `json:"-"`
DialDuration time.Duration `json:"-"`
resolver *resolver.Resolver `json:"-"`
ResolveParameters *resolver.ResolveParameters
dialConfig *DialConfig `json:"-"`
meekConfig *MeekConfig `json:"-"`
}
// MakeDialParameters creates a new DialParameters for the candidate server
// entry, including selecting a protocol and all the various protocol
// attributes. The input selectProtocol is used to comply with any active
// protocol selection constraints.
//
// When stored dial parameters are available and may be used,
// MakeDialParameters may replay previous dial parameters in an effort to
// leverage "known working" values instead of always chosing at random from a
// large space.
//
// MakeDialParameters will return nil/nil in cases where the candidate server
// entry should be skipped.
//
// To support replay, the caller must call DialParameters.Succeeded when a
// successful tunnel is established with the returned DialParameters; and must
// call DialParameters.Failed when a tunnel dial or activation fails, except
// when establishment is cancelled.
func MakeDialParameters(
config *Config,
upstreamProxyErrorCallback func(error),
canReplay func(serverEntry *protocol.ServerEntry, replayProtocol string) bool,
selectProtocol func(serverEntry *protocol.ServerEntry) (string, bool),
serverEntry *protocol.ServerEntry,
isTactics bool,
candidateNumber int,
establishedTunnelsCount int) (*DialParameters, error) {
networkID := config.GetNetworkID()
p := config.GetParameters().Get()
ttl := p.Duration(parameters.ReplayDialParametersTTL)
replayBPF := p.Bool(parameters.ReplayBPF)
replaySSH := p.Bool(parameters.ReplaySSH)
replayObfuscatorPadding := p.Bool(parameters.ReplayObfuscatorPadding)
replayFragmentor := p.Bool(parameters.ReplayFragmentor)
replayTLSProfile := p.Bool(parameters.ReplayTLSProfile)
replayRandomizedTLSProfile := p.Bool(parameters.ReplayRandomizedTLSProfile)
replayFronting := p.Bool(parameters.ReplayFronting)
replayHostname := p.Bool(parameters.ReplayHostname)
replayQUICVersion := p.Bool(parameters.ReplayQUICVersion)
replayObfuscatedQUIC := p.Bool(parameters.ReplayObfuscatedQUIC)
replayConjureRegistration := p.Bool(parameters.ReplayConjureRegistration)
replayConjureTransport := p.Bool(parameters.ReplayConjureTransport)
replayLivenessTest := p.Bool(parameters.ReplayLivenessTest)
replayUserAgent := p.Bool(parameters.ReplayUserAgent)
replayAPIRequestPadding := p.Bool(parameters.ReplayAPIRequestPadding)
replayHoldOffTunnel := p.Bool(parameters.ReplayHoldOffTunnel)
replayResolveParameters := p.Bool(parameters.ReplayResolveParameters)
// Check for existing dial parameters for this server/network ID.
dialParams, err := GetDialParameters(
config, serverEntry.IpAddress, networkID)
if err != nil {
NoticeWarning("GetDialParameters failed: %s", err)
dialParams = nil
// Proceed, without existing dial parameters.
}
// Check if replay is permitted:
// - TTL must be > 0 and existing dial parameters must not have expired
// as indicated by LastUsedTimestamp + TTL.
// - Config/tactics/server entry values must be unchanged from when
// previous dial parameters were established.
// - The protocol selection constraints must permit replay, as indicated
// by canReplay.
// - Must not be using an obsolete TLS profile that is no longer supported.
// - Must be using the latest Conjure API URL.
//
// When existing dial parameters don't meet these conditions, dialParams
// is reset to nil and new dial parameters will be generated.
var currentTimestamp time.Time
var configStateHash []byte
// When TTL is 0, replay is disabled; the timestamp remains 0 and the
// output DialParameters will not be stored by Success.
if ttl > 0 {
currentTimestamp = time.Now()
configStateHash = getConfigStateHash(config, p, serverEntry)
}
if dialParams != nil &&
(ttl <= 0 ||
dialParams.LastUsedTimestamp.Before(currentTimestamp.Add(-ttl)) ||
!bytes.Equal(dialParams.LastUsedConfigStateHash, configStateHash) ||
(dialParams.TLSProfile != "" &&
!common.Contains(protocol.SupportedTLSProfiles, dialParams.TLSProfile)) ||
(dialParams.QUICVersion != "" &&
!common.Contains(protocol.SupportedQUICVersions, dialParams.QUICVersion)) ||
// Legacy clients use ConjureAPIRegistrarURL with
// gotapdance.tapdance.APIRegistrar and new clients use
// ConjureAPIRegistrarBidirectionalURL with
// gotapdance.tapdance.APIRegistrarBidirectional. Updated clients
// may have replay dial parameters with the old
// ConjureAPIRegistrarURL field, which is now ignored. In this
// case, ConjureAPIRegistrarBidirectionalURL will be blank. Reset
// this replay.
(dialParams.ConjureAPIRegistration && dialParams.ConjureAPIRegistrarBidirectionalURL == "")) {
// In these cases, existing dial parameters are expired or no longer
// match the config state and so are cleared to avoid rechecking them.
err = DeleteDialParameters(serverEntry.IpAddress, networkID)
if err != nil {
NoticeWarning("DeleteDialParameters failed: %s", err)
}
dialParams = nil
}
if dialParams != nil {
if config.DisableReplay ||
!canReplay(serverEntry, dialParams.TunnelProtocol) {
// In these ephemeral cases, existing dial parameters may still be valid
// and used in future establishment phases, and so are retained.
dialParams = nil
}
}
// IsExchanged:
//
// Dial parameters received via client-to-client exchange are partially
// initialized. Only the exchange fields are retained, and all other dial
// parameters fields must be initialized. This is not considered or logged as
// a replay. The exchange case is identified by the IsExchanged flag.
//
// When previously stored, IsExchanged dial parameters will have set the same
// timestamp and state hash used for regular dial parameters and the same
// logic above should invalidate expired or invalid exchanged dial
// parameters.
//
// Limitation: metrics will indicate when an exchanged server entry is used
// (source "EXCHANGED") but will not indicate when exchanged dial parameters
// are used vs. a redial after discarding dial parameters.
isReplay := (dialParams != nil)
isExchanged := isReplay && dialParams.IsExchanged
if !isReplay {
dialParams = &DialParameters{}
}
// Point to the current resolver to be used in dials.
dialParams.resolver = config.GetResolver()
if dialParams.resolver == nil {
return nil, errors.TraceNew("missing resolver")
}
if isExchanged {
// Set isReplay to false to cause all non-exchanged values to be
// initialized; this also causes the exchange case to not log as replay.
isReplay = false
}
// Set IsExchanged such that full dial parameters are stored and replayed
// upon success.
dialParams.IsExchanged = false
dialParams.ServerEntry = serverEntry
dialParams.NetworkID = networkID
dialParams.IsReplay = isReplay
dialParams.CandidateNumber = candidateNumber
dialParams.EstablishedTunnelsCount = establishedTunnelsCount
// Even when replaying, LastUsedTimestamp is updated to extend the TTL of
// replayed dial parameters which will be updated in the datastore upon
// success.
dialParams.LastUsedTimestamp = currentTimestamp
dialParams.LastUsedConfigStateHash = configStateHash
// Initialize dial parameters.
//
// When not replaying, all required parameters are initialized. When
// replaying, existing parameters are retaing, subject to the replay-X
// tactics flags.
// Select a network latency multiplier for this dial. This allows clients to
// explore and discover timeout values appropriate for the current network.
// The selection applies per tunnel, to avoid delaying all establishment
// candidates due to excessive timeouts. The random selection is bounded by a
// min/max set in tactics and an exponential distribution is used so as to
// heavily favor values close to the min, which should be set to the
// singleton NetworkLatencyMultiplier tactics value.
//
// For NetworkLatencyMultiplierLambda close to 2.0, values near min are
// very approximately 10x more likely to be selected than values near
// max, while for NetworkLatencyMultiplierLambda close to 0.1, the
// distribution is close to uniform.
//
// Not all existing, persisted DialParameters will have a custom
// NetworkLatencyMultiplier value. Its zero value will cause the singleton
// NetworkLatencyMultiplier tactics value to be used instead, which is
// consistent with the pre-custom multiplier behavior in the older client
// version which persisted that DialParameters.
networkLatencyMultiplierMin := p.Float(parameters.NetworkLatencyMultiplierMin)
networkLatencyMultiplierMax := p.Float(parameters.NetworkLatencyMultiplierMax)
if !isReplay ||
// Was selected...
(dialParams.NetworkLatencyMultiplier != 0.0 &&
// But is now outside tactics range...
(dialParams.NetworkLatencyMultiplier < networkLatencyMultiplierMin ||
dialParams.NetworkLatencyMultiplier > networkLatencyMultiplierMax)) {
dialParams.NetworkLatencyMultiplier = prng.ExpFloat64Range(
networkLatencyMultiplierMin,
networkLatencyMultiplierMax,
p.Float(parameters.NetworkLatencyMultiplierLambda))
}
// After this point, any tactics parameters that apply the network latency
// multiplier will use this selected value.
p = config.GetParameters().GetCustom(dialParams.NetworkLatencyMultiplier)
if !isReplay && !isExchanged {
// TODO: should there be a pre-check of selectProtocol before incurring
// overhead of unmarshaling dial parameters? In may be that a server entry
// is fully incapable of satisfying the current protocol selection
// constraints.
selectedProtocol, ok := selectProtocol(serverEntry)
if !ok {
return nil, nil
}
dialParams.TunnelProtocol = selectedProtocol
}
// Skip this candidate when the clients tactics restrict usage of the
// fronting provider ID. See the corresponding server-side enforcement
// comments in server.TacticsListener.accept.
if protocol.TunnelProtocolUsesFrontedMeek(dialParams.TunnelProtocol) &&
common.Contains(
p.Strings(parameters.RestrictFrontingProviderIDs),
dialParams.ServerEntry.FrontingProviderID) {
if p.WeightedCoinFlip(
parameters.RestrictFrontingProviderIDsClientProbability) {
// When skipping, return nil/nil as no error should be logged.
// NoticeSkipServerEntry emits each skip reason, regardless
// of server entry, at most once per session.
NoticeSkipServerEntry(
"restricted fronting provider ID: %s",
dialParams.ServerEntry.FrontingProviderID)
return nil, nil
}
}
if config.UseUpstreamProxy() {
// When UpstreamProxy is configured, ServerEntry.GetSupportedProtocols, when
// called via selectProtocol, will filter out protocols such that will not
// select a protocol incompatible with UpstreamProxy. This additional check
// will catch cases where selectProtocol does not apply this filter.
if !protocol.TunnelProtocolSupportsUpstreamProxy(dialParams.TunnelProtocol) {
NoticeSkipServerEntry(
"protocol does not support upstream proxy: %s",
dialParams.TunnelProtocol)
return nil, nil
}
// Skip this candidate when the server entry is not to be used with an
// upstream proxy. By not exposing servers from sources that are
// relatively hard to enumerate, this mechanism mitigates the risk of
// a malicious upstream proxy enumerating Psiphon servers. Populate
// the allowed sources with fronted servers to provide greater
// blocking resistence for clients using upstream proxy clients that
// are subject to blocking.
source := dialParams.ServerEntry.LocalSource
if !protocol.AllowServerEntrySourceWithUpstreamProxy(source) &&
!p.Bool(parameters.UpstreamProxyAllowAllServerEntrySources) {
NoticeSkipServerEntry(
"server entry source disallowed with upstream proxy: %s",
source)
return nil, nil
}
}
if (!isReplay || !replayBPF) &&
ClientBPFEnabled() &&
protocol.TunnelProtocolUsesTCP(dialParams.TunnelProtocol) {
if p.WeightedCoinFlip(parameters.BPFClientTCPProbability) {
dialParams.BPFProgramName = ""
dialParams.BPFProgramInstructions = nil
ok, name, rawInstructions := p.BPFProgram(parameters.BPFClientTCPProgram)
if ok {
dialParams.BPFProgramName = name
dialParams.BPFProgramInstructions = rawInstructions
}
}
}
if !isReplay || !replaySSH {
dialParams.SelectedSSHClientVersion = true
dialParams.SSHClientVersion = values.GetSSHClientVersion()
dialParams.SSHKEXSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
if !isReplay || !replayObfuscatorPadding {
dialParams.ObfuscatorPaddingSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
if protocol.TunnelProtocolUsesMeek(dialParams.TunnelProtocol) {
dialParams.MeekObfuscatorPaddingSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
}
if !isReplay || !replayFragmentor {
dialParams.FragmentorSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
if (!isReplay || !replayConjureRegistration) &&
protocol.TunnelProtocolUsesConjure(dialParams.TunnelProtocol) {
dialParams.ConjureCachedRegistrationTTL = p.Duration(parameters.ConjureCachedRegistrationTTL)
apiURL := p.String(parameters.ConjureAPIRegistrarBidirectionalURL)
decoyWidth := p.Int(parameters.ConjureDecoyRegistrarWidth)
dialParams.ConjureAPIRegistration = apiURL != ""
dialParams.ConjureDecoyRegistration = decoyWidth != 0
// We select only one of API or decoy registration. When both are enabled,
// ConjureDecoyRegistrarProbability determines the probability of using
// decoy registration.
//
// In general, we disable retries in gotapdance and rely on Psiphon
// establishment to try/retry different registration schemes. This allows us
// to control the proportion of registration types attempted. And, in good
// network conditions, individual candidates are most likely to be cancelled
// before they exhaust their retry options.
if dialParams.ConjureAPIRegistration && dialParams.ConjureDecoyRegistration {
if p.WeightedCoinFlip(parameters.ConjureDecoyRegistrarProbability) {
dialParams.ConjureAPIRegistration = false
}
}
if dialParams.ConjureAPIRegistration {
// While Conjure API registration uses MeekConn and specifies common meek
// parameters, the meek address and SNI configuration is implemented in this
// code block and not in common code blocks below. The exception is TLS
// configuration.
//
// Accordingly, replayFronting/replayHostname have no effect on Conjure API
// registration replay.
dialParams.ConjureAPIRegistrarBidirectionalURL = apiURL
frontingSpecs := p.FrontingSpecs(parameters.ConjureAPIRegistrarFrontingSpecs)
dialParams.FrontingProviderID,
dialParams.MeekFrontingDialAddress,
dialParams.MeekSNIServerName,
dialParams.MeekVerifyServerName,
dialParams.MeekVerifyPins,
dialParams.MeekFrontingHost,
err = frontingSpecs.SelectParameters()
if err != nil {
return nil, errors.Trace(err)
}
dialParams.MeekDialAddress = net.JoinHostPort(dialParams.MeekFrontingDialAddress, "443")
dialParams.MeekHostHeader = dialParams.MeekFrontingHost
// For a FrontingSpec, an SNI value of "" indicates to disable/omit SNI, so
// never transform in that case.
if dialParams.MeekSNIServerName != "" {
if p.WeightedCoinFlip(parameters.TransformHostNameProbability) {
dialParams.MeekSNIServerName = selectHostName(dialParams.TunnelProtocol, p)
dialParams.MeekTransformedHostName = true
}
}
// The minimum delay value is determined by the Conjure station, which
// performs an asynchronous "liveness test" against the selected phantom
// IPs. The min/max range allows us to introduce some jitter so that we
// don't present a trivial inter-flow fingerprint: CDN connection, fixed
// delay, phantom dial.
minDelay := p.Duration(parameters.ConjureAPIRegistrarMinDelay)
maxDelay := p.Duration(parameters.ConjureAPIRegistrarMaxDelay)
dialParams.ConjureAPIRegistrarDelay = prng.Period(minDelay, maxDelay)
} else if dialParams.ConjureDecoyRegistration {
dialParams.ConjureDecoyRegistrarWidth = decoyWidth
minDelay := p.Duration(parameters.ConjureDecoyRegistrarMinDelay)
maxDelay := p.Duration(parameters.ConjureDecoyRegistrarMaxDelay)
dialParams.ConjureAPIRegistrarDelay = prng.Period(minDelay, maxDelay)
} else {
return nil, errors.TraceNew("no Conjure registrar configured")
}
}
if (!isReplay || !replayConjureTransport) &&
protocol.TunnelProtocolUsesConjure(dialParams.TunnelProtocol) {
dialParams.ConjureTransport = protocol.CONJURE_TRANSPORT_MIN_OSSH
if p.WeightedCoinFlip(
parameters.ConjureTransportObfs4Probability) {
dialParams.ConjureTransport = protocol.CONJURE_TRANSPORT_OBFS4_OSSH
}
}
usingTLS := protocol.TunnelProtocolUsesMeekHTTPS(dialParams.TunnelProtocol) ||
dialParams.ConjureAPIRegistration
if (!isReplay || !replayTLSProfile) && usingTLS {
dialParams.SelectedTLSProfile = true
requireTLS12SessionTickets := protocol.TunnelProtocolRequiresTLS12SessionTickets(
dialParams.TunnelProtocol)
isFronted := protocol.TunnelProtocolUsesFrontedMeek(dialParams.TunnelProtocol) ||
dialParams.ConjureAPIRegistration
dialParams.TLSProfile = SelectTLSProfile(
requireTLS12SessionTickets, isFronted, serverEntry.FrontingProviderID, p)
dialParams.NoDefaultTLSSessionID = p.WeightedCoinFlip(
parameters.NoDefaultTLSSessionIDProbability)
}
if (!isReplay || !replayRandomizedTLSProfile) && usingTLS &&
protocol.TLSProfileIsRandomized(dialParams.TLSProfile) {
dialParams.RandomizedTLSProfileSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
if (!isReplay || !replayTLSProfile) && usingTLS {
// Since "Randomized-v2"/CustomTLSProfiles may be TLS 1.2 or TLS 1.3,
// construct the ClientHello to determine if it's TLS 1.3. This test also
// covers non-randomized TLS 1.3 profiles. This check must come after
// dialParams.TLSProfile and dialParams.RandomizedTLSProfileSeed are set. No
// actual dial is made here.
utlsClientHelloID, utlsClientHelloSpec, err := getUTLSClientHelloID(
p, dialParams.TLSProfile)
if err != nil {
return nil, errors.Trace(err)
}
if protocol.TLSProfileIsRandomized(dialParams.TLSProfile) {
utlsClientHelloID.Seed = new(utls.PRNGSeed)
*utlsClientHelloID.Seed = [32]byte(*dialParams.RandomizedTLSProfileSeed)
}
dialParams.TLSVersion, err = getClientHelloVersion(
utlsClientHelloID, utlsClientHelloSpec)
if err != nil {
return nil, errors.Trace(err)
}
}
if (!isReplay || !replayFronting) &&
protocol.TunnelProtocolUsesFrontedMeek(dialParams.TunnelProtocol) {
dialParams.FrontingProviderID = serverEntry.FrontingProviderID
dialParams.MeekFrontingDialAddress, dialParams.MeekFrontingHost, err =
selectFrontingParameters(serverEntry)
if err != nil {
return nil, errors.Trace(err)
}
}
if !isReplay || !replayHostname {
if protocol.TunnelProtocolUsesMeekHTTPS(dialParams.TunnelProtocol) ||
protocol.TunnelProtocolUsesFrontedMeekQUIC(dialParams.TunnelProtocol) {
dialParams.MeekSNIServerName = ""
if p.WeightedCoinFlip(parameters.TransformHostNameProbability) {
dialParams.MeekSNIServerName = selectHostName(dialParams.TunnelProtocol, p)
dialParams.MeekTransformedHostName = true
}
} else if protocol.TunnelProtocolUsesMeekHTTP(dialParams.TunnelProtocol) {
dialParams.MeekHostHeader = ""
hostname := serverEntry.IpAddress
if p.WeightedCoinFlip(parameters.TransformHostNameProbability) {
hostname = selectHostName(dialParams.TunnelProtocol, p)
dialParams.MeekTransformedHostName = true
}
if serverEntry.MeekServerPort == 80 {
dialParams.MeekHostHeader = hostname
} else {
dialParams.MeekHostHeader = net.JoinHostPort(
hostname, strconv.Itoa(serverEntry.MeekServerPort))
}
} else if protocol.TunnelProtocolUsesQUIC(dialParams.TunnelProtocol) {
dialParams.QUICDialSNIAddress = net.JoinHostPort(
selectHostName(dialParams.TunnelProtocol, p),
strconv.Itoa(serverEntry.SshObfuscatedQUICPort))
}
}
if (!isReplay || !replayQUICVersion) &&
protocol.TunnelProtocolUsesQUIC(dialParams.TunnelProtocol) {
isFronted := protocol.TunnelProtocolUsesFrontedMeekQUIC(dialParams.TunnelProtocol)
dialParams.QUICVersion = selectQUICVersion(isFronted, serverEntry, p)
if protocol.QUICVersionHasRandomizedClientHello(dialParams.QUICVersion) {
dialParams.QUICClientHelloSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
dialParams.QUICDisablePathMTUDiscovery =
protocol.QUICVersionUsesPathMTUDiscovery(dialParams.QUICVersion) &&
p.WeightedCoinFlip(parameters.QUICDisableClientPathMTUDiscoveryProbability)
}
if (!isReplay || !replayObfuscatedQUIC) &&
protocol.QUICVersionIsObfuscated(dialParams.QUICVersion) {
dialParams.ObfuscatedQUICPaddingSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
if !isReplay || !replayLivenessTest {
// TODO: initialize only when LivenessTestMaxUp/DownstreamBytes > 0?
dialParams.LivenessTestSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
if !isReplay || !replayAPIRequestPadding {
dialParams.APIRequestPaddingSeed, err = prng.NewSeed()
if err != nil {
return nil, errors.Trace(err)
}
}
// Initialize dialParams.ResolveParameters for dials that will resolve
// domain names, which currently includes fronted meek and Conjure API
// registration, where the dial address is not an IP address.
//
// dialParams.ResolveParameters must be nil when the dial address is an IP
// address to ensure that no DNS dial parameters are reported in metrics
// or diagnostics when when no domain is resolved.
useResolver := (protocol.TunnelProtocolUsesFrontedMeek(dialParams.TunnelProtocol) ||
dialParams.ConjureAPIRegistration) &&
net.ParseIP(dialParams.MeekFrontingDialAddress) == nil
if (!isReplay || !replayResolveParameters) && useResolver {
dialParams.ResolveParameters, err = dialParams.resolver.MakeResolveParameters(
p, dialParams.FrontingProviderID)
if err != nil {
return nil, errors.Trace(err)
}
}
if !isReplay || !replayHoldOffTunnel {
if common.Contains(
p.TunnelProtocols(parameters.HoldOffTunnelProtocols), dialParams.TunnelProtocol) ||
(protocol.TunnelProtocolUsesFrontedMeek(dialParams.TunnelProtocol) &&
common.Contains(
p.Strings(parameters.HoldOffTunnelFrontingProviderIDs),
dialParams.FrontingProviderID)) {
if p.WeightedCoinFlip(parameters.HoldOffTunnelProbability) {
dialParams.HoldOffTunnelDuration = prng.Period(
p.Duration(parameters.HoldOffTunnelMinDuration),
p.Duration(parameters.HoldOffTunnelMaxDuration))
}
}
}
// Set dial address fields. This portion of configuration is
// deterministic, given the parameters established or replayed so far.
dialPortNumber, err := serverEntry.GetDialPortNumber(dialParams.TunnelProtocol)
if err != nil {
return nil, errors.Trace(err)
}
dialParams.DialPortNumber = strconv.Itoa(dialPortNumber)
switch dialParams.TunnelProtocol {
case protocol.TUNNEL_PROTOCOL_SSH,
protocol.TUNNEL_PROTOCOL_OBFUSCATED_SSH,
protocol.TUNNEL_PROTOCOL_TAPDANCE_OBFUSCATED_SSH,
protocol.TUNNEL_PROTOCOL_CONJURE_OBFUSCATED_SSH,
protocol.TUNNEL_PROTOCOL_QUIC_OBFUSCATED_SSH:
dialParams.DirectDialAddress = net.JoinHostPort(serverEntry.IpAddress, dialParams.DialPortNumber)
case protocol.TUNNEL_PROTOCOL_FRONTED_MEEK,
protocol.TUNNEL_PROTOCOL_FRONTED_MEEK_QUIC_OBFUSCATED_SSH:
dialParams.MeekDialAddress = net.JoinHostPort(dialParams.MeekFrontingDialAddress, dialParams.DialPortNumber)
dialParams.MeekHostHeader = dialParams.MeekFrontingHost
if serverEntry.MeekFrontingDisableSNI {
dialParams.MeekSNIServerName = ""
// When SNI is omitted, the transformed host name is not used.
dialParams.MeekTransformedHostName = false
} else if !dialParams.MeekTransformedHostName {
dialParams.MeekSNIServerName = dialParams.MeekFrontingDialAddress
}
case protocol.TUNNEL_PROTOCOL_FRONTED_MEEK_HTTP:
dialParams.MeekDialAddress = net.JoinHostPort(dialParams.MeekFrontingDialAddress, dialParams.DialPortNumber)
dialParams.MeekHostHeader = dialParams.MeekFrontingHost
// For FRONTED HTTP, the Host header cannot be transformed.
dialParams.MeekTransformedHostName = false
case protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK:
dialParams.MeekDialAddress = net.JoinHostPort(serverEntry.IpAddress, dialParams.DialPortNumber)
if !dialParams.MeekTransformedHostName {
if dialPortNumber == 80 {
dialParams.MeekHostHeader = serverEntry.IpAddress
} else {
dialParams.MeekHostHeader = dialParams.MeekDialAddress
}
}
case protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_HTTPS,
protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET:
dialParams.MeekDialAddress = net.JoinHostPort(serverEntry.IpAddress, dialParams.DialPortNumber)
if !dialParams.MeekTransformedHostName {
// Note: IP address in SNI field will be omitted.
dialParams.MeekSNIServerName = serverEntry.IpAddress
}
if dialPortNumber == 443 {
dialParams.MeekHostHeader = serverEntry.IpAddress
} else {
dialParams.MeekHostHeader = dialParams.MeekDialAddress
}
default:
return nil, errors.Tracef(
"unknown tunnel protocol: %s", dialParams.TunnelProtocol)
}
if protocol.TunnelProtocolUsesMeek(dialParams.TunnelProtocol) {
host, _, _ := net.SplitHostPort(dialParams.MeekDialAddress)
if p.Bool(parameters.MeekDialDomainsOnly) {
if net.ParseIP(host) != nil {
// No error, as this is a "not supported" case.
return nil, nil
}
}
// The underlying TLS implementation will automatically omit SNI for
// IP address server name values; we have this explicit check here so
// we record the correct value for stats.
if net.ParseIP(dialParams.MeekSNIServerName) != nil {
dialParams.MeekSNIServerName = ""
}
}
// Initialize/replay User-Agent header for HTTP upstream proxy and meek protocols.
if config.UseUpstreamProxy() {
// Note: UpstreamProxyURL will be validated in the dial
proxyURL, err := common.SafeParseURL(config.UpstreamProxyURL)
if err == nil {
dialParams.UpstreamProxyType = proxyURL.Scheme
}
}
dialCustomHeaders := makeDialCustomHeaders(config, p)
if protocol.TunnelProtocolUsesMeek(dialParams.TunnelProtocol) ||
dialParams.UpstreamProxyType == "http" ||
dialParams.ConjureAPIRegistration {
if !isReplay || !replayUserAgent {
dialParams.SelectedUserAgent, dialParams.UserAgent = selectUserAgentIfUnset(p, dialCustomHeaders)
}
if dialParams.SelectedUserAgent {
dialCustomHeaders.Set("User-Agent", dialParams.UserAgent)
}
}
// UpstreamProxyCustomHeaderNames is a reported metric. Just the names and
// not the values are reported, in case the values are identifying.
if len(config.CustomHeaders) > 0 {
dialParams.UpstreamProxyCustomHeaderNames = make([]string, 0)
for name := range dialCustomHeaders {
if name == "User-Agent" && dialParams.SelectedUserAgent {
continue
}
dialParams.UpstreamProxyCustomHeaderNames = append(dialParams.UpstreamProxyCustomHeaderNames, name)
}
}
// Initialize Dial/MeekConfigs to be passed to the corresponding dialers.
// Custom ResolveParameters are set only when useResolver is true, but
// DialConfig.ResolveIP is required and wired up unconditionally. Any
// misconfigured or miscoded domain dial cases will use default
// ResolveParameters.
//
// ResolveIP will use the networkID obtained above, as it will be used
// almost immediately, instead of incurring the overhead of calling
// GetNetworkID again.
resolveIP := func(ctx context.Context, hostname string) ([]net.IP, error) {
IPs, err := dialParams.resolver.ResolveIP(
ctx,
networkID,
dialParams.ResolveParameters,
hostname)
if err != nil {
return nil, errors.Trace(err)
}
return IPs, nil
}
dialParams.dialConfig = &DialConfig{
DiagnosticID: serverEntry.GetDiagnosticID(),
UpstreamProxyURL: config.UpstreamProxyURL,
CustomHeaders: dialCustomHeaders,
BPFProgramInstructions: dialParams.BPFProgramInstructions,
DeviceBinder: config.deviceBinder,
IPv6Synthesizer: config.IPv6Synthesizer,
ResolveIP: resolveIP,
TrustedCACertificatesFilename: config.TrustedCACertificatesFilename,
FragmentorConfig: fragmentor.NewUpstreamConfig(p, dialParams.TunnelProtocol, dialParams.FragmentorSeed),
UpstreamProxyErrorCallback: upstreamProxyErrorCallback,
}
// Unconditionally initialize MeekResolvedIPAddress, so a valid string can
// always be read.
dialParams.MeekResolvedIPAddress.Store("")
if protocol.TunnelProtocolUsesMeek(dialParams.TunnelProtocol) ||
dialParams.ConjureAPIRegistration {
dialParams.meekConfig = &MeekConfig{
DiagnosticID: serverEntry.GetDiagnosticID(),
Parameters: config.GetParameters(),
DialAddress: dialParams.MeekDialAddress,
UseQUIC: protocol.TunnelProtocolUsesFrontedMeekQUIC(dialParams.TunnelProtocol),
QUICVersion: dialParams.QUICVersion,
QUICClientHelloSeed: dialParams.QUICClientHelloSeed,
QUICDisablePathMTUDiscovery: dialParams.QUICDisablePathMTUDiscovery,
UseHTTPS: usingTLS,
TLSProfile: dialParams.TLSProfile,
LegacyPassthrough: serverEntry.ProtocolUsesLegacyPassthrough(dialParams.TunnelProtocol),
NoDefaultTLSSessionID: dialParams.NoDefaultTLSSessionID,
RandomizedTLSProfileSeed: dialParams.RandomizedTLSProfileSeed,
UseObfuscatedSessionTickets: dialParams.TunnelProtocol == protocol.TUNNEL_PROTOCOL_UNFRONTED_MEEK_SESSION_TICKET,
SNIServerName: dialParams.MeekSNIServerName,
VerifyServerName: dialParams.MeekVerifyServerName,
VerifyPins: dialParams.MeekVerifyPins,
HostHeader: dialParams.MeekHostHeader,
TransformedHostName: dialParams.MeekTransformedHostName,
ClientTunnelProtocol: dialParams.TunnelProtocol,
MeekCookieEncryptionPublicKey: serverEntry.MeekCookieEncryptionPublicKey,
MeekObfuscatedKey: serverEntry.MeekObfuscatedKey,
MeekObfuscatorPaddingSeed: dialParams.MeekObfuscatorPaddingSeed,
NetworkLatencyMultiplier: dialParams.NetworkLatencyMultiplier,
}
// Use an asynchronous callback to record the resolved IP address when
// dialing a domain name. Note that DialMeek doesn't immediately
// establish any HTTP connections, so the resolved IP address won't be
// reported in all cases until after SSH traffic is relayed or a
// endpoint request is made over the meek connection.
dialParams.dialConfig.ResolvedIPCallback = func(IPAddress string) {
dialParams.MeekResolvedIPAddress.Store(IPAddress)
}
if isTactics {
dialParams.meekConfig.Mode = MeekModeObfuscatedRoundTrip
} else if dialParams.ConjureAPIRegistration {
dialParams.meekConfig.Mode = MeekModePlaintextRoundTrip
} else {
dialParams.meekConfig.Mode = MeekModeRelay
}
}
return dialParams, nil
}
func (dialParams *DialParameters) GetDialConfig() *DialConfig {
return dialParams.dialConfig
}
func (dialParams *DialParameters) GetMeekConfig() *MeekConfig {
return dialParams.meekConfig
}
// GetNetworkType returns a network type name, suitable for metrics, which is
// derived from the network ID.
func (dialParams *DialParameters) GetNetworkType() string {
// Unlike the logic in loggingNetworkIDGetter.GetNetworkID, we don't take the
// arbitrary text before the first "-" since some platforms without network
// detection support stub in random values to enable tactics. Instead we
// check for and use the common network type prefixes currently used in
// NetworkIDGetter implementations.