trafficRules.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. "errors"
  23. "fmt"
  24. "net"
  25. "time"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. )
  28. const (
  29. DEFAULT_IDLE_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS = 30000
  30. DEFAULT_IDLE_UDP_PORT_FORWARD_TIMEOUT_MILLISECONDS = 30000
  31. DEFAULT_DIAL_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS = 10000
  32. DEFAULT_MAX_TCP_DIALING_PORT_FORWARD_COUNT = 64
  33. DEFAULT_MAX_TCP_PORT_FORWARD_COUNT = 512
  34. DEFAULT_MAX_UDP_PORT_FORWARD_COUNT = 32
  35. DEFAULT_MEEK_RATE_LIMITER_GARBAGE_COLLECTOR_TRIGGER_COUNT = 5000
  36. DEFAULT_MEEK_RATE_LIMITER_REAP_HISTORY_FREQUENCY_SECONDS = 600
  37. )
  38. // TrafficRulesSet represents the various traffic rules to
  39. // apply to Psiphon client tunnels. The Reload function supports
  40. // hot reloading of rules data while the server is running.
  41. //
  42. // For a given client, the traffic rules are determined by starting
  43. // with DefaultRules, then finding the first (if any)
  44. // FilteredTrafficRules match and overriding the defaults with fields
  45. // set in the selected FilteredTrafficRules.
  46. type TrafficRulesSet struct {
  47. common.ReloadableFile
  48. // DefaultRules are the base values to use as defaults for all
  49. // clients.
  50. DefaultRules TrafficRules
  51. // FilteredTrafficRules is an ordered list of filter/rules pairs.
  52. // For each client, the first matching Filter in FilteredTrafficRules
  53. // determines the additional Rules that are selected and applied
  54. // on top of DefaultRules.
  55. FilteredRules []struct {
  56. Filter TrafficRulesFilter
  57. Rules TrafficRules
  58. }
  59. // MeekRateLimiterHistorySize enables the late-stage meek rate limiter and
  60. // sets its history size. The late-stage meek rate limiter acts on client
  61. // IPs relayed in MeekProxyForwardedForHeaders, and so it must wait for
  62. // the HTTP headers to be read. This rate limiter immediately terminates
  63. // any client endpoint request or any request to create a new session, but
  64. // not any meek request for an existing session, if the
  65. // MeekRateLimiterHistorySize requests occur in
  66. // MeekRateLimiterThresholdSeconds. The scope of rate limiting may be
  67. // limited using LimitMeekRateLimiterRegions and LimitMeekRateLimiterISPs.
  68. //
  69. // Hot reloading a new history size will result in existing history being
  70. // truncated.
  71. MeekRateLimiterHistorySize int
  72. // MeekRateLimiterThresholdSeconds is part of the meek rate limiter
  73. // specification and must be set when MeekRateLimiterHistorySize is set.
  74. MeekRateLimiterThresholdSeconds int
  75. // MeekRateLimiterRegions, if set, limits application of the meek
  76. // late-stage rate limiter to clients in the specified list of GeoIP
  77. // countries. When omitted or empty, meek rate limiting, if configured,
  78. // is applied to all client countries.
  79. MeekRateLimiterRegions []string
  80. // MeekRateLimiterISPs, if set, limits application of the meek
  81. // late-stage rate limiter to clients in the specified list of GeoIP
  82. // ISPs. When omitted or empty, meek rate limiting, if configured,
  83. // is applied to all client ISPs.
  84. MeekRateLimiterISPs []string
  85. // MeekRateLimiterGarbageCollectionTriggerCount specifies the number of
  86. // rate limit events after which garbage collection is manually triggered
  87. // in order to reclaim memory used by rate limited and other rejected
  88. // requests.
  89. // A default of 5000 is used when
  90. // MeekRateLimiterGarbageCollectionTriggerCount is 0.
  91. MeekRateLimiterGarbageCollectionTriggerCount int
  92. // MeekRateLimiterReapHistoryFrequencySeconds specifies a schedule for
  93. // reaping old records from the rate limit history.
  94. // A default of 600 is used when
  95. // MeekRateLimiterReapHistoryFrequencySeconds is 0.
  96. MeekRateLimiterReapHistoryFrequencySeconds int
  97. }
  98. // TrafficRulesFilter defines a filter to match against client attributes.
  99. type TrafficRulesFilter struct {
  100. // TunnelProtocols is a list of client tunnel protocols that must be
  101. // in use to match this filter. When omitted or empty, any protocol
  102. // matches.
  103. TunnelProtocols []string
  104. // Regions is a list of countries that the client must geolocate to in
  105. // order to match this filter. When omitted or empty, any client country
  106. // matches.
  107. Regions []string
  108. // ISPs is a list of ISPs that the client must geolocate to in order to
  109. // match this filter. When omitted or empty, any client ISP matches.
  110. ISPs []string
  111. // APIProtocol specifies whether the client must use the SSH
  112. // API protocol (when "ssh") or the web API protocol (when "web").
  113. // When omitted or blank, any API protocol matches.
  114. APIProtocol string
  115. // HandshakeParameters specifies handshake API parameter names and
  116. // a list of values, one of which must be specified to match this
  117. // filter. Only scalar string API parameters may be filtered.
  118. // Values may be patterns containing the '*' wildcard.
  119. HandshakeParameters map[string][]string
  120. // AuthorizedAccessTypes specifies a list of access types, at least
  121. // one of which the client must have presented an active authorization
  122. // for and which must not be revoked.
  123. // AuthorizedAccessTypes is ignored when AuthorizationsRevoked is true.
  124. AuthorizedAccessTypes []string
  125. // AuthorizationsRevoked indicates whether the client's authorizations
  126. // must have been revoked. When true, authorizations must have been
  127. // revoked. When omitted or false, this field is ignored.
  128. AuthorizationsRevoked bool
  129. regionLookup map[string]bool
  130. ispLookup map[string]bool
  131. }
  132. // TrafficRules specify the limits placed on client traffic.
  133. type TrafficRules struct {
  134. // RateLimits specifies data transfer rate limits for the
  135. // client traffic.
  136. RateLimits RateLimits
  137. // DialTCPPortForwardTimeoutMilliseconds is the timeout period
  138. // for dialing TCP port forwards. A value of 0 specifies no timeout.
  139. // When omitted in DefaultRules,
  140. // DEFAULT_TCP_PORT_FORWARD_DIAL_TIMEOUT_MILLISECONDS is used.
  141. DialTCPPortForwardTimeoutMilliseconds *int
  142. // IdleTCPPortForwardTimeoutMilliseconds is the timeout period
  143. // after which idle (no bytes flowing in either direction)
  144. // client TCP port forwards are preemptively closed.
  145. // A value of 0 specifies no idle timeout. When omitted in
  146. // DefaultRules, DEFAULT_IDLE_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS
  147. // is used.
  148. IdleTCPPortForwardTimeoutMilliseconds *int
  149. // IdleUDPPortForwardTimeoutMilliseconds is the timeout period
  150. // after which idle (no bytes flowing in either direction)
  151. // client UDP port forwards are preemptively closed.
  152. // A value of 0 specifies no idle timeout. When omitted in
  153. // DefaultRules, DEFAULT_IDLE_UDP_PORT_FORWARD_TIMEOUT_MILLISECONDS
  154. // is used.
  155. IdleUDPPortForwardTimeoutMilliseconds *int
  156. // MaxTCPDialingPortForwardCount is the maximum number of dialing
  157. // TCP port forwards each client may have open concurrently. When
  158. // persistently at the limit, new TCP port forwards are rejected.
  159. // A value of 0 specifies no maximum. When omitted in
  160. // DefaultRules, DEFAULT_MAX_TCP_DIALING_PORT_FORWARD_COUNT is used.
  161. MaxTCPDialingPortForwardCount *int
  162. // MaxTCPPortForwardCount is the maximum number of established TCP
  163. // port forwards each client may have open concurrently. If at the
  164. // limit when a new TCP port forward is established, the LRU
  165. // established TCP port forward is closed.
  166. // A value of 0 specifies no maximum. When omitted in
  167. // DefaultRules, DEFAULT_MAX_TCP_PORT_FORWARD_COUNT is used.
  168. MaxTCPPortForwardCount *int
  169. // MaxUDPPortForwardCount is the maximum number of UDP port
  170. // forwards each client may have open concurrently. If at the
  171. // limit when a new UDP port forward is created, the LRU
  172. // UDP port forward is closed.
  173. // A value of 0 specifies no maximum. When omitted in
  174. // DefaultRules, DEFAULT_MAX_UDP_PORT_FORWARD_COUNT is used.
  175. MaxUDPPortForwardCount *int
  176. // AllowTCPPorts specifies a whitelist of TCP ports that
  177. // are permitted for port forwarding. When set, only ports
  178. // in the list are accessible to clients.
  179. AllowTCPPorts []int
  180. // AllowUDPPorts specifies a whitelist of UDP ports that
  181. // are permitted for port forwarding. When set, only ports
  182. // in the list are accessible to clients.
  183. AllowUDPPorts []int
  184. // AllowSubnets specifies a list of IP address subnets for
  185. // which all TCP and UDP ports are allowed. This list is
  186. // consulted if a port is disallowed by the AllowTCPPorts
  187. // or AllowUDPPorts configuration. Each entry is a IP subnet
  188. // in CIDR notation.
  189. // Limitation: currently, AllowSubnets only matches port
  190. // forwards where the client sends an IP address. Domain
  191. // names are not resolved before checking AllowSubnets.
  192. AllowSubnets []string
  193. allowTCPPortsLookup map[int]bool
  194. allowUDPPortsLookup map[int]bool
  195. }
  196. // RateLimits is a clone of common.RateLimits with pointers
  197. // to fields to enable distinguishing between zero values and
  198. // omitted values in JSON serialized traffic rules.
  199. // See common.RateLimits for field descriptions.
  200. type RateLimits struct {
  201. ReadUnthrottledBytes *int64
  202. ReadBytesPerSecond *int64
  203. WriteUnthrottledBytes *int64
  204. WriteBytesPerSecond *int64
  205. CloseAfterExhausted *bool
  206. // UnthrottleFirstTunnelOnly specifies whether any
  207. // ReadUnthrottledBytes/WriteUnthrottledBytes apply
  208. // only to the first tunnel in a session.
  209. UnthrottleFirstTunnelOnly *bool
  210. }
  211. // CommonRateLimits converts a RateLimits to a common.RateLimits.
  212. func (rateLimits *RateLimits) CommonRateLimits() common.RateLimits {
  213. return common.RateLimits{
  214. ReadUnthrottledBytes: *rateLimits.ReadUnthrottledBytes,
  215. ReadBytesPerSecond: *rateLimits.ReadBytesPerSecond,
  216. WriteUnthrottledBytes: *rateLimits.WriteUnthrottledBytes,
  217. WriteBytesPerSecond: *rateLimits.WriteBytesPerSecond,
  218. CloseAfterExhausted: *rateLimits.CloseAfterExhausted,
  219. }
  220. }
  221. // NewTrafficRulesSet initializes a TrafficRulesSet with
  222. // the rules data in the specified config file.
  223. func NewTrafficRulesSet(filename string) (*TrafficRulesSet, error) {
  224. set := &TrafficRulesSet{}
  225. set.ReloadableFile = common.NewReloadableFile(
  226. filename,
  227. true,
  228. func(fileContent []byte, _ time.Time) error {
  229. var newSet TrafficRulesSet
  230. err := json.Unmarshal(fileContent, &newSet)
  231. if err != nil {
  232. return common.ContextError(err)
  233. }
  234. err = newSet.Validate()
  235. if err != nil {
  236. return common.ContextError(err)
  237. }
  238. // Modify actual traffic rules only after validation
  239. set.MeekRateLimiterHistorySize = newSet.MeekRateLimiterHistorySize
  240. set.MeekRateLimiterThresholdSeconds = newSet.MeekRateLimiterThresholdSeconds
  241. set.MeekRateLimiterRegions = newSet.MeekRateLimiterRegions
  242. set.MeekRateLimiterISPs = newSet.MeekRateLimiterISPs
  243. set.MeekRateLimiterGarbageCollectionTriggerCount = newSet.MeekRateLimiterGarbageCollectionTriggerCount
  244. set.MeekRateLimiterReapHistoryFrequencySeconds = newSet.MeekRateLimiterReapHistoryFrequencySeconds
  245. set.DefaultRules = newSet.DefaultRules
  246. set.FilteredRules = newSet.FilteredRules
  247. set.initLookups()
  248. return nil
  249. })
  250. _, err := set.Reload()
  251. if err != nil {
  252. return nil, common.ContextError(err)
  253. }
  254. return set, nil
  255. }
  256. // Validate checks for correct input formats in a TrafficRulesSet.
  257. func (set *TrafficRulesSet) Validate() error {
  258. if set.MeekRateLimiterHistorySize < 0 ||
  259. set.MeekRateLimiterThresholdSeconds < 0 ||
  260. set.MeekRateLimiterGarbageCollectionTriggerCount < 0 ||
  261. set.MeekRateLimiterReapHistoryFrequencySeconds < 0 {
  262. return common.ContextError(
  263. errors.New("MeekRateLimiter values must be >= 0"))
  264. }
  265. if set.MeekRateLimiterHistorySize > 0 {
  266. if set.MeekRateLimiterThresholdSeconds <= 0 {
  267. return common.ContextError(
  268. errors.New("MeekRateLimiterThresholdSeconds must be > 0"))
  269. }
  270. }
  271. validateTrafficRules := func(rules *TrafficRules) error {
  272. if (rules.RateLimits.ReadUnthrottledBytes != nil && *rules.RateLimits.ReadUnthrottledBytes < 0) ||
  273. (rules.RateLimits.ReadBytesPerSecond != nil && *rules.RateLimits.ReadBytesPerSecond < 0) ||
  274. (rules.RateLimits.WriteUnthrottledBytes != nil && *rules.RateLimits.WriteUnthrottledBytes < 0) ||
  275. (rules.RateLimits.WriteBytesPerSecond != nil && *rules.RateLimits.WriteBytesPerSecond < 0) ||
  276. (rules.DialTCPPortForwardTimeoutMilliseconds != nil && *rules.DialTCPPortForwardTimeoutMilliseconds < 0) ||
  277. (rules.IdleTCPPortForwardTimeoutMilliseconds != nil && *rules.IdleTCPPortForwardTimeoutMilliseconds < 0) ||
  278. (rules.IdleUDPPortForwardTimeoutMilliseconds != nil && *rules.IdleUDPPortForwardTimeoutMilliseconds < 0) ||
  279. (rules.MaxTCPDialingPortForwardCount != nil && *rules.MaxTCPDialingPortForwardCount < 0) ||
  280. (rules.MaxTCPPortForwardCount != nil && *rules.MaxTCPPortForwardCount < 0) ||
  281. (rules.MaxUDPPortForwardCount != nil && *rules.MaxUDPPortForwardCount < 0) {
  282. return common.ContextError(
  283. errors.New("TrafficRules values must be >= 0"))
  284. }
  285. for _, subnet := range rules.AllowSubnets {
  286. _, _, err := net.ParseCIDR(subnet)
  287. if err != nil {
  288. return common.ContextError(
  289. fmt.Errorf("invalid subnet: %s %s", subnet, err))
  290. }
  291. }
  292. return nil
  293. }
  294. err := validateTrafficRules(&set.DefaultRules)
  295. if err != nil {
  296. return common.ContextError(err)
  297. }
  298. for _, filteredRule := range set.FilteredRules {
  299. for paramName := range filteredRule.Filter.HandshakeParameters {
  300. validParamName := false
  301. for _, paramSpec := range baseRequestParams {
  302. if paramSpec.name == paramName {
  303. validParamName = true
  304. break
  305. }
  306. }
  307. if !validParamName {
  308. return common.ContextError(
  309. fmt.Errorf("invalid parameter name: %s", paramName))
  310. }
  311. }
  312. err := validateTrafficRules(&filteredRule.Rules)
  313. if err != nil {
  314. return common.ContextError(err)
  315. }
  316. }
  317. return nil
  318. }
  319. const stringLookupThreshold = 5
  320. const intLookupThreshold = 10
  321. // initLookups creates map lookups for filters where the number of string/int
  322. // values to compare against exceeds a threshold where benchmarks show maps
  323. // are faster than looping through a string/int slice.
  324. func (set *TrafficRulesSet) initLookups() {
  325. initTrafficRulesLookups := func(rules *TrafficRules) {
  326. if len(rules.AllowTCPPorts) >= intLookupThreshold {
  327. rules.allowTCPPortsLookup = make(map[int]bool)
  328. for _, port := range rules.AllowTCPPorts {
  329. rules.allowTCPPortsLookup[port] = true
  330. }
  331. }
  332. if len(rules.AllowUDPPorts) >= intLookupThreshold {
  333. rules.allowUDPPortsLookup = make(map[int]bool)
  334. for _, port := range rules.AllowUDPPorts {
  335. rules.allowUDPPortsLookup[port] = true
  336. }
  337. }
  338. }
  339. initTrafficRulesFilterLookups := func(filter *TrafficRulesFilter) {
  340. if len(filter.Regions) >= stringLookupThreshold {
  341. filter.regionLookup = make(map[string]bool)
  342. for _, region := range filter.Regions {
  343. filter.regionLookup[region] = true
  344. }
  345. }
  346. if len(filter.ISPs) >= stringLookupThreshold {
  347. filter.ispLookup = make(map[string]bool)
  348. for _, ISP := range filter.ISPs {
  349. filter.ispLookup[ISP] = true
  350. }
  351. }
  352. }
  353. initTrafficRulesLookups(&set.DefaultRules)
  354. for i, _ := range set.FilteredRules {
  355. initTrafficRulesFilterLookups(&set.FilteredRules[i].Filter)
  356. initTrafficRulesLookups(&set.FilteredRules[i].Rules)
  357. }
  358. // TODO: add lookups for MeekRateLimiter?
  359. }
  360. // GetTrafficRules determines the traffic rules for a client based on its attributes.
  361. // For the return value TrafficRules, all pointer and slice fields are initialized,
  362. // so nil checks are not required. The caller must not modify the returned TrafficRules.
  363. func (set *TrafficRulesSet) GetTrafficRules(
  364. isFirstTunnelInSession bool,
  365. tunnelProtocol string,
  366. geoIPData GeoIPData,
  367. state handshakeState) TrafficRules {
  368. set.ReloadableFile.RLock()
  369. defer set.ReloadableFile.RUnlock()
  370. // Start with a copy of the DefaultRules, and then select the first
  371. // matching Rules from FilteredTrafficRules, taking only the explicitly
  372. // specified fields from that Rules.
  373. //
  374. // Notes:
  375. // - Scalar pointers are used in TrafficRules and RateLimits to distinguish between
  376. // omitted fields (in serialized JSON) and default values. For example, if a filtered
  377. // Rules specifies a field value of 0, this will override the default; but if the
  378. // serialized filtered rule omits the field, the default is to be retained.
  379. // - We use shallow copies and slices and scalar pointers are shared between the
  380. // return value TrafficRules, so callers must treat the return value as immutable.
  381. // This also means that these slices and pointers can remain referenced in memory even
  382. // after a hot reload.
  383. trafficRules := set.DefaultRules
  384. // Populate defaults for omitted DefaultRules fields
  385. if trafficRules.RateLimits.ReadUnthrottledBytes == nil {
  386. trafficRules.RateLimits.ReadUnthrottledBytes = new(int64)
  387. }
  388. if trafficRules.RateLimits.ReadBytesPerSecond == nil {
  389. trafficRules.RateLimits.ReadBytesPerSecond = new(int64)
  390. }
  391. if trafficRules.RateLimits.WriteUnthrottledBytes == nil {
  392. trafficRules.RateLimits.WriteUnthrottledBytes = new(int64)
  393. }
  394. if trafficRules.RateLimits.WriteBytesPerSecond == nil {
  395. trafficRules.RateLimits.WriteBytesPerSecond = new(int64)
  396. }
  397. if trafficRules.RateLimits.CloseAfterExhausted == nil {
  398. trafficRules.RateLimits.CloseAfterExhausted = new(bool)
  399. }
  400. if trafficRules.RateLimits.UnthrottleFirstTunnelOnly == nil {
  401. trafficRules.RateLimits.UnthrottleFirstTunnelOnly = new(bool)
  402. }
  403. intPtr := func(i int) *int {
  404. return &i
  405. }
  406. if trafficRules.DialTCPPortForwardTimeoutMilliseconds == nil {
  407. trafficRules.DialTCPPortForwardTimeoutMilliseconds =
  408. intPtr(DEFAULT_DIAL_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS)
  409. }
  410. if trafficRules.IdleTCPPortForwardTimeoutMilliseconds == nil {
  411. trafficRules.IdleTCPPortForwardTimeoutMilliseconds =
  412. intPtr(DEFAULT_IDLE_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS)
  413. }
  414. if trafficRules.IdleUDPPortForwardTimeoutMilliseconds == nil {
  415. trafficRules.IdleUDPPortForwardTimeoutMilliseconds =
  416. intPtr(DEFAULT_IDLE_UDP_PORT_FORWARD_TIMEOUT_MILLISECONDS)
  417. }
  418. if trafficRules.MaxTCPDialingPortForwardCount == nil {
  419. trafficRules.MaxTCPDialingPortForwardCount =
  420. intPtr(DEFAULT_MAX_TCP_DIALING_PORT_FORWARD_COUNT)
  421. }
  422. if trafficRules.MaxTCPPortForwardCount == nil {
  423. trafficRules.MaxTCPPortForwardCount =
  424. intPtr(DEFAULT_MAX_TCP_PORT_FORWARD_COUNT)
  425. }
  426. if trafficRules.MaxUDPPortForwardCount == nil {
  427. trafficRules.MaxUDPPortForwardCount =
  428. intPtr(DEFAULT_MAX_UDP_PORT_FORWARD_COUNT)
  429. }
  430. if trafficRules.AllowTCPPorts == nil {
  431. trafficRules.AllowTCPPorts = make([]int, 0)
  432. }
  433. if trafficRules.AllowUDPPorts == nil {
  434. trafficRules.AllowUDPPorts = make([]int, 0)
  435. }
  436. if trafficRules.AllowSubnets == nil {
  437. trafficRules.AllowSubnets = make([]string, 0)
  438. }
  439. // TODO: faster lookup?
  440. for _, filteredRules := range set.FilteredRules {
  441. log.WithContextFields(LogFields{"filter": filteredRules.Filter}).Debug("filter check")
  442. if len(filteredRules.Filter.TunnelProtocols) > 0 {
  443. if !common.Contains(filteredRules.Filter.TunnelProtocols, tunnelProtocol) {
  444. continue
  445. }
  446. }
  447. if len(filteredRules.Filter.Regions) > 0 {
  448. if filteredRules.Filter.regionLookup != nil {
  449. if !filteredRules.Filter.regionLookup[geoIPData.Country] {
  450. continue
  451. }
  452. } else {
  453. if !common.Contains(filteredRules.Filter.Regions, geoIPData.Country) {
  454. continue
  455. }
  456. }
  457. }
  458. if len(filteredRules.Filter.ISPs) > 0 {
  459. if filteredRules.Filter.ispLookup != nil {
  460. if !filteredRules.Filter.ispLookup[geoIPData.ISP] {
  461. continue
  462. }
  463. } else {
  464. if !common.Contains(filteredRules.Filter.ISPs, geoIPData.ISP) {
  465. continue
  466. }
  467. }
  468. }
  469. if filteredRules.Filter.APIProtocol != "" {
  470. if !state.completed {
  471. continue
  472. }
  473. if state.apiProtocol != filteredRules.Filter.APIProtocol {
  474. continue
  475. }
  476. }
  477. if filteredRules.Filter.HandshakeParameters != nil {
  478. if !state.completed {
  479. continue
  480. }
  481. mismatch := false
  482. for name, values := range filteredRules.Filter.HandshakeParameters {
  483. clientValue, err := getStringRequestParam(state.apiParams, name)
  484. if err != nil || !common.ContainsWildcard(values, clientValue) {
  485. mismatch = true
  486. break
  487. }
  488. }
  489. if mismatch {
  490. continue
  491. }
  492. }
  493. if filteredRules.Filter.AuthorizationsRevoked {
  494. if !state.completed {
  495. continue
  496. }
  497. if !state.authorizationsRevoked {
  498. continue
  499. }
  500. } else if len(filteredRules.Filter.AuthorizedAccessTypes) > 0 {
  501. if !state.completed {
  502. continue
  503. }
  504. if state.authorizationsRevoked {
  505. continue
  506. }
  507. if !common.ContainsAny(filteredRules.Filter.AuthorizedAccessTypes, state.authorizedAccessTypes) {
  508. continue
  509. }
  510. }
  511. log.WithContextFields(LogFields{"filter": filteredRules.Filter}).Debug("filter match")
  512. // This is the first match. Override defaults using provided fields from selected rules, and return result.
  513. if filteredRules.Rules.RateLimits.ReadUnthrottledBytes != nil {
  514. trafficRules.RateLimits.ReadUnthrottledBytes = filteredRules.Rules.RateLimits.ReadUnthrottledBytes
  515. }
  516. if filteredRules.Rules.RateLimits.ReadBytesPerSecond != nil {
  517. trafficRules.RateLimits.ReadBytesPerSecond = filteredRules.Rules.RateLimits.ReadBytesPerSecond
  518. }
  519. if filteredRules.Rules.RateLimits.WriteUnthrottledBytes != nil {
  520. trafficRules.RateLimits.WriteUnthrottledBytes = filteredRules.Rules.RateLimits.WriteUnthrottledBytes
  521. }
  522. if filteredRules.Rules.RateLimits.WriteBytesPerSecond != nil {
  523. trafficRules.RateLimits.WriteBytesPerSecond = filteredRules.Rules.RateLimits.WriteBytesPerSecond
  524. }
  525. if filteredRules.Rules.RateLimits.CloseAfterExhausted != nil {
  526. trafficRules.RateLimits.CloseAfterExhausted = filteredRules.Rules.RateLimits.CloseAfterExhausted
  527. }
  528. if filteredRules.Rules.RateLimits.UnthrottleFirstTunnelOnly != nil {
  529. trafficRules.RateLimits.UnthrottleFirstTunnelOnly = filteredRules.Rules.RateLimits.UnthrottleFirstTunnelOnly
  530. }
  531. if filteredRules.Rules.DialTCPPortForwardTimeoutMilliseconds != nil {
  532. trafficRules.DialTCPPortForwardTimeoutMilliseconds = filteredRules.Rules.DialTCPPortForwardTimeoutMilliseconds
  533. }
  534. if filteredRules.Rules.IdleTCPPortForwardTimeoutMilliseconds != nil {
  535. trafficRules.IdleTCPPortForwardTimeoutMilliseconds = filteredRules.Rules.IdleTCPPortForwardTimeoutMilliseconds
  536. }
  537. if filteredRules.Rules.IdleUDPPortForwardTimeoutMilliseconds != nil {
  538. trafficRules.IdleUDPPortForwardTimeoutMilliseconds = filteredRules.Rules.IdleUDPPortForwardTimeoutMilliseconds
  539. }
  540. if filteredRules.Rules.MaxTCPDialingPortForwardCount != nil {
  541. trafficRules.MaxTCPDialingPortForwardCount = filteredRules.Rules.MaxTCPDialingPortForwardCount
  542. }
  543. if filteredRules.Rules.MaxTCPPortForwardCount != nil {
  544. trafficRules.MaxTCPPortForwardCount = filteredRules.Rules.MaxTCPPortForwardCount
  545. }
  546. if filteredRules.Rules.MaxUDPPortForwardCount != nil {
  547. trafficRules.MaxUDPPortForwardCount = filteredRules.Rules.MaxUDPPortForwardCount
  548. }
  549. if filteredRules.Rules.AllowTCPPorts != nil {
  550. trafficRules.AllowTCPPorts = filteredRules.Rules.AllowTCPPorts
  551. trafficRules.allowTCPPortsLookup = filteredRules.Rules.allowTCPPortsLookup
  552. }
  553. if filteredRules.Rules.AllowUDPPorts != nil {
  554. trafficRules.AllowUDPPorts = filteredRules.Rules.AllowUDPPorts
  555. trafficRules.allowUDPPortsLookup = filteredRules.Rules.allowUDPPortsLookup
  556. }
  557. if filteredRules.Rules.AllowSubnets != nil {
  558. trafficRules.AllowSubnets = filteredRules.Rules.AllowSubnets
  559. }
  560. break
  561. }
  562. if *trafficRules.RateLimits.UnthrottleFirstTunnelOnly && !isFirstTunnelInSession {
  563. trafficRules.RateLimits.ReadUnthrottledBytes = new(int64)
  564. trafficRules.RateLimits.WriteUnthrottledBytes = new(int64)
  565. }
  566. log.WithContextFields(LogFields{"trafficRules": trafficRules}).Debug("selected traffic rules")
  567. return trafficRules
  568. }
  569. func (rules *TrafficRules) AllowTCPPort(remoteIP net.IP, port int) bool {
  570. if len(rules.AllowTCPPorts) == 0 {
  571. return true
  572. }
  573. if rules.allowTCPPortsLookup != nil {
  574. if rules.allowTCPPortsLookup[port] == true {
  575. return true
  576. }
  577. } else {
  578. for _, allowPort := range rules.AllowTCPPorts {
  579. if port == allowPort {
  580. return true
  581. }
  582. }
  583. }
  584. return rules.allowSubnet(remoteIP)
  585. }
  586. func (rules *TrafficRules) AllowUDPPort(remoteIP net.IP, port int) bool {
  587. if len(rules.AllowUDPPorts) == 0 {
  588. return true
  589. }
  590. if rules.allowUDPPortsLookup != nil {
  591. if rules.allowUDPPortsLookup[port] == true {
  592. return true
  593. }
  594. } else {
  595. for _, allowPort := range rules.AllowUDPPorts {
  596. if port == allowPort {
  597. return true
  598. }
  599. }
  600. }
  601. return rules.allowSubnet(remoteIP)
  602. }
  603. func (rules *TrafficRules) allowSubnet(remoteIP net.IP) bool {
  604. for _, subnet := range rules.AllowSubnets {
  605. // Note: ignoring error as config has been validated
  606. _, network, _ := net.ParseCIDR(subnet)
  607. if network.Contains(remoteIP) {
  608. return true
  609. }
  610. }
  611. return false
  612. }
  613. // GetMeekRateLimiterConfig gets a snapshot of the meek rate limiter
  614. // configuration values.
  615. func (set *TrafficRulesSet) GetMeekRateLimiterConfig() (int, int, []string, []string, int, int) {
  616. set.ReloadableFile.RLock()
  617. defer set.ReloadableFile.RUnlock()
  618. GCTriggerCount := set.MeekRateLimiterGarbageCollectionTriggerCount
  619. if GCTriggerCount <= 0 {
  620. GCTriggerCount = DEFAULT_MEEK_RATE_LIMITER_GARBAGE_COLLECTOR_TRIGGER_COUNT
  621. }
  622. reapFrequencySeconds := set.MeekRateLimiterReapHistoryFrequencySeconds
  623. if reapFrequencySeconds <= 0 {
  624. reapFrequencySeconds = DEFAULT_MEEK_RATE_LIMITER_REAP_HISTORY_FREQUENCY_SECONDS
  625. }
  626. return set.MeekRateLimiterHistorySize,
  627. set.MeekRateLimiterThresholdSeconds,
  628. set.MeekRateLimiterRegions,
  629. set.MeekRateLimiterISPs,
  630. GCTriggerCount,
  631. reapFrequencySeconds
  632. }