You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tor.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2019 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package connection_limits
  4. import (
  5. "errors"
  6. "sync"
  7. "time"
  8. )
  9. var (
  10. ErrLimitExceeded = errors.New("too many concurrent connections")
  11. ErrThrottleExceeded = errors.New("too many recent connection attempts")
  12. )
  13. // TorLimiter is a combined limiter and throttler for use on connections
  14. // proxied from a Tor hidden service (so we don't have meaningful IPs,
  15. // a notion of CIDR width, etc.)
  16. type TorLimiter struct {
  17. sync.Mutex
  18. numConnections int
  19. maxConnections int
  20. throttle GenericThrottle
  21. }
  22. func (tl *TorLimiter) Configure(maxConnections int, duration time.Duration, maxConnectionsPerDuration int) {
  23. tl.Lock()
  24. defer tl.Unlock()
  25. tl.maxConnections = maxConnections
  26. tl.throttle.Duration = duration
  27. tl.throttle.Limit = maxConnectionsPerDuration
  28. }
  29. func (tl *TorLimiter) AddClient() error {
  30. tl.Lock()
  31. defer tl.Unlock()
  32. if tl.maxConnections != 0 && tl.maxConnections <= tl.numConnections {
  33. return ErrLimitExceeded
  34. }
  35. throttled, _ := tl.throttle.Touch()
  36. if throttled {
  37. return ErrThrottleExceeded
  38. }
  39. tl.numConnections += 1
  40. return nil
  41. }
  42. func (tl *TorLimiter) RemoveClient() {
  43. tl.Lock()
  44. tl.numConnections -= 1
  45. tl.Unlock()
  46. }