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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. session *Session
  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(session *Session) {
  49. it.session = session
  50. it.registerTimeout = RegisterTimeout
  51. it.idleTimeout, it.quitTimeout = it.recomputeDurations()
  52. registered := session.client.Registered()
  53. it.Lock()
  54. defer it.Unlock()
  55. if registered {
  56. it.state = TimerActive
  57. } else {
  58. it.state = TimerUnregistered
  59. }
  60. it.resetTimeout()
  61. }
  62. // recomputeDurations recomputes the idle and quit durations, given the client's caps.
  63. func (it *IdleTimer) recomputeDurations() (idleTimeout, quitTimeout time.Duration) {
  64. totalTimeout := DefaultTotalTimeout
  65. // if they have the resume cap, wait longer before pinging them out
  66. // to give them a chance to resume their connection
  67. if it.session.capabilities.Has(caps.Resume) {
  68. totalTimeout = ResumeableTotalTimeout
  69. }
  70. idleTimeout = DefaultIdleTimeout
  71. if it.session.client.isTor {
  72. idleTimeout = TorIdleTimeout
  73. }
  74. quitTimeout = totalTimeout - idleTimeout
  75. return
  76. }
  77. func (it *IdleTimer) Touch() {
  78. idleTimeout, quitTimeout := it.recomputeDurations()
  79. it.Lock()
  80. defer it.Unlock()
  81. it.idleTimeout, it.quitTimeout = idleTimeout, quitTimeout
  82. // a touch transitions TimerUnregistered or TimerIdle into TimerActive
  83. if it.state != TimerDead {
  84. it.state = TimerActive
  85. it.resetTimeout()
  86. }
  87. }
  88. func (it *IdleTimer) processTimeout() {
  89. idleTimeout, quitTimeout := it.recomputeDurations()
  90. var previousState TimerState
  91. func() {
  92. it.Lock()
  93. defer it.Unlock()
  94. it.idleTimeout, it.quitTimeout = idleTimeout, quitTimeout
  95. previousState = it.state
  96. // TimerActive transitions to TimerIdle, all others to TimerDead
  97. if it.state == TimerActive {
  98. // send them a ping, give them time to respond
  99. it.state = TimerIdle
  100. it.resetTimeout()
  101. } else {
  102. it.state = TimerDead
  103. }
  104. }()
  105. if previousState == TimerActive {
  106. it.session.Ping()
  107. } else {
  108. it.session.client.Quit(it.quitMessage(previousState), it.session)
  109. it.session.client.destroy(false, it.session)
  110. }
  111. }
  112. // Stop stops counting idle time.
  113. func (it *IdleTimer) Stop() {
  114. if it == nil {
  115. return
  116. }
  117. it.Lock()
  118. defer it.Unlock()
  119. it.state = TimerDead
  120. it.resetTimeout()
  121. }
  122. func (it *IdleTimer) resetTimeout() {
  123. if it.timer != nil {
  124. it.timer.Stop()
  125. }
  126. var nextTimeout time.Duration
  127. switch it.state {
  128. case TimerUnregistered:
  129. nextTimeout = it.registerTimeout
  130. case TimerActive:
  131. nextTimeout = it.idleTimeout
  132. case TimerIdle:
  133. nextTimeout = it.quitTimeout
  134. case TimerDead:
  135. return
  136. }
  137. it.timer = time.AfterFunc(nextTimeout, it.processTimeout)
  138. }
  139. func (it *IdleTimer) quitMessage(state TimerState) string {
  140. switch state {
  141. case TimerUnregistered:
  142. return fmt.Sprintf("Registration timeout: %v", it.registerTimeout)
  143. case TimerIdle:
  144. // how many seconds before registered clients are timed out (IdleTimeout plus QuitTimeout).
  145. it.Lock()
  146. defer it.Unlock()
  147. return fmt.Sprintf("Ping timeout: %v", (it.idleTimeout + it.quitTimeout))
  148. default:
  149. // shouldn't happen
  150. return ""
  151. }
  152. }
  153. // NickTimer manages timing out of clients who are squatting reserved nicks
  154. type NickTimer struct {
  155. sync.Mutex // tier 1
  156. // immutable after construction
  157. client *Client
  158. // mutable
  159. nick string
  160. accountForNick string
  161. account string
  162. timeout time.Duration
  163. timer *time.Timer
  164. enabled uint32
  165. }
  166. // Initialize sets up a NickTimer, based on server config settings.
  167. func (nt *NickTimer) Initialize(client *Client) {
  168. if nt.client == nil {
  169. nt.client = client // placate the race detector
  170. }
  171. config := &client.server.Config().Accounts.NickReservation
  172. enabled := config.Enabled && (config.Method == NickReservationWithTimeout || config.AllowCustomEnforcement)
  173. nt.Lock()
  174. defer nt.Unlock()
  175. nt.timeout = config.RenameTimeout
  176. if enabled {
  177. atomic.StoreUint32(&nt.enabled, 1)
  178. } else {
  179. nt.stopInternal()
  180. }
  181. }
  182. func (nt *NickTimer) Enabled() bool {
  183. return atomic.LoadUint32(&nt.enabled) == 1
  184. }
  185. func (nt *NickTimer) Timeout() (timeout time.Duration) {
  186. nt.Lock()
  187. timeout = nt.timeout
  188. nt.Unlock()
  189. return
  190. }
  191. // Touch records a nick change and updates the timer as necessary
  192. func (nt *NickTimer) Touch(rb *ResponseBuffer) {
  193. if !nt.Enabled() {
  194. return
  195. }
  196. var session *Session
  197. if rb != nil {
  198. session = rb.session
  199. }
  200. cfnick, skeleton := nt.client.uniqueIdentifiers()
  201. account := nt.client.Account()
  202. accountForNick, method := nt.client.server.accounts.EnforcementStatus(cfnick, skeleton)
  203. enforceTimeout := method == NickReservationWithTimeout
  204. var shouldWarn, shouldRename bool
  205. func() {
  206. nt.Lock()
  207. defer nt.Unlock()
  208. // the timer will not reset as long as the squatter is targeting the same account
  209. accountChanged := accountForNick != nt.accountForNick
  210. // change state
  211. nt.nick = cfnick
  212. nt.account = account
  213. nt.accountForNick = accountForNick
  214. delinquent := accountForNick != "" && accountForNick != account
  215. if nt.timer != nil && (!enforceTimeout || !delinquent || accountChanged) {
  216. nt.timer.Stop()
  217. nt.timer = nil
  218. }
  219. if enforceTimeout && delinquent && (accountChanged || nt.timer == nil) {
  220. nt.timer = time.AfterFunc(nt.timeout, nt.processTimeout)
  221. shouldWarn = true
  222. } else if method == NickReservationStrict && delinquent {
  223. shouldRename = true // this can happen if reservation was enabled by rehash
  224. }
  225. }()
  226. if shouldWarn {
  227. tnick := nt.client.Nick()
  228. message := fmt.Sprintf(ircfmt.Unescape(nt.client.t(nsTimeoutNotice)), nt.Timeout())
  229. // #449
  230. for _, mSession := range nt.client.Sessions() {
  231. if mSession == session {
  232. rb.Add(nil, "NickServ", "NOTICE", tnick, message)
  233. } else {
  234. mSession.Send(nil, "NickServ", "NOTICE", tnick, message)
  235. }
  236. }
  237. } else if shouldRename {
  238. nt.client.Notice(nt.client.t("Nickname is reserved by a different account"))
  239. nt.client.server.RandomlyRename(nt.client)
  240. }
  241. }
  242. // Stop stops counting time and cleans up the timer
  243. func (nt *NickTimer) Stop() {
  244. nt.Lock()
  245. defer nt.Unlock()
  246. nt.stopInternal()
  247. }
  248. func (nt *NickTimer) stopInternal() {
  249. if nt.timer != nil {
  250. nt.timer.Stop()
  251. nt.timer = nil
  252. }
  253. atomic.StoreUint32(&nt.enabled, 0)
  254. }
  255. func (nt *NickTimer) processTimeout() {
  256. baseMsg := "Nick is reserved and authentication timeout expired: %v"
  257. nt.client.Notice(fmt.Sprintf(nt.client.t(baseMsg), nt.Timeout()))
  258. nt.client.server.RandomlyRename(nt.client)
  259. }