frontedHTTP_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2024, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package psiphon
  20. import (
  21. "crypto/rand"
  22. "crypto/rsa"
  23. "crypto/x509"
  24. "encoding/base64"
  25. "fmt"
  26. "os"
  27. "testing"
  28. "time"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  31. utls "github.com/Psiphon-Labs/utls"
  32. "github.com/stretchr/testify/assert"
  33. )
  34. func TestFrontedHTTPClientInstance(t *testing.T) {
  35. // Generate server keys
  36. sk, err := rsa.GenerateKey(rand.Reader, 2048)
  37. if err != nil {
  38. t.Fatalf("error generating key: %s", err)
  39. }
  40. pubKeyBytes, err := x509.MarshalPKIXPublicKey(&sk.PublicKey)
  41. if err != nil {
  42. t.Fatalf("error marshaling public key: %s", err)
  43. }
  44. // Setup client
  45. networkID := fmt.Sprintf("WIFI-%s", time.Now().String())
  46. clientConfigJSON := fmt.Sprintf(`
  47. {
  48. "ClientPlatform" : "Android_10_com.test.app",
  49. "ClientVersion" : "0",
  50. "SponsorId" : "0000000000000000",
  51. "PropagationChannelId" : "0000000000000000",
  52. "DeviceLocation" : "gzzzz",
  53. "DeviceRegion" : "US",
  54. "DisableRemoteServerListFetcher" : true,
  55. "EnableFeedbackUpload" : true,
  56. "DisableTactics" : true,
  57. "FeedbackEncryptionPublicKey" : "%s",
  58. "NetworkID" : "%s"
  59. }`,
  60. base64.StdEncoding.EncodeToString(pubKeyBytes),
  61. networkID)
  62. config, err := LoadConfig([]byte(clientConfigJSON))
  63. if err != nil {
  64. t.Fatalf("error processing configuration file: %s", err)
  65. }
  66. testDataDirName, err := os.MkdirTemp("", "psiphon-feedback-test")
  67. if err != nil {
  68. t.Fatalf("TempDir failed: %s", err)
  69. }
  70. config.DataRootDirectory = testDataDirName
  71. address := "example.org"
  72. addressRegex := `[a-z0-9]{5,10}\.example\.org`
  73. url := fmt.Sprintf("https://%s", address)
  74. frontingSpecs := parameters.FrontingSpecs{
  75. {
  76. FrontingProviderID: prng.HexString(8),
  77. Addresses: []string{addressRegex},
  78. DisableSNI: prng.FlipCoin(),
  79. SkipVerify: true,
  80. Host: "example.org",
  81. },
  82. }
  83. config.FeedbackUploadURLs = parameters.TransferURLs{
  84. {
  85. URL: base64.StdEncoding.EncodeToString([]byte(url)),
  86. SkipVerify: true,
  87. OnlyAfterAttempts: 0,
  88. B64EncodedPublicKey: base64.StdEncoding.EncodeToString(pubKeyBytes),
  89. RequestHeaders: map[string]string{},
  90. FrontingSpecs: frontingSpecs,
  91. },
  92. }
  93. err = config.Commit(true)
  94. if err != nil {
  95. t.Fatalf("error committing configuration file: %s", err)
  96. }
  97. resolver := NewResolver(config, false)
  98. defer resolver.Stop()
  99. config.SetResolver(resolver)
  100. err = OpenDataStore(config)
  101. if err != nil {
  102. t.Fatalf("OpenDataStore failed: %s", err)
  103. }
  104. defer CloseDataStore()
  105. // Make fronted HTTP client instance
  106. tlsCache := utls.NewLRUClientSessionCache(0)
  107. // TODO: test that replay is disabled when there is a tunnel
  108. var tunnel *Tunnel = nil
  109. useDeviceBinder := true
  110. skipVerify := false
  111. payloadSecure := true
  112. client, err := newFrontedHTTPClientInstance(
  113. config, tunnel, frontingSpecs, nil, useDeviceBinder, skipVerify, config.DisableSystemRootCAs, payloadSecure, tlsCache)
  114. if err != nil {
  115. t.Fatalf("newFrontedHTTPClientInstance failed: %s", err)
  116. }
  117. client.frontedHTTPClientRoundTripperSucceeded()
  118. // Do replay
  119. prevClient := client
  120. client, err = newFrontedHTTPClientInstance(
  121. config, tunnel, frontingSpecs, nil, useDeviceBinder, skipVerify, config.DisableSystemRootCAs, payloadSecure, tlsCache)
  122. if err != nil {
  123. t.Fatalf("newFrontedHTTPClientInstance failed: %s", err)
  124. }
  125. if !client.frontedHTTPDialParameters.isReplay {
  126. t.Fatal("expected replay")
  127. }
  128. // Note: only exported FrontedHTTPDialParameters fields are stored for replay.
  129. assert.EqualExportedValues(t, prevClient.frontedHTTPDialParameters, client.frontedHTTPDialParameters)
  130. // Change network ID so there should be no replay.
  131. config.NetworkID = fmt.Sprintf("CELLULAR-%s", time.Now().String())
  132. err = config.Commit(true)
  133. if err != nil {
  134. t.Fatalf("error committing configuration file: %s", err)
  135. }
  136. client, err = newFrontedHTTPClientInstance(
  137. config, tunnel, frontingSpecs, nil, useDeviceBinder, skipVerify,
  138. config.DisableSystemRootCAs, payloadSecure, tlsCache)
  139. if err != nil {
  140. t.Fatalf("newFrontedHTTPClientInstance failed: %s", err)
  141. }
  142. if client.frontedHTTPDialParameters.isReplay {
  143. t.Fatal("expected no replay")
  144. }
  145. }