]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Replace unsafe conn.Read with io.ReadFull
[rbdr/mobius] / hotline / server.go
index b0ddf2d06d7db5ea233c6db8f37dc749ff98ea80..2bd5c04bc01dbe6cd05bb6d9f27848c0cd6b2685 100644 (file)
@@ -7,6 +7,7 @@ import (
        "fmt"
        "go.uber.org/zap"
        "io"
        "fmt"
        "go.uber.org/zap"
        "io"
+       "io/fs"
        "io/ioutil"
        "math/big"
        "math/rand"
        "io/ioutil"
        "math/big"
        "math/rand"
@@ -358,6 +359,38 @@ func (s *Server) NewUser(login, name, password string, access []byte) error {
        return FS.WriteFile(s.ConfigDir+"Users/"+login+".yaml", out, 0666)
 }
 
        return FS.WriteFile(s.ConfigDir+"Users/"+login+".yaml", out, 0666)
 }
 
+func (s *Server) UpdateUser(login, newLogin, name, password string, access []byte) error {
+       s.mux.Lock()
+       defer s.mux.Unlock()
+
+       fmt.Printf("login: %v, newLogin: %v: ", login, newLogin)
+
+       // update renames the user login
+       if login != newLogin {
+               err := os.Rename(s.ConfigDir+"Users/"+login+".yaml", s.ConfigDir+"Users/"+newLogin+".yaml")
+               if err != nil {
+                       return err
+               }
+               s.Accounts[newLogin] = s.Accounts[login]
+               delete(s.Accounts, login)
+       }
+
+       account := s.Accounts[newLogin]
+       account.Access = &access
+       account.Name = name
+       account.Password = password
+
+       out, err := yaml.Marshal(&account)
+       if err != nil {
+               return err
+       }
+       if err := os.WriteFile(s.ConfigDir+"Users/"+newLogin+".yaml", out, 0666); err != nil {
+               return err
+       }
+
+       return nil
+}
+
 // DeleteUser deletes the user account
 func (s *Server) DeleteUser(login string) error {
        s.mux.Lock()
 // DeleteUser deletes the user account
 func (s *Server) DeleteUser(login string) error {
        s.mux.Lock()
@@ -445,17 +478,29 @@ const (
        minTransactionLen = 22 // minimum length of any transaction
 )
 
        minTransactionLen = 22 // minimum length of any transaction
 )
 
+// dontPanic recovers and logs panics instead of crashing
+// TODO: remove this after known issues are fixed
+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(conn net.Conn) error {
 // handleNewConnection takes a new net.Conn and performs the initial login sequence
 func (s *Server) handleNewConnection(conn net.Conn) error {
-       handshakeBuf := make([]byte, 12) // handshakes are always 12 bytes in length
-       if _, err := conn.Read(handshakeBuf); err != nil {
+       defer dontPanic(s.Logger)
+
+       handshakeBuf := make([]byte, 12)
+       if _, err := io.ReadFull(conn, handshakeBuf); err != nil {
                return err
        }
                return err
        }
-       if err := Handshake(conn, handshakeBuf[:12]); err != nil {
+       if err := Handshake(conn, handshakeBuf); err != nil {
                return err
        }
 
        buf := make([]byte, 1024)
                return err
        }
 
        buf := make([]byte, 1024)
+       // TODO: fix potential short read with io.ReadFull
        readLen, err := conn.Read(buf)
        if readLen < minTransactionLen {
                return err
        readLen, err := conn.Read(buf)
        if readLen < minTransactionLen {
                return err
@@ -471,13 +516,6 @@ func (s *Server) handleNewConnection(conn net.Conn) error {
 
        c := s.NewClientConn(conn)
        defer c.Disconnect()
 
        c := s.NewClientConn(conn)
        defer c.Disconnect()
-       defer func() {
-               if r := recover(); r != nil {
-                       fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
-                       c.Server.Logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack()))
-                       c.Disconnect()
-               }
-       }()
 
        encodedLogin := clientLogin.GetField(fieldUserLogin).Data
        encodedPassword := clientLogin.GetField(fieldUserPassword).Data
 
        encodedLogin := clientLogin.GetField(fieldUserLogin).Data
        encodedPassword := clientLogin.GetField(fieldUserPassword).Data
@@ -613,25 +651,37 @@ const dlFldrActionNextFile = 3
 // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
 func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
        defer func() {
 // handleFileTransfer receives a client net.Conn from the file transfer server, performs the requested transfer type, then closes the connection
 func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
        defer func() {
+
                if err := conn.Close(); err != nil {
                        s.Logger.Errorw("error closing connection", "error", err)
                }
        }()
 
                if err := conn.Close(); err != nil {
                        s.Logger.Errorw("error closing connection", "error", err)
                }
        }()
 
+       defer dontPanic(s.Logger)
+
        txBuf := make([]byte, 16)
        txBuf := make([]byte, 16)
-       _, err := conn.Read(txBuf)
-       if err != nil {
+       if _, err := io.ReadFull(conn, txBuf); err != nil {
                return err
        }
 
        var t transfer
                return err
        }
 
        var t transfer
-       _, err = t.Write(txBuf)
-       if err != nil {
+       if _, err := t.Write(txBuf); err != nil {
                return err
        }
 
        transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
                return err
        }
 
        transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
-       fileTransfer := s.FileTransfers[transferRefNum]
+       defer func() {
+               s.mux.Lock()
+               delete(s.FileTransfers, transferRefNum)
+               s.mux.Unlock()
+       }()
+
+       s.mux.Lock()
+       fileTransfer, ok := s.FileTransfers[transferRefNum]
+       s.mux.Unlock()
+       if !ok {
+               return errors.New("invalid transaction ID")
+       }
 
        switch fileTransfer.Type {
        case FileDownload:
 
        switch fileTransfer.Type {
        case FileDownload:
@@ -640,20 +690,23 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                        return err
                }
 
                        return err
                }
 
-               ffo, err := NewFlattenedFileObject(
-                       s.Config.FileRoot,
-                       fileTransfer.FilePath,
-                       fileTransfer.FileName,
-               )
+               var dataOffset int64
+               if fileTransfer.fileResumeData != nil {
+                       dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:]))
+               }
+
+               ffo, err := NewFlattenedFileObject(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName, dataOffset)
                if err != nil {
                        return err
                }
 
                s.Logger.Infow("File download started", "filePath", fullFilePath, "transactionRef", fileTransfer.ReferenceNumber)
 
                if err != nil {
                        return err
                }
 
                s.Logger.Infow("File download started", "filePath", fullFilePath, "transactionRef", fileTransfer.ReferenceNumber)
 
-               // Start by sending flat file object to client
-               if _, err := conn.Write(ffo.BinaryMarshal()); err != nil {
-                       return err
+               if fileTransfer.options == nil {
+                       // Start by sending flat file object to client
+                       if _, err := conn.Write(ffo.BinaryMarshal()); err != nil {
+                               return err
+                       }
                }
 
                file, err := FS.Open(fullFilePath)
                }
 
                file, err := FS.Open(fullFilePath)
@@ -662,32 +715,49 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                }
 
                sendBuffer := make([]byte, 1048576)
                }
 
                sendBuffer := make([]byte, 1048576)
+               var totalSent int64
                for {
                        var bytesRead int
                for {
                        var bytesRead int
-                       if bytesRead, err = file.Read(sendBuffer); err == io.EOF {
+                       if bytesRead, err = file.ReadAt(sendBuffer, dataOffset+totalSent); err == io.EOF {
+                               if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil {
+                                       return err
+                               }
                                break
                        }
                                break
                        }
+                       if err != nil {
+                               return err
+                       }
+                       totalSent += int64(bytesRead)
 
                        fileTransfer.BytesSent += bytesRead
 
 
                        fileTransfer.BytesSent += bytesRead
 
-                       delete(s.FileTransfers, transferRefNum)
-
                        if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil {
                                return err
                        }
                }
        case FileUpload:
                destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName)
                        if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil {
                                return err
                        }
                }
        case FileUpload:
                destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName)
-               newFile, err := FS.Create(destinationFile)
-               if err != nil {
-                       return err
+               tmpFile := destinationFile + ".incomplete"
+
+               file, err := effectiveFile(destinationFile)
+               if errors.Is(err, fs.ErrNotExist) {
+                       file, err = FS.Create(tmpFile)
+                       if err != nil {
+                               return err
+                       }
                }
                }
-               defer func() { _ = newFile.Close() }()
+
+               defer func() { _ = file.Close() }()
 
                s.Logger.Infow("File upload started", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
 
 
                s.Logger.Infow("File upload started", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
 
-               if err := receiveFile(conn, newFile, nil); err != nil {
-                       s.Logger.Errorw("file upload error", "error", err)
+               // TODO: replace io.Discard with a real file when ready to implement storing of resource fork data
+               if err := receiveFile(conn, file, io.Discard); err != nil {
+                       return err
+               }
+
+               if err := os.Rename(destinationFile+".incomplete", destinationFile); err != nil {
+                       return err
                }
 
                s.Logger.Infow("File upload complete", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
                }
 
                s.Logger.Infow("File upload complete", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
@@ -708,7 +778,7 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                //                      [2]byte // Resume data size
                //                      []byte file resume data (see myField_FileResumeData)
                //
                //                      [2]byte // Resume data size
                //                      []byte file resume data (see myField_FileResumeData)
                //
-               // 3. Otherwise download of the file is requested and client sends []byte{0x00, 0x01}
+               // 3. Otherwise, download of the file is requested and client sends []byte{0x00, 0x01}
                //
                // When download is requested (case 2 or 3), server replies with:
                //                      [4]byte - file size
                //
                // When download is requested (case 2 or 3), server replies with:
                //                      [4]byte - file size
@@ -729,7 +799,7 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                s.Logger.Infow("Start folder download", "path", fullFilePath, "ReferenceNumber", fileTransfer.ReferenceNumber)
 
                nextAction := make([]byte, 2)
                s.Logger.Infow("Start folder download", "path", fullFilePath, "ReferenceNumber", fileTransfer.ReferenceNumber)
 
                nextAction := make([]byte, 2)
-               if _, err := conn.Read(nextAction); err != nil {
+               if _, err := io.ReadFull(conn, nextAction); err != nil {
                        return err
                }
 
                        return err
                }
 
@@ -755,26 +825,45 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                        }
 
                        // Read the client's Next Action request
                        }
 
                        // Read the client's Next Action request
-                       if _, err := conn.Read(nextAction); err != nil {
+                       if _, err := io.ReadFull(conn, nextAction); err != nil {
                                return err
                        }
                                return err
                        }
-                       if nextAction[1] == 3 {
-                               return nil
-                       }
 
                        s.Logger.Infow("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
 
 
                        s.Logger.Infow("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
 
+                       var dataOffset int64
+
+                       switch nextAction[1] {
+                       case dlFldrActionResumeFile:
+                               // client asked to resume this file
+                               var frd FileResumeData
+                               // get size of resumeData
+                               if _, err := io.ReadFull(conn, nextAction); err != nil {
+                                       return err
+                               }
+
+                               resumeDataLen := binary.BigEndian.Uint16(nextAction)
+                               resumeDataBytes := make([]byte, resumeDataLen)
+                               if _, err := io.ReadFull(conn, resumeDataBytes); err != nil {
+                                       return err
+                               }
+
+                               if err := frd.UnmarshalBinary(resumeDataBytes); err != nil {
+                                       return err
+                               }
+                               dataOffset = int64(binary.BigEndian.Uint32(frd.ForkInfoList[0].DataSize[:]))
+                       case dlFldrActionNextFile:
+                               // client asked to skip this file
+                               return nil
+                       }
+
                        if info.IsDir() {
                                return nil
                        }
 
                        splitPath := strings.Split(path, "/")
 
                        if info.IsDir() {
                                return nil
                        }
 
                        splitPath := strings.Split(path, "/")
 
-                       ffo, err := NewFlattenedFileObject(
-                               strings.Join(splitPath[:len(splitPath)-1], "/"),
-                               nil,
-                               []byte(info.Name()),
-                       )
+                       ffo, err := NewFlattenedFileObject(strings.Join(splitPath[:len(splitPath)-1], "/"), nil, []byte(info.Name()), dataOffset)
                        if err != nil {
                                return err
                        }
                        if err != nil {
                                return err
                        }
@@ -801,22 +890,42 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                                return err
                        }
 
                                return err
                        }
 
-                       // Copy N bytes from file to connection
-                       _, err = io.CopyN(conn, file, int64(binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:])))
-                       if err != nil {
-                               return err
+                       // // Copy N bytes from file to connection
+                       // _, err = io.CopyN(conn, file, int64(binary.BigEndian.Uint32(ffo.FlatFileDataForkHeader.DataSize[:])))
+                       // if err != nil {
+                       //      return err
+                       // }
+                       // file.Close()
+                       sendBuffer := make([]byte, 1048576)
+                       var totalSent int64
+                       for {
+                               var bytesRead int
+                               if bytesRead, err = file.ReadAt(sendBuffer, dataOffset+totalSent); err == io.EOF {
+                                       if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil {
+                                               return err
+                                       }
+                                       break
+                               }
+                               if err != nil {
+                                       panic(err)
+                               }
+                               totalSent += int64(bytesRead)
+
+                               fileTransfer.BytesSent += bytesRead
+
+                               if _, err := conn.Write(sendBuffer[:bytesRead]); err != nil {
+                                       return err
+                               }
                        }
                        }
-                       file.Close()
 
                        // TODO: optionally send resource fork header and resource fork data
 
 
                        // TODO: optionally send resource fork header and resource fork data
 
-                       // Read the client's Next Action request
-                       if _, err := conn.Read(nextAction); err != nil {
+                       // Read the client's Next Action request.  This is always 3, I think?
+                       if _, err := io.ReadFull(conn, nextAction); err != nil {
                                return err
                        }
                                return err
                        }
-                       // TODO: switch behavior based on possible next action
 
 
-                       return err
+                       return nil
                })
 
        case FolderUpload:
                })
 
        case FolderUpload:
@@ -834,9 +943,8 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
 
                // Check if the target folder exists.  If not, create it.
                if _, err := FS.Stat(dstPath); os.IsNotExist(err) {
 
                // Check if the target folder exists.  If not, create it.
                if _, err := FS.Stat(dstPath); os.IsNotExist(err) {
-                       s.Logger.Infow("Creating target path", "dstPath", dstPath)
                        if err := FS.Mkdir(dstPath, 0777); err != nil {
                        if err := FS.Mkdir(dstPath, 0777); err != nil {
-                               s.Logger.Error(err)
+                               return err
                        }
                }
 
                        }
                }
 
@@ -846,10 +954,10 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                }
 
                fileSize := make([]byte, 4)
                }
 
                fileSize := make([]byte, 4)
-               itemCount := binary.BigEndian.Uint16(fileTransfer.FolderItemCount)
-
                readBuffer := make([]byte, 1024)
                readBuffer := make([]byte, 1024)
-               for i := uint16(0); i < itemCount; i++ {
+
+               for i := 0; i < fileTransfer.ItemCount(); i++ {
+                       // TODO: fix potential short read with io.ReadFull
                        _, err := conn.Read(readBuffer)
                        if err != nil {
                                return err
                        _, err := conn.Read(readBuffer)
                        if err != nil {
                                return err
@@ -866,48 +974,106 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
 
                        if fu.IsFolder == [2]byte{0, 1} {
                                if _, err := os.Stat(dstPath + "/" + fu.FormattedPath()); os.IsNotExist(err) {
 
                        if fu.IsFolder == [2]byte{0, 1} {
                                if _, err := os.Stat(dstPath + "/" + fu.FormattedPath()); os.IsNotExist(err) {
-                                       s.Logger.Infow("Target path does not exist; Creating...", "dstPath", dstPath)
                                        if err := os.Mkdir(dstPath+"/"+fu.FormattedPath(), 0777); err != nil {
                                        if err := os.Mkdir(dstPath+"/"+fu.FormattedPath(), 0777); err != nil {
-                                               s.Logger.Error(err)
+                                               return err
                                        }
                                }
 
                                // Tell client to send next file
                                if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil {
                                        }
                                }
 
                                // Tell client to send next file
                                if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil {
-                                       s.Logger.Error(err)
                                        return err
                                }
                        } else {
                                        return err
                                }
                        } else {
-                               // TODO: Check if we have the full file already.  If so, send dlFldrAction_NextFile to client to skip.
-                               // TODO: Check if we have a partial file already.  If so, send dlFldrAction_ResumeFile to client to resume upload.
-                               // Send dlFldrAction_SendFile to client to begin transfer
-                               if _, err := conn.Write([]byte{0, dlFldrActionSendFile}); err != nil {
+                               nextAction := dlFldrActionSendFile
+
+                               // Check if we have the full file already.  If so, send dlFldrAction_NextFile to client to skip.
+                               _, err := os.Stat(dstPath + "/" + fu.FormattedPath())
+                               if err != nil && !errors.Is(err, fs.ErrNotExist) {
                                        return err
                                }
                                        return err
                                }
+                               if err == nil {
+                                       nextAction = dlFldrActionNextFile
+                               }
 
 
-                               if _, err := conn.Read(fileSize); err != nil {
+                               //  Check if we have a partial file already.  If so, send dlFldrAction_ResumeFile to client to resume upload.
+                               inccompleteFile, err := os.Stat(dstPath + "/" + fu.FormattedPath() + incompleteFileSuffix)
+                               if err != nil && !errors.Is(err, fs.ErrNotExist) {
                                        return err
                                }
                                        return err
                                }
+                               if err == nil {
+                                       nextAction = dlFldrActionResumeFile
+                               }
 
 
-                               filePath := dstPath + "/" + fu.FormattedPath()
-                               s.Logger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "totalFiles", itemCount, "fileSize", binary.BigEndian.Uint32(fileSize))
+                               fmt.Printf("Next Action: %v\n", nextAction)
 
 
-                               newFile, err := FS.Create(filePath)
-                               if err != nil {
+                               if _, err := conn.Write([]byte{0, uint8(nextAction)}); err != nil {
                                        return err
                                }
 
                                        return err
                                }
 
-                               if err := receiveFile(conn, newFile, ioutil.Discard); err != nil {
-                                       s.Logger.Error(err)
+                               switch nextAction {
+                               case dlFldrActionNextFile:
+                                       continue
+                               case dlFldrActionResumeFile:
+                                       offset := make([]byte, 4)
+                                       binary.BigEndian.PutUint32(offset, uint32(inccompleteFile.Size()))
+
+                                       file, err := os.OpenFile(dstPath+"/"+fu.FormattedPath()+incompleteFileSuffix, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+                                       if err != nil {
+                                               return err
+                                       }
+
+                                       fileResumeData := NewFileResumeData([]ForkInfoList{
+                                               *NewForkInfoList(offset),
+                                       })
+
+                                       b, _ := fileResumeData.BinaryMarshal()
+
+                                       bs := make([]byte, 2)
+                                       binary.BigEndian.PutUint16(bs, uint16(len(b)))
+
+                                       if _, err := conn.Write(append(bs, b...)); err != nil {
+                                               return err
+                                       }
+
+                                       if _, err := io.ReadFull(conn, fileSize); err != nil {
+                                               return err
+                                       }
+
+                                       if err := receiveFile(conn, file, ioutil.Discard); err != nil {
+                                               s.Logger.Error(err)
+                                       }
+
+                                       err = os.Rename(dstPath+"/"+fu.FormattedPath()+".incomplete", dstPath+"/"+fu.FormattedPath())
+                                       if err != nil {
+                                               return err
+                                       }
+
+                               case dlFldrActionSendFile:
+                                       if _, err := conn.Read(fileSize); err != nil {
+                                               return err
+                                       }
+
+                                       filePath := dstPath + "/" + fu.FormattedPath()
+                                       s.Logger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "totalFiles", "zz", "fileSize", binary.BigEndian.Uint32(fileSize))
+
+                                       newFile, err := FS.Create(filePath + ".incomplete")
+                                       if err != nil {
+                                               return err
+                                       }
+
+                                       if err := receiveFile(conn, newFile, ioutil.Discard); err != nil {
+                                               s.Logger.Error(err)
+                                       }
+                                       _ = newFile.Close()
+                                       if err := os.Rename(filePath+".incomplete", filePath); err != nil {
+                                               return err
+                                       }
                                }
                                }
-                               _ = newFile.Close()
 
                                // Tell client to send next file
                                if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil {
 
                                // Tell client to send next file
                                if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil {
-                                       s.Logger.Error(err)
                                        return err
                                }
                                        return err
                                }
-
                        }
                }
                s.Logger.Infof("Folder upload complete")
                        }
                }
                s.Logger.Infof("Folder upload complete")