|
|
@@ -18,7 +18,7 @@ import (
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
- "github.com/mk6i/open-oscar-server/config"
|
|
|
+ "github.com/mk6i/open-oscar-server/server/oscarclient"
|
|
|
"github.com/mk6i/open-oscar-server/state"
|
|
|
"github.com/mk6i/open-oscar-server/wire"
|
|
|
)
|
|
|
@@ -38,11 +38,8 @@ type OSCARProxy struct {
|
|
|
HTTPIPRateLimiter *IPRateLimiter
|
|
|
LocateService LocateService
|
|
|
Logger *slog.Logger
|
|
|
- // OSCARServer serves the OSCAR connections TOC opens for its users.
|
|
|
- OSCARServer OSCARServer
|
|
|
- // OSCAREndpoint is the OSCAR listener that TOC's connections appear to
|
|
|
- // arrive on.
|
|
|
- OSCAREndpoint config.Endpoint
|
|
|
+ // OSCARClient signs TOC's users on to the OSCAR server.
|
|
|
+ OSCARClient oscarclient.Client
|
|
|
SessionRetriever SessionRetriever
|
|
|
TOCConfigStore TOCConfigStore
|
|
|
// RandIntn is the source for feedbag item ID generation.
|
|
|
@@ -53,11 +50,11 @@ type OSCARProxy struct {
|
|
|
// signonTimeout bounds how long await waits for a reply during signon.
|
|
|
const signonTimeout = 10 * time.Second
|
|
|
|
|
|
-// Request contexts are cached with each SNAC that TOC sends to the OSCAR
|
|
|
-// server, under the SNAC's request ID (see snacCache). When the reply or error
|
|
|
-// comes back, cmd_server.go uses the context's type to tell which TOC command
|
|
|
-// the reply answers, and the context's fields for what the reply doesn't
|
|
|
-// carry.
|
|
|
+// Request contexts are tracked with each SNAC that TOC sends to the OSCAR
|
|
|
+// server, under the SNAC's request ID (see oscarclient.RequestTracker). When
|
|
|
+// the reply or error comes back, cmd_server.go uses the context's type to tell
|
|
|
+// which TOC command the reply answers, and the context's fields for what the
|
|
|
+// reply doesn't carry.
|
|
|
type (
|
|
|
// getStatusReqCtx is the request context of a toc_get_status user info
|
|
|
// query.
|
|
|
@@ -213,22 +210,22 @@ func (s OSCARProxy) RecvClientCmd(ctx context.Context, me *tocConn, payload []by
|
|
|
}
|
|
|
|
|
|
// send sends a SNAC request to the OSCAR server over conn. The request is
|
|
|
-// cached by request ID along with data so that cmd_server.go can handle the
|
|
|
+// tracked by request ID along with data so that cmd_server.go can handle the
|
|
|
// reply. It returns a TOC error if the SNAC could not be sent.
|
|
|
func (s OSCARProxy) send(ctx context.Context, me *tocConn, conn oscarConn, foodGroup uint16, subGroup uint16, body any, data any) []string {
|
|
|
frame := wire.SNACFrame{
|
|
|
FoodGroup: foodGroup,
|
|
|
SubGroup: subGroup,
|
|
|
}
|
|
|
- me.cache.add(&frame, data)
|
|
|
+ frame = me.requests.Track(frame, data)
|
|
|
return s.sendFrame(ctx, me, conn, frame, body)
|
|
|
}
|
|
|
|
|
|
-// sendFrame sends a SNAC request whose frame is already cached. It returns a
|
|
|
+// sendFrame sends a SNAC request whose frame is already tracked. It returns a
|
|
|
// TOC error if the SNAC could not be sent.
|
|
|
func (s OSCARProxy) sendFrame(ctx context.Context, me *tocConn, conn oscarConn, frame wire.SNACFrame, body any) []string {
|
|
|
if err := conn.SendSNAC(frame, body); err != nil {
|
|
|
- me.cache.remove(frame.RequestID)
|
|
|
+ me.requests.Remove(frame.RequestID)
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("SendSNAC(%s, %s): %w",
|
|
|
wire.FoodGroupName(frame.FoodGroup), wire.SubGroupName(frame.FoodGroup, frame.SubGroup), err))
|
|
|
}
|
|
|
@@ -517,10 +514,14 @@ func (s OSCARProxy) requestChatService(ctx context.Context, me *tocConn, chatID
|
|
|
// connectChat opens a connection to the chat room identified by loginCookie
|
|
|
// and signs the user into the room.
|
|
|
func (s OSCARProxy) connectChat(ctx context.Context, me *tocConn, loginCookie []byte) (oscarConn, error) {
|
|
|
- conn, err := s.connectService(me.clientAddr, loginCookie)
|
|
|
+ conn, err := s.OSCARClient.ConnectService(ctx, me.clientAddr, loginCookie)
|
|
|
if err != nil {
|
|
|
- return nil, fmt.Errorf("connectService: %w", err)
|
|
|
+ return nil, fmt.Errorf("ConnectService: %w", err)
|
|
|
}
|
|
|
+ me.goAsync(func(ctx context.Context) error {
|
|
|
+ conn.Run(ctx)
|
|
|
+ return nil
|
|
|
+ })
|
|
|
|
|
|
subscribe := wire.SNAC_0x01_0x08_OServiceRateParamsSubAdd{
|
|
|
ClassIDs: []uint16{1, 2, 3, 4, 5},
|
|
|
@@ -1072,18 +1073,18 @@ func (s OSCARProxy) deleteFeedbag(ctx context.Context, me *tocConn, items []wire
|
|
|
// sendFeedbagChange sends a feedbag change and records it in the feedbag
|
|
|
// mirror as pending until the server confirms it.
|
|
|
func (s OSCARProxy) sendFeedbagChange(ctx context.Context, me *tocConn, subGroup uint16, body any, del bool, items []wire.FeedbagItem) []string {
|
|
|
- me.feedbag.sendMu.Lock()
|
|
|
- defer me.feedbag.sendMu.Unlock()
|
|
|
+ me.feedbagSendMu.Lock()
|
|
|
+ defer me.feedbagSendMu.Unlock()
|
|
|
|
|
|
frame := wire.SNACFrame{
|
|
|
FoodGroup: wire.Feedbag,
|
|
|
SubGroup: subGroup,
|
|
|
}
|
|
|
- me.cache.add(&frame, feedbagChangeReqCtx{})
|
|
|
- me.feedbag.sent(frame.RequestID, del, items)
|
|
|
+ frame = me.requests.Track(frame, feedbagChangeReqCtx{})
|
|
|
+ me.feedbag.Sent(frame.RequestID, del, items)
|
|
|
|
|
|
if msg := s.sendFrame(ctx, me, me.bos, frame, body); msg != nil {
|
|
|
- me.feedbag.discard(frame.RequestID)
|
|
|
+ me.feedbag.Discard(frame.RequestID)
|
|
|
return msg
|
|
|
}
|
|
|
return nil
|
|
|
@@ -1092,11 +1093,11 @@ func (s OSCARProxy) sendFeedbagChange(ctx context.Context, me *tocConn, subGroup
|
|
|
// reloadFeedbag queries the server's feedbag to replace the feedbag mirror.
|
|
|
// FeedbagReply finishes the reload.
|
|
|
func (s OSCARProxy) reloadFeedbag(ctx context.Context, me *tocConn) []string {
|
|
|
- me.feedbag.sendMu.Lock()
|
|
|
- defer me.feedbag.sendMu.Unlock()
|
|
|
+ me.feedbagSendMu.Lock()
|
|
|
+ defer me.feedbagSendMu.Unlock()
|
|
|
|
|
|
return s.send(ctx, me, me.bos, wire.Feedbag, wire.FeedbagQuery, struct{}{}, feedbagReloadReqCtx{
|
|
|
- marker: me.feedbag.mark(),
|
|
|
+ marker: me.feedbag.Mark(),
|
|
|
})
|
|
|
}
|
|
|
|
|
|
@@ -1124,7 +1125,7 @@ func (s OSCARProxy) SetPDMode(ctx context.Context, me *tocConn, args []byte) []s
|
|
|
return s.runtimeErr(ctx, errors.New("invalid pd mode specified"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
fl.SetMode(uint8(mode))
|
|
|
|
|
|
if pending := fl.PendingUpdates(); len(pending) > 0 {
|
|
|
@@ -1155,7 +1156,7 @@ func (s OSCARProxy) NewGroup(ctx context.Context, me *tocConn, args []byte) []st
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("empty group name"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
fl.AddGroup(groupName)
|
|
|
|
|
|
if pending := fl.PendingUpdates(); len(pending) > 0 {
|
|
|
@@ -1185,7 +1186,7 @@ func (s OSCARProxy) DelGroup(ctx context.Context, me *tocConn, args []byte) []st
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("empty group name"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
fl.DeleteGroup(groupName)
|
|
|
|
|
|
pending := fl.PendingDeletes()
|
|
|
@@ -1234,7 +1235,7 @@ func (s OSCARProxy) NewBuddies(ctx context.Context, me *tocConn, args []byte) []
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("parseTOC2Config: %w", err))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
|
|
|
var replies []string
|
|
|
|
|
|
@@ -1331,7 +1332,7 @@ func (s OSCARProxy) RemoveBuddy2(ctx context.Context, me *tocConn, args []byte)
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("missing params: need at least one screenname and group"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
|
|
|
groupName := params[len(params)-1]
|
|
|
screenNames := params[:len(params)-1]
|
|
|
@@ -1376,7 +1377,7 @@ func (s OSCARProxy) AddPermit2(ctx context.Context, me *tocConn, args []byte) []
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("no screennames provided"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
|
|
|
for _, sn := range screenNames {
|
|
|
fl.PermitUser(sn)
|
|
|
@@ -1409,7 +1410,7 @@ func (s OSCARProxy) RemovePermit2(ctx context.Context, me *tocConn, args []byte)
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("no screennames provided"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
|
|
|
for _, sn := range screenNames {
|
|
|
fl.DeletePermit(sn)
|
|
|
@@ -1442,7 +1443,7 @@ func (s OSCARProxy) AddDeny2(ctx context.Context, me *tocConn, args []byte) []st
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("no screennames provided"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
|
|
|
for _, sn := range screenNames {
|
|
|
fl.DenyUser(sn)
|
|
|
@@ -1475,7 +1476,7 @@ func (s OSCARProxy) RemoveDeny2(ctx context.Context, me *tocConn, args []byte) [
|
|
|
return s.runtimeErr(ctx, fmt.Errorf("no screennames provided"))
|
|
|
}
|
|
|
|
|
|
- fl := state.NewFeedbagList(me.feedbag.snapshot(), s.RandIntn)
|
|
|
+ fl := state.NewFeedbagList(me.feedbag.Snapshot(), s.RandIntn)
|
|
|
|
|
|
for _, sn := range screenNames {
|
|
|
fl.DeleteDeny(sn)
|
|
|
@@ -1883,7 +1884,7 @@ func (s OSCARProxy) SetInfo(ctx context.Context, me *tocConn, args []byte) []str
|
|
|
// Command syntax: toc_signon <authorizer host> <authorizer port> <User Name> <Password> <language> <version>
|
|
|
// Command syntax: toc2_signon <authorizer host> <authorizer port> <User Name> <Password> <language> <version>
|
|
|
// Command syntax: toc2_login <authorizer host> <authorizer port> <User Name> <Password> <language> <version> [additional params...]
|
|
|
-func (s OSCARProxy) Signon(ctx context.Context, args []byte, clientAddr net.Addr) (*tocConn, []string) {
|
|
|
+func (s OSCARProxy) Signon(ctx context.Context, args []byte, clientAddr net.Addr, goAsync func(f func(ctx context.Context) error)) (*tocConn, []string) {
|
|
|
var cmd, userName, password string
|
|
|
|
|
|
if _, err := parseArgs(args, &cmd, nil, nil, &userName, &password); err != nil {
|
|
|
@@ -1913,9 +1914,9 @@ func (s OSCARProxy) Signon(ctx context.Context, args []byte, clientAddr net.Addr
|
|
|
signonTLVs = append(signonTLVs, wire.NewTLVBE(wire.LoginTLVTagsMultiConnFlags, wire.MultiConnFlagsRecentClient))
|
|
|
}
|
|
|
|
|
|
- block, err := s.authenticate(clientAddr, signonTLVs)
|
|
|
+ block, err := s.OSCARClient.Login(clientAddr, signonTLVs)
|
|
|
if err != nil {
|
|
|
- return nil, s.runtimeErr(ctx, fmt.Errorf("authenticate: %w", err))
|
|
|
+ return nil, s.runtimeErr(ctx, fmt.Errorf("OSCARClient.Login: %w", err))
|
|
|
}
|
|
|
|
|
|
if code, failed := block.Uint16BE(wire.LoginTLVTagsErrorSubcode); failed {
|
|
|
@@ -1931,10 +1932,14 @@ func (s OSCARProxy) Signon(ctx context.Context, args []byte, clientAddr net.Addr
|
|
|
return nil, s.runtimeErr(ctx, fmt.Errorf("unable to get session id from payload"))
|
|
|
}
|
|
|
|
|
|
- bos, err := s.connectService(clientAddr, authCookie)
|
|
|
+ bos, err := s.OSCARClient.ConnectService(ctx, clientAddr, authCookie)
|
|
|
if err != nil {
|
|
|
- return nil, s.runtimeErr(ctx, fmt.Errorf("connectService: %w", err))
|
|
|
+ return nil, s.runtimeErr(ctx, fmt.Errorf("ConnectService: %w", err))
|
|
|
}
|
|
|
+ goAsync(func(ctx context.Context) error {
|
|
|
+ bos.Run(ctx)
|
|
|
+ return nil
|
|
|
+ })
|
|
|
|
|
|
me := newTOCConn(bos, state.NewIdentScreenName(userName), clientAddr)
|
|
|
me.isTOC2 = cmd != "toc_signon"
|
|
|
@@ -2006,7 +2011,7 @@ func (s OSCARProxy) signonConfig(ctx context.Context, me *tocConn, cmd string) (
|
|
|
return nil, fmt.Errorf("unexpected feedbag reply %v", reply.Frame)
|
|
|
}
|
|
|
|
|
|
- me.feedbag.reset(feedbag.Items)
|
|
|
+ me.feedbag.Reset(feedbag.Items)
|
|
|
|
|
|
cfg, err := buildToc2Config(feedbag.Items)
|
|
|
if err != nil {
|
|
|
@@ -2023,8 +2028,8 @@ func (s OSCARProxy) await(ctx context.Context, me *tocConn, foodGroup uint16, su
|
|
|
FoodGroup: foodGroup,
|
|
|
SubGroup: subGroup,
|
|
|
}
|
|
|
- me.cache.add(&frame, nil)
|
|
|
- defer me.cache.remove(frame.RequestID)
|
|
|
+ frame = me.requests.Track(frame, nil)
|
|
|
+ defer me.requests.Remove(frame.RequestID)
|
|
|
|
|
|
if err := me.bos.SendSNAC(frame, body); err != nil {
|
|
|
return wire.SNACMessage{}, fmt.Errorf("SendSNAC: %w", err)
|
|
|
@@ -2045,7 +2050,7 @@ func (s OSCARProxy) await(ctx context.Context, me *tocConn, foodGroup uint16, su
|
|
|
}
|
|
|
return msg, nil
|
|
|
case <-me.bos.Closed():
|
|
|
- return wire.SNACMessage{}, errSignedOff
|
|
|
+ return wire.SNACMessage{}, oscarclient.ErrSignedOff
|
|
|
case <-timeout.C:
|
|
|
return wire.SNACMessage{}, errors.New("timed out waiting for reply")
|
|
|
case <-ctx.Done():
|