frontingSpec.go 4.1 KB

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