livenessTest.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2025, 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 "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  21. const LIVENESS_ANY = ""
  22. // LivenessTestSpec specifies the parameters for a Liveness Test.
  23. type LivenessTestSpec struct {
  24. MinUpstreamBytes int
  25. MaxUpstreamBytes int
  26. MinDownstreamBytes int
  27. MaxDownstreamBytes int
  28. }
  29. // LivenessTestSpecs is a map of tunnel protocol patterns to Liveness Test spec.
  30. // Patterns may contain the '*' wildcard.
  31. type LivenessTestSpecs map[string]*LivenessTestSpec
  32. func (l LivenessTestSpecs) Validate() error {
  33. // Check that there is a LIVENESS_ANY entry.
  34. if _, ok := l[LIVENESS_ANY]; !ok {
  35. return errors.TraceNew("missing LIVENESS_ANY entry")
  36. }
  37. // Check that all entries are well-formed.
  38. for _, spec := range l {
  39. if spec.MinUpstreamBytes < 0 {
  40. return errors.TraceNew("invalid MinUpstreamBytes")
  41. }
  42. if spec.MaxUpstreamBytes < 0 {
  43. return errors.TraceNew("invalid MaxUpstreamBytes")
  44. }
  45. if spec.MinDownstreamBytes < 0 {
  46. return errors.TraceNew("invalid MinDownstreamBytes")
  47. }
  48. if spec.MaxDownstreamBytes < 0 {
  49. return errors.TraceNew("invalid MaxDownstreamBytes")
  50. }
  51. }
  52. return nil
  53. }