packetman.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. specs := p.PacketManipulationSpecs(parameters.ServerPacketManipulationSpecs)
  85. return specs, nil
  86. }
  87. func reloadPacketManipulationSpecs(support *SupportServices) error {
  88. specs, err := getPacketManipulationSpecs(support)
  89. if err != nil {
  90. return errors.Trace(err)
  91. }
  92. err = support.PacketManipulator.SetSpecs(specs)
  93. if err != nil {
  94. return errors.Trace(err)
  95. }
  96. return nil
  97. }
  98. func selectPacketManipulationSpec(
  99. support *SupportServices, protocolPort int, clientIP net.IP) (string, error) {
  100. geoIPData := support.GeoIPService.Lookup(clientIP.String())
  101. tactics, err := support.TacticsServer.GetTactics(
  102. true, common.GeoIPData(geoIPData), make(common.APIParameters))
  103. if err != nil {
  104. return "", errors.Trace(err)
  105. }
  106. if tactics == nil {
  107. // This server isn't configured with tactics.
  108. return "", nil
  109. }
  110. if !prng.FlipWeightedCoin(tactics.Probability) {
  111. // Skip tactics with the configured probability.
  112. return "", nil
  113. }
  114. clientParameters, err := parameters.NewClientParameters(nil)
  115. if err != nil {
  116. return "", errors.Trace(err)
  117. }
  118. _, err = clientParameters.Set("", false, tactics.Parameters)
  119. if err != nil {
  120. return "", errors.Trace(err)
  121. }
  122. p := clientParameters.Get()
  123. // GeoIP tactics filtering is applied before getting
  124. // ServerPacketManipulationProbability and ServerProtocolPacketManipulations.
  125. //
  126. // The intercepted packet source/protocol port is used to determine the
  127. // tunnel protocol name, which is used to lookup enabled packet manipulation
  128. // specs in ServerProtocolPacketManipulations.
  129. //
  130. // When there are multiple enabled specs, one is selected at random.
  131. //
  132. // Specs under the key "All" apply to all protocols. Duplicate specs per
  133. // entry are allowed, enabling weighted selection. If a spec appears in both
  134. // "All" and a specific protocol, the duplicate(s) are retained.
  135. if !p.WeightedCoinFlip(parameters.ServerPacketManipulationProbability) {
  136. return "", nil
  137. }
  138. targetTunnelProtocol := ""
  139. for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
  140. if port == protocolPort {
  141. targetTunnelProtocol = tunnelProtocol
  142. break
  143. }
  144. }
  145. if targetTunnelProtocol == "" {
  146. return "", errors.Tracef(
  147. "packet manipulation protocol port not found: %d", protocolPort)
  148. }
  149. protocolSpecs := p.ProtocolPacketManipulations(
  150. parameters.ServerProtocolPacketManipulations)
  151. // TODO: cache merged per-protocol + "All" lists?
  152. specNames, ok := protocolSpecs[targetTunnelProtocol]
  153. if !ok {
  154. specNames = []string{}
  155. }
  156. allProtocolsSpecNames, ok := protocolSpecs[protocol.TUNNEL_PROTOCOLS_ALL]
  157. if ok {
  158. specNames = append(specNames, allProtocolsSpecNames...)
  159. }
  160. if len(specNames) < 1 {
  161. // Tactics contains no candidate specs for this protocol.
  162. return "", nil
  163. }
  164. return specNames[prng.Range(0, len(specNames)-1)], nil
  165. }