X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c681cfa3c4782b74c49a16bf08f2694cb9acba59..854a92fc2755ace61c405df335ddf69b02a3d932:/hotline/server.go diff --git a/hotline/server.go b/hotline/server.go index 392c8f3..5b8aad0 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -18,7 +18,6 @@ import ( "net" "os" "path/filepath" - "runtime/debug" "strings" "sync" "time" @@ -278,7 +277,7 @@ func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS File if err := register(t, tr); err != nil { server.Logger.Errorw("unable to register with tracker %v", "error", err) } - server.Logger.Infow("Sent Tracker registration", "data", tr) + server.Logger.Debugw("Sent Tracker registration", "addr", t) } time.Sleep(trackerUpdateFrequency * time.Second) @@ -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) } }