transferURLs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. It 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. }
  41. // TransferURLs is a list of transfer URLs.
  42. type TransferURLs []*TransferURL
  43. // DecodeAndValidate validates a list of download URLs.
  44. //
  45. // At least one TransferURL in the list must have OnlyAfterAttempts of 0,
  46. // or no TransferURL would be selected on the first attempt.
  47. func (t TransferURLs) DecodeAndValidate() error {
  48. hasOnlyAfterZero := false
  49. for _, transferURL := range t {
  50. if transferURL.OnlyAfterAttempts == 0 {
  51. hasOnlyAfterZero = true
  52. }
  53. decodedURL, err := base64.StdEncoding.DecodeString(transferURL.URL)
  54. if err != nil {
  55. return errors.Tracef("failed to decode URL: %s", err)
  56. }
  57. transferURL.URL = string(decodedURL)
  58. }
  59. if !hasOnlyAfterZero {
  60. return errors.Tracef("must be at least one TransferURL with OnlyAfterAttempts = 0")
  61. }
  62. return nil
  63. }
  64. // Select chooses a TransferURL from the list.
  65. //
  66. // The first return value is the canonical URL, to be used
  67. // as a key when storing information related to the TransferURLs,
  68. // such as an ETag.
  69. //
  70. // The second return value is the chosen transfer URL, which is
  71. // selected based at random from the candidates allowed in the
  72. // specified attempt.
  73. func (t TransferURLs) Select(attempt int) (string, string, bool) {
  74. // The first OnlyAfterAttempts = 0 URL is the canonical URL. This
  75. // is the value used as the key for SetUrlETag when multiple download
  76. // URLs can be used to fetch a single entity.
  77. canonicalURL := ""
  78. for _, transferURL := range t {
  79. if transferURL.OnlyAfterAttempts == 0 {
  80. canonicalURL = transferURL.URL
  81. break
  82. }
  83. }
  84. candidates := make([]int, 0)
  85. for index, URL := range t {
  86. if attempt >= URL.OnlyAfterAttempts {
  87. candidates = append(candidates, index)
  88. }
  89. }
  90. if len(candidates) < 1 {
  91. // This case is not expected, as DecodeAndValidate should reject configs
  92. // that would have no candidates for 0 attempts.
  93. return "", "", true
  94. }
  95. selection := prng.Intn(len(candidates))
  96. transferURL := t[candidates[selection]]
  97. return transferURL.URL, canonicalURL, transferURL.SkipVerify
  98. }