session.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package relay
  2. import (
  3. "errors"
  4. "net"
  5. "net/netip"
  6. "slices"
  7. "sync"
  8. "time"
  9. "github.com/mk6i/open-oscar-server/state"
  10. )
  11. var (
  12. errNoSession = errors.New("no session for cookie")
  13. errSessionFull = errors.New("session already has two sides")
  14. )
  15. // session is one relayed connection, identified by the rendezvous cookie.
  16. type session struct {
  17. cookie [8]byte
  18. mu sync.Mutex
  19. first net.Conn
  20. paired bool
  21. abandoned bool
  22. accepted bool
  23. // arrived closes when the second side turns up.
  24. arrived chan struct{}
  25. // released closes when the waiting side has stopped watching its
  26. // connection, so the second side can use it.
  27. released chan struct{}
  28. // finished closes when the relay ends.
  29. finished chan struct{}
  30. // cancelled closes when a client cancels the rendezvous.
  31. cancelled chan struct{}
  32. offerer *state.SessionInstance
  33. offererIP netip.Addr
  34. recipient state.IdentScreenName
  35. // recipientIPs holds the address of each of the recipient's instances,
  36. // since any of them can accept.
  37. recipientIPs []netip.Addr
  38. capability [16]byte
  39. expiry *time.Timer
  40. created time.Time
  41. }
  42. // awaits reports whether the next side to arrive dials from ip. With ready
  43. // set, it also has to have been told to dial: the recipient by accepting, the
  44. // offerer by the counter-offer that follows the recipient arriving.
  45. func (s *session) awaits(ip netip.Addr, ready bool) bool {
  46. s.mu.Lock()
  47. defer s.mu.Unlock()
  48. switch {
  49. case s.abandoned || s.paired:
  50. return false
  51. case s.first == nil:
  52. return slices.Contains(s.recipientIPs, ip) && (s.accepted || !ready)
  53. }
  54. return s.offererIP == ip
  55. }
  56. func (s *session) markAccepted() {
  57. s.mu.Lock()
  58. defer s.mu.Unlock()
  59. s.accepted = true
  60. }
  61. // claim records a side arriving. It reports whether this side is the first.
  62. func (s *session) claim(conn net.Conn) (first bool, err error) {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. switch {
  66. case s.abandoned:
  67. return false, errNoSession
  68. case s.first == nil:
  69. s.first = conn
  70. return true, nil
  71. case !s.paired:
  72. s.paired = true
  73. close(s.arrived)
  74. return false, nil
  75. }
  76. return false, errSessionFull
  77. }
  78. // abandon records that the first side has gone. A second side already paired
  79. // is released, and its first write fails.
  80. func (s *session) abandon() {
  81. s.mu.Lock()
  82. defer s.mu.Unlock()
  83. s.abandoned = true
  84. if s.paired {
  85. close(s.released)
  86. }
  87. }
  88. // registry holds the sessions waiting for or running a relay.
  89. type registry struct {
  90. mu sync.Mutex
  91. sessions map[[8]byte]*session
  92. }
  93. func newRegistry() *registry {
  94. return &registry{sessions: make(map[[8]byte]*session)}
  95. }
  96. // reserve opens a session for an offer. It returns false if the cookie is
  97. // already in use.
  98. func (r *registry) reserve(sess *session, ttl time.Duration) bool {
  99. r.mu.Lock()
  100. defer r.mu.Unlock()
  101. if _, ok := r.sessions[sess.cookie]; ok {
  102. return false
  103. }
  104. sess.arrived = make(chan struct{})
  105. sess.released = make(chan struct{})
  106. sess.finished = make(chan struct{})
  107. sess.cancelled = make(chan struct{})
  108. sess.created = time.Now()
  109. sess.expiry = time.AfterFunc(ttl, func() { r.expire(sess) })
  110. r.sessions[sess.cookie] = sess
  111. return true
  112. }
  113. // find returns the oldest session whose next side dials from ip.
  114. func (r *registry) find(ip netip.Addr, ready bool) *session {
  115. r.mu.Lock()
  116. defer r.mu.Unlock()
  117. var found *session
  118. for _, sess := range r.sessions {
  119. if sess.awaits(ip, ready) && (found == nil || sess.created.Before(found.created)) {
  120. found = sess
  121. }
  122. }
  123. return found
  124. }
  125. func (r *registry) expire(sess *session) {
  126. sess.mu.Lock()
  127. reached := sess.first != nil
  128. sess.mu.Unlock()
  129. if !reached {
  130. r.remove(sess.cookie)
  131. }
  132. }
  133. // cancel ends a session the clients have called off. It reports whether there
  134. // was one.
  135. func (r *registry) cancel(cookie [8]byte) bool {
  136. r.mu.Lock()
  137. defer r.mu.Unlock()
  138. sess, ok := r.sessions[cookie]
  139. if !ok {
  140. return false
  141. }
  142. delete(r.sessions, cookie)
  143. close(sess.cancelled)
  144. sess.expiry.Stop()
  145. return true
  146. }
  147. func (r *registry) lookup(cookie [8]byte) (*session, error) {
  148. r.mu.Lock()
  149. defer r.mu.Unlock()
  150. sess, ok := r.sessions[cookie]
  151. if !ok {
  152. return nil, errNoSession
  153. }
  154. return sess, nil
  155. }
  156. func (r *registry) remove(cookie [8]byte) {
  157. r.mu.Lock()
  158. defer r.mu.Unlock()
  159. if sess, ok := r.sessions[cookie]; ok {
  160. sess.expiry.Stop()
  161. }
  162. delete(r.sessions, cookie)
  163. }