packetman.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/protocol"
  23. )
  24. // PacketManipulationSpec is a work-around to avoid the client-side code size
  25. // impact of importing the packetman package and its dependencies.
  26. //
  27. // TODO: Given that packetman and its parameters are server-side only,
  28. // rearrange tactics/parameters to reference packetman.Spec directly, but only
  29. // in server code. This should allow reinstating the spec.Validate below.
  30. // PacketManipulationSpec is type-compatible with
  31. // psiphon/common.packetman.Spec.
  32. type PacketManipulationSpec struct {
  33. Name string
  34. PacketSpecs [][]string
  35. }
  36. // PacketManipulationSpecs is a list of packet manipulation specs.
  37. type PacketManipulationSpecs []*PacketManipulationSpec
  38. // Validate checks that each spec name is unique and that each spec compiles.
  39. func (specs PacketManipulationSpecs) Validate() error {
  40. specNames := make(map[string]bool)
  41. for _, spec := range specs {
  42. if spec.Name == "" {
  43. return errors.TraceNew("missing spec name")
  44. }
  45. if ok, _ := specNames[spec.Name]; ok {
  46. return errors.Tracef("duplicate spec name: %s", spec.Name)
  47. }
  48. specNames[spec.Name] = true
  49. // See PacketManipulationSpec comment above.
  50. //
  51. // Note that, even with spec.Validate disabled, spec validation will still
  52. // be performed, by packetman.Manipulator, on startup and after tactics hot
  53. // reload, with equivilent outcomes for invalid specs; however, the tactics
  54. // load itself will not fail in this case.
  55. // err := spec.Validate()
  56. // if err != nil {
  57. // return errors.Trace(err)
  58. // }
  59. }
  60. return nil
  61. }
  62. // ProtocolPacketManipulations is a map from tunnel protocol names (or "All")
  63. // to a list of packet manipulation spec names.
  64. type ProtocolPacketManipulations map[string][]string
  65. // Validate checks that tunnel protocol and spec names are valid. Duplicate
  66. // spec names are allowed in each entry, enabling weighted selection.
  67. func (manipulations ProtocolPacketManipulations) Validate(specs PacketManipulationSpecs) error {
  68. validSpecNames := make(map[string]bool)
  69. for _, spec := range specs {
  70. validSpecNames[spec.Name] = true
  71. }
  72. for tunnelProtocol, specNames := range manipulations {
  73. if tunnelProtocol != protocol.TUNNEL_PROTOCOLS_ALL {
  74. if !protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
  75. return errors.TraceNew("invalid tunnel protocol for packet manipulation")
  76. }
  77. }
  78. for _, specName := range specNames {
  79. if ok, _ := validSpecNames[specName]; !ok {
  80. return errors.Tracef("invalid spec name: %s", specName)
  81. }
  82. }
  83. }
  84. return nil
  85. }