transferURLs.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2018, 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 parameters
  20. import (
  21. "encoding/base64"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  24. )
  25. // TransferURL specifies a URL for uploading or downloading resources along
  26. // with parameters for the transfer strategy.
  27. type TransferURL struct {
  28. // URL is the location of the resource. This string is slightly obfuscated
  29. // with base64 encoding to mitigate trivial binary executable string scanning.
  30. URL string
  31. // SkipVerify indicates whether to verify HTTPS certificates. In some
  32. // circumvention scenarios, verification is not possible. This must
  33. // only be set to true when the resource has its own verification mechanism.
  34. // Overridden when a FrontingSpec in FrontingSpecs has verification fields
  35. // set.
  36. SkipVerify bool
  37. // OnlyAfterAttempts specifies how to schedule this URL when transferring
  38. // the same resource (same entity, same ETag) from multiple different
  39. // candidate locations. For a value of N, this URL is only a candidate
  40. // after N rounds of attempting the transfer to or from other URLs.
  41. OnlyAfterAttempts int
  42. // B64EncodedPublicKey is a base64-encoded RSA public key to be used for
  43. // encrypting the resource, when uploading, or for verifying a signature of
  44. // the resource, when downloading. Required by some operations, such as
  45. // uploading feedback.
  46. B64EncodedPublicKey string `json:",omitempty"`
  47. // RequestHeaders are optional HTTP headers to set on any requests made to
  48. // the destination.
  49. RequestHeaders map[string]string `json:",omitempty"`
  50. // FrontingSpecs is an optional set of domain fronting configurations to
  51. // apply to any requests made to the destination.
  52. FrontingSpecs FrontingSpecs
  53. }
  54. // TransferURLs is a list of transfer URLs.
  55. type TransferURLs []*TransferURL
  56. // DecodeAndValidate validates a list of transfer URLs.
  57. //
  58. // At least one TransferURL in the list must have OnlyAfterAttempts of 0,
  59. // or no TransferURL would be selected on the first attempt.
  60. func (t TransferURLs) DecodeAndValidate() error {
  61. hasOnlyAfterZero := false
  62. for _, transferURL := range t {
  63. // TransferURL FrontingSpecs are permitted to specify SkipVerify
  64. // because transfers have additional security at the payload level.
  65. allowSkipVerify := true
  66. err := transferURL.FrontingSpecs.Validate(allowSkipVerify)
  67. if err != nil {
  68. return errors.Trace(err)
  69. }
  70. if transferURL.OnlyAfterAttempts == 0 {
  71. hasOnlyAfterZero = true
  72. }
  73. decodedURL, err := base64.StdEncoding.DecodeString(transferURL.URL)
  74. if err != nil {
  75. return errors.Tracef("failed to decode URL: %s", err)
  76. }
  77. transferURL.URL = string(decodedURL)
  78. }
  79. if !hasOnlyAfterZero {
  80. return errors.Tracef("must be at least one TransferURL with OnlyAfterAttempts = 0")
  81. }
  82. return nil
  83. }
  84. // CanonicalURL returns the canonical URL, to be used as a key when storing
  85. // information related to the TransferURLs, such as an ETag.
  86. func (t TransferURLs) CanonicalURL() string {
  87. // The first OnlyAfterAttempts = 0 URL is the canonical URL. This
  88. // is the value used as the key for SetUrlETag when multiple download
  89. // URLs can be used to fetch a single entity.
  90. for _, transferURL := range t {
  91. if transferURL.OnlyAfterAttempts == 0 {
  92. return transferURL.URL
  93. }
  94. }
  95. return ""
  96. }
  97. // Select chooses a TransferURL from the list.
  98. //
  99. // The TransferURL is selected based at random from the candidates allowed in
  100. // the specified attempt.
  101. func (t TransferURLs) Select(attempt int) *TransferURL {
  102. candidates := make([]int, 0)
  103. for index, URL := range t {
  104. if attempt >= URL.OnlyAfterAttempts {
  105. candidates = append(candidates, index)
  106. }
  107. }
  108. if len(candidates) < 1 {
  109. // This case is not expected, as DecodeAndValidate should reject configs
  110. // that would have no candidates for 0 attempts.
  111. return nil
  112. }
  113. selection := prng.Intn(len(candidates))
  114. transferURL := t[candidates[selection]]
  115. return transferURL
  116. }