]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Refactor filestore to simplify testing
[rbdr/mobius] / hotline / server.go
index cfac442c089822854b09fd2c4b816a6f5d2b2d4a..33888e08f745a92d1f41802603ba2aadb619c3ce 100644 (file)
@@ -1,10 +1,12 @@
 package hotline
 
 import (
+       "bytes"
        "context"
        "encoding/binary"
        "errors"
        "fmt"
+       "github.com/go-playground/validator/v10"
        "go.uber.org/zap"
        "io"
        "io/fs"
@@ -30,6 +32,8 @@ const (
        trackerUpdateFrequency = 300 // time in seconds between tracker re-registration
 )
 
+var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
+
 type Server struct {
        Port          int
        Accounts      map[string]*Account
@@ -43,11 +47,10 @@ type Server struct {
        Logger        *zap.SugaredLogger
        PrivateChats  map[uint32]*PrivateChat
        NextGuestID   *uint16
-       TrackerPassID []byte
+       TrackerPassID [4]byte
        Stats         *Stats
 
-       APIListener  net.Listener
-       FileListener net.Listener
+       FS FileStore
 
        // newsReader io.Reader
        // newsWriter io.WriteCloser
@@ -64,27 +67,41 @@ type PrivateChat struct {
 }
 
 func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
-       s.Logger.Infow("Hotline server started", "version", VERSION)
+       s.Logger.Infow("Hotline server started",
+               "version", VERSION,
+               "API port", fmt.Sprintf(":%v", s.Port),
+               "Transfer port", fmt.Sprintf(":%v", s.Port+1),
+       )
+
        var wg sync.WaitGroup
 
        wg.Add(1)
-       go func() { s.Logger.Fatal(s.Serve(ctx, cancelRoot, s.APIListener)) }()
+       go func() {
+               ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port))
+               if err != nil {
+                       s.Logger.Fatal(err)
+               }
+
+               s.Logger.Fatal(s.Serve(ctx, cancelRoot, ln))
+       }()
 
        wg.Add(1)
-       go func() { s.Logger.Fatal(s.ServeFileTransfers(s.FileListener)) }()
+       go func() {
+               ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port+1))
+               if err != nil {
+                       s.Logger.Fatal(err)
+
+               }
+
+               s.Logger.Fatal(s.ServeFileTransfers(ln))
+       }()
 
        wg.Wait()
 
        return nil
 }
 
-func (s *Server) APIPort() int {
-       return s.APIListener.Addr().(*net.TCPAddr).Port
-}
-
 func (s *Server) ServeFileTransfers(ln net.Listener) error {
-       s.Logger.Infow("Hotline file transfer server started", "Addr", fmt.Sprintf(":%v", s.Port+1))
-
        for {
                conn, err := ln.Accept()
                if err != nil {
@@ -131,13 +148,12 @@ func (s *Server) sendTransaction(t Transaction) error {
                "IsReply", t.IsReply,
                "type", handler.Name,
                "sentBytes", n,
-               "remoteAddr", client.Connection.RemoteAddr(),
+               "remoteAddr", client.RemoteAddr,
        )
        return nil
 }
 
 func (s *Server) Serve(ctx context.Context, cancelRoot context.CancelFunc, ln net.Listener) error {
-       s.Logger.Infow("Hotline server started", "Addr", fmt.Sprintf(":%v", s.Port))
 
        for {
                conn, err := ln.Accept()
@@ -156,7 +172,7 @@ func (s *Server) Serve(ctx context.Context, cancelRoot context.CancelFunc, ln ne
                        }
                }()
                go func() {
-                       if err := s.handleNewConnection(conn); err != nil {
+                       if err := s.handleNewConnection(conn, conn.RemoteAddr().String()); err != nil {
                                if err == io.EOF {
                                        s.Logger.Infow("Client disconnected", "RemoteAddr", conn.RemoteAddr())
                                } else {
@@ -172,7 +188,7 @@ const (
 )
 
 // NewServer constructs a new Server from a config dir
-func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredLogger) (*Server, error) {
+func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredLogger, FS FileStore) (*Server, error) {
        server := Server{
                Port:          netPort,
                Accounts:      make(map[string]*Account),
@@ -186,32 +202,18 @@ func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredL
                outbox:        make(chan Transaction),
                Stats:         &Stats{StartTime: time.Now()},
                ThreadedNews:  &ThreadedNews{},
-               TrackerPassID: make([]byte, 4),
+               FS:            FS,
        }
 
-       ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", netInterface, netPort))
-       if err != nil {
-               return nil, err
-       }
-       server.APIListener = ln
-
-       if netPort != 0 {
-               netPort += 1
-       }
-
-       ln2, err := net.Listen("tcp", fmt.Sprintf("%s:%v", netInterface, netPort))
-       server.FileListener = ln2
-       if err != nil {
-               return nil, err
-       }
+       var err error
 
        // generate a new random passID for tracker registration
-       if _, err := rand.Read(server.TrackerPassID); err != nil {
+       if _, err := rand.Read(server.TrackerPassID[:]); err != nil {
                return nil, err
        }
 
-       server.Logger.Debugw("Loading Agreement", "path", configDir+agreementFile)
-       if server.Agreement, err = os.ReadFile(configDir + agreementFile); err != nil {
+       server.Agreement, err = os.ReadFile(configDir + agreementFile)
+       if err != nil {
                return nil, err
        }
 
@@ -245,12 +247,12 @@ func NewServer(configDir, netInterface string, netPort int, logger *zap.SugaredL
                go func() {
                        for {
                                tr := &TrackerRegistration{
-                                       Port:        []byte{0x15, 0x7c},
                                        UserCount:   server.userCount(),
-                                       PassID:      server.TrackerPassID,
+                                       PassID:      server.TrackerPassID[:],
                                        Name:        server.Config.Name,
                                        Description: server.Config.Description,
                                }
+                               binary.BigEndian.PutUint16(tr.Port[:], uint16(server.Port))
                                for _, t := range server.Config.Trackers {
                                        if err := register(t, tr); err != nil {
                                                server.Logger.Errorw("unable to register with tracker %v", "error", err)
@@ -319,7 +321,7 @@ func (s *Server) writeThreadedNews() error {
        return err
 }
 
-func (s *Server) NewClientConn(conn net.Conn) *ClientConn {
+func (s *Server) NewClientConn(conn net.Conn, remoteAddr string) *ClientConn {
        s.mux.Lock()
        defer s.mux.Unlock()
 
@@ -334,6 +336,7 @@ func (s *Server) NewClientConn(conn net.Conn) *ClientConn {
                AutoReply:  []byte{},
                Transfers:  make(map[int][]*FileTransfer),
                Agreed:     false,
+               RemoteAddr: remoteAddr,
        }
        *s.NextGuestID++
        ID := *s.NextGuestID
@@ -361,15 +364,13 @@ func (s *Server) NewUser(login, name, password string, access []byte) error {
        }
        s.Accounts[login] = &account
 
-       return FS.WriteFile(s.ConfigDir+"Users/"+login+".yaml", out, 0666)
+       return s.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")
@@ -403,7 +404,7 @@ func (s *Server) DeleteUser(login string) error {
 
        delete(s.Accounts, login)
 
-       return FS.Remove(s.ConfigDir + "Users/" + login + ".yaml")
+       return s.FS.Remove(s.ConfigDir + "Users/" + login + ".yaml")
 }
 
 func (s *Server) connectedUsers() []Field {
@@ -449,7 +450,7 @@ func (s *Server) loadAccounts(userDir string) error {
        }
 
        for _, file := range matches {
-               fh, err := FS.Open(file)
+               fh, err := s.FS.Open(file)
                if err != nil {
                        return err
                }
@@ -466,7 +467,7 @@ func (s *Server) loadAccounts(userDir string) error {
 }
 
 func (s *Server) loadConfig(path string) error {
-       fh, err := FS.Open(path)
+       fh, err := s.FS.Open(path)
        if err != nil {
                return err
        }
@@ -476,6 +477,12 @@ func (s *Server) loadConfig(path string) error {
        if err != nil {
                return err
        }
+
+       validate := validator.New()
+       err = validate.Struct(s.Config)
+       if err != nil {
+               return err
+       }
        return nil
 }
 
@@ -493,14 +500,10 @@ func dontPanic(logger *zap.SugaredLogger) {
 }
 
 // handleNewConnection takes a new net.Conn and performs the initial login sequence
-func (s *Server) handleNewConnection(conn net.Conn) error {
+func (s *Server) handleNewConnection(conn net.Conn, remoteAddr string) error {
        defer dontPanic(s.Logger)
 
-       handshakeBuf := make([]byte, 12)
-       if _, err := io.ReadFull(conn, handshakeBuf); err != nil {
-               return err
-       }
-       if err := Handshake(conn, handshakeBuf); err != nil {
+       if err := Handshake(conn); err != nil {
                return err
        }
 
@@ -519,7 +522,7 @@ func (s *Server) handleNewConnection(conn net.Conn) error {
                return err
        }
 
-       c := s.NewClientConn(conn)
+       c := s.NewClientConn(conn, remoteAddr)
        defer c.Disconnect()
 
        encodedLogin := clientLogin.GetField(fieldUserLogin).Data
@@ -561,7 +564,7 @@ func (s *Server) handleNewConnection(conn net.Conn) error {
                *c.Flags = []byte{0, 2}
        }
 
-       s.Logger.Infow("Client connection received", "login", login, "version", *c.Version, "RemoteAddr", conn.RemoteAddr().String())
+       s.Logger.Infow("Client connection received", "login", login, "version", *c.Version, "RemoteAddr", remoteAddr)
 
        s.outbox <- c.NewReply(clientLogin,
                NewField(fieldVersion, []byte{0x00, 0xbe}),
@@ -575,8 +578,8 @@ func (s *Server) handleNewConnection(conn net.Conn) error {
        // Show agreement to client
        c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement))
 
-       // assume simplified hotline v1.2.3 login flow that does not require agreement
-       if *c.Version == nil {
+       // Used simplified hotline v1.2.3 login flow for clients that do not send login info in tranAgreed
+       if *c.Version == nil || bytes.Equal(*c.Version, nostalgiaVersion) {
                c.Agreed = true
 
                c.notifyOthers(
@@ -714,7 +717,7 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                        }
                }
 
-               file, err := FS.Open(fullFilePath)
+               file, err := s.FS.Open(fullFilePath)
                if err != nil {
                        return err
                }
@@ -742,11 +745,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
                        }
@@ -890,7 +909,7 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                                return err
                        }
 
-                       file, err := FS.Open(path)
+                       file, err := s.FS.Open(path)
                        if err != nil {
                                return err
                        }
@@ -947,8 +966,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) {
-                       if err := FS.Mkdir(dstPath, 0777); err != nil {
+               if _, err := s.FS.Stat(dstPath); os.IsNotExist(err) {
+                       if err := s.FS.Mkdir(dstPath, 0777); err != nil {
                                return err
                        }
                }
@@ -1061,7 +1080,7 @@ func (s *Server) handleFileTransfer(conn io.ReadWriteCloser) error {
                                        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")
+                                       newFile, err := s.FS.Create(filePath + ".incomplete")
                                        if err != nil {
                                                return err
                                        }