frontingSpec.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // Optional/new fields use omitempty to minimize tactics tag churn.
  42. FrontingProviderID string
  43. Transports protocol.FrontingTransports `json:",omitempty"`
  44. Addresses []string
  45. DisableSNI bool `json:",omitempty"`
  46. SkipVerify bool `json:",omitempty"`
  47. VerifyServerName string `json:",omitempty"`
  48. VerifyPins []string `json:",omitempty"`
  49. Host string
  50. }
  51. // SelectParameters selects fronting parameters from the given FrontingSpecs,
  52. // first selecting a spec at random. SelectParameters is similar to
  53. // psiphon.selectFrontingParameters, which operates on server entries.
  54. //
  55. // The return values are:
  56. // - Dial Address (domain or IP address)
  57. // - Transport (e.g., protocol.FRONTING_TRANSPORT_HTTPS)
  58. // - SNI (which may be transformed; unless it is "", which indicates omit SNI)
  59. // - VerifyServerName (see psiphon.CustomTLSConfig)
  60. // - VerifyPins (see psiphon.CustomTLSConfig)
  61. // - Host (Host header value)
  62. func (specs FrontingSpecs) SelectParameters() (
  63. string, string, string, string, string, []string, string, error) {
  64. if len(specs) == 0 {
  65. return "", "", "", "", "", nil, "", errors.TraceNew("missing fronting spec")
  66. }
  67. spec := specs[prng.Intn(len(specs))]
  68. if len(spec.Addresses) == 0 {
  69. return "", "", "", "", "", nil, "", errors.TraceNew("missing fronting address")
  70. }
  71. // For backwards compatibility, the transport type defaults
  72. // to "FRONTED-HTTPS" when the FrontingSpec specifies no transport types.
  73. transport := protocol.FRONTING_TRANSPORT_HTTPS
  74. if len(spec.Transports) > 0 {
  75. transport = spec.Transports[prng.Intn(len(spec.Transports))]
  76. }
  77. frontingDialAddr, err := regen.GenerateString(
  78. spec.Addresses[prng.Intn(len(spec.Addresses))])
  79. if err != nil {
  80. return "", "", "", "", "", nil, "", errors.Trace(err)
  81. }
  82. SNIServerName := frontingDialAddr
  83. if spec.DisableSNI || net.ParseIP(frontingDialAddr) != nil {
  84. SNIServerName = ""
  85. }
  86. // When SkipVerify is true, VerifyServerName and VerifyPins must be empty,
  87. // as checked in Validate. When dialing in any mode, MeekConn will set
  88. // CustomTLSConfig.SkipVerify to true as long as VerifyServerName is "".
  89. // So SkipVerify does not need to be explicitly returned.
  90. return spec.FrontingProviderID,
  91. transport,
  92. frontingDialAddr,
  93. SNIServerName,
  94. spec.VerifyServerName,
  95. spec.VerifyPins,
  96. spec.Host,
  97. nil
  98. }
  99. // Validate checks that the JSON values are well-formed.
  100. func (specs FrontingSpecs) Validate(allowSkipVerify bool) error {
  101. // An empty FrontingSpecs is allowed as a tactics setting, but
  102. // SelectParameters will fail at runtime: code that uses FrontingSpecs must
  103. // provide some mechanism -- or check for an empty FrontingSpecs -- to
  104. // enable/disable features that use FrontingSpecs.
  105. for _, spec := range specs {
  106. if len(spec.FrontingProviderID) == 0 {
  107. return errors.TraceNew("empty fronting provider ID")
  108. }
  109. err := spec.Transports.Validate()
  110. if err != nil {
  111. return errors.Trace(err)
  112. }
  113. if len(spec.Addresses) == 0 {
  114. return errors.TraceNew("missing fronting addresses")
  115. }
  116. for _, addr := range spec.Addresses {
  117. if len(addr) == 0 {
  118. return errors.TraceNew("empty fronting address")
  119. }
  120. }
  121. if spec.SkipVerify {
  122. if !allowSkipVerify {
  123. return errors.TraceNew("invalid skip verify")
  124. }
  125. if len(spec.VerifyServerName) != 0 {
  126. return errors.TraceNew("unexpected verify server name")
  127. }
  128. if len(spec.VerifyPins) != 0 {
  129. return errors.TraceNew("unexpected verify pins")
  130. }
  131. } else {
  132. if len(spec.VerifyServerName) == 0 {
  133. return errors.TraceNew("empty verify server name")
  134. }
  135. // An empty VerifyPins is allowed.
  136. for _, pin := range spec.VerifyPins {
  137. if len(pin) == 0 {
  138. return errors.TraceNew("empty verify pin")
  139. }
  140. }
  141. }
  142. if len(spec.Host) == 0 {
  143. return errors.TraceNew("empty fronting host")
  144. }
  145. }
  146. return nil
  147. }