transferURLs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. SkipVerify bool
  35. // OnlyAfterAttempts specifies how to schedule this URL when transferring
  36. // the same resource (same entity, same ETag) from multiple different
  37. // candidate locations. For a value of N, this URL is only a candidate
  38. // after N rounds of attempting the transfer to or from other URLs.
  39. OnlyAfterAttempts int
  40. // B64EncodedPublicKey is a base64-encoded RSA public key to be used for
  41. // encrypting the resource, when uploading, or for verifying a signature of
  42. // the resource, when downloading. Required by some operations, such as
  43. // uploading feedback.
  44. B64EncodedPublicKey string `json:",omitempty"`
  45. // RequestHeaders are optional HTTP headers to set on any requests made to
  46. // the destination.
  47. RequestHeaders map[string]string `json:",omitempty"`
  48. }
  49. // TransferURLs is a list of transfer URLs.
  50. type TransferURLs []*TransferURL
  51. // DecodeAndValidate validates a list of transfer URLs.
  52. //
  53. // At least one TransferURL in the list must have OnlyAfterAttempts of 0,
  54. // or no TransferURL would be selected on the first attempt.
  55. func (t TransferURLs) DecodeAndValidate() error {
  56. hasOnlyAfterZero := false
  57. for _, transferURL := range t {
  58. if transferURL.OnlyAfterAttempts == 0 {
  59. hasOnlyAfterZero = true
  60. }
  61. decodedURL, err := base64.StdEncoding.DecodeString(transferURL.URL)
  62. if err != nil {
  63. return errors.Tracef("failed to decode URL: %s", err)
  64. }
  65. transferURL.URL = string(decodedURL)
  66. }
  67. if !hasOnlyAfterZero {
  68. return errors.Tracef("must be at least one TransferURL with OnlyAfterAttempts = 0")
  69. }
  70. return nil
  71. }
  72. // CanonicalURL returns the canonical URL, to be used as a key when storing
  73. // information related to the TransferURLs, such as an ETag.
  74. func (t TransferURLs) CanonicalURL() string {
  75. // The first OnlyAfterAttempts = 0 URL is the canonical URL. This
  76. // is the value used as the key for SetUrlETag when multiple download
  77. // URLs can be used to fetch a single entity.
  78. for _, transferURL := range t {
  79. if transferURL.OnlyAfterAttempts == 0 {
  80. return transferURL.URL
  81. }
  82. }
  83. return ""
  84. }
  85. // Select chooses a TransferURL from the list.
  86. //
  87. // The TransferURL is selected based at random from the candidates allowed in
  88. // the specified attempt.
  89. func (t TransferURLs) Select(attempt int) *TransferURL {
  90. candidates := make([]int, 0)
  91. for index, URL := range t {
  92. if attempt >= URL.OnlyAfterAttempts {
  93. candidates = append(candidates, index)
  94. }
  95. }
  96. if len(candidates) < 1 {
  97. // This case is not expected, as DecodeAndValidate should reject configs
  98. // that would have no candidates for 0 attempts.
  99. return nil
  100. }
  101. selection := prng.Intn(len(candidates))
  102. transferURL := t[candidates[selection]]
  103. return transferURL
  104. }