From: Jeff Halter Date: Tue, 21 Jun 2022 18:53:13 +0000 (-0700) Subject: Cleanup and logging improvements X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/commitdiff_plain/67db911d966913ff04ad90018af9a5361a4d8349?hp=46b48603ea0e69aab8084a3ce32b31995b31b09c Cleanup and logging improvements --- diff --git a/hotline/client_conn.go b/hotline/client_conn.go index a8ace87..4e703c2 100644 --- a/hotline/client_conn.go +++ b/hotline/client_conn.go @@ -2,6 +2,7 @@ package hotline import ( "encoding/binary" + "go.uber.org/zap" "golang.org/x/crypto/bcrypt" "io" "math/big" @@ -65,6 +66,7 @@ type ClientConn struct { AutoReply []byte Transfers map[int][]*FileTransfer Agreed bool + logger *zap.SugaredLogger } func (cc *ClientConn) sendAll(t int, fields ...Field) { @@ -97,10 +99,8 @@ func (cc *ClientConn) handleTransaction(transaction *Transaction) error { } } - cc.Server.Logger.Infow( + cc.logger.Infow( "Received Transaction", - "login", cc.Account.Login, - "name", string(cc.UserName), "RequestType", handler.Name, ) diff --git a/hotline/file_wrapper.go b/hotline/file_wrapper.go index 997d6d9..b59fc5c 100644 --- a/hotline/file_wrapper.go +++ b/hotline/file_wrapper.go @@ -105,20 +105,6 @@ func (f *fileWrapper) infoForkName() string { return fmt.Sprintf(infoForkNameTemplate, f.name) } -func (f *fileWrapper) creatorCode() []byte { - if f.ffo.FlatFileInformationFork.CreatorSignature != nil { - return f.infoFork.CreatorSignature - } - return []byte(fileTypeFromFilename(f.name).CreatorCode) -} - -func (f *fileWrapper) typeCode() []byte { - if f.infoFork != nil { - return f.infoFork.TypeSignature - } - return []byte(fileTypeFromFilename(f.name).TypeCode) -} - func (f *fileWrapper) rsrcForkWriter() (io.Writer, error) { file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { diff --git a/hotline/server.go b/hotline/server.go index ba0a668..9cec83b 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -167,25 +167,31 @@ func (s *Server) sendTransaction(t Transaction) error { return nil } +func (s *Server) processOutbox() { + for { + t := <-s.outbox + go func() { + if err := s.sendTransaction(t); err != nil { + s.Logger.Errorw("error sending transaction", "err", err) + } + }() + } +} + func (s *Server) Serve(ctx context.Context, ln net.Listener) error { + go s.processOutbox() + for { conn, err := ln.Accept() if err != nil { s.Logger.Errorw("error accepting connection", "err", err) } + connCtx := context.WithValue(ctx, contextKeyReq, requestCtx{ + remoteAddr: conn.RemoteAddr().String(), + }) go func() { - for { - t := <-s.outbox - go func() { - if err := s.sendTransaction(t); err != nil { - s.Logger.Errorw("error sending transaction", "err", err) - } - }() - } - }() - go func() { - if err := s.handleNewConnection(ctx, conn, conn.RemoteAddr().String()); err != nil { + if err := s.handleNewConnection(connCtx, conn, conn.RemoteAddr().String()); err != nil { s.Logger.Infow("New client connection established", "RemoteAddr", conn.RemoteAddr()) if err == io.EOF { s.Logger.Infow("Client disconnected", "RemoteAddr", conn.RemoteAddr()) @@ -335,7 +341,7 @@ func (s *Server) writeThreadedNews() error { return err } -func (s *Server) NewClientConn(conn net.Conn, remoteAddr string) *ClientConn { +func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *ClientConn { s.mux.Lock() defer s.mux.Unlock() @@ -579,7 +585,9 @@ func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteA *c.Flags = []byte{0, 2} } - s.Logger.Infow("Client connection received", "login", login, "version", *c.Version, "RemoteAddr", remoteAddr) + c.logger = s.Logger.With("remoteAddr", remoteAddr, "login", login) + + c.logger.Infow("Client connection received", "version", fmt.Sprintf("%x", *c.Version)) s.outbox <- c.NewReply(clientLogin, NewField(fieldVersion, []byte{0x00, 0xbe}), @@ -597,6 +605,8 @@ func (s *Server) handleNewConnection(ctx context.Context, conn net.Conn, remoteA if *c.Version == nil || bytes.Equal(*c.Version, nostalgiaVersion) { c.Agreed = true + c.logger = c.logger.With("name", string(c.UserName)) + c.notifyOthers( *NewTransaction( tranNotifyChangeUser, nil, @@ -765,6 +775,9 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro } err = sendFile(wr, rFile, int(dataOffset)) + if err != nil { + return err + } if err := wr.Flush(); err != nil { return err @@ -831,7 +844,7 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro if err := file.Close(); err != nil { return err } - + if err := s.FS.Rename(destinationFile+".incomplete", destinationFile); err != nil { return err } @@ -1004,6 +1017,10 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro return nil }) + if err != nil { + return err + } + case FolderUpload: dstPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName) if err != nil { diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index cf55c07..5763967 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -406,7 +406,9 @@ func HandleSetFileInfo(cc *ClientConn, t *Transaction) (res []Transaction, err e } } - hlFile.ffo.FlatFileInformationFork.setComment(t.GetField(fieldFileComment).Data) + if err := hlFile.ffo.FlatFileInformationFork.setComment(t.GetField(fieldFileComment).Data); err != nil { + return res, err + } w, err := hlFile.infoForkWriter() if err != nil { return res, err @@ -521,9 +523,12 @@ func HandleMoveFile(cc *ClientConn, t *Transaction) (res []Transaction, err erro return res, err } - cc.Server.Logger.Debugw("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName) + cc.logger.Infow("Move file", "src", filePath+"/"+fileName, "dst", fileNewPath+"/"+fileName) hlFile, err := newFileWrapper(cc.Server.FS, filePath, 0) + if err != nil { + return res, err + } fi, err := hlFile.dataFile() if err != nil { @@ -715,7 +720,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er if len(subFields) == 1 { login := DecodeUserString(getField(fieldData, &subFields).Data) - cc.Server.Logger.Infow("DeleteUser", "login", login) + cc.logger.Infow("DeleteUser", "login", login) if !authorize(cc.Account.Access, accessDeleteUser) { res = append(res, cc.NewErrReply(t, "You are not allowed to delete accounts.")) @@ -732,7 +737,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er // check if the login dataFile; if so, we know we are updating an existing user if acc, ok := cc.Server.Accounts[login]; ok { - cc.Server.Logger.Infow("UpdateUser", "login", login) + cc.logger.Infow("UpdateUser", "login", login) // account dataFile, so this is an update action if !authorize(cc.Account.Access, accessModifyUser) { @@ -762,7 +767,7 @@ func HandleUpdateUser(cc *ClientConn, t *Transaction) (res []Transaction, err er return res, err } } else { - cc.Server.Logger.Infow("CreateUser", "login", login) + cc.logger.Infow("CreateUser", "login", login) if !authorize(cc.Account.Access, accessCreateUser) { res = append(res, cc.NewErrReply(t, "You are not allowed to create new accounts.")) @@ -933,6 +938,8 @@ func HandleTranAgreed(cc *ClientConn, t *Transaction) (res []Transaction, err er cc.UserName = t.GetField(fieldUserName).Data *cc.Icon = t.GetField(fieldUserIconID).Data + cc.logger = cc.logger.With("name", string(cc.UserName)) + options := t.GetField(fieldOptions).Data optBitmap := big.NewInt(int64(binary.BigEndian.Uint16(options))) @@ -1054,9 +1061,6 @@ func HandleGetNewsCatNameList(cc *ClientConn, t *Transaction) (res []Transaction return res, err } - newsPath := t.GetField(fieldNewsPath).Data - cc.Server.Logger.Infow("NewsPath: ", "np", string(newsPath)) - pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) cats := cc.Server.GetNewsCatByPath(pathStrs) @@ -1119,7 +1123,7 @@ func HandleNewNewsFldr(cc *ClientConn, t *Transaction) (res []Transaction, err e name := string(t.GetField(fieldFileName).Data) pathStrs := ReadNewsPath(t.GetField(fieldNewsPath).Data) - cc.Server.Logger.Infof("Creating new news folder %s", name) + cc.logger.Infof("Creating new news folder %s", name) cats := cc.Server.GetNewsCatByPath(pathStrs) cats[name] = NewsCategoryListData15{ @@ -1224,7 +1228,7 @@ func HandleDelNewsItem(cc *ClientConn, t *Transaction) (res []Transaction, err e // TODO: determine if path is a Folder (Bundle) or Category and check for permission - cc.Server.Logger.Infof("DelNewsItem %v", pathStrs) + cc.logger.Infof("DelNewsItem %v", pathStrs) cats := cc.Server.ThreadedNews.Categories @@ -1911,7 +1915,7 @@ func HandleMakeAlias(cc *ClientConn, t *Transaction) (res []Transaction, err err return res, err } - cc.Server.Logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath) + cc.logger.Debugw("Make alias", "src", fullFilePath, "dst", fullNewFilePath) if err := cc.Server.FS.Symlink(fullFilePath, fullNewFilePath); err != nil { res = append(res, cc.NewErrReply(t, "Error creating alias"))