]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Minor cleanup
[rbdr/mobius] / hotline / server.go
index 1fafb30b2a929ec28ebbd9e6d75ad9d2f925eb57..852b7727b5da640d867b752cd69dfb1311d37a6c 100644 (file)
@@ -18,7 +18,6 @@ import (
        "net"
        "os"
        "path/filepath"
-       "runtime/debug"
        "strings"
        "sync"
        "time"
@@ -542,14 +541,6 @@ func (s *Server) loadConfig(path string) error {
        return nil
 }
 
-// dontPanic logs panics instead of crashing
-func dontPanic(logger *zap.SugaredLogger) {
-       if r := recover(); r != nil {
-               fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
-               logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack()))
-       }
-}
-
 // handleNewConnection takes a new net.Conn and performs the initial login sequence
 func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
        defer dontPanic(s.Logger)
@@ -564,9 +555,9 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
 
        scanner.Scan()
 
-       clientLogin, _, err := ReadTransaction(scanner.Bytes())
-       if err != nil {
-               panic(err)
+       var clientLogin Transaction
+       if _, err := clientLogin.Write(scanner.Bytes()); err != nil {
+               return err
        }
 
        c := s.NewClientConn(rwc, remoteAddr)
@@ -613,7 +604,7 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
 
        // If authentication fails, send error reply and close connection
        if !c.Authenticate(login, encodedPassword) {
-               t := c.NewErrReply(clientLogin, "Incorrect login.")
+               t := c.NewErrReply(&clientLogin, "Incorrect login.")
                b, err := t.MarshalBinary()
                if err != nil {
                        return err
@@ -645,7 +636,7 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
                c.Flags = []byte{0, 2}
        }
 
-       s.outbox <- c.NewReply(clientLogin,
+       s.outbox <- c.NewReply(&clientLogin,
                NewField(fieldVersion, []byte{0x00, 0xbe}),
                NewField(fieldCommunityBannerID, []byte{0, 0}),
                NewField(fieldServerName, []byte(s.Config.Name)),
@@ -694,11 +685,12 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser
                buf := make([]byte, len(scanner.Bytes()))
                copy(buf, scanner.Bytes())
 
-               t, _, err := ReadTransaction(buf)
-               if err != nil {
-                       panic(err)
+               var t Transaction
+               if _, err := t.Write(buf); err != nil {
+                       return err
                }
-               if err := c.handleTransaction(*t); err != nil {
+
+               if err := c.handleTransaction(t); err != nil {
                        c.logger.Errorw("Error handling transaction", "err", err)
                }
        }
@@ -774,7 +766,6 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro
        switch fileTransfer.Type {
        case bannerDownload:
                if err := s.bannerDownload(rwc); err != nil {
-                       panic(err)
                        return err
                }
        case FileDownload: