trafficRules.go 27 KB

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