remove error-handling-via-panic

This commit is contained in:
Artem Titoulenko 2022-02-03 14:06:33 -05:00
parent 91e7b8b287
commit add16b5143
6 changed files with 32 additions and 25 deletions

View file

@ -211,8 +211,12 @@ func main() {
return ctx return ctx
} else if flap.Header.Channel == 2 { } else if flap.Header.Channel == 2 {
snac := &oscar.SNAC{} snac := &oscar.SNAC{}
err := snac.UnmarshalBinary(flap.Data.Bytes()) if err := snac.UnmarshalBinary(flap.Data.Bytes()); err != nil {
util.PanicIfError(err) log.Printf("could not unmarshal FLAP data: %w", err)
session.Disconnect()
handleCloseFn(ctx, session)
return ctx
}
fmt.Printf("%+v\n", snac) fmt.Printf("%+v\n", snac)
if tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()); err == nil { if tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()); err == nil {

View file

@ -25,7 +25,6 @@ func MessageDelivery(sm *SessionManager) (chan *models.Message, routineFn) {
return return
} }
log.Printf("got a message: %s", message)
if s := sm.GetSession(message.To); s != nil { if s := sm.GetSession(message.To); s != nil {
messageSnac := oscar.NewSNAC(4, 7) messageSnac := oscar.NewSNAC(4, 7)
messageSnac.Data.WriteUint64(message.Cookie) messageSnac.Data.WriteUint64(message.Cookie)
@ -62,7 +61,7 @@ func MessageDelivery(sm *SessionManager) (chan *models.Message, routineFn) {
messageFlap := oscar.NewFLAP(2) messageFlap := oscar.NewFLAP(2)
messageFlap.Data.WriteBinary(messageSnac) messageFlap.Data.WriteBinary(messageSnac)
if err := s.Send(messageFlap); err != nil { 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 continue
} else { } else {
log.Printf("sent message %d to user %s at %s", message.Cookie, message.To, s.RemoteAddr()) 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 message.StoreOffline {
if err := message.MarkDelivered(context.Background(), db); err != nil { 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 { } else {

View file

@ -1,7 +1,6 @@
package oscar package oscar
import ( import (
"aim-oscar/util"
"encoding" "encoding"
"encoding/binary" "encoding/binary"
"io" "io"
@ -119,7 +118,9 @@ func (b *Buffer) Write(x []byte) (int, error) {
func (b *Buffer) WriteBinary(e encoding.BinaryMarshaler) { func (b *Buffer) WriteBinary(e encoding.BinaryMarshaler) {
d, err := e.MarshalBinary() d, err := e.MarshalBinary()
util.PanicIfError(err) if err != nil {
panic(err)
}
b.d = append(b.d, d...) b.d = append(b.d, d...)
} }

View file

@ -10,8 +10,6 @@ import (
"log" "log"
"net" "net"
"strings" "strings"
"github.com/pkg/errors"
) )
type HandlerFunc func(context.Context, *FLAP) context.Context type HandlerFunc func(context.Context, *FLAP) context.Context
@ -78,7 +76,10 @@ func (h *Handler) Handle(conn net.Conn) {
flapBuf := make([]byte, flapLength) flapBuf := make([]byte, flapLength)
buf.Read(flapBuf) buf.Read(flapBuf)
if err := flap.UnmarshalBinary(flapBuf); err != nil { 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) ctx = h.handle(ctx, flap)

View file

@ -12,7 +12,6 @@ import (
"aim-oscar/models" "aim-oscar/models"
"aim-oscar/oscar" "aim-oscar/oscar"
"aim-oscar/util"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/uptrace/bun" "github.com/uptrace/bun"
@ -99,26 +98,28 @@ func AuthenticateFLAPCookie(ctx context.Context, db *bun.DB, flap *oscar.FLAP) (
return user, nil return user, nil
} }
func (a *AuthorizationRegistrationService) GenerateCipher() string { func (a *AuthorizationRegistrationService) GenerateCipher() (string, error) {
randomBytes := make([]byte, 64) randomBytes := make([]byte, 64)
_, err := rand.Read(randomBytes) _, err := rand.Read(randomBytes)
if err != nil { 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) { func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *bun.DB, snac *oscar.SNAC) (context.Context, error) {
session, err := oscar.SessionFromContext(ctx) session, err := oscar.SessionFromContext(ctx)
if err != nil { if err != nil {
util.PanicIfError(err) return ctx, errors.Wrap(err, "could not extract session from context")
} }
switch snac.Header.Subtype { switch snac.Header.Subtype {
// Request MD5 Auth Key // Request MD5 Auth Key
case 0x06: case 0x06:
tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()) 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) screenNameTLV := oscar.FindTLV(tlvs, 1)
if screenNameTLV == nil { if screenNameTLV == nil {
@ -140,7 +141,10 @@ func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *b
} }
// Create cipher for this user // 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 { if err = user.Update(ctx, db); err != nil {
return ctx, err return ctx, err
} }
@ -156,7 +160,9 @@ func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *b
// Client Authorization Request // Client Authorization Request
case 0x02: case 0x02:
tlvs, err := oscar.UnmarshalTLVs(snac.Data.Bytes()) 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) screenNameTLV := oscar.FindTLV(tlvs, 1)
if screenNameTLV == nil { if screenNameTLV == nil {
@ -230,7 +236,9 @@ func (a *AuthorizationRegistrationService) HandleSNAC(ctx context.Context, db *b
UIN: user.UIN, UIN: user.UIN,
X: fmt.Sprintf("%x", expectedPasswordHash), 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(0x6, cookie))
authSnac.Data.WriteBinary(oscar.NewTLV(0x11, []byte(user.Email))) authSnac.Data.WriteBinary(oscar.NewTLV(0x11, []byte(user.Email)))

View file

@ -48,12 +48,6 @@ func PrettyBytes(bytes []byte) string {
return strings.TrimSpace(res) return strings.TrimSpace(res)
} }
func PanicIfError(err error) {
if err != nil {
panic(err)
}
}
func Word(x uint16) []byte { func Word(x uint16) []byte {
b := make([]byte, 2) b := make([]byte, 2)
binary.BigEndian.PutUint16(b, x) binary.BigEndian.PutUint16(b, x)