inproxy.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2023, 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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/inproxy"
  24. )
  25. // InproxyBrokerSpecsValue is a list of in-proxy broker specs.
  26. type InproxyBrokerSpecsValue []*InproxyBrokerSpec
  27. // InproxyBrokerSpec specifies the configuration to use to establish a secure
  28. // connection to an in-proxy broker.
  29. type InproxyBrokerSpec struct {
  30. BrokerPublicKey string
  31. BrokerRootObfuscationSecret string
  32. BrokerFrontingSpecs FrontingSpecs
  33. }
  34. // Validate checks that the in-proxy broker specs values are well-formed.
  35. func (specs InproxyBrokerSpecsValue) Validate(checkBrokerPublicKeyList *[]string) error {
  36. for _, spec := range specs {
  37. if _, err := inproxy.SessionPublicKeyFromString(spec.BrokerPublicKey); err != nil {
  38. return errors.Tracef("invalid broker public key: %w", err)
  39. }
  40. if checkBrokerPublicKeyList != nil && !common.Contains(*checkBrokerPublicKeyList, spec.BrokerPublicKey) {
  41. return errors.TraceNew("unknown broker public key")
  42. }
  43. if _, err := inproxy.ObfuscationSecretFromString(spec.BrokerRootObfuscationSecret); err != nil {
  44. return errors.Tracef("invalid broker root obfuscation secret: %w", err)
  45. }
  46. if len(spec.BrokerFrontingSpecs) == 0 {
  47. return errors.TraceNew("missing broker fronting spec")
  48. }
  49. err := spec.BrokerFrontingSpecs.Validate()
  50. if err != nil {
  51. return errors.Trace(err)
  52. }
  53. }
  54. return nil
  55. }
  56. // InproxyCompartmentIDsValue is a list of in-proxy common compartment IDs.
  57. type InproxyCompartmentIDsValue []string
  58. // Validate checks that the in-proxy common compartment ID values are
  59. // well-formed.
  60. func (IDs InproxyCompartmentIDsValue) Validate(checkCompartmentIDList *[]string) error {
  61. for _, ID := range IDs {
  62. if _, err := inproxy.IDFromString(ID); err != nil {
  63. return errors.Tracef("invalid compartment ID: %w", err)
  64. }
  65. if checkCompartmentIDList != nil && !common.Contains(*checkCompartmentIDList, ID) {
  66. return errors.TraceNew("unknown compartment ID")
  67. }
  68. }
  69. return nil
  70. }
  71. // InproxyDataChannelTrafficShapingParameters is type-compatible with
  72. // common/inproxy.DataChannelTrafficShapingParameters.
  73. type InproxyDataChannelTrafficShapingParametersValue struct {
  74. MinPaddedMessages int
  75. MaxPaddedMessages int
  76. MinPaddingSize int
  77. MaxPaddingSize int
  78. MinDecoyMessages int
  79. MaxDecoyMessages int
  80. MinDecoySize int
  81. MaxDecoySize int
  82. DecoyMessageProbability float64
  83. }
  84. func (p *InproxyDataChannelTrafficShapingParametersValue) Validate() error {
  85. if p.MinPaddedMessages < 0 ||
  86. p.MaxPaddedMessages < 0 ||
  87. p.MinPaddingSize < 0 ||
  88. p.MaxPaddingSize < 0 ||
  89. p.MinDecoyMessages < 0 ||
  90. p.MaxDecoyMessages < 0 ||
  91. p.MinDecoySize < 0 ||
  92. p.MaxDecoySize < 0 ||
  93. p.DecoyMessageProbability < 0.0 {
  94. return errors.TraceNew("invalid parameter")
  95. }
  96. return nil
  97. }