]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/server.go
Wrap file transfer errors with more context
[rbdr/mobius] / hotline / server.go
index 11ec21d1b9625501cb72e6d9e2d9b5458fe813aa..b7f39b62d00e282a93d872efdfe725bddb1d8d79 100644 (file)
@@ -4,21 +4,17 @@ import (
        "bufio"
        "bytes"
        "context"
+       "crypto/rand"
        "encoding/binary"
        "errors"
        "fmt"
-       "github.com/go-playground/validator/v10"
-       "go.uber.org/zap"
-       "gopkg.in/yaml.v3"
+       "golang.org/x/text/encoding/charmap"
+       "golang.org/x/time/rate"
        "io"
-       "io/fs"
-       "io/ioutil"
-       "math/big"
-       "math/rand"
+       "log"
+       "log/slog"
        "net"
        "os"
-       "path/filepath"
-       "runtime/debug"
        "strings"
        "sync"
        "time"
@@ -30,75 +26,131 @@ var contextKeyReq = contextKey("req")
 
 type requestCtx struct {
        remoteAddr string
-       login      string
-       name       string
 }
 
-const (
-       userIdleSeconds        = 300 // time in seconds before an inactive user is marked idle
-       idleCheckInterval      = 10  // time in seconds to check for idle users
-       trackerUpdateFrequency = 300 // time in seconds between tracker re-registration
-)
+// Converts bytes from Mac Roman encoding to UTF-8
+var txtDecoder = charmap.Macintosh.NewDecoder()
 
-var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client
+// Converts bytes from UTF-8 to Mac Roman encoding
+var txtEncoder = charmap.Macintosh.NewEncoder()
 
 type Server struct {
-       Port          int
-       Accounts      map[string]*Account
-       Agreement     []byte
-       Clients       map[uint16]*ClientConn
-       ThreadedNews  *ThreadedNews
-       FileTransfers map[uint32]*FileTransfer
-       Config        *Config
-       ConfigDir     string
-       Logger        *zap.SugaredLogger
-       PrivateChats  map[uint32]*PrivateChat
-       NextGuestID   *uint16
+       NetInterface string
+       Port         int
+
+       rateLimiters map[string]*rate.Limiter
+
+       handlers map[TranType]HandlerFunc
+
+       Config Config
+       Logger *slog.Logger
+
        TrackerPassID [4]byte
-       Stats         *Stats
+
+       Stats Counter
 
        FS FileStore // Storage backend to use for File storage
 
        outbox chan Transaction
-       mux    sync.Mutex
 
-       flatNewsMux sync.Mutex
-       FlatNews    []byte
+       Agreement io.ReadSeeker
+       Banner    []byte
+
+       FileTransferMgr FileTransferMgr
+       ChatMgr         ChatManager
+       ClientMgr       ClientManager
+       AccountManager  AccountManager
+       ThreadedNewsMgr ThreadedNewsMgr
+       BanList         BanMgr
+
+       MessageBoard io.ReadWriteSeeker
 }
 
-type PrivateChat struct {
-       Subject    string
-       ClientConn map[uint16]*ClientConn
+type Option = func(s *Server)
+
+func WithConfig(config Config) func(s *Server) {
+       return func(s *Server) {
+               s.Config = config
+       }
 }
 
-func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFunc) error {
-       s.Logger.Infow("Hotline server started",
-               "version", VERSION,
-               "API port", fmt.Sprintf(":%v", s.Port),
-               "Transfer port", fmt.Sprintf(":%v", s.Port+1),
-       )
+func WithLogger(logger *slog.Logger) func(s *Server) {
+       return func(s *Server) {
+               s.Logger = logger
+       }
+}
+
+// WithPort optionally overrides the default TCP port.
+func WithPort(port int) func(s *Server) {
+       return func(s *Server) {
+               s.Port = port
+       }
+}
+
+// WithInterface optionally sets a specific interface to listen on.
+func WithInterface(netInterface string) func(s *Server) {
+       return func(s *Server) {
+               s.NetInterface = netInterface
+       }
+}
+
+type ServerConfig struct {
+}
+
+func NewServer(options ...Option) (*Server, error) {
+       server := Server{
+               handlers:        make(map[TranType]HandlerFunc),
+               outbox:          make(chan Transaction),
+               rateLimiters:    make(map[string]*rate.Limiter),
+               FS:              &OSFileStore{},
+               ChatMgr:         NewMemChatManager(),
+               ClientMgr:       NewMemClientMgr(),
+               FileTransferMgr: NewMemFileTransferMgr(),
+               Stats:           NewStats(),
+       }
+
+       for _, opt := range options {
+               opt(&server)
+       }
+
+       // generate a new random passID for tracker registration
+       _, err := rand.Read(server.TrackerPassID[:])
+       if err != nil {
+               return nil, err
+       }
+
+       return &server, nil
+}
+
+func (s *Server) CurrentStats() map[string]interface{} {
+       return s.Stats.Values()
+}
+
+func (s *Server) ListenAndServe(ctx context.Context) error {
+       go s.registerWithTrackers(ctx)
+       go s.keepaliveHandler(ctx)
+       go s.processOutbox()
 
        var wg sync.WaitGroup
 
        wg.Add(1)
        go func() {
-               ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port))
+               ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, s.Port))
                if err != nil {
-                       s.Logger.Fatal(err)
+                       log.Fatal(err)
                }
 
-               s.Logger.Fatal(s.Serve(ctx, ln))
+               log.Fatal(s.Serve(ctx, ln))
        }()
 
        wg.Add(1)
        go func() {
-               ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port+1))
+               ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", s.NetInterface, s.Port+1))
                if err != nil {
-                       s.Logger.Fatal(err)
-
+                       log.Fatal(err)
                }
 
-               s.Logger.Fatal(s.ServeFileTransfers(ctx, ln))
+               log.Fatal(s.ServeFileTransfers(ctx, ln))
        }()
 
        wg.Wait()
@@ -117,53 +169,29 @@ func (s *Server) ServeFileTransfers(ctx context.Context, ln net.Listener) error
                        defer func() { _ = conn.Close() }()
 
                        err = s.handleFileTransfer(
-                               context.WithValue(ctx, contextKeyReq, requestCtx{
-                                       remoteAddr: conn.RemoteAddr().String(),
-                               }),
+                               context.WithValue(ctx, contextKeyReq, requestCtx{remoteAddr: conn.RemoteAddr().String()}),
                                conn,
                        )
 
                        if err != nil {
-                               s.Logger.Errorw("file transfer error", "reason", err)
+                               s.Logger.Error("file transfer error", "err", err)
                        }
                }()
        }
 }
 
 func (s *Server) sendTransaction(t Transaction) error {
-       requestNum := binary.BigEndian.Uint16(t.Type)
-       clientID, err := byteToInt(*t.clientID)
-       if err != nil {
-               return err
-       }
+       client := s.ClientMgr.Get(t.ClientID)
 
-       s.mux.Lock()
-       client := s.Clients[uint16(clientID)]
-       s.mux.Unlock()
        if client == nil {
-               return fmt.Errorf("invalid client id %v", *t.clientID)
+               return nil
        }
-       userName := string(client.UserName)
-       login := client.Account.Login
 
-       handler := TransactionHandlers[requestNum]
-
-       b, err := t.MarshalBinary()
+       _, err := io.Copy(client.Connection, &t)
        if err != nil {
-               return err
+               return fmt.Errorf("failed to send transaction to client %v: %v", t.ClientID, err)
        }
-       var n int
-       if n, err = client.Connection.Write(b); err != nil {
-               return err
-       }
-       s.Logger.Debugw("Sent Transaction",
-               "name", userName,
-               "login", login,
-               "IsReply", t.IsReply,
-               "type", handler.Name,
-               "sentBytes", n,
-               "remoteAddr", client.RemoteAddr,
-       )
+
        return nil
 }
 
@@ -172,1033 +200,431 @@ func (s *Server) processOutbox() {
                t := <-s.outbox
                go func() {
                        if err := s.sendTransaction(t); err != nil {
-                               s.Logger.Errorw("error sending transaction", "err", err)
+                               s.Logger.Error("error sending transaction", "err", err)
                        }
                }()
        }
 }
 
-func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
-       go s.processOutbox()
+// perIPRateLimit controls how frequently an IP address can connect before being throttled.
+// 0.5 = 1 connection every 2 seconds
+const perIPRateLimit = rate.Limit(0.5)
 
+func (s *Server) Serve(ctx context.Context, ln net.Listener) error {
        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() {
-                       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())
-                               } else {
-                                       s.Logger.Errorw("error serving request", "RemoteAddr", conn.RemoteAddr(), "err", err)
-                               }
+               select {
+               case <-ctx.Done():
+                       s.Logger.Info("Server shutting down")
+                       return ctx.Err()
+               default:
+                       conn, err := ln.Accept()
+                       if err != nil {
+                               s.Logger.Error("Error accepting connection", "err", err)
+                               continue
                        }
-               }()
-       }
-}
-
-const (
-       agreementFile = "Agreement.txt"
-)
-
-// NewServer constructs a new Server from a config dir
-func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS FileStore) (*Server, error) {
-       server := Server{
-               Port:          netPort,
-               Accounts:      make(map[string]*Account),
-               Config:        new(Config),
-               Clients:       make(map[uint16]*ClientConn),
-               FileTransfers: make(map[uint32]*FileTransfer),
-               PrivateChats:  make(map[uint32]*PrivateChat),
-               ConfigDir:     configDir,
-               Logger:        logger,
-               NextGuestID:   new(uint16),
-               outbox:        make(chan Transaction),
-               Stats:         &Stats{StartTime: time.Now()},
-               ThreadedNews:  &ThreadedNews{},
-               FS:            FS,
-       }
-
-       var err error
 
-       // generate a new random passID for tracker registration
-       if _, err := rand.Read(server.TrackerPassID[:]); err != nil {
-               return nil, err
-       }
+                       go func() {
+                               ipAddr := strings.Split(conn.RemoteAddr().(*net.TCPAddr).String(), ":")[0]
 
-       server.Agreement, err = os.ReadFile(filepath.Join(configDir, agreementFile))
-       if err != nil {
-               return nil, err
-       }
+                               connCtx := context.WithValue(ctx, contextKeyReq, requestCtx{
+                                       remoteAddr: conn.RemoteAddr().String(),
+                               })
 
-       if server.FlatNews, err = os.ReadFile(filepath.Join(configDir, "MessageBoard.txt")); err != nil {
-               return nil, err
-       }
+                               s.Logger.Info("Connection established", "ip", ipAddr)
+                               defer conn.Close()
 
-       if err := server.loadThreadedNews(filepath.Join(configDir, "ThreadedNews.yaml")); err != nil {
-               return nil, err
-       }
+                               // Check if we have an existing rate limit for the IP and create one if we do not.
+                               rl, ok := s.rateLimiters[ipAddr]
+                               if !ok {
+                                       rl = rate.NewLimiter(perIPRateLimit, 1)
+                                       s.rateLimiters[ipAddr] = rl
+                               }
 
-       if err := server.loadConfig(filepath.Join(configDir, "config.yaml")); err != nil {
-               return nil, err
-       }
+                               // Check if the rate limit is exceeded and close the connection if so.
+                               if !rl.Allow() {
+                                       s.Logger.Info("Rate limit exceeded", "RemoteAddr", conn.RemoteAddr())
+                                       conn.Close()
+                                       return
+                               }
 
-       if err := server.loadAccounts(filepath.Join(configDir, "Users/")); err != nil {
-               return nil, err
+                               if err := s.handleNewConnection(connCtx, conn, conn.RemoteAddr().String()); err != nil {
+                                       if err == io.EOF {
+                                               s.Logger.Info("Client disconnected", "RemoteAddr", conn.RemoteAddr())
+                                       } else {
+                                               s.Logger.Error("Error serving request", "RemoteAddr", conn.RemoteAddr(), "err", err)
+                                       }
+                               }
+                       }()
+               }
        }
+}
 
-       server.Config.FileRoot = filepath.Join(configDir, "Files")
-
-       *server.NextGuestID = 1
-
-       if server.Config.EnableTrackerRegistration {
-               server.Logger.Infow(
-                       "Tracker registration enabled",
-                       "frequency", fmt.Sprintf("%vs", trackerUpdateFrequency),
-                       "trackers", server.Config.Trackers,
-               )
+// time in seconds between tracker re-registration
+const trackerUpdateFrequency = 300
 
-               go func() {
-                       for {
+// registerWithTrackers runs every trackerUpdateFrequency seconds to update the server's tracker entry on all configured
+// trackers.
+func (s *Server) registerWithTrackers(ctx context.Context) {
+       for {
+               if s.Config.EnableTrackerRegistration {
+                       for _, t := range s.Config.Trackers {
                                tr := &TrackerRegistration{
-                                       UserCount:   server.userCount(),
-                                       PassID:      server.TrackerPassID[:],
-                                       Name:        server.Config.Name,
-                                       Description: server.Config.Description,
+                                       UserCount:   len(s.ClientMgr.List()),
+                                       PassID:      s.TrackerPassID,
+                                       Name:        s.Config.Name,
+                                       Description: s.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)
-                                       }
-                                       server.Logger.Infow("Sent Tracker registration", "data", tr)
+                               binary.BigEndian.PutUint16(tr.Port[:], uint16(s.Port))
+
+                               // Check the tracker string for a password.  This is janky but avoids a breaking change to the Config
+                               // Trackers field.
+                               splitAddr := strings.Split(":", t)
+                               if len(splitAddr) == 3 {
+                                       tr.Password = splitAddr[2]
                                }
 
-                               time.Sleep(trackerUpdateFrequency * time.Second)
+                               if err := register(&RealDialer{}, t, tr); err != nil {
+                                       s.Logger.Error(fmt.Sprintf("Unable to register with tracker %v", t), "error", err)
+                               }
                        }
-               }()
+               }
+               // Using time.Ticker with for/select would be more idiomatic, but it's super annoying that it doesn't tick on
+               // first pass.  Revist, maybe.
+               // https://github.com/golang/go/issues/17601
+               time.Sleep(trackerUpdateFrequency * time.Second)
        }
-
-       // Start Client Keepalive go routine
-       go server.keepaliveHandler()
-
-       return &server, nil
 }
 
-func (s *Server) userCount() int {
-       s.mux.Lock()
-       defer s.mux.Unlock()
+const (
+       userIdleSeconds   = 300 // time in seconds before an inactive user is marked idle
+       idleCheckInterval = 10  // time in seconds to check for idle users
+)
 
-       return len(s.Clients)
-}
+// keepaliveHandler runs every idleCheckInterval seconds and increments a user's idle time by idleCheckInterval seconds.
+// If the updated idle time exceeds userIdleSeconds and the user was not previously idle, we notify all connected clients
+// that the user has gone idle.  For most clients, this turns the user grey in the user list.
+func (s *Server) keepaliveHandler(ctx context.Context) {
+       ticker := time.NewTicker(idleCheckInterval * time.Second)
+       defer ticker.Stop()
 
-func (s *Server) keepaliveHandler() {
        for {
-               time.Sleep(idleCheckInterval * time.Second)
-               s.mux.Lock()
-
-               for _, c := range s.Clients {
-                       c.IdleTime += idleCheckInterval
-                       if c.IdleTime > userIdleSeconds && !c.Idle {
-                               c.Idle = true
-
-                               flagBitmap := big.NewInt(int64(binary.BigEndian.Uint16(*c.Flags)))
-                               flagBitmap.SetBit(flagBitmap, userFlagAway, 1)
-                               binary.BigEndian.PutUint16(*c.Flags, uint16(flagBitmap.Int64()))
-
-                               c.sendAll(
-                                       tranNotifyChangeUser,
-                                       NewField(fieldUserID, *c.ID),
-                                       NewField(fieldUserFlags, *c.Flags),
-                                       NewField(fieldUserName, c.UserName),
-                                       NewField(fieldUserIconID, *c.Icon),
-                               )
+               select {
+               case <-ctx.Done():
+                       return
+               case <-ticker.C:
+                       for _, c := range s.ClientMgr.List() {
+                               c.mu.Lock()
+                               c.IdleTime += idleCheckInterval
+
+                               // Check if the user
+                               if c.IdleTime > userIdleSeconds && !c.Flags.IsSet(UserFlagAway) {
+                                       c.Flags.Set(UserFlagAway, 1)
+
+                                       c.SendAll(
+                                               TranNotifyChangeUser,
+                                               NewField(FieldUserID, c.ID[:]),
+                                               NewField(FieldUserFlags, c.Flags[:]),
+                                               NewField(FieldUserName, c.UserName),
+                                               NewField(FieldUserIconID, c.Icon),
+                                       )
+                               }
+                               c.mu.Unlock()
                        }
                }
-               s.mux.Unlock()
-       }
-}
-
-func (s *Server) writeThreadedNews() error {
-       s.mux.Lock()
-       defer s.mux.Unlock()
-
-       out, err := yaml.Marshal(s.ThreadedNews)
-       if err != nil {
-               return err
        }
-       err = ioutil.WriteFile(
-               filepath.Join(s.ConfigDir, "ThreadedNews.yaml"),
-               out,
-               0666,
-       )
-       return err
 }
 
 func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *ClientConn {
-       s.mux.Lock()
-       defer s.mux.Unlock()
-
        clientConn := &ClientConn{
-               ID:         &[]byte{0, 0},
-               Icon:       &[]byte{0, 0},
-               Flags:      &[]byte{0, 0},
-               UserName:   []byte{},
+               Icon:       []byte{0, 0}, // TODO: make array type
                Connection: conn,
                Server:     s,
-               Version:    &[]byte{},
-               AutoReply:  []byte{},
-               Transfers:  make(map[int][]*FileTransfer),
-               Agreed:     false,
                RemoteAddr: remoteAddr,
-       }
-       *s.NextGuestID++
-       ID := *s.NextGuestID
-
-       binary.BigEndian.PutUint16(*clientConn.ID, ID)
-       s.Clients[ID] = clientConn
-
-       return clientConn
-}
-
-// NewUser creates a new user account entry in the server map and config file
-func (s *Server) NewUser(login, name, password string, access []byte) error {
-       s.mux.Lock()
-       defer s.mux.Unlock()
-
-       account := Account{
-               Login:    login,
-               Name:     name,
-               Password: hashAndSalt([]byte(password)),
-               Access:   &access,
-       }
-       out, err := yaml.Marshal(&account)
-       if err != nil {
-               return err
-       }
-       s.Accounts[login] = &account
-
-       return s.FS.WriteFile(filepath.Join(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()
-
-       // update renames the user login
-       if login != newLogin {
-               err := os.Rename(filepath.Join(s.ConfigDir, "Users", login+".yaml"), filepath.Join(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(filepath.Join(s.ConfigDir, "Users", newLogin+".yaml"), out, 0666); err != nil {
-               return err
+               ClientFileTransferMgr: NewClientFileTransferMgr(),
        }
 
-       return nil
-}
-
-// DeleteUser deletes the user account
-func (s *Server) DeleteUser(login string) error {
-       s.mux.Lock()
-       defer s.mux.Unlock()
-
-       delete(s.Accounts, login)
-
-       return s.FS.Remove(filepath.Join(s.ConfigDir, "Users", login+".yaml"))
-}
-
-func (s *Server) connectedUsers() []Field {
-       s.mux.Lock()
-       defer s.mux.Unlock()
+       s.ClientMgr.Add(clientConn)
 
-       var connectedUsers []Field
-       for _, c := range sortedClients(s.Clients) {
-               if !c.Agreed {
-                       continue
-               }
-               user := User{
-                       ID:    *c.ID,
-                       Icon:  *c.Icon,
-                       Flags: *c.Flags,
-                       Name:  string(c.UserName),
-               }
-               connectedUsers = append(connectedUsers, NewField(fieldUsernameWithInfo, user.Payload()))
-       }
-       return connectedUsers
+       return clientConn
 }
 
-// loadThreadedNews loads the threaded news data from disk
-func (s *Server) loadThreadedNews(threadedNewsPath string) error {
-       fh, err := os.Open(threadedNewsPath)
-       if err != nil {
-               return err
-       }
-       decoder := yaml.NewDecoder(fh)
-
-       return decoder.Decode(s.ThreadedNews)
+func sendBanMessage(rwc io.Writer, message string) {
+       t := NewTransaction(
+               TranServerMsg,
+               [2]byte{0, 0},
+               NewField(FieldData, []byte(message)),
+               NewField(FieldChatOptions, []byte{0, 0}),
+       )
+       _, _ = io.Copy(rwc, &t)
+       time.Sleep(1 * time.Second)
 }
 
-// loadAccounts loads account data from disk
-func (s *Server) loadAccounts(userDir string) error {
-       matches, err := filepath.Glob(filepath.Join(userDir, "*.yaml"))
-       if err != nil {
-               return err
-       }
+// handleNewConnection takes a new net.Conn and performs the initial login sequence
+func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser, remoteAddr string) error {
+       defer dontPanic(s.Logger)
 
-       if len(matches) == 0 {
-               return errors.New("no user accounts found in " + userDir)
+       if err := performHandshake(rwc); err != nil {
+               return fmt.Errorf("perform handshake: %w", err)
        }
 
-       for _, file := range matches {
-               fh, err := s.FS.Open(file)
-               if err != nil {
-                       return err
+       // Check if remoteAddr is present in the ban list
+       ipAddr := strings.Split(remoteAddr, ":")[0]
+       if isBanned, banUntil := s.BanList.IsBanned(ipAddr); isBanned {
+               // permaban
+               if banUntil == nil {
+                       sendBanMessage(rwc, "You are permanently banned on this server")
+                       s.Logger.Debug("Disconnecting permanently banned IP", "remoteAddr", ipAddr)
+                       return nil
                }
 
-               account := Account{}
-               decoder := yaml.NewDecoder(fh)
-               if err := decoder.Decode(&account); err != nil {
-                       return err
+               // temporary ban
+               if time.Now().Before(*banUntil) {
+                       sendBanMessage(rwc, "You are temporarily banned on this server")
+                       s.Logger.Debug("Disconnecting temporarily banned IP", "remoteAddr", ipAddr)
+                       return nil
                }
-
-               s.Accounts[account.Login] = &account
        }
-       return nil
-}
-
-func (s *Server) loadConfig(path string) error {
-       fh, err := s.FS.Open(path)
-       if err != nil {
-               return err
-       }
-
-       decoder := yaml.NewDecoder(fh)
-       err = decoder.Decode(s.Config)
-       if err != nil {
-               return err
-       }
-
-       validate := validator.New()
-       err = validate.Struct(s.Config)
-       if err != nil {
-               return err
-       }
-       return nil
-}
 
-const (
-       minTransactionLen = 22 // minimum length of any transaction
-)
+       // Create a new scanner for parsing incoming bytes into transaction tokens
+       scanner := bufio.NewScanner(rwc)
+       scanner.Split(transactionScanner)
 
-// 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()))
-       }
-}
+       scanner.Scan()
 
-// handleNewConnection takes a new net.Conn and performs the initial login sequence
-func (s *Server) handleNewConnection(ctx context.Context, conn io.ReadWriteCloser, remoteAddr string) error {
-       defer dontPanic(s.Logger)
+       // Make a new []byte slice and copy the scanner bytes to it.  This is critical to avoid a data race as the
+       // scanner re-uses the buffer for subsequent scans.
+       buf := make([]byte, len(scanner.Bytes()))
+       copy(buf, scanner.Bytes())
 
-       if err := Handshake(conn); err != nil {
-               return err
+       var clientLogin Transaction
+       if _, err := clientLogin.Write(buf); err != nil {
+               return fmt.Errorf("error writing login transaction: %w", err)
        }
 
-       buf := make([]byte, 1024)
-       // TODO: fix potential short read with io.ReadFull
-       readLen, err := conn.Read(buf)
-       if readLen < minTransactionLen {
-               return err
-       }
-       if err != nil {
-               return err
-       }
-
-       clientLogin, _, err := ReadTransaction(buf[:readLen])
-       if err != nil {
-               return err
-       }
-
-       c := s.NewClientConn(conn, remoteAddr)
+       c := s.NewClientConn(rwc, remoteAddr)
        defer c.Disconnect()
 
-       encodedLogin := clientLogin.GetField(fieldUserLogin).Data
-       encodedPassword := clientLogin.GetField(fieldUserPassword).Data
-       *c.Version = clientLogin.GetField(fieldVersion).Data
+       encodedPassword := clientLogin.GetField(FieldUserPassword).Data
+       c.Version = clientLogin.GetField(FieldVersion).Data
 
-       var login string
-       for _, char := range encodedLogin {
-               login += string(rune(255 - uint(char)))
-       }
+       login := clientLogin.GetField(FieldUserLogin).DecodeObfuscatedString()
        if login == "" {
                login = GuestAccount
        }
 
+       c.Logger = s.Logger.With("ip", ipAddr, "login", login)
+
        // If authentication fails, send error reply and close connection
        if !c.Authenticate(login, encodedPassword) {
-               t := c.NewErrReply(clientLogin, "Incorrect login.")
-               b, err := t.MarshalBinary()
+               t := c.NewErrReply(&clientLogin, "Incorrect login.")[0]
+
+               _, err := io.Copy(rwc, &t)
                if err != nil {
                        return err
                }
-               if _, err := conn.Write(b); err != nil {
-                       return err
-               }
-               return fmt.Errorf("incorrect login")
-       }
 
-       if clientLogin.GetField(fieldUserName).Data != nil {
-               c.UserName = clientLogin.GetField(fieldUserName).Data
-       }
+               c.Logger.Info("Incorrect login")
 
-       if clientLogin.GetField(fieldUserIconID).Data != nil {
-               *c.Icon = clientLogin.GetField(fieldUserIconID).Data
+               return nil
        }
 
-       c.Account = c.Server.Accounts[login]
+       if clientLogin.GetField(FieldUserIconID).Data != nil {
+               c.Icon = clientLogin.GetField(FieldUserIconID).Data
+       }
 
-       if c.Authorize(accessDisconUser) {
-               *c.Flags = []byte{0, 2}
+       c.Account = c.Server.AccountManager.Get(login)
+       if c.Account == nil {
+               return nil
        }
 
-       c.logger = s.Logger.With("remoteAddr", remoteAddr, "login", login)
+       if clientLogin.GetField(FieldUserName).Data != nil {
+               if c.Authorize(AccessAnyName) {
+                       c.UserName = clientLogin.GetField(FieldUserName).Data
+               } else {
+                       c.UserName = []byte(c.Account.Name)
+               }
+       }
 
-       c.logger.Infow("Client connection received", "version", fmt.Sprintf("%x", *c.Version))
+       if c.Authorize(AccessDisconUser) {
+               c.Flags.Set(UserFlagAdmin, 1)
+       }
 
-       s.outbox <- c.NewReply(clientLogin,
-               NewField(fieldVersion, []byte{0x00, 0xbe}),
-               NewField(fieldCommunityBannerID, []byte{0x00, 0x01}),
-               NewField(fieldServerName, []byte(s.Config.Name)),
+       s.outbox <- c.NewReply(&clientLogin,
+               NewField(FieldVersion, []byte{0x00, 0xbe}),
+               NewField(FieldCommunityBannerID, []byte{0, 0}),
+               NewField(FieldServerName, []byte(s.Config.Name)),
        )
 
        // Send user access privs so client UI knows how to behave
-       c.Server.outbox <- *NewTransaction(tranUserAccess, c.ID, NewField(fieldUserAccess, *c.Account.Access))
-
-       // Show agreement to client
-       c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement))
-
-       // 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.logger = c.logger.With("name", string(c.UserName))
-
-               for _, t := range c.notifyOthers(
-                       *NewTransaction(
-                               tranNotifyChangeUser, nil,
-                               NewField(fieldUserName, c.UserName),
-                               NewField(fieldUserID, *c.ID),
-                               NewField(fieldUserIconID, *c.Icon),
-                               NewField(fieldUserFlags, *c.Flags),
+       c.Server.outbox <- NewTransaction(TranUserAccess, c.ID, NewField(FieldUserAccess, c.Account.Access[:]))
+
+       // Accounts with AccessNoAgreement do not receive the server agreement on login.  The behavior is different between
+       // client versions.  For 1.2.3 client, we do not send TranShowAgreement.  For other client versions, we send
+       // TranShowAgreement but with the NoServerAgreement field set to 1.
+       if c.Authorize(AccessNoAgreement) {
+               // If client version is nil, then the client uses the 1.2.3 login behavior
+               if c.Version != nil {
+                       c.Server.outbox <- NewTransaction(TranShowAgreement, c.ID, NewField(FieldNoServerAgreement, []byte{1}))
+               }
+       } else {
+               _, _ = c.Server.Agreement.Seek(0, 0)
+               data, _ := io.ReadAll(c.Server.Agreement)
+
+               c.Server.outbox <- NewTransaction(TranShowAgreement, c.ID, NewField(FieldData, data))
+       }
+
+       // If the client has provided a username as part of the login, we can infer that it is using the 1.2.3 login
+       // flow and not the 1.5+ flow.
+       if len(c.UserName) != 0 {
+               // Add the client username to the logger.  For 1.5+ clients, we don't have this information yet as it comes as
+               // part of TranAgreed
+               c.Logger = c.Logger.With("name", string(c.UserName))
+               c.Logger.Info("Login successful")
+
+               // Notify other clients on the server that the new user has logged in.  For 1.5+ clients we don't have this
+               // information yet, so we do it in TranAgreed instead
+               for _, t := range c.NotifyOthers(
+                       NewTransaction(
+                               TranNotifyChangeUser, [2]byte{0, 0},
+                               NewField(FieldUserName, c.UserName),
+                               NewField(FieldUserID, c.ID[:]),
+                               NewField(FieldUserIconID, c.Icon),
+                               NewField(FieldUserFlags, c.Flags[:]),
                        ),
                ) {
                        c.Server.outbox <- t
                }
        }
 
-       c.Server.Stats.LoginCount += 1
-
-       const readBuffSize = 1024000 // 1KB - TODO: what should this be?
-       tranBuff := make([]byte, 0)
-       tReadlen := 0
-       // Infinite loop where take action on incoming client requests until the connection is closed
-       for {
-               buf = make([]byte, readBuffSize)
-               tranBuff = tranBuff[tReadlen:]
+       c.Server.Stats.Increment(StatConnectionCounter, StatCurrentlyConnected)
+       defer c.Server.Stats.Decrement(StatCurrentlyConnected)
 
-               readLen, err := c.Connection.Read(buf)
-               if err != nil {
-                       return err
-               }
-               tranBuff = append(tranBuff, buf[:readLen]...)
-
-               // We may have read multiple requests worth of bytes from Connection.Read.  readTransactions splits them
-               // into a slice of transactions
-               var transactions []Transaction
-               if transactions, tReadlen, err = readTransactions(tranBuff); err != nil {
-                       c.logger.Errorw("Error handling transaction", "err", err)
-               }
-
-               // iterate over all the transactions that were parsed from the byte slice and handle them
-               for _, t := range transactions {
-                       if err := c.handleTransaction(&t); err != nil {
-                               c.logger.Errorw("Error handling transaction", "err", err)
-                       }
-               }
+       if len(s.ClientMgr.List()) > c.Server.Stats.Get(StatConnectionPeak) {
+               c.Server.Stats.Set(StatConnectionPeak, len(s.ClientMgr.List()))
        }
-}
 
-// NewTransactionRef generates a random ID for the file transfer.  The Hotline client includes this ID
-// in the transfer request payload, and the file transfer server will use it to map the request
-// to a transfer
-func (s *Server) NewTransactionRef() []byte {
-       transactionRef := make([]byte, 4)
-       rand.Read(transactionRef)
+       // Scan for new transactions and handle them as they come in.
+       for scanner.Scan() {
+               // Copy the scanner bytes to a new slice to it to avoid a data race when the scanner re-uses the buffer.
+               tmpBuf := make([]byte, len(scanner.Bytes()))
+               copy(tmpBuf, scanner.Bytes())
 
-       return transactionRef
-}
-
-func (s *Server) NewPrivateChat(cc *ClientConn) []byte {
-       s.mux.Lock()
-       defer s.mux.Unlock()
-
-       randID := make([]byte, 4)
-       rand.Read(randID)
-       data := binary.BigEndian.Uint32(randID[:])
+               var t Transaction
+               if _, err := t.Write(tmpBuf); err != nil {
+                       return err
+               }
 
-       s.PrivateChats[data] = &PrivateChat{
-               Subject:    "",
-               ClientConn: make(map[uint16]*ClientConn),
+               c.handleTransaction(t)
        }
-       s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc
-
-       return randID
+       return nil
 }
 
-const dlFldrActionSendFile = 1
-const dlFldrActionResumeFile = 2
-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(ctx context.Context, rwc io.ReadWriter) error {
        defer dontPanic(s.Logger)
 
-       txBuf := make([]byte, 16)
-       if _, err := io.ReadFull(rwc, txBuf); err != nil {
-               return err
+       // The first 16 bytes contain the file transfer.
+       var t transfer
+       if _, err := io.CopyN(&t, rwc, 16); err != nil {
+               return fmt.Errorf("error reading file transfer: %w", err)
        }
 
-       var t transfer
-       if _, err := t.Write(txBuf); err != nil {
-               return err
+       fileTransfer := s.FileTransferMgr.Get(t.ReferenceNumber)
+       if fileTransfer == nil {
+               return errors.New("invalid transaction ID")
        }
 
-       transferRefNum := binary.BigEndian.Uint32(t.ReferenceNumber[:])
        defer func() {
-               s.mux.Lock()
-               delete(s.FileTransfers, transferRefNum)
-               s.mux.Unlock()
-       }()
+               s.FileTransferMgr.Delete(t.ReferenceNumber)
 
-       s.mux.Lock()
-       fileTransfer, ok := s.FileTransfers[transferRefNum]
-       s.mux.Unlock()
-       if !ok {
-               return errors.New("invalid transaction ID")
-       }
+               // Wait a few seconds before closing the connection: this is a workaround for problems
+               // observed with Windows clients where the client must initiate close of the TCP connection before
+               // the server does.  This is gross and seems unnecessary.  TODO: Revisit?
+               time.Sleep(3 * time.Second)
+       }()
 
        rLogger := s.Logger.With(
                "remoteAddr", ctx.Value(contextKeyReq).(requestCtx).remoteAddr,
-               "xferID", transferRefNum,
+               "login", fileTransfer.ClientConn.Account.Login,
+               "Name", string(fileTransfer.ClientConn.UserName),
        )
 
-       switch fileTransfer.Type {
-       case FileDownload:
-               s.Stats.DownloadCounter += 1
-
-               fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
-               if err != nil {
-                       return err
-               }
-
-               var dataOffset int64
-               if fileTransfer.fileResumeData != nil {
-                       dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.fileResumeData.ForkInfoList[0].DataSize[:]))
-               }
-
-               fw, err := newFileWrapper(s.FS, fullFilePath, 0)
-               if err != nil {
-                       return err
-               }
-
-               rLogger.Infow("File download started", "filePath", fullFilePath, "transactionRef", fileTransfer.ReferenceNumber)
-
-               wr := bufio.NewWriterSize(rwc, 1460)
-
-               // if file transfer options are included, that means this is a "quick preview" request from a 1.5+ client
-               if fileTransfer.options == nil {
-                       // Start by sending flat file object to client
-                       if _, err := wr.Write(fw.ffo.BinaryMarshal()); err != nil {
-                               return err
-                       }
-               }
-
-               file, err := fw.dataForkReader()
-               if err != nil {
-                       return err
-               }
-
-               if err := sendFile(wr, file, int(dataOffset)); err != nil {
-                       return err
-               }
-
-               if err := wr.Flush(); err != nil {
-                       return err
-               }
-
-               // if the client requested to resume transfer, do not send the resource fork, or it will be appended into the fileWrapper data
-               if fileTransfer.fileResumeData == nil {
-                       err = binary.Write(wr, binary.BigEndian, fw.rsrcForkHeader())
-                       if err != nil {
-                               return err
-                       }
-                       if err := wr.Flush(); err != nil {
-                               return err
-                       }
-               }
+       fullPath, err := ReadPath(fileTransfer.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
+       if err != nil {
+               return err
+       }
 
-               rFile, err := fw.rsrcForkFile()
-               if err != nil {
-                       return nil
+       switch fileTransfer.Type {
+       case BannerDownload:
+               if _, err := io.Copy(rwc, bytes.NewBuffer(s.Banner)); err != nil {
+                       return fmt.Errorf("banner download: %w", err)
                }
+       case FileDownload:
+               s.Stats.Increment(StatDownloadCounter, StatDownloadsInProgress)
+               defer func() {
+                       s.Stats.Decrement(StatDownloadsInProgress)
+               }()
 
-               err = sendFile(wr, rFile, int(dataOffset))
+               err = DownloadHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, true)
                if err != nil {
-                       return err
-               }
-
-               if err := wr.Flush(); err != nil {
-                       return err
+                       return fmt.Errorf("file download: %w", err)
                }
 
        case FileUpload:
-               s.Stats.UploadCounter += 1
-
-               destinationFile, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
-               if err != nil {
-                       return err
-               }
-
-               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
-               //  We have to infer which case applies by inspecting what is already on the filesystem
-
-               // 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) {
-                       // 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
-                       }
-               }
+               s.Stats.Increment(StatUploadCounter, StatUploadsInProgress)
+               defer func() {
+                       s.Stats.Decrement(StatUploadsInProgress)
+               }()
 
-               f, err := newFileWrapper(s.FS, destinationFile, 0)
+               err = UploadHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, s.Config.PreserveResourceForks)
                if err != nil {
-                       return err
-               }
-
-               s.Logger.Infow("File upload started", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
-
-               rForkWriter := io.Discard
-               iForkWriter := io.Discard
-               if s.Config.PreserveResourceForks {
-                       rForkWriter, err = f.rsrcForkWriter()
-                       if err != nil {
-                               return err
-                       }
-
-                       iForkWriter, err = f.infoForkWriter()
-                       if err != nil {
-                               return err
-                       }
+                       return fmt.Errorf("file upload: %w", err)
                }
 
-               if err := receiveFile(rwc, file, rForkWriter, iForkWriter); err != nil {
-                       return err
-               }
-
-               if err := file.Close(); err != nil {
-                       return err
-               }
-
-               if err := s.FS.Rename(destinationFile+".incomplete", destinationFile); err != nil {
-                       return err
-               }
-
-               s.Logger.Infow("File upload complete", "transactionRef", fileTransfer.ReferenceNumber, "dstFile", destinationFile)
        case FolderDownload:
-               // Folder Download flow:
-               // 1. Get filePath from the transfer
-               // 2. Iterate over files
-               // 3. For each fileWrapper:
-               //       Send fileWrapper header to client
-               // The client can reply in 3 ways:
-               //
-               // 1. If type is an odd number (unknown type?), or fileWrapper download for the current fileWrapper is completed:
-               //              client sends []byte{0x00, 0x03} to tell the server to continue to the next fileWrapper
-               //
-               // 2. If download of a fileWrapper is to be resumed:
-               //              client sends:
-               //                      []byte{0x00, 0x02} // download folder action
-               //                      [2]byte // Resume data size
-               //                      []byte fileWrapper resume data (see myField_FileResumeData)
-               //
-               // 3. Otherwise, download of the fileWrapper is requested and client sends []byte{0x00, 0x01}
-               //
-               // When download is requested (case 2 or 3), server replies with:
-               //                      [4]byte - fileWrapper size
-               //                      []byte  - Flattened File Object
-               //
-               // After every fileWrapper download, client could request next fileWrapper with:
-               //                      []byte{0x00, 0x03}
-               //
-               // This notifies the server to send the next item header
-
-               fullFilePath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
-               if err != nil {
-                       return err
-               }
-
-               basePathLen := len(fullFilePath)
-
-               s.Logger.Infow("Start folder download", "path", fullFilePath, "ReferenceNumber", fileTransfer.ReferenceNumber)
-
-               nextAction := make([]byte, 2)
-               if _, err := io.ReadFull(rwc, nextAction); err != nil {
-                       return err
-               }
-
-               i := 0
-               err = filepath.Walk(fullFilePath+"/", func(path string, info os.FileInfo, err error) error {
-                       s.Stats.DownloadCounter += 1
-                       i += 1
-
-                       if err != nil {
-                               return err
-                       }
-
-                       // skip dot files
-                       if strings.HasPrefix(info.Name(), ".") {
-                               return nil
-                       }
-
-                       hlFile, err := newFileWrapper(s.FS, path, 0)
-                       if err != nil {
-                               return err
-                       }
-
-                       subPath := path[basePathLen+1:]
-                       s.Logger.Infow("Sending fileheader", "i", i, "path", path, "fullFilePath", fullFilePath, "subPath", subPath, "IsDir", info.IsDir())
-
-                       if i == 1 {
-                               return nil
-                       }
-
-                       fileHeader := NewFileHeader(subPath, info.IsDir())
-
-                       // Send the fileWrapper header to client
-                       if _, err := rwc.Write(fileHeader.Payload()); err != nil {
-                               s.Logger.Errorf("error sending file header: %v", err)
-                               return err
-                       }
-
-                       // Read the client's Next Action request
-                       if _, err := io.ReadFull(rwc, nextAction); err != nil {
-                               return err
-                       }
-
-                       s.Logger.Infow("Client folder download action", "action", fmt.Sprintf("%X", nextAction[0:2]))
-
-                       var dataOffset int64
-
-                       switch nextAction[1] {
-                       case dlFldrActionResumeFile:
-                               // get size of resumeData
-                               resumeDataByteLen := make([]byte, 2)
-                               if _, err := io.ReadFull(rwc, resumeDataByteLen); err != nil {
-                                       return err
-                               }
-
-                               resumeDataLen := binary.BigEndian.Uint16(resumeDataByteLen)
-                               resumeDataBytes := make([]byte, resumeDataLen)
-                               if _, err := io.ReadFull(rwc, resumeDataBytes); err != nil {
-                                       return err
-                               }
-
-                               var frd FileResumeData
-                               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
-                       }
-
-                       s.Logger.Infow("File download started",
-                               "fileName", info.Name(),
-                               "transactionRef", fileTransfer.ReferenceNumber,
-                               "TransferSize", fmt.Sprintf("%x", hlFile.ffo.TransferSize(dataOffset)),
-                       )
-
-                       // Send file size to client
-                       if _, err := rwc.Write(hlFile.ffo.TransferSize(dataOffset)); err != nil {
-                               s.Logger.Error(err)
-                               return err
-                       }
-
-                       // Send ffo bytes to client
-                       if _, err := rwc.Write(hlFile.ffo.BinaryMarshal()); err != nil {
-                               s.Logger.Error(err)
-                               return err
-                       }
-
-                       file, err := s.FS.Open(path)
-                       if err != nil {
-                               return err
-                       }
-
-                       // wr := bufio.NewWriterSize(rwc, 1460)
-                       err = sendFile(rwc, file, int(dataOffset))
-                       if err != nil {
-                               return err
-                       }
-
-                       if nextAction[1] != 2 && hlFile.ffo.FlatFileHeader.ForkCount[1] == 3 {
-                               err = binary.Write(rwc, binary.BigEndian, hlFile.rsrcForkHeader())
-                               if err != nil {
-                                       return err
-                               }
-
-                               rFile, err := hlFile.rsrcForkFile()
-                               if err != nil {
-                                       return err
-                               }
-
-                               err = sendFile(rwc, rFile, int(dataOffset))
-                               if err != nil {
-                                       return err
-                               }
-                       }
-
-                       // Read the client's Next Action request.  This is always 3, I think?
-                       if _, err := io.ReadFull(rwc, nextAction); err != nil {
-                               return err
-                       }
-
-                       return nil
-               })
+               s.Stats.Increment(StatDownloadCounter, StatDownloadsInProgress)
+               defer func() {
+                       s.Stats.Decrement(StatDownloadsInProgress)
+               }()
 
+               err = DownloadFolderHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, s.Config.PreserveResourceForks)
                if err != nil {
-                       return err
+                       return fmt.Errorf("folder download: %w", err)
                }
 
        case FolderUpload:
-               dstPath, err := readPath(s.Config.FileRoot, fileTransfer.FilePath, fileTransfer.FileName)
-               if err != nil {
-                       return err
-               }
+               s.Stats.Increment(StatUploadCounter, StatUploadsInProgress)
+               defer func() {
+                       s.Stats.Decrement(StatUploadsInProgress)
+               }()
 
-               s.Logger.Infow(
+               rLogger.Info(
                        "Folder upload started",
-                       "transactionRef", fileTransfer.ReferenceNumber,
-                       "dstPath", dstPath,
-                       "TransferSize", fmt.Sprintf("%x", fileTransfer.TransferSize),
+                       "dstPath", fullPath,
+                       "TransferSize", binary.BigEndian.Uint32(fileTransfer.TransferSize),
                        "FolderItemCount", fileTransfer.FolderItemCount,
                )
 
-               // Check if the target folder exists.  If not, create it.
-               if _, err := s.FS.Stat(dstPath); os.IsNotExist(err) {
-                       if err := s.FS.Mkdir(dstPath, 0777); err != nil {
-                               return err
-                       }
-               }
-
-               // Begin the folder upload flow by sending the "next file action" to client
-               if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
-                       return err
+               err = UploadFolderHandler(rwc, fullPath, fileTransfer, s.FS, rLogger, s.Config.PreserveResourceForks)
+               if err != nil {
+                       return fmt.Errorf("folder upload: %w", err)
                }
+       }
+       return nil
+}
 
-               fileSize := make([]byte, 4)
-
-               for i := 0; i < fileTransfer.ItemCount(); i++ {
-                       s.Stats.UploadCounter += 1
-
-                       var fu folderUpload
-                       if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil {
-                               return err
-                       }
-                       if _, err := io.ReadFull(rwc, fu.IsFolder[:]); err != nil {
-                               return err
-                       }
-                       if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil {
-                               return err
-                       }
-
-                       fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes
-
-                       if _, err := io.ReadFull(rwc, fu.FileNamePath); err != nil {
-                               return err
-                       }
-
-                       s.Logger.Infow(
-                               "Folder upload continued",
-                               "transactionRef", fmt.Sprintf("%x", fileTransfer.ReferenceNumber),
-                               "FormattedPath", fu.FormattedPath(),
-                               "IsFolder", fmt.Sprintf("%x", fu.IsFolder),
-                               "PathItemCount", binary.BigEndian.Uint16(fu.PathItemCount[:]),
-                       )
-
-                       if fu.IsFolder == [2]byte{0, 1} {
-                               if _, err := os.Stat(filepath.Join(dstPath, fu.FormattedPath())); os.IsNotExist(err) {
-                                       if err := os.Mkdir(filepath.Join(dstPath, fu.FormattedPath()), 0777); err != nil {
-                                               return err
-                                       }
-                               }
-
-                               // Tell client to send next file
-                               if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
-                                       return err
-                               }
-                       } else {
-                               nextAction := dlFldrActionSendFile
-
-                               // Check if we have the full file already.  If so, send dlFldrAction_NextFile to client to skip.
-                               _, err = os.Stat(filepath.Join(dstPath, fu.FormattedPath()))
-                               if err != nil && !errors.Is(err, fs.ErrNotExist) {
-                                       return err
-                               }
-                               if err == nil {
-                                       nextAction = dlFldrActionNextFile
-                               }
-
-                               //  Check if we have a partial file already.  If so, send dlFldrAction_ResumeFile to client to resume upload.
-                               incompleteFile, err := os.Stat(filepath.Join(dstPath, fu.FormattedPath()+incompleteFileSuffix))
-                               if err != nil && !errors.Is(err, fs.ErrNotExist) {
-                                       return err
-                               }
-                               if err == nil {
-                                       nextAction = dlFldrActionResumeFile
-                               }
-
-                               if _, err := rwc.Write([]byte{0, uint8(nextAction)}); err != nil {
-                                       return err
-                               }
-
-                               switch nextAction {
-                               case dlFldrActionNextFile:
-                                       continue
-                               case dlFldrActionResumeFile:
-                                       offset := make([]byte, 4)
-                                       binary.BigEndian.PutUint32(offset, uint32(incompleteFile.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 := rwc.Write(append(bs, b...)); err != nil {
-                                               return err
-                                       }
-
-                                       if _, err := io.ReadFull(rwc, fileSize); err != nil {
-                                               return err
-                                       }
-
-                                       if err := receiveFile(rwc, file, ioutil.Discard, 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 := io.ReadFull(rwc, fileSize); err != nil {
-                                               return err
-                                       }
-
-                                       filePath := filepath.Join(dstPath, fu.FormattedPath())
-
-                                       hlFile, err := newFileWrapper(s.FS, filePath, 0)
-                                       if err != nil {
-                                               return err
-                                       }
-
-                                       s.Logger.Infow("Starting file transfer", "path", filePath, "fileNum", i+1, "fileSize", binary.BigEndian.Uint32(fileSize))
-
-                                       incWriter, err := hlFile.incFileWriter()
-                                       if err != nil {
-                                               return err
-                                       }
+func (s *Server) SendAll(t TranType, fields ...Field) {
+       for _, c := range s.ClientMgr.List() {
+               s.outbox <- NewTransaction(t, c.ID, fields...)
+       }
+}
 
-                                       rForkWriter := io.Discard
-                                       iForkWriter := io.Discard
-                                       if s.Config.PreserveResourceForks {
-                                               iForkWriter, err = hlFile.infoForkWriter()
-                                               if err != nil {
-                                                       return err
-                                               }
-
-                                               rForkWriter, err = hlFile.rsrcForkWriter()
-                                               if err != nil {
-                                                       return err
-                                               }
-                                       }
-                                       if err := receiveFile(rwc, incWriter, rForkWriter, iForkWriter); err != nil {
-                                               return err
-                                       }
-                                       // _ = newFile.Close()
-                                       if err := os.Rename(filePath+".incomplete", filePath); err != nil {
-                                               return err
-                                       }
-                               }
+func (s *Server) Shutdown(msg []byte) {
+       s.Logger.Info("Shutdown signal received")
+       s.SendAll(TranDisconnectMsg, NewField(FieldData, msg))
 
-                               // Tell client to send next fileWrapper
-                               if _, err := rwc.Write([]byte{0, dlFldrActionNextFile}); err != nil {
-                                       return err
-                               }
-                       }
-               }
-               s.Logger.Infof("Folder upload complete")
-       }
+       time.Sleep(3 * time.Second)
 
-       return nil
+       os.Exit(0)
 }