packetman.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/errors"
  23. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/packetman"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  27. )
  28. func makePacketManipulatorConfig(
  29. support *SupportServices) (*packetman.Config, error) {
  30. // Packet interception is configured for any tunnel protocol port that _may_
  31. // use packet manipulation. A future hot reload of tactics may apply specs to
  32. // any of these protocols.
  33. var ports []int
  34. for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
  35. if protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
  36. ports = append(ports, port)
  37. }
  38. }
  39. selectSpecName := func(protocolPort int, clientIP net.IP) (string, interface{}) {
  40. specName, extraData, err := selectPacketManipulationSpec(
  41. 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 "", nil
  47. }
  48. return specName, extraData
  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 ServerTacticsParametersCache
  66. // ignores Tactics.Probability.
  67. p, err := support.ServerTacticsParametersCache.Get(NewGeoIPData())
  68. if err != nil {
  69. return nil, errors.Trace(err)
  70. }
  71. if p.IsNil() {
  72. // No tactics are configured; return an empty spec list.
  73. return nil, nil
  74. }
  75. paramSpecs := p.PacketManipulationSpecs(parameters.ServerPacketManipulationSpecs)
  76. specs := make([]*packetman.Spec, len(paramSpecs))
  77. for i, spec := range paramSpecs {
  78. packetmanSpec := packetman.Spec(*spec)
  79. specs[i] = &packetmanSpec
  80. }
  81. return specs, nil
  82. }
  83. func reloadPacketManipulationSpecs(support *SupportServices) error {
  84. specs, err := getPacketManipulationSpecs(support)
  85. if err != nil {
  86. return errors.Trace(err)
  87. }
  88. err = support.PacketManipulator.SetSpecs(specs)
  89. if err != nil {
  90. return errors.Trace(err)
  91. }
  92. return nil
  93. }
  94. func selectPacketManipulationSpec(
  95. support *SupportServices,
  96. protocolPort int,
  97. clientIP net.IP) (string, interface{}, error) {
  98. // First check for replay, then check tactics.
  99. // The intercepted packet source/protocol port is used to determine the
  100. // tunnel protocol name, which is used to lookup first replay and then
  101. // enabled packet manipulation specs in ServerProtocolPacketManipulations.
  102. //
  103. // This assumes that all TunnelProtocolMayUseServerPacketManipulation
  104. // protocols run on distinct ports, which is true when all such protocols run
  105. // over TCP.
  106. targetTunnelProtocol := ""
  107. for tunnelProtocol, port := range support.Config.TunnelProtocolPorts {
  108. if port == protocolPort && protocol.TunnelProtocolMayUseServerPacketManipulation(tunnelProtocol) {
  109. targetTunnelProtocol = tunnelProtocol
  110. break
  111. }
  112. }
  113. if targetTunnelProtocol == "" {
  114. return "", nil, errors.Tracef(
  115. "packet manipulation protocol port not found: %d", protocolPort)
  116. }
  117. geoIPData := support.GeoIPService.LookupIP(clientIP)
  118. specName, doReplay := support.ReplayCache.GetReplayPacketManipulation(
  119. targetTunnelProtocol, geoIPData)
  120. // extraData records the is_server_replay metric.
  121. extraData := doReplay
  122. if doReplay {
  123. return specName, extraData, nil
  124. }
  125. // GeoIP tactics filtering is applied when getting
  126. // ServerPacketManipulationProbability and ServerProtocolPacketManipulations.
  127. //
  128. // When there are multiple enabled specs, one is selected at random.
  129. //
  130. // Specs under the key "All" apply to all protocols. Duplicate specs per
  131. // entry are allowed, enabling weighted selection. If a spec appears in both
  132. // "All" and a specific protocol, the duplicate(s) are retained.
  133. p, err := support.ServerTacticsParametersCache.Get(geoIPData)
  134. if err != nil {
  135. return "", nil, errors.Trace(err)
  136. }
  137. if p.IsNil() {
  138. // No tactics are configured; select no spec.
  139. return "", extraData, nil
  140. }
  141. if !p.WeightedCoinFlip(parameters.ServerPacketManipulationProbability) {
  142. return "", extraData, nil
  143. }
  144. protocolSpecs := p.ProtocolPacketManipulations(
  145. parameters.ServerProtocolPacketManipulations)
  146. // TODO: cache merged per-protocol + "All" lists?
  147. specNames, ok := protocolSpecs[targetTunnelProtocol]
  148. if !ok {
  149. specNames = []string{}
  150. }
  151. allProtocolsSpecNames, ok := protocolSpecs[protocol.TUNNEL_PROTOCOLS_ALL]
  152. if ok {
  153. specNames = append(specNames, allProtocolsSpecNames...)
  154. }
  155. if len(specNames) < 1 {
  156. // Tactics contains no candidate specs for this protocol.
  157. return "", extraData, nil
  158. }
  159. return specNames[prng.Range(0, len(specNames)-1)], extraData, nil
  160. }