frontingSpec.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2021, 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. "net"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/regen"
  26. )
  27. // FrontingSpecs is a list of domain fronting specs.
  28. type FrontingSpecs []*FrontingSpec
  29. // FrontingSpec specifies a domain fronting configuration, to be used with
  30. // MeekConn and MeekModePlaintextRoundTrip. In MeekModePlaintextRoundTrip, the
  31. // fronted origin is an arbitrary web server, not a Psiphon server. This
  32. // MeekConn mode requires HTTPS and server certificate validation:
  33. // VerifyServerName is required; VerifyPins is recommended. See also
  34. // psiphon.MeekConfig and psiphon.MeekConn.
  35. //
  36. // FrontingSpec.Addresses supports the functionality of both
  37. // ServerEntry.MeekFrontingAddressesRegex and
  38. // ServerEntry.MeekFrontingAddresses: multiple candidates are supported, and
  39. // each candidate may be a regex, or a static value (with regex syntax).
  40. type FrontingSpec struct {
  41. FrontingProviderID string
  42. Transports protocol.FrontingTransports
  43. Addresses []string
  44. DisableSNI bool
  45. VerifyServerName string
  46. VerifyPins []string
  47. Host string
  48. }
  49. // SelectParameters selects fronting parameters from the given FrontingSpecs,
  50. // first selecting a spec at random. SelectParameters is similar to
  51. // psiphon.selectFrontingParameters, which operates on server entries.
  52. //
  53. // The return values are:
  54. // - Dial Address (domain or IP address)
  55. // - Transport (e.g., protocol.FRONTING_TRANSPORT_HTTPS)
  56. // - SNI (which may be transformed; unless it is "", which indicates omit SNI)
  57. // - VerifyServerName (see psiphon.CustomTLSConfig)
  58. // - VerifyPins (see psiphon.CustomTLSConfig)
  59. // - Host (Host header value)
  60. func (specs FrontingSpecs) SelectParameters() (
  61. string, string, string, string, string, []string, string, error) {
  62. if len(specs) == 0 {
  63. return "", "", "", "", "", nil, "", errors.TraceNew("missing fronting spec")
  64. }
  65. spec := specs[prng.Intn(len(specs))]
  66. if len(spec.Addresses) == 0 {
  67. return "", "", "", "", "", nil, "", errors.TraceNew("missing fronting address")
  68. }
  69. // For backwards compatibility, the transport type defaults
  70. // to "FRONTED-HTTPS" when the FrontingSpec specifies no transport types.
  71. transport := protocol.FRONTING_TRANSPORT_HTTPS
  72. if len(spec.Transports) > 0 {
  73. transport = spec.Transports[prng.Intn(len(spec.Transports))]
  74. }
  75. frontingDialAddr, err := regen.GenerateString(
  76. spec.Addresses[prng.Intn(len(spec.Addresses))])
  77. if err != nil {
  78. return "", "", "", "", "", nil, "", errors.Trace(err)
  79. }
  80. SNIServerName := frontingDialAddr
  81. if spec.DisableSNI || net.ParseIP(frontingDialAddr) != nil {
  82. SNIServerName = ""
  83. }
  84. return spec.FrontingProviderID,
  85. transport,
  86. frontingDialAddr,
  87. SNIServerName,
  88. spec.VerifyServerName,
  89. spec.VerifyPins,
  90. spec.Host,
  91. nil
  92. }
  93. // Validate checks that the JSON values are well-formed.
  94. func (specs FrontingSpecs) Validate() error {
  95. // An empty FrontingSpecs is allowed as a tactics setting, but
  96. // SelectParameters will fail at runtime: code that uses FrontingSpecs must
  97. // provide some mechanism -- or check for an empty FrontingSpecs -- to
  98. // enable/disable features that use FrontingSpecs.
  99. for _, spec := range specs {
  100. if len(spec.FrontingProviderID) == 0 {
  101. return errors.TraceNew("empty fronting provider ID")
  102. }
  103. err := spec.Transports.Validate()
  104. if err != nil {
  105. return errors.Trace(err)
  106. }
  107. if len(spec.Addresses) == 0 {
  108. return errors.TraceNew("missing fronting addresses")
  109. }
  110. for _, addr := range spec.Addresses {
  111. if len(addr) == 0 {
  112. return errors.TraceNew("empty fronting address")
  113. }
  114. }
  115. if len(spec.VerifyServerName) == 0 {
  116. return errors.TraceNew("empty verify server name")
  117. }
  118. // An empty VerifyPins is allowed.
  119. for _, pin := range spec.VerifyPins {
  120. if len(pin) == 0 {
  121. return errors.TraceNew("empty verify pin")
  122. }
  123. }
  124. if len(spec.Host) == 0 {
  125. return errors.TraceNew("empty fronting host")
  126. }
  127. }
  128. return nil
  129. }