permission.go 991 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package allocation
  2. import (
  3. "net"
  4. "time"
  5. "github.com/pion/logging"
  6. )
  7. const permissionTimeout = time.Duration(5) * time.Minute
  8. // Permission represents a TURN permission. TURN permissions mimic the address-restricted
  9. // filtering mechanism of NATs that comply with [RFC4787].
  10. // https://tools.ietf.org/html/rfc5766#section-2.3
  11. type Permission struct {
  12. Addr net.Addr
  13. allocation *Allocation
  14. lifetimeTimer *time.Timer
  15. log logging.LeveledLogger
  16. }
  17. // NewPermission create a new Permission
  18. func NewPermission(addr net.Addr, log logging.LeveledLogger) *Permission {
  19. return &Permission{
  20. Addr: addr,
  21. log: log,
  22. }
  23. }
  24. func (p *Permission) start(lifetime time.Duration) {
  25. p.lifetimeTimer = time.AfterFunc(lifetime, func() {
  26. p.allocation.RemovePermission(p.Addr)
  27. })
  28. }
  29. func (p *Permission) refresh(lifetime time.Duration) {
  30. if !p.lifetimeTimer.Reset(lifetime) {
  31. p.log.Errorf("Failed to reset permission timer for %v %v", p.Addr, p.allocation.fiveTuple)
  32. }
  33. }