@@ -19,6 +19,15 @@ import (
19
19
"testing"
20
20
)
21
21
22
+ // These test cases use a websocket client (Dialer)/proxy/websocket server (Upgrader)
23
+ // to validate the cases where a proxy is an intermediary between a websocket client
24
+ // and server. The test cases usually 1) create a websocket server which echoes any
25
+ // data received back to the client, 2) a basic duplex streaming proxy, and 3) a
26
+ // websocket client which sends random data to the server through the proxy,
27
+ // validating any subsequent data received is the same as the data sent. The various
28
+ // permutations include the proxy and backend schemes (HTTP or HTTPS), as well as
29
+ // the custom dial functions (e.g NetDialContext, NetDial) set on the Dialer.
30
+
22
31
const (
23
32
subprotocolv1 = "subprotocol-version-1"
24
33
subprotocolv2 = "subprotocol-version-2"
@@ -288,13 +297,13 @@ func TestHTTPProxyWithNetDialContext(t *testing.T) {
288
297
// TLS Config: set (used for both proxy and backend TLS)
289
298
func TestHTTPSProxyAndBackend (t * testing.T ) {
290
299
// Start the websocket server running TLS.
291
- cert , err := tls .X509KeyPair (localhostCert , localhostKey )
300
+ websocketCert , err := tls .X509KeyPair (websocketServerCert , websocketServerKey )
292
301
if err != nil {
293
302
t .Fatalf ("error creating TLS key pair: %v" , err )
294
303
}
295
304
websocketServer := httptest .NewUnstartedServer (websocketEchoHandler )
296
305
websocketServer .TLS = & tls.Config {
297
- Certificates : []tls.Certificate {cert },
306
+ Certificates : []tls.Certificate {websocketCert },
298
307
}
299
308
websocketServer .StartTLS ()
300
309
defer websocketServer .Close ()
@@ -303,13 +312,17 @@ func TestHTTPSProxyAndBackend(t *testing.T) {
303
312
t .Fatalf ("error parsing websocket server URL: %v" , err )
304
313
}
305
314
// Start the proxy server running TLS.
315
+ proxyCert , err := tls .X509KeyPair (proxyServerCert , proxyServerKey )
316
+ if err != nil {
317
+ t .Fatalf ("error creating TLS key pair: %v" , err )
318
+ }
306
319
var proxyCalled atomic.Int64
307
320
proxyServer := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
308
321
proxyCalled .Add (1 )
309
322
proxyHandler .ServeHTTP (w , req )
310
323
}))
311
324
proxyServer .TLS = & tls.Config {
312
- Certificates : []tls.Certificate {cert },
325
+ Certificates : []tls.Certificate {proxyCert },
313
326
}
314
327
proxyServer .StartTLS ()
315
328
defer proxyServer .Close ()
@@ -320,7 +333,8 @@ func TestHTTPSProxyAndBackend(t *testing.T) {
320
333
// Dial the websocket server to create the websocket connection,
321
334
// setting the proxy URL, the TLS CA data, and the requested subprotocol.
322
335
certPool := x509 .NewCertPool ()
323
- certPool .AppendCertsFromPEM (localhostCert )
336
+ certPool .AppendCertsFromPEM (websocketServerCert )
337
+ certPool .AppendCertsFromPEM (proxyServerCert )
324
338
dialer := Dialer {
325
339
Proxy : http .ProxyURL (proxyServerURL ),
326
340
TLSClientConfig : & tls.Config {
@@ -362,7 +376,7 @@ func TestHTTPSProxyAndBackend(t *testing.T) {
362
376
// TLS Config: set (used for both proxy and backend TLS)
363
377
func TestHTTPSProxyUsingNetDial (t * testing.T ) {
364
378
// Start the websocket server running TLS.
365
- cert , err := tls .X509KeyPair (localhostCert , localhostKey )
379
+ cert , err := tls .X509KeyPair (websocketServerCert , websocketServerKey )
366
380
if err != nil {
367
381
t .Fatalf ("error creating TLS key pair: %v" , err )
368
382
}
@@ -377,13 +391,17 @@ func TestHTTPSProxyUsingNetDial(t *testing.T) {
377
391
t .Fatalf ("error parsing websocket server URL: %v" , err )
378
392
}
379
393
// Start the proxy server running TLS.
394
+ proxyCert , err := tls .X509KeyPair (proxyServerCert , proxyServerKey )
395
+ if err != nil {
396
+ t .Fatalf ("error creating TLS key pair: %v" , err )
397
+ }
380
398
var proxyCalled atomic.Int64
381
399
proxyServer := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
382
400
proxyCalled .Add (1 )
383
401
proxyHandler .ServeHTTP (w , req )
384
402
}))
385
403
proxyServer .TLS = & tls.Config {
386
- Certificates : []tls.Certificate {cert },
404
+ Certificates : []tls.Certificate {proxyCert },
387
405
}
388
406
proxyServer .StartTLS ()
389
407
defer proxyServer .Close ()
@@ -396,7 +414,8 @@ func TestHTTPSProxyUsingNetDial(t *testing.T) {
396
414
// Also, set the "NetDial" function to dial the proxy (with the
397
415
// TLSClientConfig for the TLS handshake).
398
416
certPool := x509 .NewCertPool ()
399
- certPool .AppendCertsFromPEM (localhostCert )
417
+ certPool .AppendCertsFromPEM (websocketServerCert )
418
+ certPool .AppendCertsFromPEM (proxyServerCert )
400
419
var netDialCalled atomic.Int64
401
420
dialer := Dialer {
402
421
NetDial : func (network , addr string ) (net.Conn , error ) {
@@ -446,13 +465,13 @@ func TestHTTPSProxyUsingNetDial(t *testing.T) {
446
465
// TLS Config: set (used for both proxy and backend TLS)
447
466
func TestHTTPSProxyUsingNetDialContext (t * testing.T ) {
448
467
// Start the websocket server running TLS.
449
- cert , err := tls .X509KeyPair (localhostCert , localhostKey )
468
+ websocketCert , err := tls .X509KeyPair (websocketServerCert , websocketServerKey )
450
469
if err != nil {
451
470
t .Fatalf ("error creating TLS key pair: %v" , err )
452
471
}
453
472
websocketServer := httptest .NewUnstartedServer (websocketEchoHandler )
454
473
websocketServer .TLS = & tls.Config {
455
- Certificates : []tls.Certificate {cert },
474
+ Certificates : []tls.Certificate {websocketCert },
456
475
}
457
476
websocketServer .StartTLS ()
458
477
defer websocketServer .Close ()
@@ -461,13 +480,17 @@ func TestHTTPSProxyUsingNetDialContext(t *testing.T) {
461
480
t .Fatalf ("error parsing websocket server URL: %v" , err )
462
481
}
463
482
// Start the proxy server running TLS.
483
+ proxyCert , err := tls .X509KeyPair (proxyServerCert , proxyServerKey )
484
+ if err != nil {
485
+ t .Fatalf ("error creating TLS key pair: %v" , err )
486
+ }
464
487
var proxyCalled atomic.Int64
465
488
proxyServer := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
466
489
proxyCalled .Add (1 )
467
490
proxyHandler .ServeHTTP (w , req )
468
491
}))
469
492
proxyServer .TLS = & tls.Config {
470
- Certificates : []tls.Certificate {cert },
493
+ Certificates : []tls.Certificate {proxyCert },
471
494
}
472
495
proxyServer .StartTLS ()
473
496
defer proxyServer .Close ()
@@ -480,7 +503,8 @@ func TestHTTPSProxyUsingNetDialContext(t *testing.T) {
480
503
// Also, set the "NetDialContext" function to dial the proxy (with the
481
504
// TLSClientConfig for the TLS handshake).
482
505
certPool := x509 .NewCertPool ()
483
- certPool .AppendCertsFromPEM (localhostCert )
506
+ certPool .AppendCertsFromPEM (websocketServerCert )
507
+ certPool .AppendCertsFromPEM (proxyServerCert )
484
508
var netDialCalled atomic.Int64
485
509
dialer := Dialer {
486
510
NetDialContext : func (ctx context.Context , network , addr string ) (net.Conn , error ) {
@@ -530,13 +554,13 @@ func TestHTTPSProxyUsingNetDialContext(t *testing.T) {
530
554
// TLS Config: set (used for backend TLS)
531
555
func TestHTTPSProxyUsingNetDialTLSContext (t * testing.T ) {
532
556
// Start the websocket server running TLS.
533
- cert , err := tls .X509KeyPair (localhostCert , localhostKey )
557
+ websocketCert , err := tls .X509KeyPair (websocketServerCert , websocketServerKey )
534
558
if err != nil {
535
559
t .Fatalf ("error creating TLS key pair: %v" , err )
536
560
}
537
561
websocketServer := httptest .NewUnstartedServer (websocketEchoHandler )
538
562
websocketServer .TLS = & tls.Config {
539
- Certificates : []tls.Certificate {cert },
563
+ Certificates : []tls.Certificate {websocketCert },
540
564
}
541
565
websocketServer .StartTLS ()
542
566
defer websocketServer .Close ()
@@ -545,13 +569,17 @@ func TestHTTPSProxyUsingNetDialTLSContext(t *testing.T) {
545
569
t .Fatalf ("error parsing websocket server URL: %v" , err )
546
570
}
547
571
// Start the proxy server running TLS.
572
+ proxyCert , err := tls .X509KeyPair (proxyServerCert , proxyServerKey )
573
+ if err != nil {
574
+ t .Fatalf ("error creating TLS key pair: %v" , err )
575
+ }
548
576
var proxyCalled atomic.Int64
549
577
proxyServer := httptest .NewUnstartedServer (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
550
578
proxyCalled .Add (1 )
551
579
proxyHandler .ServeHTTP (w , req )
552
580
}))
553
581
proxyServer .TLS = & tls.Config {
554
- Certificates : []tls.Certificate {cert },
582
+ Certificates : []tls.Certificate {proxyCert },
555
583
}
556
584
proxyServer .StartTLS ()
557
585
defer proxyServer .Close ()
@@ -564,7 +592,8 @@ func TestHTTPSProxyUsingNetDialTLSContext(t *testing.T) {
564
592
// performs the TLS handshake. NOTE: Subsequent TLS handshake to backend
565
593
// (over proxied connection) uses TLSClientConfig for handshake.
566
594
certPool := x509 .NewCertPool ()
567
- certPool .AppendCertsFromPEM (localhostCert )
595
+ certPool .AppendCertsFromPEM (websocketServerCert )
596
+ certPool .AppendCertsFromPEM (proxyServerCert )
568
597
tlsConfig := & tls.Config {RootCAs : certPool }
569
598
var netDialCalled atomic.Int64
570
599
dialer := Dialer {
@@ -623,7 +652,7 @@ func TestHTTPSProxyUsingNetDialTLSContextWithHTTPBackend(t *testing.T) {
623
652
t .Fatalf ("error parsing websocket server URL: %v" , err )
624
653
}
625
654
// Start the proxy server running TLS.
626
- cert , err := tls .X509KeyPair (localhostCert , localhostKey )
655
+ proxyCert , err := tls .X509KeyPair (proxyServerCert , proxyServerKey )
627
656
if err != nil {
628
657
t .Fatalf ("error creating TLS key pair: %v" , err )
629
658
}
@@ -633,7 +662,7 @@ func TestHTTPSProxyUsingNetDialTLSContextWithHTTPBackend(t *testing.T) {
633
662
proxyHandler .ServeHTTP (w , req )
634
663
}))
635
664
proxyServer .TLS = & tls.Config {
636
- Certificates : []tls.Certificate {cert },
665
+ Certificates : []tls.Certificate {proxyCert },
637
666
}
638
667
proxyServer .StartTLS ()
639
668
defer proxyServer .Close ()
@@ -643,7 +672,7 @@ func TestHTTPSProxyUsingNetDialTLSContextWithHTTPBackend(t *testing.T) {
643
672
}
644
673
// Dials websocket backend through HTTPS proxy, using NetDialTLSContext.
645
674
certPool := x509 .NewCertPool ()
646
- certPool .AppendCertsFromPEM (localhostCert )
675
+ certPool .AppendCertsFromPEM (proxyServerCert )
647
676
tlsConfig := & tls.Config {RootCAs : certPool }
648
677
var netDialCalled atomic.Int64
649
678
dialer := Dialer {
@@ -684,10 +713,12 @@ func TestHTTPSProxyUsingNetDialTLSContextWithHTTPBackend(t *testing.T) {
684
713
}
685
714
}
686
715
687
- // localhostCert was generated from crypto/tls/generate_cert.go with the following command:
716
+ // proxyServerCert was generated from crypto/tls/generate_cert.go with the following command:
688
717
//
689
718
// go run generate_cert.go --rsa-bits 2048 --host 127.0.0.1,::1,example.com --ca --start-date "Jan 1 00:00:00 1970" --duration=1000000h
690
- var localhostCert = []byte (`-----BEGIN CERTIFICATE-----
719
+ //
720
+ // proxyServerCert is a self-signed.
721
+ var proxyServerCert = []byte (`-----BEGIN CERTIFICATE-----
691
722
MIIDGTCCAgGgAwIBAgIRALL5AZcefF4kkYV1SEG6YrMwDQYJKoZIhvcNAQELBQAw
692
723
EjEQMA4GA1UEChMHQWNtZSBDbzAgFw03MDAxMDEwMDAwMDBaGA8yMDg0MDEyOTE2
693
724
MDAwMFowEjEQMA4GA1UEChMHQWNtZSBDbzCCASIwDQYJKoZIhvcNAQEBBQADggEP
@@ -707,8 +738,8 @@ MGYMzP0u4nw47aRz9shB8w+taPKHx2BVwE1m/yp3nHVioOjXqA1fwRQVGclCJSH1
707
738
D2iq3hWVHRENgjTjANBPICLo9AZ4JfN6PH19mnU=
708
739
-----END CERTIFICATE-----` )
709
740
710
- // localhostKey is the private key for localhostCert .
711
- var localhostKey = []byte (`-----BEGIN RSA PRIVATE KEY-----
741
+ // proxyServerKey is the private key for proxyServerCert .
742
+ var proxyServerKey = []byte (`-----BEGIN RSA PRIVATE KEY-----
712
743
MIIEogIBAAKCAQEAtD8UdzJXB0UfEBFtsPYoG0NRPsSeL7yKg12O0Zya1eoG/jkQ
713
744
LUIk6qoYlOugUYnpD2RAhn0WofkglHZ844kP2Q5O54bhW3UljWuPUpumN5+7xeV5
714
745
nktIHAhZWc3+USwRu4qaPs3aAu3kAffMxmIEjWaDW71nllkdhsKJOkGvCyrpxOW9
@@ -736,3 +767,56 @@ LiAGaec8xjl6QK/DdXmFuQBKqyKJ14rljFODP4QuE9WJid94bGqjpf3j99ltznZP
736
767
KR8NJEkK99Vh/tew6jAMll70xFrE7aF8VLXJVE7w4sQzuvHxl9Q=
737
768
-----END RSA PRIVATE KEY-----
738
769
` )
770
+
771
+ // websocketServerCert is self-signed.
772
+ var websocketServerCert = []byte (`-----BEGIN CERTIFICATE-----
773
+ MIIDOTCCAiGgAwIBAgIQYSN1VY/favsLUo+B7gJ5tTANBgkqhkiG9w0BAQsFADAS
774
+ MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw
775
+ MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
776
+ MIIBCgKCAQEApBlintjkL1fO1Sk2pzNvl862CtTwU7/Jy6EZqWzI17wEbPn4sbSD
777
+ bHhfDlPl2nmw3hVkc6LNK+eqzm2GX/ai4tgMiaH7kyyNit1K3g7y7GISMf9poWIa
778
+ POJhid2wmhKHbEtHECSdQ5c/jEN1UVzB4go5LO7MEEVo9kyQ+yBqS6gISyFmfaT4
779
+ qOsPJBir33bBpptSend1JSXaRTXqRa1p+oudw2ILa4U7KfuKK3emp21m5/HYAuSf
780
+ CV4WqqDoDiBPMpsQ0kPEPugWZKFeF3qanmqFFvptYx+zJbOznWYY2D3idWsvcg6q
781
+ VLPEB19oXaVBV0HXPFtObm5m1jCpl8FI1wIDAQABo4GIMIGFMA4GA1UdDwEB/wQE
782
+ AwICpDATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNVHRMBAf8EBTADAQH/MB0GA1Ud
783
+ DgQWBBQcSkjqA9rgos1daegNj49BpRCA0jAuBgNVHREEJzAlggtleGFtcGxlLmNv
784
+ bYcEfwAAAYcQAAAAAAAAAAAAAAAAAAAAATANBgkqhkiG9w0BAQsFAAOCAQEAnk9i
785
+ 9rogNTi9B1pn+Fbk3WALKdEjv/uyePsTnwdyvswVbeYbQweU9TrhYT2+eXbMA5kY
786
+ 7TaQm46idRqxCKMgc3Ip3DADJdm8cJX9p2ExU4fKdkPc1KD/J+4QHHx1W2Ml5S2o
787
+ foOo6j1F0UdZP/rBj0UumEZp32qW+4DhVV/QQjUB8J0gaDC7yZBMdyMIeClR0RqE
788
+ YfZdCJbQHqtTwBXN+imQUHPGmksYkRDpFRvw/4crpcMIE04mVVd99nOpFCQnK61t
789
+ 9US1y17VW1lYpkqlCS+rkcAtor4Z5naSf9/oLGCxEAwyW0pwHGO6MXtMxvB/JD20
790
+ hJdlz1I7wlSfF4MiRQ==
791
+ -----END CERTIFICATE-----` )
792
+
793
+ // websocketServerKey is the private key for websocketServerCert.
794
+ var websocketServerKey = []byte (`-----BEGIN PRIVATE KEY-----
795
+ MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCkGWKe2OQvV87V
796
+ KTanM2+XzrYK1PBTv8nLoRmpbMjXvARs+fixtINseF8OU+XaebDeFWRzos0r56rO
797
+ bYZf9qLi2AyJofuTLI2K3UreDvLsYhIx/2mhYho84mGJ3bCaEodsS0cQJJ1Dlz+M
798
+ Q3VRXMHiCjks7swQRWj2TJD7IGpLqAhLIWZ9pPio6w8kGKvfdsGmm1J6d3UlJdpF
799
+ NepFrWn6i53DYgtrhTsp+4ord6anbWbn8dgC5J8JXhaqoOgOIE8ymxDSQ8Q+6BZk
800
+ oV4XepqeaoUW+m1jH7Mls7OdZhjYPeJ1ay9yDqpUs8QHX2hdpUFXQdc8W05ubmbW
801
+ MKmXwUjXAgMBAAECggEAE6BkTDgH//rnkP/Ej/Y17Zkv6qxnMLe/4evwZB7PsrBu
802
+ cxOUAWUOpvA1UO215bh87+2XvcDbUISnyC1kpKDyAGGeC5llER2DXE11VokWgtvZ
803
+ Q0OXavw5w83A+WVGFFdiUmXP0l10CxEm7OwQjFz6D21GQ1qC65tG9NZZghTxbFTe
804
+ iZ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