X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/c4208f864659a6d5b5d9a2a7e0fe069565033f6b..c26c20e3539233a6cde9e64aa9d9a89b2017a772:/hotline/server.go diff --git a/hotline/server.go b/hotline/server.go index 2bd5c04..f164df1 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -236,9 +236,15 @@ func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredL *server.NextGuestID = 1 if server.Config.EnableTrackerRegistration { + server.Logger.Infow( + "Tracker registration enabled", + "frequency", fmt.Sprintf("%vs", trackerUpdateFrequency), + "trackers", server.Config.Trackers, + ) + go func() { for { - tr := TrackerRegistration{ + tr := &TrackerRegistration{ Port: []byte{0x15, 0x7c}, UserCount: server.userCount(), PassID: server.TrackerPassID, @@ -246,11 +252,10 @@ func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredL Description: server.Config.Description, } for _, t := range server.Config.Trackers { - server.Logger.Infof("Registering with tracker %v", t) - 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) } time.Sleep(trackerUpdateFrequency * time.Second) @@ -737,11 +742,27 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error { } case FileUpload: destinationFile := s.Config.FileRoot + ReadFilePath(fileTransfer.FilePath) + "/" + string(fileTransfer.FileName) - tmpFile := destinationFile + ".incomplete" - file, err := effectiveFile(destinationFile) + var file *os.File + + // A file upload has three possible cases: + // 1) Upload a new file + // 2) Resume a partially transferred file + // 3) Replace a fully uploaded file + // Unfortunately we have to infer which case applies by inspecting what is already on the file system + + // 1) Check for existing file: + _, err := os.Stat(destinationFile) + if err == nil { + // If found, that means this upload is intended to replace the file + if err = os.Remove(destinationFile); err != nil { + return err + } + file, err = os.Create(destinationFile + incompleteFileSuffix) + } if errors.Is(err, fs.ErrNotExist) { - file, err = FS.Create(tmpFile) + // If not found, open or create a new incomplete file + file, err = os.OpenFile(destinationFile+incompleteFileSuffix, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return err }