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.

idletimer.go 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright (c) 2017 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "fmt"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "github.com/goshuirc/irc-go/ircfmt"
  10. "github.com/oragono/oragono/irc/caps"
  11. )
  12. const (
  13. // RegisterTimeout is how long clients have to register before we disconnect them
  14. RegisterTimeout = time.Minute
  15. // DefaultIdleTimeout is how long without traffic before we send the client a PING
  16. DefaultIdleTimeout = time.Minute + 30*time.Second
  17. // For Tor clients, we send a PING at least every 30 seconds, as a workaround for this bug
  18. // (single-onion circuits will close unless the client sends data once every 60 seconds):
  19. // https://bugs.torproject.org/29665
  20. TorIdleTimeout = time.Second * 30
  21. // This is how long a client gets without sending any message, including the PONG to our
  22. // PING, before we disconnect them:
  23. DefaultTotalTimeout = 2*time.Minute + 30*time.Second
  24. // Resumeable clients (clients who have negotiated caps.Resume) get longer:
  25. ResumeableTotalTimeout = 3*time.Minute + 30*time.Second
  26. )
  27. // client idleness state machine
  28. type TimerState uint
  29. const (
  30. TimerUnregistered TimerState = iota // client is unregistered
  31. TimerActive // client is actively sending commands
  32. TimerIdle // client is idle, we sent PING and are waiting for PONG
  33. TimerDead // client was terminated
  34. )
  35. type IdleTimer struct {
  36. sync.Mutex // tier 1
  37. // immutable after construction
  38. registerTimeout time.Duration
  39. client *Client
  40. // mutable
  41. idleTimeout time.Duration
  42. quitTimeout time.Duration
  43. state TimerState
  44. timer *time.Timer
  45. }
  46. // Initialize sets up an IdleTimer and starts counting idle time;
  47. // if there is no activity from the client, it will eventually be stopped.
  48. func (it *IdleTimer) Initialize(client *Client) {
  49. it.client = client
  50. it.registerTimeout = RegisterTimeout
  51. it.idleTimeout, it.quitTimeout = it.recomputeDurations()
  52. it.Lock()
  53. defer it.Unlock()
  54. it.state = TimerUnregistered
  55. it.resetTimeout()
  56. }
  57. // recomputeDurations recomputes the idle and quit durations, given the client's caps.
  58. func (it *IdleTimer) recomputeDurations() (idleTimeout, quitTimeout time.Duration) {
  59. totalTimeout := DefaultTotalTimeout
  60. // if they have the resume cap, wait longer before pinging them out
  61. // to give them a chance to resume their connection
  62. if it.client.capabilities.Has(caps.Resume) {
  63. totalTimeout = ResumeableTotalTimeout
  64. }
  65. idleTimeout = DefaultIdleTimeout
  66. if it.client.isTor {
  67. idleTimeout = TorIdleTimeout
  68. }
  69. quitTimeout = totalTimeout - idleTimeout
  70. return
  71. }
  72. func (it *IdleTimer) Touch() {
  73. idleTimeout, quitTimeout := it.recomputeDurations()
  74. it.Lock()
  75. defer it.Unlock()
  76. it.idleTimeout, it.quitTimeout = idleTimeout, quitTimeout
  77. // a touch transitions TimerUnregistered or TimerIdle into TimerActive
  78. if it.state != TimerDead {
  79. it.state = TimerActive
  80. it.resetTimeout()
  81. }
  82. }
  83. func (it *IdleTimer) processTimeout() {
  84. idleTimeout, quitTimeout := it.recomputeDurations()
  85. var previousState TimerState
  86. func() {
  87. it.Lock()
  88. defer it.Unlock()
  89. it.idleTimeout, it.quitTimeout = idleTimeout, quitTimeout
  90. previousState = it.state
  91. // TimerActive transitions to TimerIdle, all others to TimerDead
  92. if it.state == TimerActive {
  93. // send them a ping, give them time to respond
  94. it.state = TimerIdle
  95. it.resetTimeout()
  96. } else {
  97. it.state = TimerDead
  98. }
  99. }()
  100. if previousState == TimerActive {
  101. it.client.Ping()
  102. } else {
  103. it.client.Quit(it.quitMessage(previousState))
  104. it.client.destroy(false)
  105. }
  106. }
  107. // Stop stops counting idle time.
  108. func (it *IdleTimer) Stop() {
  109. if it == nil {
  110. return
  111. }
  112. it.Lock()
  113. defer it.Unlock()
  114. it.state = TimerDead
  115. it.resetTimeout()
  116. }
  117. func (it *IdleTimer) resetTimeout() {
  118. if it.timer != nil {
  119. it.timer.Stop()
  120. }
  121. var nextTimeout time.Duration
  122. switch it.state {
  123. case TimerUnregistered:
  124. nextTimeout = it.registerTimeout
  125. case TimerActive:
  126. nextTimeout = it.idleTimeout
  127. case TimerIdle:
  128. nextTimeout = it.quitTimeout
  129. case TimerDead:
  130. return
  131. }
  132. it.timer = time.AfterFunc(nextTimeout, it.processTimeout)
  133. }
  134. func (it *IdleTimer) quitMessage(state TimerState) string {
  135. switch state {
  136. case TimerUnregistered:
  137. return fmt.Sprintf("Registration timeout: %v", it.registerTimeout)
  138. case TimerIdle:
  139. // how many seconds before registered clients are timed out (IdleTimeout plus QuitTimeout).
  140. it.Lock()
  141. defer it.Unlock()
  142. return fmt.Sprintf("Ping timeout: %v", (it.idleTimeout + it.quitTimeout))
  143. default:
  144. // shouldn't happen
  145. return ""
  146. }
  147. }
  148. // NickTimer manages timing out of clients who are squatting reserved nicks
  149. type NickTimer struct {
  150. sync.Mutex // tier 1
  151. // immutable after construction
  152. client *Client
  153. // mutable
  154. nick string
  155. accountForNick string
  156. account string
  157. timeout time.Duration
  158. timer *time.Timer
  159. enabled uint32
  160. }
  161. // Initialize sets up a NickTimer, based on server config settings.
  162. func (nt *NickTimer) Initialize(client *Client) {
  163. if nt.client == nil {
  164. nt.client = client // placate the race detector
  165. }
  166. config := &client.server.Config().Accounts.NickReservation
  167. enabled := config.Enabled && (config.Method == NickReservationWithTimeout || config.AllowCustomEnforcement)
  168. nt.Lock()
  169. defer nt.Unlock()
  170. nt.timeout = config.RenameTimeout
  171. if enabled {
  172. atomic.StoreUint32(&nt.enabled, 1)
  173. } else {
  174. nt.stopInternal()
  175. }
  176. }
  177. func (nt *NickTimer) Enabled() bool {
  178. return atomic.LoadUint32(&nt.enabled) == 1
  179. }
  180. func (nt *NickTimer) Timeout() (timeout time.Duration) {
  181. nt.Lock()
  182. timeout = nt.timeout
  183. nt.Unlock()
  184. return
  185. }
  186. // Touch records a nick change and updates the timer as necessary
  187. func (nt *NickTimer) Touch() {
  188. if !nt.Enabled() {
  189. return
  190. }
  191. cfnick, skeleton := nt.client.uniqueIdentifiers()
  192. account := nt.client.Account()
  193. accountForNick, method := nt.client.server.accounts.EnforcementStatus(cfnick, skeleton)
  194. enforceTimeout := method == NickReservationWithTimeout
  195. var shouldWarn, shouldRename bool
  196. func() {
  197. nt.Lock()
  198. defer nt.Unlock()
  199. // the timer will not reset as long as the squatter is targeting the same account
  200. accountChanged := accountForNick != nt.accountForNick
  201. // change state
  202. nt.nick = cfnick
  203. nt.account = account
  204. nt.accountForNick = accountForNick
  205. delinquent := accountForNick != "" && accountForNick != account
  206. if nt.timer != nil && (!enforceTimeout || !delinquent || accountChanged) {
  207. nt.timer.Stop()
  208. nt.timer = nil
  209. }
  210. if enforceTimeout && delinquent && (accountChanged || nt.timer == nil) {
  211. nt.timer = time.AfterFunc(nt.timeout, nt.processTimeout)
  212. shouldWarn = true
  213. } else if method == NickReservationStrict && delinquent {
  214. shouldRename = true // this can happen if reservation was enabled by rehash
  215. }
  216. }()
  217. if shouldWarn {
  218. nt.client.Send(nil, "NickServ", "NOTICE", nt.client.Nick(), fmt.Sprintf(ircfmt.Unescape(nt.client.t(nsTimeoutNotice)), nt.Timeout()))
  219. } else if shouldRename {
  220. nt.client.Notice(nt.client.t("Nickname is reserved by a different account"))
  221. nt.client.server.RandomlyRename(nt.client)
  222. }
  223. }
  224. // Stop stops counting time and cleans up the timer
  225. func (nt *NickTimer) Stop() {
  226. nt.Lock()
  227. defer nt.Unlock()
  228. nt.stopInternal()
  229. }
  230. func (nt *NickTimer) stopInternal() {
  231. if nt.timer != nil {
  232. nt.timer.Stop()
  233. nt.timer = nil
  234. }
  235. atomic.StoreUint32(&nt.enabled, 0)
  236. }
  237. func (nt *NickTimer) processTimeout() {
  238. baseMsg := "Nick is reserved and authentication timeout expired: %v"
  239. nt.client.Notice(fmt.Sprintf(nt.client.t(baseMsg), nt.Timeout()))
  240. nt.client.server.RandomlyRename(nt.client)
  241. }