trafficRules.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2016, 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. "encoding/json"
  22. "io/ioutil"
  23. "strings"
  24. "sync"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon"
  26. )
  27. // TrafficRulesSet represents the various traffic rules to
  28. // apply to Psiphon client tunnels. The Reload function supports
  29. // hot reloading of rules data while the server is running.
  30. type TrafficRulesSet struct {
  31. sync.RWMutex
  32. // DefaultRules specifies the traffic rules to be used when no
  33. // regional-specific rules are set or apply to a particular
  34. // client.
  35. DefaultRules TrafficRules
  36. // RegionalRules specifies the traffic rules for particular client
  37. // regions (countries) as determined by GeoIP lookup of the client
  38. // IP address. The key for each regional traffic rule entry is one
  39. // or more space delimited ISO 3166-1 alpha-2 country codes.
  40. RegionalRules map[string]TrafficRules
  41. }
  42. // RateLimits specify the rate limits for tunneled data transfer
  43. // between an individual client and the server.
  44. type RateLimits struct {
  45. // DownstreamUnlimitedBytes specifies the number of downstream
  46. // bytes to transfer, approximately, before starting rate
  47. // limiting.
  48. DownstreamUnlimitedBytes int64
  49. // DownstreamBytesPerSecond specifies a rate limit for downstream
  50. // data transfer. The default, 0, is no limit.
  51. DownstreamBytesPerSecond int
  52. // UpstreamUnlimitedBytes specifies the number of upstream
  53. // bytes to transfer, approximately, before starting rate
  54. // limiting.
  55. UpstreamUnlimitedBytes int64
  56. // UpstreamBytesPerSecond specifies a rate limit for upstream
  57. // data transfer. The default, 0, is no limit.
  58. UpstreamBytesPerSecond int
  59. }
  60. // TrafficRules specify the limits placed on client traffic.
  61. type TrafficRules struct {
  62. // DefaultLimits are the rate limits to be applied when
  63. // no protocol-specific rates are set.
  64. DefaultLimits RateLimits
  65. // ProtocolLimits specifies the rate limits for particular
  66. // tunnel protocols. The key for each rate limit entry is one
  67. // or more space delimited Psiphon tunnel protocol names. Valid
  68. // tunnel protocols includes the same list as for
  69. // TunnelProtocolPorts.
  70. ProtocolLimits map[string]RateLimits
  71. // IdleTCPPortForwardTimeoutMilliseconds is the timeout period
  72. // after which idle (no bytes flowing in either direction)
  73. // client TCP port forwards are preemptively closed.
  74. // The default, 0, is no idle timeout.
  75. IdleTCPPortForwardTimeoutMilliseconds int
  76. // IdleUDPPortForwardTimeoutMilliseconds is the timeout period
  77. // after which idle (no bytes flowing in either direction)
  78. // client UDP port forwards are preemptively closed.
  79. // The default, 0, is no idle timeout.
  80. IdleUDPPortForwardTimeoutMilliseconds int
  81. // MaxTCPPortForwardCount is the maximum number of TCP port
  82. // forwards each client may have open concurrently.
  83. // The default, 0, is no maximum.
  84. MaxTCPPortForwardCount int
  85. // MaxUDPPortForwardCount is the maximum number of UDP port
  86. // forwards each client may have open concurrently.
  87. // The default, 0, is no maximum.
  88. MaxUDPPortForwardCount int
  89. // AllowTCPPorts specifies a whitelist of TCP ports that
  90. // are permitted for port forwarding. When set, only ports
  91. // in the list are accessible to clients.
  92. AllowTCPPorts []int
  93. // AllowUDPPorts specifies a whitelist of UDP ports that
  94. // are permitted for port forwarding. When set, only ports
  95. // in the list are accessible to clients.
  96. AllowUDPPorts []int
  97. // DenyTCPPorts specifies a blacklist of TCP ports that
  98. // are not permitted for port forwarding. When set, the
  99. // ports in the list are inaccessible to clients.
  100. DenyTCPPorts []int
  101. // DenyUDPPorts specifies a blacklist of UDP ports that
  102. // are not permitted for port forwarding. When set, the
  103. // ports in the list are inaccessible to clients.
  104. DenyUDPPorts []int
  105. }
  106. // NewTrafficRulesSet initializes a TrafficRulesSet with
  107. // the rules data in the specified config file.
  108. func NewTrafficRulesSet(ruleSetFilename string) (*TrafficRulesSet, error) {
  109. set := &TrafficRulesSet{}
  110. return set, set.Reload(ruleSetFilename)
  111. }
  112. // Reload [re]initializes the TrafficRulesSet with the rules data
  113. // in the specified file. This function obtains a write lock on
  114. // the database, blocking all readers. When Reload fails, the previous
  115. // state is retained.
  116. func (set *TrafficRulesSet) Reload(ruleSetFilename string) error {
  117. set.Lock()
  118. defer set.Unlock()
  119. if ruleSetFilename == "" {
  120. // No traffic rules filename in the config
  121. return nil
  122. }
  123. configJSON, err := ioutil.ReadFile(ruleSetFilename)
  124. if err != nil {
  125. return psiphon.ContextError(err)
  126. }
  127. var newSet TrafficRulesSet
  128. err = json.Unmarshal(configJSON, &newSet)
  129. if err != nil {
  130. return psiphon.ContextError(err)
  131. }
  132. set.DefaultRules = newSet.DefaultRules
  133. set.RegionalRules = newSet.RegionalRules
  134. return nil
  135. }
  136. // GetTrafficRules looks up the traffic rules for the specified country. If there
  137. // are no regional TrafficRules for the country, default TrafficRules are returned.
  138. func (set *TrafficRulesSet) GetTrafficRules(clientCountryCode string) TrafficRules {
  139. set.RLock()
  140. defer set.RUnlock()
  141. // TODO: faster lookup?
  142. for countryCodes, trafficRules := range set.RegionalRules {
  143. for _, countryCode := range strings.Split(countryCodes, " ") {
  144. if countryCode == clientCountryCode {
  145. return trafficRules
  146. }
  147. }
  148. }
  149. return set.DefaultRules
  150. }
  151. // GetRateLimits looks up the rate limits for the specified tunnel protocol.
  152. // If there are no specific RateLimits for the protocol, default RateLimits are
  153. // returned.
  154. func (rules *TrafficRules) GetRateLimits(clientTunnelProtocol string) RateLimits {
  155. // TODO: faster lookup?
  156. for tunnelProtocols, rateLimits := range rules.ProtocolLimits {
  157. for _, tunnelProtocol := range strings.Split(tunnelProtocols, " ") {
  158. if tunnelProtocol == clientTunnelProtocol {
  159. return rateLimits
  160. }
  161. }
  162. }
  163. return rules.DefaultLimits
  164. }