broker.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package relay
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "log/slog"
  6. "net/netip"
  7. "slices"
  8. "strings"
  9. "github.com/google/uuid"
  10. "github.com/mk6i/open-oscar-server/state"
  11. "github.com/mk6i/open-oscar-server/wire"
  12. )
  13. // relayedCaps lists the services the server relays. Everything else, such as
  14. // room invites and voice, is passed on as usual.
  15. var relayedCaps = []uuid.UUID{wire.CapDirectICBM, wire.CapFileTransfer, wire.CapFileSharing}
  16. // BrokerRendezvous points a direct offer at the server, so neither client
  17. // needs to reach the other. It implements foodgroup.RendezvousBroker.
  18. func (s *Server) BrokerRendezvous(instance *state.SessionInstance, recipient state.IdentScreenName, frag wire.ICBMCh2Fragment) (wire.ICBMCh2Fragment, bool) {
  19. if frag.Type == wire.ICBMRdvMessageCancel {
  20. s.registry.cancel(frag.Cookie)
  21. return frag, false
  22. }
  23. if frag.Type != wire.ICBMRdvMessagePropose || !slices.Contains(relayedCaps, uuid.UUID(frag.Capability)) || frag.HasTag(wire.ICBMRdvTLVTagsUseARS) {
  24. return frag, false
  25. }
  26. if seq, ok := frag.Uint16BE(wire.ICBMRdvTLVTagsSeqNum); !ok || seq != 1 {
  27. return frag, false
  28. }
  29. sess := &session{
  30. cookie: frag.Cookie,
  31. offerer: instance,
  32. offererIP: instanceIP(instance),
  33. recipient: recipient,
  34. recipientIPs: s.instanceIPs(recipient),
  35. capability: frag.Capability,
  36. }
  37. if !s.registry.reserve(sess, s.acceptTimeout) {
  38. return frag, false
  39. }
  40. tagsIn := tagList(frag)
  41. s.setAddress(&frag)
  42. s.logger.Info("relaying rendezvous",
  43. "offerer", instance.IdentScreenName().String(),
  44. "recipient", recipient.String(),
  45. "cap", uuid.UUID(frag.Capability).String(),
  46. "cookie", fmt.Sprintf("%x", frag.Cookie),
  47. "tags_in", tagsIn,
  48. "tags_out", tagList(frag))
  49. return frag, true
  50. }
  51. // HoldRendezvous keeps back the recipient's accept. It only says the
  52. // recipient reached the server, and the offerer has a counter-offer to follow.
  53. // It implements foodgroup.RendezvousBroker.
  54. func (s *Server) HoldRendezvous(instance *state.SessionInstance, frag wire.ICBMCh2Fragment) bool {
  55. if frag.Type != wire.ICBMRdvMessageAccept {
  56. return false
  57. }
  58. sess, err := s.registry.lookup(frag.Cookie)
  59. if err != nil || instance.IdentScreenName() != sess.recipient {
  60. return false
  61. }
  62. sess.markAccepted()
  63. s.logger.Info("held rendezvous accept", "cookie", fmt.Sprintf("%x", frag.Cookie))
  64. return true
  65. }
  66. // RefuseRendezvous ends a session the recipient turned down. AIM declines with
  67. // a client error, not a rendezvous cancel. It implements
  68. // foodgroup.RendezvousBroker.
  69. func (s *Server) RefuseRendezvous(cookie uint64) {
  70. var id [8]byte
  71. binary.BigEndian.PutUint64(id[:], cookie)
  72. if s.registry.cancel(id) {
  73. s.logger.Info("rendezvous refused", "cookie", fmt.Sprintf("%x", id))
  74. }
  75. }
  76. // setAddress writes the server's address into a rendezvous offer.
  77. func (s *Server) setAddress(frag *wire.ICBMCh2Fragment) {
  78. ip := s.advertisedIP.As4()
  79. frag.Remove(wire.ICBMRdvTLVTagsRdvIP)
  80. frag.Remove(wire.ICBMRdvTLVTagsIPXOR)
  81. frag.Remove(wire.ICBMRdvTLVTagsPortXOR)
  82. frag.Set(wire.NewTLVBE(wire.ICBMRdvTLVTagsRequesterIP, ip[:]))
  83. frag.Set(wire.NewTLVBE(wire.ICBMRdvTLVTagsVerifiedIP, ip[:]))
  84. frag.Set(wire.NewTLVBE(wire.ICBMRdvTLVTagsPort, s.advertisedPort))
  85. }
  86. // counterOffer tells the offerer to dial the server too. To the offerer it
  87. // reads as the recipient asking it to connect instead.
  88. func (s *Server) counterOffer(sess *session, logger *slog.Logger) {
  89. recipient := s.sessionRetriever.RetrieveSession(sess.recipient)
  90. if recipient == nil {
  91. logger.Info("recipient signed off before the counter-offer")
  92. return
  93. }
  94. frag := wire.ICBMCh2Fragment{
  95. Type: wire.ICBMRdvMessagePropose,
  96. Cookie: sess.cookie,
  97. Capability: sess.capability,
  98. }
  99. frag.Append(wire.NewTLVBE(wire.ICBMRdvTLVTagsSeqNum, uint16(2)))
  100. s.setAddress(&frag)
  101. msg := wire.SNAC_0x04_0x07_ICBMChannelMsgToClient{
  102. Cookie: binary.BigEndian.Uint64(sess.cookie[:]),
  103. ChannelID: wire.ICBMChannelRendezvous,
  104. TLVUserInfo: recipient.TLVUserInfo(),
  105. }
  106. msg.Append(wire.NewTLVBE(wire.ICBMTLVData, frag))
  107. status := sess.offerer.RelayMessageToInstance(wire.SNACMessage{
  108. Frame: wire.SNACFrame{
  109. FoodGroup: wire.ICBM,
  110. SubGroup: wire.ICBMChannelMsgToClient,
  111. RequestID: wire.ReqIDFromServer,
  112. },
  113. Body: msg,
  114. })
  115. logger.Info("sent counter-offer", "offerer", sess.offerer.IdentScreenName().String(), "status", status)
  116. }
  117. func (s *Server) instanceIPs(screenName state.IdentScreenName) []netip.Addr {
  118. session := s.sessionRetriever.RetrieveSession(screenName)
  119. if session == nil {
  120. return nil
  121. }
  122. var ips []netip.Addr
  123. for _, instance := range session.Instances() {
  124. ips = append(ips, instanceIP(instance))
  125. }
  126. return ips
  127. }
  128. func instanceIP(instance *state.SessionInstance) netip.Addr {
  129. if addr := instance.RemoteAddr(); addr != nil {
  130. return addr.Addr().Unmap()
  131. }
  132. return netip.Addr{}
  133. }
  134. // tagList names a rendezvous message's fields, in the order they were sent.
  135. func tagList(frag wire.ICBMCh2Fragment) string {
  136. tags := make([]string, 0, len(frag.TLVList))
  137. for _, tlv := range frag.TLVList {
  138. tags = append(tags, fmt.Sprintf("%04x", tlv.Tag))
  139. }
  140. return strings.Join(tags, " ")
  141. }