trafficRules.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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 list of TCP ports that are permitted for port
  177. // forwarding. When set, only ports in the list are accessible to clients.
  178. AllowTCPPorts []int
  179. // AllowUDPPorts specifies a list of UDP ports that are permitted for port
  180. // forwarding. When set, only ports in the list are accessible to clients.
  181. AllowUDPPorts []int
  182. // DisallowTCPPorts specifies a list of TCP ports that are not permitted for
  183. // port forwarding. DisallowTCPPorts takes priority over AllowTCPPorts and
  184. // AllowSubnets.
  185. DisallowTCPPorts []int
  186. // DisallowUDPPorts specifies a list of UDP ports that are not permitted for
  187. // port forwarding. DisallowUDPPorts takes priority over AllowUDPPorts and
  188. // AllowSubnets.
  189. DisallowUDPPorts []int
  190. // AllowSubnets specifies a list of IP address subnets for which all TCP and
  191. // UDP ports are allowed. This list is consulted if a port is disallowed by
  192. // the AllowTCPPorts or AllowUDPPorts configuration. Each entry is a IP
  193. // subnet in CIDR notation.
  194. //
  195. // Limitation: currently, AllowSubnets only matches port forwards where the
  196. // client sends an IP address. Domain names are not resolved before checking
  197. // AllowSubnets.
  198. AllowSubnets []string
  199. allowTCPPortsLookup map[int]bool
  200. allowUDPPortsLookup map[int]bool
  201. disallowTCPPortsLookup map[int]bool
  202. disallowUDPPortsLookup map[int]bool
  203. }
  204. // RateLimits is a clone of common.RateLimits with pointers
  205. // to fields to enable distinguishing between zero values and
  206. // omitted values in JSON serialized traffic rules.
  207. // See common.RateLimits for field descriptions.
  208. type RateLimits struct {
  209. ReadUnthrottledBytes *int64
  210. ReadBytesPerSecond *int64
  211. WriteUnthrottledBytes *int64
  212. WriteBytesPerSecond *int64
  213. CloseAfterExhausted *bool
  214. // UnthrottleFirstTunnelOnly specifies whether any
  215. // ReadUnthrottledBytes/WriteUnthrottledBytes apply
  216. // only to the first tunnel in a session.
  217. UnthrottleFirstTunnelOnly *bool
  218. }
  219. // CommonRateLimits converts a RateLimits to a common.RateLimits.
  220. func (rateLimits *RateLimits) CommonRateLimits() common.RateLimits {
  221. return common.RateLimits{
  222. ReadUnthrottledBytes: *rateLimits.ReadUnthrottledBytes,
  223. ReadBytesPerSecond: *rateLimits.ReadBytesPerSecond,
  224. WriteUnthrottledBytes: *rateLimits.WriteUnthrottledBytes,
  225. WriteBytesPerSecond: *rateLimits.WriteBytesPerSecond,
  226. CloseAfterExhausted: *rateLimits.CloseAfterExhausted,
  227. }
  228. }
  229. // NewTrafficRulesSet initializes a TrafficRulesSet with
  230. // the rules data in the specified config file.
  231. func NewTrafficRulesSet(filename string) (*TrafficRulesSet, error) {
  232. set := &TrafficRulesSet{}
  233. set.ReloadableFile = common.NewReloadableFile(
  234. filename,
  235. true,
  236. func(fileContent []byte, _ time.Time) error {
  237. var newSet TrafficRulesSet
  238. err := json.Unmarshal(fileContent, &newSet)
  239. if err != nil {
  240. return common.ContextError(err)
  241. }
  242. err = newSet.Validate()
  243. if err != nil {
  244. return common.ContextError(err)
  245. }
  246. // Modify actual traffic rules only after validation
  247. set.MeekRateLimiterHistorySize = newSet.MeekRateLimiterHistorySize
  248. set.MeekRateLimiterThresholdSeconds = newSet.MeekRateLimiterThresholdSeconds
  249. set.MeekRateLimiterRegions = newSet.MeekRateLimiterRegions
  250. set.MeekRateLimiterISPs = newSet.MeekRateLimiterISPs
  251. set.MeekRateLimiterGarbageCollectionTriggerCount = newSet.MeekRateLimiterGarbageCollectionTriggerCount
  252. set.MeekRateLimiterReapHistoryFrequencySeconds = newSet.MeekRateLimiterReapHistoryFrequencySeconds
  253. set.DefaultRules = newSet.DefaultRules
  254. set.FilteredRules = newSet.FilteredRules
  255. set.initLookups()
  256. return nil
  257. })
  258. _, err := set.Reload()
  259. if err != nil {
  260. return nil, common.ContextError(err)
  261. }
  262. return set, nil
  263. }
  264. // Validate checks for correct input formats in a TrafficRulesSet.
  265. func (set *TrafficRulesSet) Validate() error {
  266. if set.MeekRateLimiterHistorySize < 0 ||
  267. set.MeekRateLimiterThresholdSeconds < 0 ||
  268. set.MeekRateLimiterGarbageCollectionTriggerCount < 0 ||
  269. set.MeekRateLimiterReapHistoryFrequencySeconds < 0 {
  270. return common.ContextError(
  271. errors.New("MeekRateLimiter values must be >= 0"))
  272. }
  273. if set.MeekRateLimiterHistorySize > 0 {
  274. if set.MeekRateLimiterThresholdSeconds <= 0 {
  275. return common.ContextError(
  276. errors.New("MeekRateLimiterThresholdSeconds must be > 0"))
  277. }
  278. }
  279. validateTrafficRules := func(rules *TrafficRules) error {
  280. if (rules.RateLimits.ReadUnthrottledBytes != nil && *rules.RateLimits.ReadUnthrottledBytes < 0) ||
  281. (rules.RateLimits.ReadBytesPerSecond != nil && *rules.RateLimits.ReadBytesPerSecond < 0) ||
  282. (rules.RateLimits.WriteUnthrottledBytes != nil && *rules.RateLimits.WriteUnthrottledBytes < 0) ||
  283. (rules.RateLimits.WriteBytesPerSecond != nil && *rules.RateLimits.WriteBytesPerSecond < 0) ||
  284. (rules.DialTCPPortForwardTimeoutMilliseconds != nil && *rules.DialTCPPortForwardTimeoutMilliseconds < 0) ||
  285. (rules.IdleTCPPortForwardTimeoutMilliseconds != nil && *rules.IdleTCPPortForwardTimeoutMilliseconds < 0) ||
  286. (rules.IdleUDPPortForwardTimeoutMilliseconds != nil && *rules.IdleUDPPortForwardTimeoutMilliseconds < 0) ||
  287. (rules.MaxTCPDialingPortForwardCount != nil && *rules.MaxTCPDialingPortForwardCount < 0) ||
  288. (rules.MaxTCPPortForwardCount != nil && *rules.MaxTCPPortForwardCount < 0) ||
  289. (rules.MaxUDPPortForwardCount != nil && *rules.MaxUDPPortForwardCount < 0) {
  290. return common.ContextError(
  291. errors.New("TrafficRules values must be >= 0"))
  292. }
  293. for _, subnet := range rules.AllowSubnets {
  294. _, _, err := net.ParseCIDR(subnet)
  295. if err != nil {
  296. return common.ContextError(
  297. fmt.Errorf("invalid subnet: %s %s", subnet, err))
  298. }
  299. }
  300. return nil
  301. }
  302. err := validateTrafficRules(&set.DefaultRules)
  303. if err != nil {
  304. return common.ContextError(err)
  305. }
  306. for _, filteredRule := range set.FilteredRules {
  307. for paramName := range filteredRule.Filter.HandshakeParameters {
  308. validParamName := false
  309. for _, paramSpec := range baseRequestParams {
  310. if paramSpec.name == paramName {
  311. validParamName = true
  312. break
  313. }
  314. }
  315. if !validParamName {
  316. return common.ContextError(
  317. fmt.Errorf("invalid parameter name: %s", paramName))
  318. }
  319. }
  320. err := validateTrafficRules(&filteredRule.Rules)
  321. if err != nil {
  322. return common.ContextError(err)
  323. }
  324. }
  325. return nil
  326. }
  327. const stringLookupThreshold = 5
  328. const intLookupThreshold = 10
  329. // initLookups creates map lookups for filters where the number of string/int
  330. // values to compare against exceeds a threshold where benchmarks show maps
  331. // are faster than looping through a string/int slice.
  332. func (set *TrafficRulesSet) initLookups() {
  333. initTrafficRulesLookups := func(rules *TrafficRules) {
  334. if len(rules.AllowTCPPorts) >= intLookupThreshold {
  335. rules.allowTCPPortsLookup = make(map[int]bool)
  336. for _, port := range rules.AllowTCPPorts {
  337. rules.allowTCPPortsLookup[port] = true
  338. }
  339. }
  340. if len(rules.AllowUDPPorts) >= intLookupThreshold {
  341. rules.allowUDPPortsLookup = make(map[int]bool)
  342. for _, port := range rules.AllowUDPPorts {
  343. rules.allowUDPPortsLookup[port] = true
  344. }
  345. }
  346. if len(rules.DisallowTCPPorts) >= intLookupThreshold {
  347. rules.disallowTCPPortsLookup = make(map[int]bool)
  348. for _, port := range rules.DisallowTCPPorts {
  349. rules.disallowTCPPortsLookup[port] = true
  350. }
  351. }
  352. if len(rules.DisallowUDPPorts) >= intLookupThreshold {
  353. rules.disallowUDPPortsLookup = make(map[int]bool)
  354. for _, port := range rules.DisallowUDPPorts {
  355. rules.disallowUDPPortsLookup[port] = true
  356. }
  357. }
  358. }
  359. initTrafficRulesFilterLookups := func(filter *TrafficRulesFilter) {
  360. if len(filter.Regions) >= stringLookupThreshold {
  361. filter.regionLookup = make(map[string]bool)
  362. for _, region := range filter.Regions {
  363. filter.regionLookup[region] = true
  364. }
  365. }
  366. if len(filter.ISPs) >= stringLookupThreshold {
  367. filter.ispLookup = make(map[string]bool)
  368. for _, ISP := range filter.ISPs {
  369. filter.ispLookup[ISP] = true
  370. }
  371. }
  372. }
  373. initTrafficRulesLookups(&set.DefaultRules)
  374. for i, _ := range set.FilteredRules {
  375. initTrafficRulesFilterLookups(&set.FilteredRules[i].Filter)
  376. initTrafficRulesLookups(&set.FilteredRules[i].Rules)
  377. }
  378. // TODO: add lookups for MeekRateLimiter?
  379. }
  380. // GetTrafficRules determines the traffic rules for a client based on its attributes.
  381. // For the return value TrafficRules, all pointer and slice fields are initialized,
  382. // so nil checks are not required. The caller must not modify the returned TrafficRules.
  383. func (set *TrafficRulesSet) GetTrafficRules(
  384. isFirstTunnelInSession bool,
  385. tunnelProtocol string,
  386. geoIPData GeoIPData,
  387. state handshakeState) TrafficRules {
  388. set.ReloadableFile.RLock()
  389. defer set.ReloadableFile.RUnlock()
  390. // Start with a copy of the DefaultRules, and then select the first
  391. // matching Rules from FilteredTrafficRules, taking only the explicitly
  392. // specified fields from that Rules.
  393. //
  394. // Notes:
  395. // - Scalar pointers are used in TrafficRules and RateLimits to distinguish between
  396. // omitted fields (in serialized JSON) and default values. For example, if a filtered
  397. // Rules specifies a field value of 0, this will override the default; but if the
  398. // serialized filtered rule omits the field, the default is to be retained.
  399. // - We use shallow copies and slices and scalar pointers are shared between the
  400. // return value TrafficRules, so callers must treat the return value as immutable.
  401. // This also means that these slices and pointers can remain referenced in memory even
  402. // after a hot reload.
  403. trafficRules := set.DefaultRules
  404. // Populate defaults for omitted DefaultRules fields
  405. if trafficRules.RateLimits.ReadUnthrottledBytes == nil {
  406. trafficRules.RateLimits.ReadUnthrottledBytes = new(int64)
  407. }
  408. if trafficRules.RateLimits.ReadBytesPerSecond == nil {
  409. trafficRules.RateLimits.ReadBytesPerSecond = new(int64)
  410. }
  411. if trafficRules.RateLimits.WriteUnthrottledBytes == nil {
  412. trafficRules.RateLimits.WriteUnthrottledBytes = new(int64)
  413. }
  414. if trafficRules.RateLimits.WriteBytesPerSecond == nil {
  415. trafficRules.RateLimits.WriteBytesPerSecond = new(int64)
  416. }
  417. if trafficRules.RateLimits.CloseAfterExhausted == nil {
  418. trafficRules.RateLimits.CloseAfterExhausted = new(bool)
  419. }
  420. if trafficRules.RateLimits.UnthrottleFirstTunnelOnly == nil {
  421. trafficRules.RateLimits.UnthrottleFirstTunnelOnly = new(bool)
  422. }
  423. intPtr := func(i int) *int {
  424. return &i
  425. }
  426. if trafficRules.DialTCPPortForwardTimeoutMilliseconds == nil {
  427. trafficRules.DialTCPPortForwardTimeoutMilliseconds =
  428. intPtr(DEFAULT_DIAL_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS)
  429. }
  430. if trafficRules.IdleTCPPortForwardTimeoutMilliseconds == nil {
  431. trafficRules.IdleTCPPortForwardTimeoutMilliseconds =
  432. intPtr(DEFAULT_IDLE_TCP_PORT_FORWARD_TIMEOUT_MILLISECONDS)
  433. }
  434. if trafficRules.IdleUDPPortForwardTimeoutMilliseconds == nil {
  435. trafficRules.IdleUDPPortForwardTimeoutMilliseconds =
  436. intPtr(DEFAULT_IDLE_UDP_PORT_FORWARD_TIMEOUT_MILLISECONDS)
  437. }
  438. if trafficRules.MaxTCPDialingPortForwardCount == nil {
  439. trafficRules.MaxTCPDialingPortForwardCount =
  440. intPtr(DEFAULT_MAX_TCP_DIALING_PORT_FORWARD_COUNT)
  441. }
  442. if trafficRules.MaxTCPPortForwardCount == nil {
  443. trafficRules.MaxTCPPortForwardCount =
  444. intPtr(DEFAULT_MAX_TCP_PORT_FORWARD_COUNT)
  445. }
  446. if trafficRules.MaxUDPPortForwardCount == nil {
  447. trafficRules.MaxUDPPortForwardCount =
  448. intPtr(DEFAULT_MAX_UDP_PORT_FORWARD_COUNT)
  449. }
  450. if trafficRules.AllowTCPPorts == nil {
  451. trafficRules.AllowTCPPorts = make([]int, 0)
  452. }
  453. if trafficRules.AllowUDPPorts == nil {
  454. trafficRules.AllowUDPPorts = make([]int, 0)
  455. }
  456. if trafficRules.AllowSubnets == nil {
  457. trafficRules.AllowSubnets = make([]string, 0)
  458. }
  459. // TODO: faster lookup?
  460. for _, filteredRules := range set.FilteredRules {
  461. log.WithContextFields(LogFields{"filter": filteredRules.Filter}).Debug("filter check")
  462. if len(filteredRules.Filter.TunnelProtocols) > 0 {
  463. if !common.Contains(filteredRules.Filter.TunnelProtocols, tunnelProtocol) {
  464. continue
  465. }
  466. }
  467. if len(filteredRules.Filter.Regions) > 0 {
  468. if filteredRules.Filter.regionLookup != nil {
  469. if !filteredRules.Filter.regionLookup[geoIPData.Country] {
  470. continue
  471. }
  472. } else {
  473. if !common.Contains(filteredRules.Filter.Regions, geoIPData.Country) {
  474. continue
  475. }
  476. }
  477. }
  478. if len(filteredRules.Filter.ISPs) > 0 {
  479. if filteredRules.Filter.ispLookup != nil {
  480. if !filteredRules.Filter.ispLookup[geoIPData.ISP] {
  481. continue
  482. }
  483. } else {
  484. if !common.Contains(filteredRules.Filter.ISPs, geoIPData.ISP) {
  485. continue
  486. }
  487. }
  488. }
  489. if filteredRules.Filter.APIProtocol != "" {
  490. if !state.completed {
  491. continue
  492. }
  493. if state.apiProtocol != filteredRules.Filter.APIProtocol {
  494. continue
  495. }
  496. }
  497. if filteredRules.Filter.HandshakeParameters != nil {
  498. if !state.completed {
  499. continue
  500. }
  501. mismatch := false
  502. for name, values := range filteredRules.Filter.HandshakeParameters {
  503. clientValue, err := getStringRequestParam(state.apiParams, name)
  504. if err != nil || !common.ContainsWildcard(values, clientValue) {
  505. mismatch = true
  506. break
  507. }
  508. }
  509. if mismatch {
  510. continue
  511. }
  512. }
  513. if filteredRules.Filter.AuthorizationsRevoked {
  514. if !state.completed {
  515. continue
  516. }
  517. if !state.authorizationsRevoked {
  518. continue
  519. }
  520. } else if len(filteredRules.Filter.AuthorizedAccessTypes) > 0 {
  521. if !state.completed {
  522. continue
  523. }
  524. if state.authorizationsRevoked {
  525. continue
  526. }
  527. if !common.ContainsAny(filteredRules.Filter.AuthorizedAccessTypes, state.authorizedAccessTypes) {
  528. continue
  529. }
  530. }
  531. log.WithContextFields(LogFields{"filter": filteredRules.Filter}).Debug("filter match")
  532. // This is the first match. Override defaults using provided fields from selected rules, and return result.
  533. if filteredRules.Rules.RateLimits.ReadUnthrottledBytes != nil {
  534. trafficRules.RateLimits.ReadUnthrottledBytes = filteredRules.Rules.RateLimits.ReadUnthrottledBytes
  535. }
  536. if filteredRules.Rules.RateLimits.ReadBytesPerSecond != nil {
  537. trafficRules.RateLimits.ReadBytesPerSecond = filteredRules.Rules.RateLimits.ReadBytesPerSecond
  538. }
  539. if filteredRules.Rules.RateLimits.WriteUnthrottledBytes != nil {
  540. trafficRules.RateLimits.WriteUnthrottledBytes = filteredRules.Rules.RateLimits.WriteUnthrottledBytes
  541. }
  542. if filteredRules.Rules.RateLimits.WriteBytesPerSecond != nil {
  543. trafficRules.RateLimits.WriteBytesPerSecond = filteredRules.Rules.RateLimits.WriteBytesPerSecond
  544. }
  545. if filteredRules.Rules.RateLimits.CloseAfterExhausted != nil {
  546. trafficRules.RateLimits.CloseAfterExhausted = filteredRules.Rules.RateLimits.CloseAfterExhausted
  547. }
  548. if filteredRules.Rules.RateLimits.UnthrottleFirstTunnelOnly != nil {
  549. trafficRules.RateLimits.UnthrottleFirstTunnelOnly = filteredRules.Rules.RateLimits.UnthrottleFirstTunnelOnly
  550. }
  551. if filteredRules.Rules.DialTCPPortForwardTimeoutMilliseconds != nil {
  552. trafficRules.DialTCPPortForwardTimeoutMilliseconds = filteredRules.Rules.DialTCPPortForwardTimeoutMilliseconds
  553. }
  554. if filteredRules.Rules.IdleTCPPortForwardTimeoutMilliseconds != nil {
  555. trafficRules.IdleTCPPortForwardTimeoutMilliseconds = filteredRules.Rules.IdleTCPPortForwardTimeoutMilliseconds
  556. }
  557. if filteredRules.Rules.IdleUDPPortForwardTimeoutMilliseconds != nil {
  558. trafficRules.IdleUDPPortForwardTimeoutMilliseconds = filteredRules.Rules.IdleUDPPortForwardTimeoutMilliseconds
  559. }
  560. if filteredRules.Rules.MaxTCPDialingPortForwardCount != nil {
  561. trafficRules.MaxTCPDialingPortForwardCount = filteredRules.Rules.MaxTCPDialingPortForwardCount
  562. }
  563. if filteredRules.Rules.MaxTCPPortForwardCount != nil {
  564. trafficRules.MaxTCPPortForwardCount = filteredRules.Rules.MaxTCPPortForwardCount
  565. }
  566. if filteredRules.Rules.MaxUDPPortForwardCount != nil {
  567. trafficRules.MaxUDPPortForwardCount = filteredRules.Rules.MaxUDPPortForwardCount
  568. }
  569. if filteredRules.Rules.AllowTCPPorts != nil {
  570. trafficRules.AllowTCPPorts = filteredRules.Rules.AllowTCPPorts
  571. trafficRules.allowTCPPortsLookup = filteredRules.Rules.allowTCPPortsLookup
  572. }
  573. if filteredRules.Rules.AllowUDPPorts != nil {
  574. trafficRules.AllowUDPPorts = filteredRules.Rules.AllowUDPPorts
  575. trafficRules.allowUDPPortsLookup = filteredRules.Rules.allowUDPPortsLookup
  576. }
  577. if filteredRules.Rules.DisallowTCPPorts != nil {
  578. trafficRules.DisallowTCPPorts = filteredRules.Rules.DisallowTCPPorts
  579. trafficRules.disallowTCPPortsLookup = filteredRules.Rules.disallowTCPPortsLookup
  580. }
  581. if filteredRules.Rules.DisallowUDPPorts != nil {
  582. trafficRules.DisallowUDPPorts = filteredRules.Rules.DisallowUDPPorts
  583. trafficRules.disallowUDPPortsLookup = filteredRules.Rules.disallowUDPPortsLookup
  584. }
  585. if filteredRules.Rules.AllowSubnets != nil {
  586. trafficRules.AllowSubnets = filteredRules.Rules.AllowSubnets
  587. }
  588. break
  589. }
  590. if *trafficRules.RateLimits.UnthrottleFirstTunnelOnly && !isFirstTunnelInSession {
  591. trafficRules.RateLimits.ReadUnthrottledBytes = new(int64)
  592. trafficRules.RateLimits.WriteUnthrottledBytes = new(int64)
  593. }
  594. log.WithContextFields(LogFields{"trafficRules": trafficRules}).Debug("selected traffic rules")
  595. return trafficRules
  596. }
  597. func (rules *TrafficRules) AllowTCPPort(remoteIP net.IP, port int) bool {
  598. if len(rules.DisallowTCPPorts) > 0 {
  599. if rules.disallowTCPPortsLookup != nil {
  600. if rules.disallowTCPPortsLookup[port] == true {
  601. return false
  602. }
  603. } else {
  604. for _, disallowPort := range rules.DisallowTCPPorts {
  605. if port == disallowPort {
  606. return false
  607. }
  608. }
  609. }
  610. }
  611. if len(rules.AllowTCPPorts) == 0 {
  612. return true
  613. }
  614. if rules.allowTCPPortsLookup != nil {
  615. if rules.allowTCPPortsLookup[port] == true {
  616. return true
  617. }
  618. } else {
  619. for _, allowPort := range rules.AllowTCPPorts {
  620. if port == allowPort {
  621. return true
  622. }
  623. }
  624. }
  625. return rules.allowSubnet(remoteIP)
  626. }
  627. func (rules *TrafficRules) AllowUDPPort(remoteIP net.IP, port int) bool {
  628. if len(rules.DisallowUDPPorts) > 0 {
  629. if rules.disallowUDPPortsLookup != nil {
  630. if rules.disallowUDPPortsLookup[port] == true {
  631. return false
  632. }
  633. } else {
  634. for _, disallowPort := range rules.DisallowUDPPorts {
  635. if port == disallowPort {
  636. return false
  637. }
  638. }
  639. }
  640. }
  641. if len(rules.AllowUDPPorts) == 0 {
  642. return true
  643. }
  644. if rules.allowUDPPortsLookup != nil {
  645. if rules.allowUDPPortsLookup[port] == true {
  646. return true
  647. }
  648. } else {
  649. for _, allowPort := range rules.AllowUDPPorts {
  650. if port == allowPort {
  651. return true
  652. }
  653. }
  654. }
  655. return rules.allowSubnet(remoteIP)
  656. }
  657. func (rules *TrafficRules) allowSubnet(remoteIP net.IP) bool {
  658. for _, subnet := range rules.AllowSubnets {
  659. // Note: ignoring error as config has been validated
  660. _, network, _ := net.ParseCIDR(subnet)
  661. if network.Contains(remoteIP) {
  662. return true
  663. }
  664. }
  665. return false
  666. }
  667. // GetMeekRateLimiterConfig gets a snapshot of the meek rate limiter
  668. // configuration values.
  669. func (set *TrafficRulesSet) GetMeekRateLimiterConfig() (int, int, []string, []string, int, int) {
  670. set.ReloadableFile.RLock()
  671. defer set.ReloadableFile.RUnlock()
  672. GCTriggerCount := set.MeekRateLimiterGarbageCollectionTriggerCount
  673. if GCTriggerCount <= 0 {
  674. GCTriggerCount = DEFAULT_MEEK_RATE_LIMITER_GARBAGE_COLLECTOR_TRIGGER_COUNT
  675. }
  676. reapFrequencySeconds := set.MeekRateLimiterReapHistoryFrequencySeconds
  677. if reapFrequencySeconds <= 0 {
  678. reapFrequencySeconds = DEFAULT_MEEK_RATE_LIMITER_REAP_HISTORY_FREQUENCY_SECONDS
  679. }
  680. return set.MeekRateLimiterHistorySize,
  681. set.MeekRateLimiterThresholdSeconds,
  682. set.MeekRateLimiterRegions,
  683. set.MeekRateLimiterISPs,
  684. GCTriggerCount,
  685. reapFrequencySeconds
  686. }