packetman.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 server
  20. import (
  21. "net"
  22. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/packetman"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  28. )
  29. func makePacketManipulatorConfig(
  30. support *SupportServices) (*packetman.Config, error) {
  31. // Packet interception is configured for any tunnel protocol port that _may_
  32. // use packet manipulation. A future hot reload of tactics may apply specs to
  33. // any of these protocols.
  34. var ports []int
  35. for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
  36. if protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
  37. ports = append(ports, port)
  38. }
  39. }
  40. selectSpecName := func(protocolPort int, clientIP net.IP) string {
  41. specName, err := selectPacketManipulationSpec(support, protocolPort, clientIP)
  42. if err != nil {
  43. log.WithTraceFields(
  44. LogFields{"error": err}).Warning(
  45. "failed to get tactics for packet manipulation")
  46. return ""
  47. }
  48. return specName
  49. }
  50. specs, err := getPacketManipulationSpecs(support)
  51. if err != nil {
  52. return nil, errors.Trace(err)
  53. }
  54. return &packetman.Config{
  55. Logger: CommonLogger(log),
  56. SudoNetworkConfigCommands: support.Config.PacketTunnelSudoNetworkConfigCommands,
  57. QueueNumber: 1,
  58. ProtocolPorts: ports,
  59. Specs: specs,
  60. SelectSpecName: selectSpecName,
  61. }, nil
  62. }
  63. func getPacketManipulationSpecs(support *SupportServices) ([]*packetman.Spec, error) {
  64. // By convention, parameters.ServerPacketManipulationSpecs should be in
  65. // DefaultTactics, not FilteredTactics; and Tactics.Probability is ignored.
  66. tactics, err := support.TacticsServer.GetTactics(
  67. true, common.GeoIPData(NewGeoIPData()), make(common.APIParameters))
  68. if err != nil {
  69. return nil, errors.Trace(err)
  70. }
  71. if tactics == nil {
  72. // This server isn't configured with tactics.
  73. return []*packetman.Spec{}, nil
  74. }
  75. clientParameters, err := parameters.NewClientParameters(nil)
  76. if err != nil {
  77. return nil, errors.Trace(err)
  78. }
  79. _, err = clientParameters.Set("", false, tactics.Parameters)
  80. if err != nil {
  81. return nil, errors.Trace(err)
  82. }
  83. p := clientParameters.Get()
  84. paramSpecs := p.PacketManipulationSpecs(parameters.ServerPacketManipulationSpecs)
  85. specs := make([]*packetman.Spec, len(paramSpecs))
  86. for i, spec := range paramSpecs {
  87. packetmanSpec := packetman.Spec(*spec)
  88. specs[i] = &packetmanSpec
  89. }
  90. return specs, nil
  91. }
  92. func reloadPacketManipulationSpecs(support *SupportServices) error {
  93. specs, err := getPacketManipulationSpecs(support)
  94. if err != nil {
  95. return errors.Trace(err)
  96. }
  97. err = support.PacketManipulator.SetSpecs(specs)
  98. if err != nil {
  99. return errors.Trace(err)
  100. }
  101. return nil
  102. }
  103. func selectPacketManipulationSpec(
  104. support *SupportServices, protocolPort int, clientIP net.IP) (string, error) {
  105. geoIPData := support.GeoIPService.Lookup(clientIP.String())
  106. tactics, err := support.TacticsServer.GetTactics(
  107. true, common.GeoIPData(geoIPData), make(common.APIParameters))
  108. if err != nil {
  109. return "", errors.Trace(err)
  110. }
  111. if tactics == nil {
  112. // This server isn't configured with tactics.
  113. return "", nil
  114. }
  115. if !prng.FlipWeightedCoin(tactics.Probability) {
  116. // Skip tactics with the configured probability.
  117. return "", nil
  118. }
  119. clientParameters, err := parameters.NewClientParameters(nil)
  120. if err != nil {
  121. return "", errors.Trace(err)
  122. }
  123. _, err = clientParameters.Set("", false, tactics.Parameters)
  124. if err != nil {
  125. return "", errors.Trace(err)
  126. }
  127. p := clientParameters.Get()
  128. // GeoIP tactics filtering is applied before getting
  129. // ServerPacketManipulationProbability and ServerProtocolPacketManipulations.
  130. //
  131. // The intercepted packet source/protocol port is used to determine the
  132. // tunnel protocol name, which is used to lookup enabled packet manipulation
  133. // specs in ServerProtocolPacketManipulations.
  134. //
  135. // When there are multiple enabled specs, one is selected at random.
  136. //
  137. // Specs under the key "All" apply to all protocols. Duplicate specs per
  138. // entry are allowed, enabling weighted selection. If a spec appears in both
  139. // "All" and a specific protocol, the duplicate(s) are retained.
  140. if !p.WeightedCoinFlip(parameters.ServerPacketManipulationProbability) {
  141. return "", nil
  142. }
  143. targetTunnelProtocol := ""
  144. for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
  145. if port == protocolPort {
  146. targetTunnelProtocol = tunnelProtocol
  147. break
  148. }
  149. }
  150. if targetTunnelProtocol == "" {
  151. return "", errors.Tracef(
  152. "packet manipulation protocol port not found: %d", protocolPort)
  153. }
  154. protocolSpecs := p.ProtocolPacketManipulations(
  155. parameters.ServerProtocolPacketManipulations)
  156. // TODO: cache merged per-protocol + "All" lists?
  157. specNames, ok := protocolSpecs[targetTunnelProtocol]
  158. if !ok {
  159. specNames = []string{}
  160. }
  161. allProtocolsSpecNames, ok := protocolSpecs[protocol.TUNNEL_PROTOCOLS_ALL]
  162. if ok {
  163. specNames = append(specNames, allProtocolsSpecNames...)
  164. }
  165. if len(specNames) < 1 {
  166. // Tactics contains no candidate specs for this protocol.
  167. return "", nil
  168. }
  169. return specNames[prng.Range(0, len(specNames)-1)], nil
  170. }