"fmt"
"go.uber.org/zap"
"io"
+ "io/fs"
"io/ioutil"
"math/big"
"math/rand"
"sync"
"time"
- "gopkg.in/yaml.v2"
+ "gopkg.in/yaml.v3"
)
const (
}
s.Accounts[login] = &account
- return ioutil.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
return err
}
decoder := yaml.NewDecoder(fh)
- decoder.SetStrict(true)
return decoder.Decode(s.ThreadedNews)
}
account := Account{}
decoder := yaml.NewDecoder(fh)
- decoder.SetStrict(true)
if err := decoder.Decode(&account); err != nil {
return err
}
}
decoder := yaml.NewDecoder(fh)
- decoder.SetStrict(true)
err = decoder.Decode(s.Config)
if err != nil {
return err
// 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)
}
}()
+ defer func() {
+ if r := recover(); r != nil {
+ fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
+ s.Logger.Errorw("PANIC", "err", r, "trace", string(debug.Stack()))
+ }
+ }()
+
txBuf := make([]byte, 16)
- _, err := conn.Read(txBuf)
- if err != nil {
+ if _, err := io.ReadFull(conn, txBuf); err != nil {
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[:])
- 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:
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)
- // 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)
}
sendBuffer := make([]byte, 1048576)
+ var totalSent int64
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
}
+ if err != nil {
+ return err
+ }
+ totalSent += int64(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)
- 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)
- 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)
// [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
if _, err := conn.Read(nextAction); err != nil {
return err
}
- if nextAction[1] == 3 {
- return nil
- }
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 := conn.Read(nextAction); err != nil {
+ return err
+ }
+
+ resumeDataLen := binary.BigEndian.Uint16(nextAction)
+ resumeDataBytes := make([]byte, resumeDataLen)
+ if _, err := conn.Read(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, "/")
- 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
}
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
- // Read the client's Next Action request
+ // Read the client's Next Action request. This is always 3, I think?
if _, err := conn.Read(nextAction); err != nil {
return err
}
- // TODO: switch behavior based on possible next action
- return err
+ return nil
})
case FolderUpload:
// 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 {
- s.Logger.Error(err)
+ return err
}
}
}
fileSize := make([]byte, 4)
- itemCount := binary.BigEndian.Uint16(fileTransfer.FolderItemCount)
-
readBuffer := make([]byte, 1024)
- for i := uint16(0); i < itemCount; i++ {
+
+ for i := 0; i < fileTransfer.ItemCount(); i++ {
+
_, err := conn.Read(readBuffer)
if err != nil {
return 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 {
- s.Logger.Error(err)
+ return err
}
}
// Tell client to send next file
if _, err := conn.Write([]byte{0, dlFldrActionNextFile}); err != nil {
- s.Logger.Error(err)
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
}
+ 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
}
+ 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
}
- 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 := conn.Read(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 {
- s.Logger.Error(err)
return err
}
-
}
}
s.Logger.Infof("Folder upload complete")