packetman.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2020, 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/errors"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/packetman"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  24. )
  25. // PacketManipulationSpecs is a list of packet manipulation specs.
  26. type PacketManipulationSpecs []*packetman.Spec
  27. // Validate checks that each spec name is unique and that each spec compiles.
  28. func (specs PacketManipulationSpecs) Validate() error {
  29. specNames := make(map[string]bool)
  30. for _, spec := range specs {
  31. if spec.Name == "" {
  32. return errors.TraceNew("missing spec name")
  33. }
  34. if ok, _ := specNames[spec.Name]; ok {
  35. return errors.TraceNew("duplicate spec name")
  36. }
  37. specNames[spec.Name] = true
  38. err := spec.Validate()
  39. if err != nil {
  40. return errors.Trace(err)
  41. }
  42. }
  43. return nil
  44. }
  45. // ProtocolPacketManipulations is a map from tunnel protocol names (or "All")
  46. // to a list of packet manipulation spec names.
  47. type ProtocolPacketManipulations map[string][]string
  48. // Validate checks that tunnel protocol and spec names are valid. Duplicate
  49. // spec names are allowed in each entry, enabling weighted selection.
  50. func (manipulations ProtocolPacketManipulations) Validate(specs PacketManipulationSpecs) error {
  51. validSpecNames := make(map[string]bool)
  52. for _, spec := range specs {
  53. validSpecNames[spec.Name] = true
  54. }
  55. for tunnelProtocol, specNames := range manipulations {
  56. if tunnelProtocol != protocol.TUNNEL_PROTOCOLS_ALL {
  57. if !protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
  58. return errors.TraceNew("invalid tunnel protocol for packet manipulation")
  59. }
  60. }
  61. for _, specName := range specNames {
  62. if ok, _ := validSpecNames[specName]; !ok {
  63. return errors.TraceNew("invalid spec name")
  64. }
  65. }
  66. }
  67. return nil
  68. }