X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/69af8ddbdff8e6d0dd551e9bdc72284469a86fc0..c8bfd6061f5079f6c6c0155a2de1e8cd32d8a39a:/hotline/server.go diff --git a/hotline/server.go b/hotline/server.go index 852b772..500087a 100644 --- a/hotline/server.go +++ b/hotline/server.go @@ -2,7 +2,6 @@ package hotline import ( "bufio" - "bytes" "context" "encoding/binary" "errors" @@ -12,7 +11,6 @@ import ( "gopkg.in/yaml.v3" "io" "io/fs" - "io/ioutil" "math/big" "math/rand" "net" @@ -33,14 +31,6 @@ type requestCtx struct { 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 -) - -var nostalgiaVersion = []byte{0, 0, 2, 0x2c} // version ID used by the Nostalgia client - type Server struct { Port int Accounts map[string]*Account @@ -48,13 +38,18 @@ type Server struct { Clients map[uint16]*ClientConn fileTransfers map[[4]byte]*FileTransfer - Config *Config - ConfigDir string - Logger *zap.SugaredLogger - PrivateChats map[uint32]*PrivateChat + Config *Config + ConfigDir string + Logger *zap.SugaredLogger + + PrivateChatsMu sync.Mutex + PrivateChats map[uint32]*PrivateChat + NextGuestID *uint16 TrackerPassID [4]byte - Stats *Stats + + StatsMu sync.Mutex + Stats *Stats FS FileStore // Storage backend to use for File storage @@ -71,6 +66,16 @@ type Server struct { banList map[string]*time.Time } +func (s *Server) CurrentStats() Stats { + s.StatsMu.Lock() + defer s.StatsMu.Unlock() + + stats := s.Stats + stats.CurrentlyConnected = len(s.Clients) + + return *stats +} + type PrivateChat struct { Subject string ClientConn map[uint16]*ClientConn @@ -100,7 +105,6 @@ func (s *Server) ListenAndServe(ctx context.Context, cancelRoot context.CancelFu ln, err := net.Listen("tcp", fmt.Sprintf("%s:%v", "", s.Port+1)) if err != nil { s.Logger.Fatal(err) - } s.Logger.Fatal(s.ServeFileTransfers(ctx, ln)) @@ -143,18 +147,18 @@ func (s *Server) sendTransaction(t Transaction) error { s.mux.Lock() client := s.Clients[uint16(clientID)] + s.mux.Unlock() if client == nil { return fmt.Errorf("invalid client id %v", *t.clientID) } - s.mux.Unlock() - b, err := t.MarshalBinary() if err != nil { return err } - if _, err := client.Connection.Write(b); err != nil { + _, err = client.Connection.Write(b) + if err != nil { return err } @@ -204,7 +208,7 @@ const ( ) // NewServer constructs a new Server from a config dir -func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS FileStore) (*Server, error) { +func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, fs FileStore) (*Server, error) { server := Server{ Port: netPort, Accounts: make(map[string]*Account), @@ -216,9 +220,9 @@ func NewServer(configDir string, netPort int, logger *zap.SugaredLogger, FS File Logger: logger, NextGuestID: new(uint16), outbox: make(chan Transaction), - Stats: &Stats{StartTime: time.Now()}, + Stats: &Stats{Since: time.Now()}, ThreadedNews: &ThreadedNews{}, - FS: FS, + FS: fs, banList: make(map[string]*time.Time), } @@ -313,11 +317,11 @@ func (s *Server) keepaliveHandler() { 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), + TranNotifyChangeUser, + NewField(FieldUserID, *c.ID), + NewField(FieldUserFlags, c.Flags), + NewField(FieldUserName, c.UserName), + NewField(FieldUserIconID, c.Icon), ) } } @@ -333,7 +337,7 @@ func (s *Server) writeBanList() error { if err != nil { return err } - err = ioutil.WriteFile( + err = os.WriteFile( filepath.Join(s.ConfigDir, "Banlist.yaml"), out, 0666, @@ -371,7 +375,6 @@ func (s *Server) NewClientConn(conn io.ReadWriteCloser, remoteAddr string) *Clie Version: []byte{}, AutoReply: []byte{}, transfers: map[int]map[[4]byte]*FileTransfer{}, - Agreed: false, RemoteAddr: remoteAddr, } clientConn.transfers = map[int]map[[4]byte]*FileTransfer{ @@ -458,16 +461,13 @@ func (s *Server) connectedUsers() []Field { 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())) + connectedUsers = append(connectedUsers, NewField(FieldUsernameWithInfo, user.Payload())) } return connectedUsers } @@ -555,42 +555,70 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser scanner.Scan() + // 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()) + var clientLogin Transaction - if _, err := clientLogin.Write(scanner.Bytes()); err != nil { + if _, err := clientLogin.Write(buf); err != nil { return err } - c := s.NewClientConn(rwc, remoteAddr) - // check if remoteAddr is present in the ban list if banUntil, ok := s.banList[strings.Split(remoteAddr, ":")[0]]; ok { // permaban if banUntil == nil { - s.outbox <- *NewTransaction( - tranServerMsg, - c.ID, - NewField(fieldData, []byte("You are permanently banned on this server")), - NewField(fieldChatOptions, []byte{0, 0}), + t := NewTransaction( + TranServerMsg, + &[]byte{0, 0}, + NewField(FieldData, []byte("You are permanently banned on this server")), + NewField(FieldChatOptions, []byte{0, 0}), ) + + b, err := t.MarshalBinary() + if err != nil { + return err + } + + _, err = rwc.Write(b) + if err != nil { + return err + } + time.Sleep(1 * time.Second) return nil - } else if time.Now().Before(*banUntil) { - s.outbox <- *NewTransaction( - tranServerMsg, - c.ID, - NewField(fieldData, []byte("You are temporarily banned on this server")), - NewField(fieldChatOptions, []byte{0, 0}), + } + + // temporary ban + if time.Now().Before(*banUntil) { + t := NewTransaction( + TranServerMsg, + &[]byte{0, 0}, + NewField(FieldData, []byte("You are temporarily banned on this server")), + NewField(FieldChatOptions, []byte{0, 0}), ) + b, err := t.MarshalBinary() + if err != nil { + return err + } + + _, err = rwc.Write(b) + if err != nil { + return err + } + time.Sleep(1 * time.Second) return nil } - } + + c := s.NewClientConn(rwc, remoteAddr) defer c.Disconnect() - encodedLogin := clientLogin.GetField(fieldUserLogin).Data - encodedPassword := clientLogin.GetField(fieldUserPassword).Data - c.Version = clientLogin.GetField(fieldVersion).Data + encodedLogin := clientLogin.GetField(FieldUserLogin).Data + encodedPassword := clientLogin.GetField(FieldUserPassword).Data + c.Version = clientLogin.GetField(FieldVersion).Data var login string for _, char := range encodedLogin { @@ -618,15 +646,15 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser return nil } - if clientLogin.GetField(fieldUserIconID).Data != nil { - c.Icon = clientLogin.GetField(fieldUserIconID).Data + if clientLogin.GetField(FieldUserIconID).Data != nil { + c.Icon = clientLogin.GetField(FieldUserIconID).Data } c.Account = c.Server.Accounts[login] - if clientLogin.GetField(fieldUserName).Data != nil { + if clientLogin.GetField(FieldUserName).Data != nil { if c.Authorize(accessAnyName) { - c.UserName = clientLogin.GetField(fieldUserName).Data + c.UserName = clientLogin.GetField(FieldUserName).Data } else { c.UserName = []byte(c.Account.Name) } @@ -637,46 +665,54 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser } s.outbox <- c.NewReply(&clientLogin, - NewField(fieldVersion, []byte{0x00, 0xbe}), - NewField(fieldCommunityBannerID, []byte{0, 0}), - NewField(fieldServerName, []byte(s.Config.Name)), + 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[:])) + 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. + // 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})) + c.Server.outbox <- *NewTransaction(TranShowAgreement, c.ID, NewField(FieldNoServerAgreement, []byte{1})) } } else { - c.Server.outbox <- *NewTransaction(tranShowAgreement, c.ID, NewField(fieldData, s.Agreement)) + 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 + // 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.Infow("Login successful", "clientVersion", fmt.Sprintf("%v", func() int { i, _ := byteToInt(c.Version); return i }())) + c.logger.Infow("Login successful", "clientVersion", "Not sent (probably 1.2.3)") + + // 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, nil, - NewField(fieldUserName, c.UserName), - NewField(fieldUserID, *c.ID), - NewField(fieldUserIconID, c.Icon), - NewField(fieldUserFlags, c.Flags), + TranNotifyChangeUser, nil, + 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 + c.Server.Stats.ConnectionCounter += 1 + if len(s.Clients) > c.Server.Stats.ConnectionPeak { + c.Server.Stats.ConnectionPeak = len(s.Clients) + } // Scan for new transactions and handle them as they come in. for scanner.Scan() { @@ -698,15 +734,14 @@ func (s *Server) handleNewConnection(ctx context.Context, rwc io.ReadWriteCloser } func (s *Server) NewPrivateChat(cc *ClientConn) []byte { - s.mux.Lock() - defer s.mux.Unlock() + s.PrivateChatsMu.Lock() + defer s.PrivateChatsMu.Unlock() randID := make([]byte, 4) rand.Read(randID) - data := binary.BigEndian.Uint32(randID[:]) + data := binary.BigEndian.Uint32(randID) s.PrivateChats[data] = &PrivateChat{ - Subject: "", ClientConn: make(map[uint16]*ClientConn), } s.PrivateChats[data].ClientConn[cc.uint16ID()] = cc @@ -737,6 +772,10 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro delete(s.fileTransfers, t.ReferenceNumber) s.mux.Unlock() + // 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) }() s.mux.Lock() @@ -770,6 +809,10 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro } case FileDownload: s.Stats.DownloadCounter += 1 + s.Stats.DownloadsInProgress += 1 + defer func() { + s.Stats.DownloadsInProgress -= 1 + }() var dataOffset int64 if fileTransfer.fileResumeData != nil { @@ -824,6 +867,8 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro case FileUpload: s.Stats.UploadCounter += 1 + s.Stats.UploadsInProgress += 1 + defer func() { s.Stats.UploadsInProgress -= 1 }() var file *os.File @@ -880,7 +925,12 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro } rLogger.Infow("File upload complete", "dstFile", fullPath) + case FolderDownload: + s.Stats.DownloadCounter += 1 + s.Stats.DownloadsInProgress += 1 + defer func() { s.Stats.DownloadsInProgress -= 1 }() + // Folder Download flow: // 1. Get filePath from the transfer // 2. Iterate over files @@ -1044,6 +1094,9 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro } case FolderUpload: + s.Stats.UploadCounter += 1 + s.Stats.UploadsInProgress += 1 + defer func() { s.Stats.UploadsInProgress -= 1 }() rLogger.Infow( "Folder upload started", "dstPath", fullPath, @@ -1155,7 +1208,7 @@ func (s *Server) handleFileTransfer(ctx context.Context, rwc io.ReadWriter) erro return err } - if err := receiveFile(rwc, file, ioutil.Discard, ioutil.Discard, fileTransfer.bytesSentCounter); err != nil { + if err := receiveFile(rwc, file, io.Discard, io.Discard, fileTransfer.bytesSentCounter); err != nil { s.Logger.Error(err) }