frontedHTTPClientInstance_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "github.com/stretchr/testify/assert"
  32. )
  33. func TestFrontedHTTPClientInstance(t *testing.T) {
  34. // Generate server keys
  35. sk, err := rsa.GenerateKey(rand.Reader, 2048)
  36. if err != nil {
  37. t.Fatalf("error generating key: %s", err)
  38. }
  39. pubKeyBytes, err := x509.MarshalPKIXPublicKey(&sk.PublicKey)
  40. if err != nil {
  41. t.Fatalf("error marshaling public key: %s", err)
  42. }
  43. // Setup client
  44. networkID := fmt.Sprintf("WIFI-%s", time.Now().String())
  45. clientConfigJSON := fmt.Sprintf(`
  46. {
  47. "ClientPlatform" : "Android_10_com.test.app",
  48. "ClientVersion" : "0",
  49. "SponsorId" : "0000000000000000",
  50. "PropagationChannelId" : "0000000000000000",
  51. "DeviceLocation" : "gzzzz",
  52. "DeviceRegion" : "US",
  53. "DisableRemoteServerListFetcher" : true,
  54. "EnableFeedbackUpload" : true,
  55. "DisableTactics" : true,
  56. "FeedbackEncryptionPublicKey" : "%s",
  57. "NetworkID" : "%s"
  58. }`,
  59. base64.StdEncoding.EncodeToString(pubKeyBytes),
  60. networkID)
  61. config, err := LoadConfig([]byte(clientConfigJSON))
  62. if err != nil {
  63. t.Fatalf("error processing configuration file: %s", err)
  64. }
  65. testDataDirName, err := os.MkdirTemp("", "psiphon-feedback-test")
  66. if err != nil {
  67. t.Fatalf("TempDir failed: %s", err)
  68. }
  69. config.DataRootDirectory = testDataDirName
  70. address := "example.org"
  71. addressRegex := `[a-z0-9]{5,10}\.example\.org`
  72. url := fmt.Sprintf("https://%s", address)
  73. frontingSpecs := parameters.FrontingSpecs{
  74. {
  75. FrontingProviderID: prng.HexString(8),
  76. Addresses: []string{addressRegex},
  77. DisableSNI: prng.FlipCoin(),
  78. SkipVerify: true,
  79. Host: "example.org",
  80. },
  81. }
  82. config.FeedbackUploadURLs = parameters.TransferURLs{
  83. {
  84. URL: base64.StdEncoding.EncodeToString([]byte(url)),
  85. SkipVerify: true,
  86. OnlyAfterAttempts: 0,
  87. B64EncodedPublicKey: base64.StdEncoding.EncodeToString(pubKeyBytes),
  88. RequestHeaders: map[string]string{},
  89. FrontingSpecs: frontingSpecs,
  90. },
  91. }
  92. err = config.Commit(true)
  93. if err != nil {
  94. t.Fatalf("error committing configuration file: %s", err)
  95. }
  96. resolver := NewResolver(config, false)
  97. defer resolver.Stop()
  98. config.SetResolver(resolver)
  99. err = OpenDataStore(config)
  100. if err != nil {
  101. t.Fatalf("OpenDataStore failed: %s", err)
  102. }
  103. defer CloseDataStore()
  104. // Make fronted HTTP client instance
  105. // TODO: test that replay is disabled when there is a tunnel
  106. var tunnel *Tunnel = nil
  107. useDeviceBinder := true
  108. skipVerify := false
  109. payloadSecure := true
  110. client, err := newFrontedHTTPClientInstance(
  111. config, tunnel, frontingSpecs, nil, useDeviceBinder, skipVerify, config.DisableSystemRootCAs, payloadSecure)
  112. if err != nil {
  113. t.Fatalf("newFrontedHTTPClientInstance failed: %s", err)
  114. }
  115. client.frontedHTTPClientRoundTripperSucceeded()
  116. // Do replay
  117. prevClient := client
  118. client, err = newFrontedHTTPClientInstance(
  119. config, tunnel, frontingSpecs, nil, useDeviceBinder, skipVerify, config.DisableSystemRootCAs, payloadSecure)
  120. if err != nil {
  121. t.Fatalf("newFrontedHTTPClientInstance failed: %s", err)
  122. }
  123. if !client.frontedHTTPDialParameters.isReplay {
  124. t.Fatal("expected replay")
  125. }
  126. // Note: only exported FrontedHTTPDialParameters fields are stored for replay.
  127. assert.EqualExportedValues(t, prevClient.frontedHTTPDialParameters, client.frontedHTTPDialParameters)
  128. // Change network ID so there should be no replay.
  129. config.NetworkID = fmt.Sprintf("CELLULAR-%s", time.Now().String())
  130. err = config.Commit(true)
  131. if err != nil {
  132. t.Fatalf("error committing configuration file: %s", err)
  133. }
  134. client, err = newFrontedHTTPClientInstance(
  135. config, tunnel, frontingSpecs, nil, useDeviceBinder, skipVerify,
  136. config.DisableSystemRootCAs, payloadSecure)
  137. if err != nil {
  138. t.Fatalf("newFrontedHTTPClientInstance failed: %s", err)
  139. }
  140. if client.frontedHTTPDialParameters.isReplay {
  141. t.Fatal("expected no replay")
  142. }
  143. }