client.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package oscarclient
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "log/slog"
  7. "net"
  8. "time"
  9. "github.com/mk6i/open-oscar-server/wire"
  10. )
  11. // OSCARServer serves OSCAR connections that in-process clients open.
  12. type OSCARServer interface {
  13. // ServeConn starts serving conn as if it had been accepted on one of
  14. // the server's listeners. The server owns the goroutine that serves it.
  15. // It returns an error if the server won't serve the connection.
  16. ServeConn(conn net.Conn) error
  17. }
  18. // Client signs users on to the OSCAR server over in-memory connections, as
  19. // an ordinary OSCAR client would.
  20. type Client struct {
  21. // Server serves the connections.
  22. Server OSCARServer
  23. // Logger records problems on the connections, such as SNACs that don't
  24. // decode or a consumer that falls too far behind.
  25. Logger *slog.Logger
  26. }
  27. // clientAddrConn is the server end of an in-memory OSCAR connection. It
  28. // reports the client's address as its remote address, so that the OSCAR
  29. // server sees where the user really connects from.
  30. type clientAddrConn struct {
  31. net.Conn
  32. clientAddr net.Addr
  33. }
  34. func (c clientAddrConn) RemoteAddr() net.Addr {
  35. return c.clientAddr
  36. }
  37. // dial opens an in-memory connection to the OSCAR server on behalf of the
  38. // client at clientAddr.
  39. func (c Client) dial(clientAddr net.Addr) (net.Conn, error) {
  40. clientEnd, serverEnd := net.Pipe()
  41. if err := c.Server.ServeConn(clientAddrConn{Conn: serverEnd, clientAddr: clientAddr}); err != nil {
  42. _ = clientEnd.Close()
  43. return nil, fmt.Errorf("ServeConn: %w", err)
  44. }
  45. return clientEnd, nil
  46. }
  47. // Login signs on to the OSCAR auth service with the given signon TLVs
  48. // and returns the server's reply TLVs, which hold either a login cookie or an
  49. // error code.
  50. func (c Client) Login(clientAddr net.Addr, signonTLVs []wire.TLV) (wire.TLVRestBlock, error) {
  51. conn, err := c.dial(clientAddr)
  52. if err != nil {
  53. return wire.TLVRestBlock{}, err
  54. }
  55. defer func() { _ = conn.Close() }()
  56. flapc := wire.NewFlapClient(0, conn, conn)
  57. if _, err := flapc.ReceiveSignonFrame(); err != nil {
  58. return wire.TLVRestBlock{}, fmt.Errorf("ReceiveSignonFrame: %w", err)
  59. }
  60. if err := flapc.SendSignonFrame(signonTLVs); err != nil {
  61. return wire.TLVRestBlock{}, fmt.Errorf("SendSignonFrame: %w", err)
  62. }
  63. frame, err := flapc.ReceiveFLAP()
  64. if err != nil {
  65. return wire.TLVRestBlock{}, fmt.Errorf("ReceiveFLAP: %w", err)
  66. }
  67. switch frame.FrameType {
  68. case wire.FLAPFrameSignoff:
  69. block := wire.TLVRestBlock{}
  70. if err := wire.UnmarshalBE(&block, bytes.NewReader(frame.Payload)); err != nil {
  71. return wire.TLVRestBlock{}, fmt.Errorf("UnmarshalBE: %w", err)
  72. }
  73. return block, nil
  74. case wire.FLAPFrameData:
  75. // the server answers with a BUCP login response when it rate limits
  76. // an address that recently signed on with BUCP
  77. msg, err := decodeSNAC(frame.Payload)
  78. if err != nil {
  79. return wire.TLVRestBlock{}, fmt.Errorf("decodeSNAC: %w", err)
  80. }
  81. reply, ok := msg.Body.(wire.SNAC_0x17_0x03_BUCPLoginResponse)
  82. if !ok {
  83. return wire.TLVRestBlock{}, fmt.Errorf("unexpected login reply %s",
  84. wire.SubGroupName(msg.Frame.FoodGroup, msg.Frame.SubGroup))
  85. }
  86. return reply.TLVRestBlock, nil
  87. default:
  88. return wire.TLVRestBlock{}, fmt.Errorf("unexpected login reply frame type %d", frame.FrameType)
  89. }
  90. }
  91. // ConnectService connects to the OSCAR service that loginCookie grants access
  92. // to, either BOS or a chat room, and waits for the server to report the
  93. // service online. The deadline of ctx, if it has one, bounds the wait. The
  94. // caller must start the returned connection's Run right away.
  95. func (c Client) ConnectService(ctx context.Context, clientAddr net.Addr, loginCookie []byte) (*FLAPConn, error) {
  96. conn, err := c.dial(clientAddr)
  97. if err != nil {
  98. return nil, err
  99. }
  100. if deadline, ok := ctx.Deadline(); ok {
  101. _ = conn.SetDeadline(deadline)
  102. }
  103. flapc := wire.NewFlapClient(0, conn, conn)
  104. if err := func() error {
  105. if _, err := flapc.ReceiveSignonFrame(); err != nil {
  106. return fmt.Errorf("ReceiveSignonFrame: %w", err)
  107. }
  108. if err := flapc.SendSignonFrame([]wire.TLV{wire.NewTLVBE(wire.OServiceTLVTagsLoginCookie, loginCookie)}); err != nil {
  109. return fmt.Errorf("SendSignonFrame: %w", err)
  110. }
  111. frame, err := flapc.ReceiveFLAP()
  112. if err != nil {
  113. return fmt.Errorf("ReceiveFLAP: %w", err)
  114. }
  115. if frame.FrameType == wire.FLAPFrameSignoff {
  116. return ErrSignedOff
  117. }
  118. msg, err := decodeSNAC(frame.Payload)
  119. if err != nil {
  120. return fmt.Errorf("decodeSNAC: %w", err)
  121. }
  122. if _, ok := msg.Body.(wire.SNAC_0x01_0x03_OServiceHostOnline); !ok {
  123. return fmt.Errorf("expected host online, got %s",
  124. wire.SubGroupName(msg.Frame.FoodGroup, msg.Frame.SubGroup))
  125. }
  126. return nil
  127. }(); err != nil {
  128. _ = conn.Close()
  129. return nil, err
  130. }
  131. _ = conn.SetDeadline(time.Time{})
  132. return newFLAPConn(conn, flapc, c.Logger), nil
  133. }