From add16b51438561186d0a60a6bcb990ca02df2505 Mon Sep 17 00:00:00 2001 From: Artem Titoulenko Date: Thu, 3 Feb 2022 14:06:33 -0500 Subject: [PATCH] remove error-handling-via-panic --- main.go | 8 ++++-- message_delivery_routine.go | 5 ++-- oscar/buf.go | 5 ++-- oscar/server.go | 7 ++--- ...0x17_authorization_registration_service.go | 26 ++++++++++++------- util/util.go | 6 ----- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index 4ca6d08..fed2247 100644 --- a/main.go +++ b/main.go @@ -211,8 +211,12 @@ func main() { return ctx } else if flap.Header.Channel == 2 { snac := &oscar.SNAC{} - err := snac.UnmarshalBinary(flap.Data.Bytes()) - util.PanicIfError(err) + if err := snac.UnmarshalBinary(flap.Data.Bytes()); err != nil { + log.Printf("could not unmarshal FLAP data: %w", err) + session.Disconnect() + handleCloseFn(ctx, session) + return ctx + } fmt.Printf("%+v\n", snac) if tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()); err == nil { diff --git a/message_delivery_routine.go b/message_delivery_routine.go index 86bc71d..833af9e 100644 --- a/message_delivery_routine.go +++ b/message_delivery_routine.go @@ -25,7 +25,6 @@ func MessageDelivery(sm *SessionManager) (chan *models.Message, routineFn) { return } - log.Printf("got a message: %s", message) if s := sm.GetSession(message.To); s != nil { messageSnac := oscar.NewSNAC(4, 7) messageSnac.Data.WriteUint64(message.Cookie) @@ -62,7 +61,7 @@ func MessageDelivery(sm *SessionManager) (chan *models.Message, routineFn) { messageFlap := oscar.NewFLAP(2) messageFlap.Data.WriteBinary(messageSnac) if err := s.Send(messageFlap); err != nil { - log.Panicf("could not deliver message %d: %s", message.Cookie, err.Error()) + log.Println("could not deliver message %d: %s", message.Cookie, err.Error()) continue } else { log.Printf("sent message %d to user %s at %s", message.Cookie, message.To, s.RemoteAddr()) @@ -70,7 +69,7 @@ func MessageDelivery(sm *SessionManager) (chan *models.Message, routineFn) { if message.StoreOffline { if err := message.MarkDelivered(context.Background(), db); err != nil { - log.Panicf("could not mark message %d as delivered: %s", message.Cookie, err.Error()) + log.Println("could not mark message %d as delivered: %s", message.Cookie, err.Error()) } } } else { diff --git a/oscar/buf.go b/oscar/buf.go index eb5966a..e0f1a99 100644 --- a/oscar/buf.go +++ b/oscar/buf.go @@ -1,7 +1,6 @@ package oscar import ( - "aim-oscar/util" "encoding" "encoding/binary" "io" @@ -119,7 +118,9 @@ func (b *Buffer) Write(x []byte) (int, error) { func (b *Buffer) WriteBinary(e encoding.BinaryMarshaler) { d, err := e.MarshalBinary() - util.PanicIfError(err) + if err != nil { + panic(err) + } b.d = append(b.d, d...) } diff --git a/oscar/server.go b/oscar/server.go index 01dee54..ff6afa2 100644 --- a/oscar/server.go +++ b/oscar/server.go @@ -10,8 +10,6 @@ import ( "log" "net" "strings" - - "github.com/pkg/errors" ) type HandlerFunc func(context.Context, *FLAP) context.Context @@ -78,7 +76,10 @@ func (h *Handler) Handle(conn net.Conn) { flapBuf := make([]byte, flapLength) buf.Read(flapBuf) if err := flap.UnmarshalBinary(flapBuf); err != nil { - util.PanicIfError(errors.Wrap(err, "could not unmarshal FLAP")) + log.Printf("could not unmarshal FLAP: %w", err) + // Toss out everything + buf.Reset() + break } ctx = h.handle(ctx, flap) diff --git a/services/0x17_authorization_registration_service.go b/services/0x17_authorization_registration_service.go index 3862fe3..1bd99b1 100644 --- a/services/0x17_authorization_registration_service.go +++ b/services/0x17_authorization_registration_service.go @@ -12,7 +12,6 @@ import ( "aim-oscar/models" "aim-oscar/oscar" - "aim-oscar/util" "github.com/pkg/errors" "github.com/uptrace/bun" @@ -99,26 +98,28 @@ func AuthenticateFLAPCookie(ctx context.Context, db *bun.DB, flap *oscar.FLAP) ( return user, nil } -func (a *AuthorizationRegistrationService) GenerateCipher() string { +func (a *AuthorizationRegistrationService) GenerateCipher() (string, error) { randomBytes := make([]byte, 64) _, err := rand.Read(randomBytes) if err != nil { - panic(err) + return "", errors.Wrap(err, "could not generate cipher") } - return base32.StdEncoding.EncodeToString(randomBytes)[:CIPHER_LENGTH] + return base32.StdEncoding.EncodeToString(randomBytes)[:CIPHER_LENGTH], nil } func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *bun.DB, snac *oscar.SNAC) (context.Context, error) { session, err := oscar.SessionFromContext(ctx) if err != nil { - util.PanicIfError(err) + return ctx, errors.Wrap(err, "could not extract session from context") } switch snac.Header.Subtype { // Request MD5 Auth Key case 0x06: tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()) - util.PanicIfError(err) + if err != nil { + return ctx, errors.Wrap(err, "could not unmarshal TLVs") + } screenNameTLV := oscar.FindTLV(tlvs, 1) if screenNameTLV == nil { @@ -140,7 +141,10 @@ func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *b } // Create cipher for this user - user.Cipher = a.GenerateCipher() + user.Cipher, err = a.GenerateCipher() + if err != nil { + return ctx, err + } if err = user.Update(ctx, db); err != nil { return ctx, err } @@ -156,7 +160,9 @@ func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *b // Client Authorization Request case 0x02: tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()) - util.PanicIfError(err) + if err != nil { + return ctx, errors.Wrap(err, "could not unmarshal TLVs") + } screenNameTLV := oscar.FindTLV(tlvs, 1) if screenNameTLV == nil { @@ -230,7 +236,9 @@ func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *b UIN: user.UIN, X: fmt.Sprintf("%x", expectedPasswordHash), }) - util.PanicIfError(err) + if err != nil { + return ctx, errors.Wrap(err, "could not marshal authorization cookie") + } authSnac.Data.WriteBinary(oscar.NewTLV(0x6, cookie)) authSnac.Data.WriteBinary(oscar.NewTLV(0x11, []byte(user.Email))) diff --git a/util/util.go b/util/util.go index 15d958e..d90964d 100644 --- a/util/util.go +++ b/util/util.go @@ -48,12 +48,6 @@ func PrettyBytes(bytes []byte) string { return strings.TrimSpace(res) } -func PanicIfError(err error) { - if err != nil { - panic(err) - } -} - func Word(x uint16) []byte { b := make([]byte, 2) binary.BigEndian.PutUint16(b, x)